Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Fcm\Controller;
- use Zend\Mvc\Controller\AbstractActionController;
- use Zend\View\Model\ViewModel;
- use Doctrine\ORM\EntityManager;
- use Fcm\Entity\Client;
- use Fcm\Form\ClientAdd;
- use Zend\Stdlib\Hydrator\HydratorInterface;
- class ClientsController extends AbstractActionController
- {
- /**
- * @var Doctrine\ORM\EntityManager
- */
- protected $em;
- /**
- * @var Zend\Stdlib\Hydrator\HydratorInterface
- */
- protected $hydrator = null;
- /**
- * SetEntityManager
- *
- * @return void
- */
- public function setEntityManager(EntityManager $em)
- {
- $this->em = $em;
- }
- /**
- * Pattern Singleton for EntityManager
- *
- * @return object EntityManager
- */
- public function getEntityManager()
- {
- if (null === $this->em) {
- $this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
- }
- return $this->em;
- }
- /**
- * Display all entities with pagination and sorting
- *
- * @return object ViewModel
- */
- public function indexAction()
- {
- // Recuperation and assignment variables
- $order_by = $this->params()->fromRoute('order_by') ?
- $this->params()->fromRoute('order_by') : 'id';
- $order = $this->params()->fromRoute('order') ?
- $this->params()->fromRoute('order') : 'DESC';
- $page = $this->params()->fromRoute('page') ?
- (int) $this->params()->fromRoute('page') : 1;
- $clients = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->displayAllEntities($order_by, $order, $page);
- $this->layout()->title = 'List clients';
- return new ViewModel(array(
- 'clients' => $clients,
- 'order_by' => $order_by,
- 'order' => $order,
- 'flashMessages' => $this->flashMessenger()->getMessages(),
- ));
- }
- /**
- * Search entities
- *
- * @return object ViewModel | redirect
- */
- public function searchAction()
- {
- $order_by = 'id';
- $order = 'DESC';
- $page = 1;
- if ($this->request->isPost()) {
- $keyword = $this->getRequest()->getPost('search');
- // Search
- $entities = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->search($keyword);
- // Redirect to list
- $this->layout()->title = 'Client search results for - "' . $keyword . '"';
- return new ViewModel(array(
- 'clients' => $entities,
- 'keyword' => $keyword,
- 'flashMessages' => $this->flashMessenger()->getMessages(),
- ));
- }
- return $this->redirect()->toRoute('clients');
- }
- /**
- * Display one entity by its primary key / identifier.
- *
- * @return object ViewModel
- */
- public function viewAction()
- {
- $entity = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->findEntity($this->params()->fromRoute('id'));
- $this->layout()->title = 'Client ' . $entity->name;
- return new ViewModel(array(
- 'client' => $entity
- ));
- }
- /**
- * Add entity
- *
- * @return object ViewModel | redirect
- */
- public function addAction()
- {
- $form = new \Fcm\Form\ClientAdd($this->getEntityManager());
- $form->setInputFilter(new \Fcm\Form\ClientAddInputFilter());
- $validData = null;
- $this->layout()->title = 'Add client';
- if ($this->request->isPost()) {
- $form->setData($this->request->getPost());
- if ($form->isValid()) {
- $formData = $form->getData();
- // Check duplicate email
- $dup = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->checkIfEmailExists($formData['fsOne']['email']);
- if (!empty($dup)) {
- $this->flashMessenger()->addMessage('<strong>Email already exists!</strong>');
- return new ViewModel(
- array(
- 'form' => $form,
- 'flashMessages' => $this->flashMessenger()->getMessages(),
- )
- );
- }
- // Insert
- $client = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->insert($formData['fsOne']);
- // Create assets folder
- $path = 'X:/Program Files/wamp/www/Projects/FCM/module/Campaign/public/'; // Windows Hack
- $client = 'client_' . $client->id;
- $client_paths = array(
- "imgPath" => $path . 'img_clients/' . $client,
- "cssPath" => $path . 'css_clients/' . $client,
- "jsPath" => $path . 'js_clients/' . $client,
- );
- foreach ($client_paths as $value) {
- if (!is_dir($value))
- mkdir($value, 0755, true);
- }
- $this->flashMessenger()->addMessage('<strong>Added!</strong> The client <strong style="text-decoration: underline;">' . $formData['fsOne']['name'] . '</strong> was successfully added.');
- return $this->redirect()->toRoute('clients');
- } else {
- $this->flashMessenger()->addMessage('<strong>Invalid input!!</strong> The form fields are not filled out correctly.');
- return new ViewModel(
- array(
- 'form' => $form,
- )
- );
- }
- }
- else
- return new ViewModel(
- array(
- 'form' => $form,
- )
- );
- }
- /**
- * Edit entity
- *
- * @return object ViewModel | redirect
- */
- public function editAction()
- {
- $form = new \Fcm\Form\ClientEdit();
- if ($this->request->isPost()) {
- $form->setInputFilter(new \Fcm\Form\ClientAddInputFilter());
- $validData = null;
- $form->setData($this->request->getPost());
- if ($form->isValid()) {
- $formData = $form->getData();
- // Update
- $this->getEntityManager()->getRepository('Fcm\Entity\Client')->update($formData['fsOne']);
- $this->flashMessenger()->addMessage('<strong>Modified!</strong> The client <strong style="text-decoration: underline;">' . $formData['fsOne']['name'] . '</strong> was successfully modified.');
- return $this->redirect()->toRoute('clients', array('action' => 'view', 'id' => $formData['fsOne']['id']));
- } else {
- $this->flashMessenger()->addMessage('<strong>Invalid input!!</strong> The form fields are not filled out correctly.');
- return new ViewModel(
- array(
- 'form' => $form,
- )
- );
- }
- }
- if (is_numeric($this->params()->fromRoute('id'))) {
- $entity = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->findEntity($this->params()->fromRoute('id'));
- $this->hydrator = new \Zend\Stdlib\Hydrator\Reflection;
- // Populate
- $form->populateValues(array('fsOne' => $this->hydrator->extract($entity)));
- $this->layout()->title = 'Edit client ' . $entity->name;
- return new ViewModel(
- array(
- 'form' => $form,
- 'id' => $this->params()->fromRoute('id'),
- )
- );
- }
- else
- return $this->redirect()->toRoute('clients');
- }
- /**
- * Delete entity
- *
- * @return object ViewModel | redirect
- */
- public function deleteAction()
- {
- $id = (int) $this->params()->fromRoute('id');
- if (!$id) {
- return $this->redirect()->toRoute('clients');
- }
- $request = $this->getRequest();
- if ($request->isPost()) {
- $del = $request->getPost('del', 'No');
- if ($del == 'Yes') {
- $entity = $this->getEntityManager()->getRepository('Fcm\Entity\Client')->findEntity($id);
- // Delete assets
- $fcmPlugin = $this->FcmPlugin();
- $fcmPlugin->deleteAssets($entity, 'client');
- // Delete entity
- $this->getEntityManager()->getRepository('Fcm\Entity\Client')->delete($id);
- $this->flashMessenger()->addMessage('<strong>Removed!</strong> The client <strong style="text-decoration: underline;">' . $entity->name . '</strong> was successfully removed.');
- }
- // Redirect to list
- return $this->redirect()->toRoute('clients');
- }
- $entity = $this->getEntityManager()->find("Fcm\Entity\Client", $id);
- $this->layout()->title = 'Delete client ' . $entity->name;
- return new ViewModel(array(
- 'client' => $entity,
- ));
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement