/** * Finds related posts */ public function showRelatedAction() { $this->view->disable(); if ($this->request->isAjax()) { //$this->view->setRenderLevel(View::LEVEL_ACTION_VIEW); $post = Posts::findFirstById($this->request->getPost('id')); if ($post) { $posts = Posts::postRelated($post); if (count($posts) > 0) { $params = ['posts' => $posts]; echo $this->view->getRender('partials', 'list-posts', $params, function ($view) { $view->setRenderLevel(View::LEVEL_ACTION_VIEW); }); } } } }
/** * Displays a post and its comments * * @param $id * @param $slug * * @return \Phalcon\Http\ResponseInterface */ public function viewAction($id, $slug) { $id = (int) $id; $userId = $this->auth->getAuth()['id']; if (!($object = Posts::findFirstById($id))) { $this->flashSession->error(t('Posts doesn\'t exist.')); return $this->indexRedirect(); } if ($object->getDeleted()) { $this->flashSession->error('The Post is deleted'); return $this->indexRedirect(); } if (!$object->isPublish()) { $this->flashSession->error('The Post have not publish'); return $this->indexRedirect(); } $ipAddress = $this->request->getClientAddress(); $parameters = ['postsId = ?0 AND ipaddress = ?1', 'bind' => [$id, $ipAddress]]; $viewed = PostsViews::count($parameters); //A view is stored by ipaddress if (!$viewed) { //Increase the number of views in the post $object->setNumberViews($object->getNumberViews() + 1); if ($object->getUsersId() != $userId) { $object->user->increaseKarma(Karma::VISIT_ON_MY_POST); if ($userId > 0) { $user = Users::findFirstById($userId); if ($user) { if ($user->getModerator() == 'Y') { $user->increaseKarma(Karma::MODERATE_VISIT_POST); } else { $user->increaseKarma(Karma::VISIT_POST); } //send log to server if (!$user->save()) { $this->saveLoger($user->getMessages()); } } } } if (!$object->save()) { $this->saveLoger($object->getMessages()); } $postView = new PostsViews(); $postView->setPostsId($id); $postView->setIpaddress($ipAddress); if (!$postView->save()) { $this->saveLoger($postView->getMessages()); } } //d($object->getPostsWithVotes($id)); $this->view->setVars(['post' => $object, 'form' => new ReplyForm(), 'votes' => $object->getVotes($id, Vote::OBJECT_POSTS), 'postsReply' => $object->getPostsWithVotes($id), 'commentForm' => new CommentForm(), 'userPosts' => $object->user, 'type' => Posts::POST_QUESTIONS, 'postRelated' => Posts::postRelated($object)]); $this->tag->setTitle($this->escaper->escapeHtml($object->getTitle())); return $this->view->pick('single'); }