Advertisement
Eddz

Untitled

Sep 3rd, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.14 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Fcm\Controller;
  4.  
  5. use Zend\Mvc\Controller\AbstractActionController;
  6. use Zend\View\Model\ViewModel;
  7. use Doctrine\ORM\EntityManager;
  8. use Fcm\Entity\Client;
  9. use Fcm\Form\ClientAdd;
  10. use Zend\Stdlib\Hydrator\HydratorInterface;
  11.  
  12. class ClientsController extends AbstractActionController
  13. {
  14.  
  15.     /**
  16.      * @var Doctrine\ORM\EntityManager
  17.      */
  18.     protected $em;
  19.  
  20.     /**
  21.      * @var Zend\Stdlib\Hydrator\HydratorInterface
  22.      */
  23.     protected $hydrator = null;
  24.  
  25.     /**
  26.      *  SetEntityManager
  27.      *
  28.      * @return void
  29.      */
  30.     public function setEntityManager(EntityManager $em)
  31.     {
  32.         $this->em = $em;
  33.     }
  34.  
  35.     /**
  36.      * Pattern Singleton for EntityManager
  37.      *
  38.      * @return object EntityManager
  39.      */
  40.     public function getEntityManager()
  41.     {
  42.         if (null === $this->em) {
  43.             $this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
  44.         }
  45.         return $this->em;
  46.     }
  47.  
  48.     /**
  49.      * Display all entities with pagination and sorting
  50.      *
  51.      * @return object ViewModel
  52.      */
  53.     public function indexAction()
  54.     {
  55.         // Recuperation and assignment variables
  56.         $order_by = $this->params()->fromRoute('order_by') ?
  57.                 $this->params()->fromRoute('order_by') : 'id';
  58.         $order = $this->params()->fromRoute('order') ?
  59.                 $this->params()->fromRoute('order') : 'DESC';
  60.         $page = $this->params()->fromRoute('page') ?
  61.                 (int) $this->params()->fromRoute('page') : 1;
  62.  
  63.         $clients = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->displayAllEntities($order_by, $order, $page);
  64.  
  65.         $this->layout()->title = 'List clients';
  66.         return new ViewModel(array(
  67.             'clients' => $clients,
  68.             'order_by' => $order_by,
  69.             'order' => $order,
  70.             'flashMessages' => $this->flashMessenger()->getMessages(),
  71.         ));
  72.     }
  73.  
  74.     /**
  75.      * Search entities
  76.      *
  77.      * @return object ViewModel | redirect
  78.      */
  79.     public function searchAction()
  80.     {
  81.         $order_by = 'id';
  82.         $order = 'DESC';
  83.         $page = 1;
  84.  
  85.         if ($this->request->isPost()) {
  86.             $keyword = $this->getRequest()->getPost('search');
  87.             // Search
  88.             $entities = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->search($keyword);
  89.             // Redirect to list
  90.             $this->layout()->title = 'Client search results for - "' . $keyword . '"';
  91.             return new ViewModel(array(
  92.                 'clients' => $entities,
  93.                 'keyword' => $keyword,
  94.                 'flashMessages' => $this->flashMessenger()->getMessages(),
  95.             ));
  96.         }
  97.         return $this->redirect()->toRoute('clients');
  98.     }
  99.  
  100.     /**
  101.      * Display one entity by its primary key / identifier.
  102.      *
  103.      * @return object ViewModel
  104.      */
  105.     public function viewAction()
  106.     {
  107.         $entity = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->findEntity($this->params()->fromRoute('id'));
  108.         $this->layout()->title = 'Client ' . $entity->name;
  109.         return new ViewModel(array(
  110.             'client' => $entity
  111.         ));
  112.     }
  113.  
  114.     /**
  115.      * Add entity
  116.      *
  117.      * @return object ViewModel | redirect
  118.      */
  119.     public function addAction()
  120.     {
  121.         $form = new \Fcm\Form\ClientAdd($this->getEntityManager());
  122.         $form->setInputFilter(new \Fcm\Form\ClientAddInputFilter());
  123.         $validData = null;
  124.         $this->layout()->title = 'Add client';
  125.  
  126.         if ($this->request->isPost()) {
  127.             $form->setData($this->request->getPost());
  128.             if ($form->isValid()) {
  129.                 $formData = $form->getData();
  130.                 // Check duplicate email
  131.                 $dup = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->checkIfEmailExists($formData['fsOne']['email']);
  132.                 if (!empty($dup)) {
  133.                     $this->flashMessenger()->addMessage('<strong>Email already exists!</strong>');
  134.                     return new ViewModel(
  135.                             array(
  136.                         'form' => $form,
  137.                         'flashMessages' => $this->flashMessenger()->getMessages(),
  138.                             )
  139.                     );
  140.                 }
  141.                 // Insert
  142.                 $client = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->insert($formData['fsOne']);
  143.  
  144.                 // Create assets folder
  145.                 $path = 'X:/Program Files/wamp/www/Projects/FCM/module/Campaign/public/'; // Windows Hack
  146.                 $client = 'client_' . $client->id;
  147.                 $client_paths = array(
  148.                     "imgPath" => $path . 'img_clients/' . $client,
  149.                     "cssPath" => $path . 'css_clients/' . $client,
  150.                     "jsPath" => $path . 'js_clients/' . $client,
  151.                 );
  152.                 foreach ($client_paths as $value) {
  153.                     if (!is_dir($value))
  154.                         mkdir($value, 0755, true);
  155.                 }
  156.  
  157.                 $this->flashMessenger()->addMessage('<strong>Added!</strong> The client <strong style="text-decoration: underline;">' . $formData['fsOne']['name'] . '</strong> was successfully added.');
  158.                 return $this->redirect()->toRoute('clients');
  159.             } else {
  160.                 $this->flashMessenger()->addMessage('<strong>Invalid input!!</strong> The form fields are not filled out correctly.');
  161.                 return new ViewModel(
  162.                         array(
  163.                     'form' => $form,
  164.                         )
  165.                 );
  166.             }
  167.         }
  168.         else
  169.             return new ViewModel(
  170.                     array(
  171.                 'form' => $form,
  172.                     )
  173.             );
  174.     }
  175.  
  176.     /**
  177.      * Edit entity
  178.      *
  179.      * @return object ViewModel | redirect
  180.      */
  181.     public function editAction()
  182.     {
  183.         $form = new \Fcm\Form\ClientEdit();
  184.  
  185.         if ($this->request->isPost()) {
  186.             $form->setInputFilter(new \Fcm\Form\ClientAddInputFilter());
  187.             $validData = null;
  188.  
  189.             $form->setData($this->request->getPost());
  190.             if ($form->isValid()) {
  191.                 $formData = $form->getData();
  192.                 // Update
  193.                 $this->getEntityManager()->getRepository('Fcm\Entity\Client')->update($formData['fsOne']);
  194.                 $this->flashMessenger()->addMessage('<strong>Modified!</strong> The client <strong style="text-decoration: underline;">' . $formData['fsOne']['name'] . '</strong> was successfully modified.');
  195.                 return $this->redirect()->toRoute('clients', array('action' => 'view', 'id' => $formData['fsOne']['id']));
  196.             } else {
  197.                 $this->flashMessenger()->addMessage('<strong>Invalid input!!</strong> The form fields are not filled out correctly.');
  198.                 return new ViewModel(
  199.                         array(
  200.                     'form' => $form,
  201.                         )
  202.                 );
  203.             }
  204.         }
  205.  
  206.         if (is_numeric($this->params()->fromRoute('id'))) {
  207.             $entity = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->findEntity($this->params()->fromRoute('id'));
  208.             $this->hydrator = new \Zend\Stdlib\Hydrator\Reflection;
  209.             // Populate
  210.             $form->populateValues(array('fsOne' => $this->hydrator->extract($entity)));
  211.  
  212.             $this->layout()->title = 'Edit client ' . $entity->name;
  213.             return new ViewModel(
  214.                     array(
  215.                 'form' => $form,
  216.                 'id' => $this->params()->fromRoute('id'),
  217.                     )
  218.             );
  219.         }
  220.         else
  221.             return $this->redirect()->toRoute('clients');
  222.     }
  223.  
  224.     /**
  225.      * Delete entity
  226.      *
  227.      * @return object ViewModel | redirect
  228.      */
  229.     public function deleteAction()
  230.     {
  231.         $id = (int) $this->params()->fromRoute('id');
  232.         if (!$id) {
  233.             return $this->redirect()->toRoute('clients');
  234.         }
  235.  
  236.         $request = $this->getRequest();
  237.         if ($request->isPost()) {
  238.             $del = $request->getPost('del', 'No');
  239.             if ($del == 'Yes') {
  240.                 $entity = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->findEntity($id);
  241.  
  242.                 // Delete assets
  243.                 $fcmPlugin = $this->FcmPlugin();
  244.                 $fcmPlugin->deleteAssets($entity, 'client');
  245.  
  246.                 // Delete entity
  247.                 $this->getEntityManager()->getRepository('Fcm\Entity\Client')->delete($id);
  248.                 $this->flashMessenger()->addMessage('<strong>Removed!</strong> The client <strong style="text-decoration: underline;">' . $entity->name . '</strong> was successfully removed.');
  249.             }
  250.             // Redirect to list
  251.             return $this->redirect()->toRoute('clients');
  252.         }
  253.         $entity = $this->getEntityManager()->find("Fcm\Entity\Client", $id);
  254.         $this->layout()->title = 'Delete client ' . $entity->name;
  255.         return new ViewModel(array(
  256.             'client' => $entity,
  257.         ));
  258.     }
  259.  
  260. }
  261.  
  262. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement