Beispiel #1
0
 /**
  * @return \Phalcon\Http\ResponseInterface
  */
 public function saveAction()
 {
     //  Is not $_POST
     if (!$this->request->isPost()) {
         $this->view->disable();
         return $this->response->redirect($this->router->getControllerName());
     }
     $id = $this->request->getPost('id');
     $auth = $this->auth->getAuth();
     if (!$auth) {
         $this->flashSession->error('You must be logged first');
         return $this->currentRedirect();
     }
     //Checking tags
     $tags = $this->request->getPost('tags', 'string', null);
     if (is_string($this->phanbook->isTags($tags))) {
         $this->flashSession->notice(t($this->isTags($tags)));
         return $this->dispatcher->forward(['controller' => $this->router->getControllerName(), 'action' => !is_null($id) ? 'edit' : 'new', 'params' => !is_null($id) ? array($id) : array()]);
     }
     if (!empty($id)) {
         $object = Posts::findFirstById($id);
         $object->setSlug(Slug::generate($this->request->getPost('title')));
         //@Todo continue When moderator or admin edit post
         //Just to save history when user is TrustModarator and the user not owner the post
         if ($this->auth->isTrustModeration() && $auth['id'] != $object->getUsersId()) {
             $object->setEditedAt(time());
             $postHistory = new PostsHistory();
             $postHistory->setPostsId($id);
             $postHistory->setUsersId($auth['id']);
             $postHistory->setContent($this->request->getPost('content'));
             if (!$postHistory->save()) {
                 $this->saveLoger($postHistory->getMessages());
             }
         }
     } else {
         $object = new Posts();
         $object->setType(Posts::POST_QUESTIONS);
         $object->setSlug(Slug::generate($this->request->getPost('title')));
         $object->setUsersId($auth['id']);
         $user = Users::findFirstById($auth['id']);
         $user->increaseKarma(Karma::ADD_NEW_POST);
         if (!$user->save()) {
             $this->saveLoger($user->getMessages());
         }
     }
     $form = new QuestionsForm($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->router->getControllerName() . (!is_null($id) ? '/edit/' . $id : '/new'));
     } else {
         if (!$object->save()) {
             $this->saveLoger($object->getMessages());
             return $this->dispatcher->forward(['controller' => $this->router->getControllerName(), 'action' => 'new']);
         } else {
             if (!$this->phanbook->saveTagsInPosts($tags, $object)) {
                 return $this->response->redirect($this->router->getControllerName() . (!is_null($id) ? '/edit/' . $id : '/new'));
             }
             $this->flashSession->success(t('Data was successfully saved'));
             return $this->response->redirect($this->router->getControllerName());
         }
     }
     $this->view->disable();
 }
 /**
  * @return \Phalcon\Http\ResponseInterface
  */
 public function saveHackernewAction()
 {
     //  Is not $_POST
     if (!$this->request->isPost()) {
         $this->view->disable();
         return $this->response->redirect($this->router->getControllerName());
     }
     $id = $this->request->getPost('id');
     $auth = $this->auth->getAuth();
     if (!$auth) {
         $this->flashSession->error('You must be logged first');
         return $this->currentRedirect();
     }
     if (!empty($id)) {
         $object = Posts::findFirstById($id);
         $object->setSlug(Slug::generate($this->request->getPost('title')));
         //@Todo continue When moderator or admin edit post
         if ($this->auth->isTrustModeration()) {
             $object->setEditedAt(time());
             $postHistory = new PostsHistory();
             $postHistory->setPostsId($id);
             $postHistory->setUsersId($auth['id']);
             $postHistory->setContent($this->request->getPost('content'));
             if (!$postHistory->save()) {
                 $this->saveLoger($postHistory->getMessages());
             }
         }
     } else {
         $object = new Posts();
         $object->setType(Posts::POST_HACKERNEWS);
         $object->setSlug(Slug::generate($this->request->getPost('title')));
         $object->setUsersId($auth['id']);
         $user = Users::findFirstById($auth['id']);
         $user->increaseKarma(Karma::ADD_NEW_POST_HACKERNEWS);
         if (!$user->save()) {
             $this->saveLoger($user->getMessages());
         }
     }
     $form = new HackernewForm($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->router->getControllerName() . (!is_null($id) ? '/edit/' . $id : '/hackernew'));
     } else {
         if (!$object->save()) {
             $this->saveLoger($object->getMessages());
         }
         $this->flashSession->success(t('Data was successfully saved'));
         return $this->response->redirect($this->router->getControllerName());
     }
     $this->view->disable();
 }