public function viewAction() { $id = (int) $this->getEvent()->getRouteMatch()->getParam('id'); if (!$id) { return $this->redirect()->toRoute('home'); } try { $article = $this->getEntityManager()->find('Cms\\Entity\\Article', $id); } catch (\Exception $e) { $response = $this->response; $event = $this->getEvent(); $routeMatch = $event->getRouteMatch(); $response->setStatusCode(404); $event->setParam('exception', new \Exception('Page Inconnue' . $id)); $event->setController('page'); return; } $entityManager = $this->getEntityManager(); $comment = new Comment(); $form = new CommentForm(); $form->remove('comCreated'); $form->remove('author'); $form->remove('article'); $auth = $this->getServiceLocator()->get('Zend\\Authentication\\AuthenticationService'); if ($auth->hasIdentity()) { $form->remove('com_email'); $form->remove('com_username'); } foreach ($form->getElements() as $element) { if (method_exists($element, 'getProxy')) { $proxy = $element->getProxy(); if (method_exists($proxy, 'setObjectManager')) { $proxy->setObjectManager($entityManager); } } } $form->setHydrator(new DoctrineHydrator($entityManager, 'Cms\\Entity\\Comment')); $form->bind($comment); return new ViewModel(array('article' => $article, 'form' => $form)); }
public function addAction() { $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('cms/default', array('controller' => 'index', 'action' => 'index')); } $entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); $comment = new Comment(); try { $repository = $entityManager->getRepository('Cms\\Entity\\Article'); $article = $repository->findOneBy(array('artcId' => $id)); $comment->setArticle($article); } catch (\Exception $ex) { return $this->redirect()->toRoute('cms/default', array('controller' => 'index', 'action' => 'index')); } $form = new CommentForm(); $form->remove('comCreated'); $form->remove('author'); $form->remove('article'); $auth = $this->getServiceLocator()->get('Zend\\Authentication\\AuthenticationService'); if ($auth->hasIdentity()) { $form->remove('com_email'); $form->remove('com_username'); } foreach ($form->getElements() as $element) { if (method_exists($element, 'getProxy')) { $proxy = $element->getProxy(); if (method_exists($proxy, 'setObjectManager')) { $proxy->setObjectManager($entityManager); } } } $form->setHydrator(new DoctrineHydrator($entityManager, 'Cms\\Entity\\Comment')); $form->bind($comment); $request = $this->getRequest(); if ($request->isPost()) { $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $this->prepareData($comment); if (!$auth->hasIdentity()) { $comment->setComUsername($data->getComUsername()); $comment->setComEmail($data->getComEmail()); } $entityManager->persist($comment); $entityManager->flush(); $entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); $admin_id = 3; $sql = "SELECT u.usrEmail, u.usrName FROM Auth\\Entity\\User u WHERE u.usrlId= " . $admin_id; $query = $entityManager->createQuery($sql); $admins = $query->getResult(); $mail = new MailController(); foreach ($admins as $admin) { $mail->initMail('commentCreated', $admin['usrEmail'], $admin['usrName'], $article->getArtcTitle()); } return $this->redirect()->toRoute('cms/default', array('controller' => 'comment', 'action' => 'index', 'id' => $id), true); } } return array('id' => $id, 'form' => $form); }