/** * Cria ou edita um post * @return void */ public function saveAction() { $form = new CommentForm(); $request = $this->getRequest(); if ($request->isPost()) { $comment = new Comment(); $form->setData($request->getPost()); if ($form->isValid()) { $data = $form->getData(); $data['comment_date'] = date('Y-m-d H:i:s'); $comment->exchangeArray($data); $saved = $this->getTable('Admin\\Entity\\Comment')->save($comment); if ($saved) { $this->flashMessenger()->setNamespace('success')->addMessage('Comentário salvo com sucesso.'); } else { $this->flashMessenger()->setNamespace('danger')->addMessage('Não foi possível salver esse comentário. Tente novamente mais tarde.'); } return $this->redirect()->toUrl('/admin/comments/index'); } else { $this->flashMessenger()->setNamespace('danger')->addMessage('Post não foi salvo. Erro de validação.'); } } $id = (int) $this->params()->fromRoute('id', 0); if ($id > 0) { $comment = $this->getTable('Admin\\Entity\\Comment')->get($id); $form->bind($comment); $form->get('submit')->setAttribute('value', 'Edit'); } $form->get('posts_id')->setValueOptions($this->getService('Admin\\Model\\PostModel')->getPostsToPopuleSelect()); return new ViewModel(array('form' => $form)); }
public function showAction() { $id = $this->params('id'); $comment = $this->getEntityManager()->getRepository('Blog\\Entity\\Comment')->find($id); if (!$comment) { throw new EntityNotFoundException('Entity Comment not found'); } $commentForm = new CommentForm(); $commentForm->get('submit')->setAttribute('value', 'Valider'); if ($comment->isState() == 1) { $commentForm->get('state')->setAttribute('checked', 'checked'); } $request = $this->getRequest(); if ($request->isPost()) { $commentData = $request->getPost(); if (isset($commentData->state)) { $commentData->state = 1; } else { $commentData->state = 0; } $commentForm->setData($commentData); if ($commentForm->isValid()) { $comment = $this->getHydrator()->hydrate($commentForm->getData(), $comment); //Persist and flush entity Comment $em = $this->getEntityManager(); $em->persist($comment); $em->flush(); $eventManager = $this->getEventManager(); $eventManager->trigger('comment.edit', null, ['comment_id' => $comment->getId(), 'comment_email' => $comment->getEmail(), 'article_title' => $comment->getArticle()->getTitle(), 'article_id' => $comment->getArticle()->getId(), 'user_id' => $this->zfcUserAuthentication()->getIdentity()->getId(), 'user_email' => $this->zfcUserAuthentication()->getIdentity()->getEmail()]); //Redirection return $this->redirect()->toRoute('admin/comment'); } } return new ViewModel(['comment' => $comment, 'commentForm' => $commentForm]); }