Example #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]);
 }
Example #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]);
 }
Example #3
0
 /**
  * Check whether the user can have the badge
  *
  * @param  Users $user
  * @return boolean
  */
 public function canHave(Users $user)
 {
     $ids = [];
     $categories = $this->getExpertQuery($user)->execute([$user->id]);
     foreach ($categories as $categoryRow) {
         $category = Categories::findFirstById($categoryRow->categories_id);
         if ($category) {
             $ids[] = $category;
         }
     }
     return $ids;
 }
Example #4
0
 /**
  * Reload categories
  */
 public function reloadCategoriesAction()
 {
     $this->view->setVar('categories', Categories::find(['order' => 'number_posts DESC, name']));
     $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
     $this->view->getCache()->delete('sidebar');
 }
Example #5
0
 /**
  * Returns those categories that don't have bounties
  *
  * @return array
  */
 public function getFullNoBountyCategories()
 {
     if (!$this->fullNoBountyCategories) {
         $this->fullNoBountyCategories = Categories::find('no_bounty = "Y"')->toArray();
     }
     return $this->fullNoBountyCategories;
 }
 /**
  *
  */
 public function reloadCategoriesAction()
 {
     $parameters = array('order' => 'number_posts DESC, name');
     $this->view->categories = Categories::find($parameters);
     $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
     $this->view->getCache()->delete('sidebar');
 }
Example #7
0
use Phalcon\Logger\Adapter\Stream;
use Phalcon\Tag;
use Faker\Factory as Faker;
/**
 * This script generates random posts
 */
require 'cli-bootstrap.php';
$faker = Faker::create();
$log = new Stream('php://stdout');
$log->info('Start');
/** @var Phalcon\Db\AdapterInterface $database */
$database = $di->getShared('db');
$database->begin();
for ($i = 0; $i <= 20; $i++) {
    $title = $faker->company;
    $category = new Categories();
    $category->name = $title;
    $category->description = $faker->sentence;
    $category->slug = Tag::friendlyTitle($title);
    $category->number_posts = 0;
    $category->no_bounty = 'N';
    $category->no_digest = 'N';
    if (!$category->save()) {
        $database->rollback();
        die(join(PHP_EOL, $category->getMessages()));
    }
    $log->info('Category: ' . $category->name);
}
for ($i = 0; $i <= 50; $i++) {
    $user = new Users();
    $user->name = $faker->name;
Example #8
0
 /**
  * This shows the create post form and also store the related post
  *
  * @param int $id Post ID
  */
 public function editAction($id)
 {
     if (!($usersId = $this->session->get('identity'))) {
         $this->flashSession->error('You must be logged first');
         $this->response->redirect();
         return;
     }
     $parameters = ["id = ?0 AND (users_id = ?1 OR 'Y' = ?2)", 'bind' => [$id, $usersId, $this->session->get('identity-moderator')]];
     if (!($post = Posts::findFirst($parameters))) {
         $this->flashSession->error('The discussion does not exist');
         $this->response->redirect();
         return;
     }
     if ($this->request->isPost()) {
         if (!$this->checkTokenPost()) {
             $this->response->redirect();
             return;
         }
         $title = $this->request->getPost('title', 'trim');
         $content = $this->request->getPost('content');
         /** @var \Phalcon\Db\Adapter\Pdo\Mysql $connection */
         $connection = $this->getDI()->getShared('db');
         $connection->begin();
         $post->categories_id = $this->request->getPost('categoryId');
         $post->title = $title;
         $post->slug = $this->slug->generate($title);
         $post->content = $content;
         $post->edited_at = time();
         if (!$post->hasPoll() || !$post->isStartVoting()) {
             foreach ($post->getPollOptions() as $option) {
                 $option->delete();
             }
             if ($pollOptions = $this->request->getPost('pollOptions', ['trim'], [])) {
                 foreach ($pollOptions as $opt) {
                     $option = new PostsPollOptions();
                     $option->posts_id = $post->id;
                     $option->title = htmlspecialchars($opt, ENT_QUOTES);
                     $option->save();
                 }
             }
         }
         if ($post->isStartVoting()) {
             $connection->rollback();
             $this->flashSession->error("The voting for the poll was started. You can't change the Poll.");
         } elseif ($post->save()) {
             if ($post->users_id != $usersId && ($user = Users::findFirstById($usersId))) {
                 $user->increaseKarma(Karma::MODERATE_POST);
                 $user->save();
             }
             $connection->commit();
             $this->response->redirect("discussion/{$post->id}/{$post->slug}");
             return;
         } else {
             $connection->rollback();
             $this->flashSession->error(join('<br>', $post->getMessages()));
         }
     } else {
         $this->tag->displayTo('id', $post->id);
         $this->tag->displayTo('title', $post->title);
         $this->tag->displayTo('content', $post->content);
         $this->tag->displayTo('categoryId', $post->categories_id);
     }
     $this->tag->setTitle('Edit Discussion: ' . $this->escaper->escapeHtml($post->title));
     $this->gravatar->setSize(48);
     $this->view->setVars(['categories' => Categories::find(['order' => 'name']), 'post' => $post, 'optionsCount' => $post->pollOptions->count()]);
 }