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
*/
protected function getDI()
public function getDI()
{
return $this->di;
}
@@ -77,7 +77,7 @@ protected function getDI()
* @param DiInterface $di
* @return $this
*/
protected function setDI(DiInterface $di)
public function setDI(DiInterface $di)
{
$this->di = $di;
@@ -19,8 +19,8 @@
use Phanbook\Models\Karma;
use Phanbook\Models\Users;
use Phanbook\Models\ModelBase;
use Phanbook\Models\PostsViews;
use Phanbook\Models\PostsHistory;
use Phanbook\Models\Services\Service;
use Phanbook\Frontend\Forms\ReplyForm;
use Phanbook\Frontend\Forms\CommentForm;
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
*
* @return \Phalcon\Mvc\View|void
*/
public function viewAction($id, $slug)
{
$id = (int) $id;
$userId = $this->auth->getUserId();
$postService = new Service\Post();
$userService = new Service\User();
if (!$object = Posts::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',
]);
}
$post = $postService->findFirstById($id);
if ($object->getDeleted()) {
if (!$post || !$postService->isPublished($post)) {
$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->isPublish()) {
$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([
$this->dispatcher->forward([
'controller' => 'posts',
'action' => 'index',
]);
return;
}
$ipAddress = $this->request->getClientAddress();
$parameters = [
'postsId = ?0 AND ipaddress = ?1',
'bind' => [$id, $ipAddress]
];
$viewed = PostsViews::count($parameters);
// 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());
$visitor = $userService->findFirstById($this->auth->getUserId());
if (!$hasView = $postService->hasViewsByIpAddress($post)) {
$postService->increaseNumberViews($post);
if ($visitor && !$postService->isAuthorVisitor($post)) {
$userService->increaseAuthorKarmaByVisit($visitor);
}
}
$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)
'post' => $post,
'form' => new ReplyForm(),
'votes' => $post->getVotes($id, Vote::OBJECT_POSTS),
'postsReply' => $post->getPostsWithVotes($id),
'commentForm' => new CommentForm(),
'userPosts' => $post->user,
'type' => Posts::POST_QUESTIONS,
'postRelated' => Posts::postRelated($post)
]
);
$this->tag->setTitle($this->escaper->escapeHtml($object->getTitle()));
return $this->view->pick('single');
$this->tag->setTitle($this->escaper->escapeHtml($post->getTitle()));
$this->view->pick('single');
}
protected function addAssetsSelect()

0 comments on commit 6fdfc8c

Please sign in to comment.