Permalink
Browse files

Merge pull request #118 from sergeyklay/develop

Cleaned Phanbook\Frontend\Controllers\PostsController
  • Loading branch information...
sergeyklay committed Nov 1, 2016
2 parents 9e06858 + d895164 commit 6fdfc8c991cbc6732842d8e901ee5aa8a37e5e4f
Showing with 29 additions and 74 deletions.
  1. +2 −2 core/common/library/Behavior/Di.php
  2. +27 −72 core/modules/frontend/controllers/PostsController.php
@@ -66,7 +66,7 @@ public function __construct(DiInterface $di = null)
* *
* @return DiInterface * @return DiInterface
*/ */
protected function getDI() public function getDI()
{ {
return $this->di; return $this->di;
} }
@@ -77,7 +77,7 @@ protected function getDI()
* @param DiInterface $di * @param DiInterface $di
* @return $this * @return $this
*/ */
protected function setDI(DiInterface $di) public function setDI(DiInterface $di)
{ {
$this->di = $di; $this->di = $di;
@@ -19,8 +19,8 @@
use Phanbook\Models\Karma; use Phanbook\Models\Karma;
use Phanbook\Models\Users; use Phanbook\Models\Users;
use Phanbook\Models\ModelBase; use Phanbook\Models\ModelBase;
use Phanbook\Models\PostsViews;
use Phanbook\Models\PostsHistory; use Phanbook\Models\PostsHistory;
use Phanbook\Models\Services\Service;
use Phanbook\Frontend\Forms\ReplyForm; use Phanbook\Frontend\Forms\ReplyForm;
use Phanbook\Frontend\Forms\CommentForm; use Phanbook\Frontend\Forms\CommentForm;
use Phanbook\Frontend\Forms\QuestionsForm; use Phanbook\Frontend\Forms\QuestionsForm;
@@ -326,100 +326,55 @@ public function newAction()
} }
/** /**
* Displays a post and its comments * Displays a Post and its comments.
* *
* @param int $id The Post id * @param int $id The Post ID
* @param string $slug The Post slug * @param string $slug The Post slug
*
* @return \Phalcon\Mvc\View|void
*/ */
public function viewAction($id, $slug) public function viewAction($id, $slug)
{ {
$id = (int) $id; $postService = new Service\Post();
$userId = $this->auth->getUserId(); $userService = new Service\User();
if (!$object = Posts::findFirstById($id)) { $post = $postService->findFirstById($id);
$this->response->setStatusCode(404);
$this->flashSession->error(t("Sorry! We can't seem to find the page you're looking for."));
return $this->dispatcher->forward([
'controller' => 'posts',
'action' => 'index',
]);
}
if ($object->getDeleted()) { if (!$post || !$postService->isPublished($post)) {
$this->response->setStatusCode(404); $this->response->setStatusCode(404);
$this->flashSession->error(t("Sorry! We can't seem to find the page you're looking for.")); $this->flashSession->error(t("Sorry! We can't seem to find the page you're looking for."));
return $this->dispatcher->forward([
'controller' => 'posts',
'action' => 'index',
]);
}
if (!$object->isPublish()) { $this->dispatcher->forward([
$this->response->setStatusCode(404);
$this->flashSession->error(t("Sorry! We can't seem to find the page you're looking for."));
return $this->dispatcher->forward([
'controller' => 'posts', 'controller' => 'posts',
'action' => 'index', 'action' => 'index',
]); ]);
return;
} }
$ipAddress = $this->request->getClientAddress(); $visitor = $userService->findFirstById($this->auth->getUserId());
$parameters = [
'postsId = ?0 AND ipaddress = ?1', if (!$hasView = $postService->hasViewsByIpAddress($post)) {
'bind' => [$id, $ipAddress] $postService->increaseNumberViews($post);
];
$viewed = PostsViews::count($parameters); if ($visitor && !$postService->isAuthorVisitor($post)) {
$userService->increaseAuthorKarmaByVisit($visitor);
// A view is stored by ipaddress
// @todo: Move this logic to separated method
if (!$viewed && $userId) {
//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->saveLogger($user->getMessages());
}
}
}
}
if (!$object->save()) {
$this->saveLogger($object->getMessages());
}
$postView = new PostsViews();
$postView->setPostsId($id);
$postView->setIpaddress($ipAddress);
if (!$postView->save()) {
$this->saveLogger($postView->getMessages());
} }
} }
$this->view->setVars( $this->view->setVars(
[ [
'post' => $object, 'post' => $post,
'form' => new ReplyForm(), 'form' => new ReplyForm(),
'votes' => $object->getVotes($id, Vote::OBJECT_POSTS), 'votes' => $post->getVotes($id, Vote::OBJECT_POSTS),
'postsReply' => $object->getPostsWithVotes($id), 'postsReply' => $post->getPostsWithVotes($id),
'commentForm' => new CommentForm(), 'commentForm' => new CommentForm(),
'userPosts' => $object->user, 'userPosts' => $post->user,
'type' => Posts::POST_QUESTIONS, 'type' => Posts::POST_QUESTIONS,
'postRelated' => Posts::postRelated($object) 'postRelated' => Posts::postRelated($post)
] ]
); );
$this->tag->setTitle($this->escaper->escapeHtml($object->getTitle())); $this->tag->setTitle($this->escaper->escapeHtml($post->getTitle()));
return $this->view->pick('single');
$this->view->pick('single');
} }
protected function addAssetsSelect() protected function addAssetsSelect()

0 comments on commit 6fdfc8c

Please sign in to comment.