/**
  * @return \Phalcon\Http\ResponseInterface
  */
 public function saveAction()
 {
     $this->view->disable();
     //  Is not $_POST
     if (!$this->request->isPost()) {
         return $this->indexRedirect();
     }
     $id = $this->request->getPost('id');
     if (!empty($id)) {
         $object = Posts::findFirstById($id);
     } else {
         $object = new Posts();
     }
     $object->setSlug(Slug::generate($this->request->getPost('title')));
     $object->setUsersId($this->auth->getAuth()['id']);
     $object->setType(Posts::POST_PAGE);
     $form = new PostsForm($object);
     $form->bind($_POST, $object);
     //  Form isn't valid
     if (!$form->isValid($this->request->getPost())) {
         $this->saveLoger($form->getMessages());
         // Redirect to edit form if we have an ID in page, otherwise redirect to add a new item page
         return $this->response->redirect($this->getPathController() . (!is_null($id) ? '/edit/' . $id : '/new'));
     } else {
         if (!$object->save()) {
             $this->saveLoger($object->getMessages());
             return $this->dispatcher->forward(['controller' => $this->getPathController(), 'action' => 'new']);
         } else {
             $this->flashSession->success(t('Data was successfully saved'));
             return $this->indexRedirect();
         }
     }
 }
Example #2
0
 /**
  * @return \Phalcon\Http\ResponseInterface
  */
 public function saveAction()
 {
     $this->view->disable();
     //  Is not $_POST
     if (!$this->request->isPost()) {
         return $this->indexRedirect();
     }
     $id = $this->request->getPost('id');
     $auth = $this->auth->getAuth();
     $tags = $this->request->getPost('tags', 'string', null);
     if (!empty($id)) {
         $object = Posts::findFirstById($id);
     } else {
         $object = new Posts();
         //@todo
     }
     $object->setSlug(Slug::generate($this->request->getPost('title')));
     $object->setUsersId($auth['id']);
     $form = new PostsForm($object);
     $form->bind($_POST, $object);
     //  Form isn't valid
     if (!$form->isValid($this->request->getPost())) {
         $this->saveLoger($form->getMessages());
         // Redirect to edit form if we have an ID in page, otherwise redirect to add a new item page
         return $this->response->redirect($this->getPathController() . (!is_null($id) ? '/edit/' . $id : '/new'));
     } else {
         $object->setStatus(Posts::PUBLISH_STATUS);
         //Save post to draft
         if ($this->request->getPost('saveDraft')) {
             $object->setStatus(Posts::DRAFT_STATUS);
         }
         if (!$object->save()) {
             $this->saveLoger($object->getMessages());
             return $this->dispatcher->forward(['controller' => $this->getPathController(), 'action' => 'new']);
         } else {
             $this->phanbook->tag()->saveTagsInPosts($tags, $object);
             $this->flashSession->success(t('Data was successfully saved'));
             return $this->indexRedirect();
         }
     }
 }