/** * The answer a question * @return mixed */ public function answerAction() { $this->view->disable(); $auth = $this->auth->getAuth(); if (!$auth) { $this->flashSession->error(t('You must be logged in first to post answer')); return $this->currentRedirect(); } if ($this->request->isPost()) { $postId = $this->request->getPost('id'); $content = $this->request->getPost('content', 'trim'); if (str_word_count($content) < 10) { $this->flashSession->error(t('Body must be at least 15 word')); return $this->currentRedirect(); } $post = Posts::findFirstById($postId); $user = Users::findFirstById($auth['id']); //Only update the number of replies if the user that commented isn't the same that posted if ($user->getId() != $post->getUsersId()) { $post->setNumberReply($post->getNumberReply() + 1); $post->user->increaseKarma(Karma::SOMEONE_REPLIED_TO_MY_POST); $user->increaseKarma(Karma::REPLY_ON_SOMEONE_ELSE_POST); if (!$post->save() || !$user->save()) { error_log('Save fail answerAction. I am on here ' . __LINE__); return false; } } $object = new PostsReply(); $object->setPostsId($postId); $object->setContent($content); $object->setUsersId($auth['id']); if (!$object->save()) { foreach ($object->getMessages() as $message) { $this->flashSession->error($message); } return $this->currentRedirect(); } $this->flashSession->success(t('Data was successfully saved')); return $this->currentRedirect(); } }