/**
  * Cria ou edita um post
  * @return void
  */
 public function saveAction()
 {
     $form = new PostForm();
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post = new Post();
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $data = $form->getData();
             $data['post_date'] = date('Y-m-d H:i:s');
             $post->exchangeArray($data);
             $saved = $this->getTable('Application\\Entity\\Post')->save($post);
             if ($saved) {
                 $this->flashMessenger()->setNamespace('success')->addMessage('Post salvo com sucesso.');
             } else {
                 $this->flashMessenger()->setNamespace('danger')->addMessage('Não foi possível salver esse poste. Tente novamente mais tarde.');
             }
             return $this->redirect()->toUrl('/admin/posts/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) {
         $post = $this->getTable('Application\\Entity\\Post')->get($id);
         $form->bind($post);
         $form->get('submit')->setAttribute('value', 'Edit');
     }
     return new ViewModel(array('form' => $form));
 }
Exemplo n.º 2
0
 public function editAction()
 {
     $this->layout("layout/admin");
     $id = $this->params('id');
     $form = new PostForm($this->user->id);
     $request = $this->getRequest();
     $postTable = $this->getPostTable();
     if ($request->isPost()) {
         $form->addInputFilter();
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $p = new Post();
             $p->exchangeArray($request->getPost());
             $postTable->edit($id, $p->toArray());
             $message = ["Post has been successfully edited", Messenger::SUCCESS];
         } else {
             $message = ["All inputs have to be filled out", Messenger::ERROR];
         }
     }
     $form->setData($postTable->select("id=" . $id)->toArray()[0]);
     return ['message' => isset($message) ? $message : null, 'form' => $form, 'images' => $this->getMediaTable()->fetchAll()->toArray()];
 }