コード例 #1
0
 /**
  * @return \Phalcon\Http\ResponseInterface
  */
 public function indexAction()
 {
     $this->tag->setTitle('Forum');
     $userId = $this->session->get('identity');
     $categories = Categories::find();
     $lastAuthor = [];
     $notRead = [];
     $postsPerCategory = [];
     $statement = $this->db->prepare("\n                SELECT * FROM posts p\n                JOIN topic_tracking tt ON tt.topic_id\n                WHERE CONCAT(p.id) AND NOT(FIND_IN_SET(p.id, tt.topic_id))\n                  AND p.categories_id = :cid AND tt.user_id = :uid\n            ");
     foreach ($categories as $category) {
         $posts = $category->getPosts();
         $postsPerCategory[$category->id] = $posts->count();
         if ($posts->count()) {
             $lastAuthor[$category->id] = $this->modelsManager->createBuilder()->from(['p' => 'Phosphorum\\Models\\Posts'])->where('p.categories_id = :cat_id: AND p.deleted = 0', ['cat_id' => $category->id], ['cat_id' => Column::BIND_PARAM_INT])->join('Phosphorum\\Models\\Users', 'u.id = p.users_id', 'u')->columns(['p.users_id as users_id', 'u.name as name_user', 'p.title as post1_title', 'p.slug as post1_slug', 'p.id as post1_id'])->orderBy('p.id DESC')->limit(1)->getQuery()->execute();
         } else {
             $postsPerCategory[$category->id] = 0;
             $lastAuthor[$category->id] = 0;
         }
         $notRead[$category->id] = $this->db->executePrepared($statement, ['cid' => $category->id, 'uid' => $userId], ['cid' => Column::BIND_PARAM_INT, 'uid' => Column::BIND_PARAM_INT])->rowCount();
     }
     if ($userId) {
         $check_topic = new TopicTracking();
         $check_topic->user_id = '' . $userId . '';
         $check_topic->topic_id = '9999999';
         $check_topic->create();
     }
     $this->view->setVars(['last_author' => $lastAuthor, 'not_read' => $notRead, 'logged' => $userId, 'categories' => $categories, 'posts_per_category' => $postsPerCategory]);
 }
コード例 #2
0
 /**
  * @return \Phalcon\Http\ResponseInterface
  */
 public function indexAction()
 {
     $this->tag->setTitle('Forum');
     $userId = $this->session->get('identity');
     $categories = Categories::find();
     $lastAuthor = [];
     $notRead = [];
     $postsPerCategory = [];
     foreach ($categories as $category) {
         /** @var \Phalcon\Mvc\Model\Resultset\Simple $posts */
         $posts = Posts::find("categories_id=" . $category->id);
         $postsPerCategory[$category->id] = $posts->count();
         if ($posts->count()) {
             $lastAuthor[$category->id] = $this->modelsManager->createBuilder()->from(['p' => 'Phosphorum\\Models\\Posts'])->where('p.categories_id = "' . $category->id . '"')->join('Phosphorum\\Models\\Users', "u.id = p.users_id", 'u')->columns(['p.users_id as users_id', 'u.name as name_user', 'p.title as post1_title', 'p.slug as post1_slug', 'p.id as post1_id'])->orderBy('p.id DESC')->limit(1)->getQuery()->execute();
         } else {
             $postsPerCategory[$category->id] = 0;
             $lastAuthor[$category->id] = 0;
         }
         // SQL
         $sql[$category->id] = "\n                SELECT *\n                FROM `posts` `p`\n                JOIN `topic_tracking` `tt` ON `tt`.`topic_id`\n                WHERE CONCAT(`p`.`id`)\n                  AND NOT(FIND_IN_SET(`p`.`id`, `tt`.`topic_id`))\n                  AND `p`.`categories_id` = '{$category->id}' AND `tt`.`user_id` = '{$userId}';\n            ";
         $notRead[$category->id] = $this->db->query($sql[$category->id]);
     }
     if ($userId) {
         $check_topic = new TopicTracking();
         $check_topic->user_id = '' . $userId . '';
         $check_topic->topic_id = '9999999';
         $check_topic->create();
     }
     $this->view->setVars(['last_author' => $lastAuthor, 'not_read' => $notRead, 'logged' => $userId, 'categories' => $categories, 'posts_per_category' => $postsPerCategory]);
 }
コード例 #3
0
 /**
  * Shows latest posts by category
  *
  * @param int $categoryId Category Id
  * @param string $slug Category Slug
  * @param int $offset Posts offset
  */
 public function viewAction($categoryId, $slug, $offset = 0)
 {
     if (!($category = Categories::findFirstById($categoryId))) {
         $this->flashSession->notice("The category doesn't exist");
         $this->logger->error("The category doesn't exist");
         $this->response->redirect();
         return;
     }
     $this->tag->setTitle("Discussions in category {$category->name}");
     $readposts = [];
     if ($userId = $this->session->get('identity')) {
         $ur = TopicTracking::findFirst(['user_id= ?0', 'bind' => [$userId]]);
         $readposts = $ur ? explode(',', $ur->topic_id) : [];
     }
     /**
      * @var \Phalcon\Mvc\Model\Query\BuilderInterface $itemBuilder
      * @var \Phalcon\Mvc\Model\Query\BuilderInterface $totalBuilder
      */
     list($itemBuilder, $totalBuilder) = $this->prepareQueries();
     $totalBuilder->where('p.categories_id = ?0 AND p.deleted = 0');
     $posts = $itemBuilder->where('p.categories_id = ?0 AND p.deleted = 0')->orderBy('p.created_at DESC')->offset((int) $offset)->getQuery()->execute([$categoryId]);
     if (!count($posts)) {
         $this->flashSession->notice('There are no posts in category: ' . $category->name);
         $this->response->redirect();
         return;
     }
     $totalPosts = $totalBuilder->getQuery()->setUniqueRow(true)->execute([$categoryId]);
     $this->view->setVars(['readposts' => $readposts, 'posts' => $posts, 'totalPosts' => $totalPosts, 'currentOrder' => null, 'offset' => (int) $offset, 'paginatorUri' => "category/{$category->id}/{$category->slug}", 'logged' => $userId]);
 }
コード例 #4
0
 /**
  * Displays a post and its comments
  *
  * @param int $id Post ID
  * @param string $slug Post slug
  *
  * @return \Phalcon\Http\ResponseInterface
  */
 public function viewAction($id, $slug)
 {
     $id = (int) $id;
     // Check read / unread topic
     if ($usersId = $this->session->get('identity')) {
         $check_topic = new TopicTracking();
         $check_topic->user_id = $usersId;
         $check_topic->topic_id = $id;
         if ($check_topic->create() == false) {
             $check_topic->updateTracking($id, $usersId);
         }
     }
     $this->gravatar->setSize(48);
     if (!$this->request->isPost()) {
         // Find the post using get
         $post = Posts::findFirstById($id);
         if (!$post) {
             $this->flashSession->error('The discussion does not exist');
             return $this->response->redirect();
         }
         if ($post->deleted) {
             $this->flashSession->error('The discussion is deleted');
             return $this->response->redirect();
         }
         $ipAddress = $this->request->getClientAddress();
         $parameters = ['posts_id = ?0 AND ipaddress = ?1', 'bind' => [$id, $ipAddress]];
         $viewed = PostsViews::count($parameters);
         // A view is stored by ip address
         if (!$viewed) {
             // Increase the number of views in the post
             $post->number_views++;
             if ($post->users_id != $usersId) {
                 $post->user->increaseKarma(Karma::VISIT_ON_MY_POST);
                 if ($usersId > 0) {
                     $user = Users::findFirstById($usersId);
                     if ($user) {
                         if ($user->moderator == 'Y') {
                             $user->increaseKarma(Karma::MODERATE_VISIT_POST);
                         } else {
                             $user->increaseKarma(Karma::VISIT_POST);
                         }
                         $user->save();
                     }
                 }
             }
             $postView = new PostsViews();
             $postView->post = $post;
             $postView->ipaddress = $ipAddress;
             if (!$postView->save()) {
                 foreach ($postView->getMessages() as $message) {
                     $this->flash->error($message);
                 }
             }
         }
         if (!$usersId) {
             // Enable cache
             $this->view->cache(['key' => 'post-' . $id]);
             // Check for a cache
             if ($this->viewCache->exists('post-' . $id)) {
                 return;
             }
         }
         // Generate canonical meta
         $this->view->setVars(['canonical' => 'discussion/' . $post->id . '/' . $post->slug, 'author' => $post->user]);
     } else {
         if (!$this->checkTokenPost()) {
             return $this->response->redirect();
         }
         if (!$usersId) {
             $this->flashSession->error('You must be logged in first to add a comment');
             return $this->response->redirect();
         }
         // Find the post using POST
         $post = Posts::findFirstById($this->request->getPost('id'));
         if (!$post) {
             $this->flashSession->error('The discussion does not exist');
             return $this->response->redirect();
         }
         if ($post->deleted) {
             $this->flashSession->error('The discussion is deleted');
             return $this->response->redirect();
         }
         $content = $this->request->getPost('content', 'trim');
         if ($content) {
             // Check if the question can have a bounty before add the reply
             $canHaveBounty = $post->canHaveBounty();
             $user = Users::findFirstById($usersId);
             if (!$user) {
                 $this->flashSession->error('You must be logged in first to add a comment');
                 return $this->response->redirect();
             }
             // Only update the number of replies if the user that commented isn't the same that posted
             if ($post->users_id != $usersId) {
                 $post->number_replies++;
                 $post->modified_at = time();
                 $post->user->increaseKarma(Karma::SOMEONE_REPLIED_TO_MY_POST);
                 $user->increaseKarma(Karma::REPLY_ON_SOMEONE_ELSE_POST);
                 $user->save();
             }
             $postReply = new PostsReplies();
             $postReply->post = $post;
             $postReply->in_reply_to_id = $this->request->getPost('reply-id', 'int');
             $postReply->users_id = $usersId;
             $postReply->content = $content;
             if ($postReply->save()) {
                 if ($post->users_id != $usersId && $canHaveBounty) {
                     $bounty = $post->getBounty();
                     $postBounty = new PostsBounties();
                     $postBounty->posts_id = $post->id;
                     $postBounty->users_id = $usersId;
                     $postBounty->posts_replies_id = $postReply->id;
                     $postBounty->points = $bounty['value'];
                     if (!$postBounty->save()) {
                         foreach ($postBounty->getMessages() as $message) {
                             $this->flash->error($message);
                         }
                     }
                 }
                 $href = 'discussion/' . $post->id . '/' . $post->slug . '#C' . $postReply->id;
                 return $this->response->redirect($href);
             }
             foreach ($postReply->getMessages() as $message) {
                 $this->flash->error($message);
             }
         }
     }
     /**
      * Set the post name as title - escaping it first
      */
     $this->tag->setTitle($this->escaper->escapeHtml($post->title) . ' - Discussion');
     $this->view->setVar('post', $post);
 }
コード例 #5
0
 /**
  * Displays a post and its comments
  *
  * @param int $id Post ID
  * @param string $slug Post slug [Optional]
  */
 public function viewAction($id, $slug = '')
 {
     $id = (int) $id;
     // Check read / unread topic
     if ($usersId = $this->session->get('identity')) {
         $check_topic = new TopicTracking();
         $check_topic->user_id = $usersId;
         $check_topic->topic_id = $id;
         if ($check_topic->create() == false) {
             $check_topic->updateTracking($id, $usersId);
         }
     }
     $this->gravatar->setSize(48);
     if (!$this->request->isPost()) {
         // Find the post using get
         if (!($post = Posts::findFirstById($id))) {
             $this->flashSession->error('The discussion does not exist');
             $this->response->redirect();
             return;
         }
         if ($post->deleted) {
             $this->flashSession->error('The discussion is deleted');
             $this->response->redirect();
             return;
         }
         $ipAddress = $this->request->getClientAddress();
         $parameters = ['posts_id = ?0 AND ipaddress = ?1', 'bind' => [$id, $ipAddress]];
         // A view is stored by ip address
         if (!($viewed = PostsViews::count($parameters))) {
             // Increase the number of views in the post
             $post->number_views++;
             if ($post->users_id != $usersId) {
                 $post->user->increaseKarma(Karma::VISIT_ON_MY_POST);
                 if ($user = Users::findFirstById($usersId)) {
                     $user->increaseKarma($user->moderator == 'Y' ? Karma::MODERATE_VISIT_POST : Karma::VISIT_POST);
                     $user->save();
                 }
             }
             $postView = new PostsViews();
             $postView->post = $post;
             $postView->ipaddress = $ipAddress;
             if (!$postView->save()) {
                 $this->flash->error(join('<br>', $postView->getMessages()));
             }
         }
         if (!$usersId) {
             // Enable cache
             $this->view->cache(['key' => 'post-' . $id]);
             // Check for a cache
             if ($this->viewCache->exists('post-' . $id)) {
                 return;
             }
         }
         // Generate canonical meta
         $this->view->setVars(['canonical' => "discussion/{$post->id}/{$post->slug}", 'author' => $post->user]);
     } else {
         if (!$this->checkTokenPost()) {
             $this->response->redirect();
             return;
         }
         if (!$usersId) {
             $this->flashSession->error('You must be logged in first to add a comment');
             $this->response->redirect();
             return;
         }
         // Find the post using POST
         if (!($post = Posts::findFirstById($this->request->getPost('id')))) {
             $this->flashSession->error('The discussion does not exist');
             $this->response->redirect();
             return;
         }
         if ($post->deleted) {
             $this->flashSession->error('The discussion is deleted');
             $this->response->redirect();
             return;
         }
         if ($content = $this->request->getPost('content', 'trim')) {
             // Check if the question can have a bounty before add the reply
             $canHaveBounty = $post->canHaveBounty();
             if (!($user = Users::findFirstById($usersId))) {
                 $this->flashSession->error('You must be logged in first to add a comment');
                 $this->response->redirect();
                 return;
             }
             // Only update the number of replies if the user that commented isn't the same that posted
             if ($post->users_id != $usersId) {
                 $post->number_replies++;
                 $post->modified_at = time();
                 $post->user->increaseKarma(Karma::SOMEONE_REPLIED_TO_MY_POST);
                 $user->increaseKarma(Karma::REPLY_ON_SOMEONE_ELSE_POST);
                 $user->save();
             }
             $postReply = new PostsReplies();
             $postReply->post = $post;
             $postReply->in_reply_to_id = $this->request->getPost('reply-id', 'int');
             $postReply->users_id = $usersId;
             $postReply->content = $content;
             if ($postReply->save()) {
                 if ($post->users_id != $usersId && $canHaveBounty) {
                     $bounty = $post->getBounty();
                     $postBounty = new PostsBounties();
                     $postBounty->posts_id = $post->id;
                     $postBounty->users_id = $usersId;
                     $postBounty->posts_replies_id = $postReply->id;
                     $postBounty->points = $bounty['value'];
                     if (!$postBounty->save()) {
                         $this->flash->error(join('<br>', $postBounty->getMessages()));
                     }
                 }
                 $this->response->redirect("discussion/{$post->id}/{$post->slug}#C{$postReply->id}");
                 return;
             }
             $this->flash->error(join('<br>', $postReply->getMessages()));
         }
     }
     $voting = [];
     if ($post->hasPoll()) {
         $totalVotes = $post->getPollVotes()->count();
         $votesCount = PostsPollVotes::count(['posts_id = ?0', 'group' => 'options_id', 'bind' => [$post->id]]);
         foreach ($votesCount as $row) {
             /** @var \Phalcon\Mvc\Model\Row $row */
             $voting[$row->offsetGet('options_id')] = round($row->offsetGet('rowcount') * 100 / $totalVotes, 1);
         }
     }
     // Set the post name as title - escaping it first
     $this->tag->setTitle($this->escaper->escapeHtml($post->title) . ' - Discussion');
     $this->view->setVars(['post' => $post, 'voted' => $post->isParticipatedInPoll($usersId), 'voting' => $voting]);
 }
コード例 #6
0
ファイル: DiscussionsController.php プロジェクト: Blkc/forum
 /**
  * Displays a post and its comments
  *
  * @param $id
  * @param $slug
  *
  * @return \Phalcon\Http\ResponseInterface
  */
 public function viewAction($id, $slug)
 {
     $id = (int) $id;
     $usersId = $this->session->get('identity');
     // Check read / unread topic
     if ($usersId != '') {
         $check_topic = new TopicTracking();
         $check_topic->user_id = $usersId;
         $check_topic->topic_id = $id;
         if ($check_topic->create() == false) {
             $sql = "UPDATE topic_tracking SET topic_id=IF(topic_id='',{$id}, CONCAT(topic_id,',{$id}')) WHERE user_id=:user_id AND NOT (FIND_IN_SET('{$id}', topic_id) OR FIND_IN_SET(' {$id}', topic_id));";
             $this->db->query($sql, array("user_id" => $usersId));
         } else {
         }
     }
     if (!$this->request->isPost()) {
         /**
          * Find the post using get
          */
         $post = Posts::findFirstById($id);
         if (!$post) {
             $this->flashSession->error('The discussion does not exist');
             return $this->response->redirect();
         }
         if ($post->deleted) {
             $this->flashSession->error('The discussion is deleted');
             return $this->response->redirect();
         }
         $ipAddress = $this->request->getClientAddress();
         $parameters = array('posts_id = ?0 AND ipaddress = ?1', 'bind' => array($id, $ipAddress));
         $viewed = PostsViews::count($parameters);
         /**
          * A view is stored by ipaddress
          */
         if (!$viewed) {
             /**
              * Increase the number of views in the post
              */
             $post->number_views++;
             if ($post->users_id != $usersId) {
                 $post->user->increaseKarma(Karma::VISIT_ON_MY_POST);
                 if ($usersId > 0) {
                     /** @var Users $user */
                     $user = Users::findFirstById($usersId);
                     if ($user) {
                         if ($user->moderator == 'Y') {
                             $user->increaseKarma(Karma::MODERATE_VISIT_POST);
                         } else {
                             $user->increaseKarma(Karma::VISIT_POST);
                         }
                         $user->save();
                     }
                 }
             }
             $postView = new PostsViews();
             $postView->post = $post;
             $postView->ipaddress = $ipAddress;
             if (!$postView->save()) {
                 foreach ($postView->getMessages() as $message) {
                     $this->flash->error($message);
                 }
             }
         }
         if (!$usersId) {
             /**
              * Enable cache
              */
             $this->view->cache(array('key' => 'post-' . $id));
             /**
              * Check for a cache
              */
             if ($this->viewCache->exists('post-' . $id)) {
                 return;
             }
         }
         /**
          * Generate cannonical meta
          */
         $this->view->canonical = 'discussion/' . $post->id . '/' . $post->slug;
         $this->view->author = $post->user;
     } else {
         if (!$this->checkTokenPost()) {
             return $this->response->redirect();
         }
         if (!$usersId) {
             $this->flashSession->error('You must be logged in first to add a comment');
             return $this->response->redirect();
         }
         /**
          * Find the post using POST
          */
         $post = Posts::findFirstById($this->request->getPost('id'));
         if (!$post) {
             $this->flashSession->error('The discussion does not exist');
             return $this->response->redirect();
         }
         if ($post->deleted) {
             $this->flashSession->error('The discussion is deleted');
             return $this->response->redirect();
         }
         $content = $this->request->getPost('content', 'trim');
         if ($content) {
             /**
              * Check if the question can have a bounty before add the reply
              */
             $canHaveBounty = $post->canHaveBounty();
             $user = Users::findFirstById($usersId);
             if (!$user) {
                 $this->flashSession->error('You must be logged in first to add a comment');
                 return $this->response->redirect();
             }
             /**
              * Only update the number of replies if the user that commented isn't the same that posted
              */
             if ($post->users_id != $usersId) {
                 $post->number_replies++;
                 $post->modified_at = time();
                 $post->user->increaseKarma(Karma::SOMEONE_REPLIED_TO_MY_POST);
                 $user->increaseKarma(Karma::REPLY_ON_SOMEONE_ELSE_POST);
                 $user->save();
             }
             $postReply = new PostsReplies();
             $postReply->post = $post;
             $postReply->in_reply_to_id = $this->request->getPost('reply-id', 'int');
             $postReply->users_id = $usersId;
             $postReply->content = $content;
             if ($postReply->save()) {
                 if ($post->users_id != $usersId && $canHaveBounty) {
                     $bounty = $post->getBounty();
                     $postBounty = new PostsBounties();
                     $postBounty->posts_id = $post->id;
                     $postBounty->users_id = $usersId;
                     $postBounty->posts_replies_id = $postReply->id;
                     $postBounty->points = $bounty['value'];
                     if (!$postBounty->save()) {
                         foreach ($postBounty->getMessages() as $message) {
                             $this->flash->error($message);
                         }
                     }
                 }
                 $href = 'discussion/' . $post->id . '/' . $post->slug . '#C' . $postReply->id;
                 return $this->response->redirect($href);
             }
             foreach ($postReply->getMessages() as $message) {
                 $this->flash->error($message);
             }
         }
     }
     /**
      * Set the post name as title - escaping it first
      */
     $this->tag->setTitle($this->escaper->escapeHtml($post->title) . ' - Discussion');
     $this->view->post = $post;
 }