/** * Implement hook beforeCreate * * Create a posts-views logging the ipaddress where the post was created * This avoids that the same session counts as post view */ public function beforeCreate() { $postView = new PostsViews(); $postView->setIpaddress($this->getDI()->getRequest()->getClientAddress()); $this->postview = $postView; $this->createdAt = time(); $this->modifiedAt = time(); }
/** * 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(); } $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()); } } $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]); $this->tag->setTitle($this->escaper->escapeHtml($object->getTitle())); return $this->view->pick('single'); }