<commit_before><?php

/*
  +------------------------------------------------------------------------+
  | Phosphorum                                                             |
  +------------------------------------------------------------------------+
  | Copyright (c) 2013-2014 Phalcon Team and contributors                  |
  +------------------------------------------------------------------------+
  | This source file is subject to the New BSD License that is bundled     |
  | with this package in the file docs/LICENSE.txt.                        |
  |                                                                        |
  | If you did not receive a copy of the license and are unable to         |
  | obtain it through the world-wide-web, please send an email             |
  | to <EMAIL> so we can send you a copy immediately.       |
  +------------------------------------------------------------------------+
*/

namespace Phosphorum\Controllers;

use Phosphorum\Models\Posts;
use Phosphorum\Models\PostsViews;
use Phosphorum\Models\PostsReplies;
use Phosphorum\Models\PostsBounties;
use Phosphorum\Models\PostsHistory;
use Phosphorum\Models\PostsVotes;
use Phosphorum\Models\PostsSubscribers;
use Phosphorum\Models\Categories;
use Phosphorum\Models\Activities;
use Phosphorum\Models\ActivityNotifications;
use Phosphorum\Models\IrcLog;
use Phosphorum\Models\Users;
use Phosphorum\Models\Karma;

use Phosphorum\Utils\Slug;
use Phosphorum\Search\Indexer;

use Phalcon\Http\Response;
use Phalcon\Mvc\Controller;
use Phalcon\Mvc\View;

/**
 * Class DiscussionsController
 *
 * @package Phosphorum\Controllers
 */
class DiscussionsController extends Controller
{

    const POSTS_IN_PAGE = 12;

    /**
     * This initializes the timezone in each request
     */
    public function initialize()
    {
        $timezone = $this->session->get('identity-timezone');
        if ($timezone) {
            date_default_timezone_set($timezone);
        }
        
        $this->view->limitPost    = self::POSTS_IN_PAGE;
    }

    /**
     * This method prepares the queries to be executed in each list of posts
     * The returned builders are used as base in the search, tagged list and index lists
     */
    protected function prepareQueries($joinReply = false)
    {

        /** @var \Phalcon\Mvc\Model\Query\BuilderInterface $itemBuilder */
        $itemBuilder = $this
            ->modelsManager
            ->createBuilder()
            ->from(array('p' => 'Phosphorum\Models\Posts'))
            ->orderBy('p.sticked DESC, p.created_at DESC');

        if ($joinReply) {
            $itemBuilder
                ->groupBy("p.id")
                ->join('Phosphorum\Models\PostsReplies', "r.posts_id = p.id", 'r');
        }

        $totalBuilder = clone $itemBuilder;

        $itemBuilder
            ->columns(array('p.*'))
            ->limit(self::POSTS_IN_PAGE);

        $totalBuilder
            ->columns('COUNT(*) AS count');

        return array($itemBuilder, $totalBuilder);
    }

    /**
     * Shows latest posts using an order clause
     */
    public function indexAction($order = null, $offset = 0)
    {

        /** @var \Phalcon\Mvc\Model\Query\BuilderInterface $itemBuilder */
        /** @var \Phalcon\Mvc\Model\Query\BuilderInterface $totalBuilder */

        if ($order == "answers") {
            list($itemBuilder, $totalBuilder) = $this->prepareQueries(true);
        } else {
            list($itemBuilder, $totalBuilder) = $this->prepareQueries();
        }

        /**
         * Create the conditions according to the parameter order
         */
        $userId = $this->session->get('identity');

        $params = null;
        switch ($order) {

            case 'hot':
                $this->tag->setTitle('Hot Discussions');
                $itemBuilder->orderBy('p.modified_at DESC');
                break;

            case 'my':
                $this->tag->setTitle('My Discussions');
                if ($userId) {
                    $params       = array($userId);
                    $myConditions = 'p.users_id = ?0';
                    $itemBuilder->where($myConditions);
                    $totalBuilder->where($myConditions);
                }
                break;

            case 'unanswered':
                $this->tag->setTitle('Unanswered Discussions');
                $unansweredConditions = 'p.number_replies = 0 AND p.accepted_answer <> "Y"';
                $itemBuilder->where($unansweredConditions);
                $totalBuilder->where($unansweredConditions);
                break;

            case 'answers':
                $this->tag->setTitle('My Answers');
                if ($userId) {
                    $params            = array($userId);
                    $answersConditions = 'r.users_id = ?0';
                    $itemBuilder->where($answersConditions);
                    $totalBuilder->where($answersConditions);
                }
                break;

            default:
                $this->tag->setTitle('Tips');
        }

        $notDeleteConditions = 'p.deleted = 0';
        $itemBuilder->andWhere($notDeleteConditions);
        $totalBuilder->andWhere($notDeleteConditions);

        if ($offset > 0) {
            $itemBuilder->offset((int)$offset);
        }

        $this->view->posts      = $itemBuilder->getQuery()->execute($params);
        $this->view->totalPosts = $totalBuilder->getQuery()->setUniqueRow(true)->execute($params);

        if (!$order) {
            $order = 'new';
        }

        $this->assets->addCss('css/index.css');
        $this->view->currentOrder = $order;
        $this->view->offset       = $offset;
        $this->view->paginatorUri = 'discussions/' . $order;
        $this->view->canonical    = '';

    }

    /**
     * Shows latest posts by category
     */
    public function categoryAction($categoryId, $slug, $offset = 0)
    {
        $this->tag->setTitle('Discussions');
        $this->assets->addCss('css/index.css');
        $category = Categories::findFirstById($categoryId);
        if (!$category) {
            $this->flashSession->notice('The category doesn\'t exist');
            return $this->response->redirect();
        }

        /** @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')<commit_msg>Fix title of category page<commit_after><?php

/*
  +------------------------------------------------------------------------+
  | Phosphorum                                                             |
  +------------------------------------------------------------------------+
  | Copyright (c) 2013-2014 Phalcon Team and contributors                  |
  +------------------------------------------------------------------------+
  | This source file is subject to the New BSD License that is bundled     |
  | with this package in the file docs/LICENSE.txt.                        |
  |                                                                        |
  | If you did not receive a copy of the license and are unable to         |
  | obtain it through the world-wide-web, please send an email             |
  | to <EMAIL> so we can send you a copy immediately.       |
  +------------------------------------------------------------------------+
*/

namespace Phosphorum\Controllers;

use Phosphorum\Models\Posts;
use Phosphorum\Models\PostsViews;
use Phosphorum\Models\PostsReplies;
use Phosphorum\Models\PostsBounties;
use Phosphorum\Models\PostsHistory;
use Phosphorum\Models\PostsVotes;
use Phosphorum\Models\PostsSubscribers;
use Phosphorum\Models\Categories;
use Phosphorum\Models\Activities;
use Phosphorum\Models\ActivityNotifications;
use Phosphorum\Models\IrcLog;
use Phosphorum\Models\Users;
use Phosphorum\Models\Karma;

use Phosphorum\Utils\Slug;
use Phosphorum\Search\Indexer;

use Phalcon\Http\Response;
use Phalcon\Mvc\Controller;
use Phalcon\Mvc\View;

/**
 * Class DiscussionsController
 *
 * @package Phosphorum\Controllers
 */
class DiscussionsController extends Controller
{

    const POSTS_IN_PAGE = 12;

    /**
     * This initializes the timezone in each request
     */
    public function initialize()
    {
        $timezone = $this->session->get('identity-timezone');
        if ($timezone) {
            date_default_timezone_set($timezone);
        }
        
        $this->view->limitPost    = self::POSTS_IN_PAGE;
    }

    /**
     * This method prepares the queries to be executed in each list of posts
     * The returned builders are used as base in the search, tagged list and index lists
     */
    protected function prepareQueries($joinReply = false)
    {

        /** @var \Phalcon\Mvc\Model\Query\BuilderInterface $itemBuilder */
        $itemBuilder = $this
            ->modelsManager
            ->createBuilder()
            ->from(array('p' => 'Phosphorum\Models\Posts'))
            ->orderBy('p.sticked DESC, p.created_at DESC');

        if ($joinReply) {
            $itemBuilder
                ->groupBy("p.id")
                ->join('Phosphorum\Models\PostsReplies', "r.posts_id = p.id", 'r');
        }

        $totalBuilder = clone $itemBuilder;

        $itemBuilder
            ->columns(array('p.*'))
            ->limit(self::POSTS_IN_PAGE);

        $totalBuilder
            ->columns('COUNT(*) AS count');

        return array($itemBuilder, $totalBuilder);
    }

    /**
     * Shows latest posts using an order clause
     */
    public function indexAction($order = null, $offset = 0)
    {

        /** @var \Phalcon\Mvc\Model\Query\BuilderInterface $itemBuilder */
        /** @var \Phalcon\Mvc\Model\Query\BuilderInterface $totalBuilder */

        if ($order == "answers") {
            list($itemBuilder, $totalBuilder) = $this->prepareQueries(true);
        } else {
            list($itemBuilder, $totalBuilder) = $this->prepareQueries();
        }

        /**
         * Create the conditions according to the parameter order
         */
        $userId = $this->session->get('identity');

        $params = null;
        switch ($order) {

            case 'hot':
                $this->tag->setTitle('Hot Discussions');
                $itemBuilder->orderBy('p.modified_at DESC');
                break;

            case 'my':
                $this->tag->setTitle('My Discussions');
                if ($userId) {
                    $params       = array($userId);
                    $myConditions = 'p.users_id = ?0';
                    $itemBuilder->where($myConditions);
                    $totalBuilder->where($myConditions);
                }
                break;

            case 'unanswered':
                $this->tag->setTitle('Unanswered Discussions');
                $unansweredConditions = 'p.number_replies = 0 AND p.accepted_answer <> "Y"';
                $itemBuilder->where($unansweredConditions);
                $totalBuilder->where($unansweredConditions);
                break;

            case 'answers':
                $this->tag->setTitle('My Answers');
                if ($userId) {
                    $params            = array($userId);
                    $answersConditions = 'r.users_id = ?0';
                    $itemBuilder->where($answersConditions);
                    $totalBuilder->where($answersConditions);
                }
                break;

            default:
                $this->tag->setTitle('Tips');
        }

        $notDeleteConditions = 'p.deleted = 0';
        $itemBuilder->andWhere($notDeleteConditions);
        $totalBuilder->andWhere($notDeleteConditions);

        if ($offset > 0) {
            $itemBuilder->offset((int)$offset);
        }

        $this->view->posts      = $itemBuilder->getQuery()->execute($params);
        $this->view->totalPosts = $totalBuilder->getQuery()->setUniqueRow(true)->execute($params);

        if (!$order) {
            $order = 'new';
        }

        $this->assets->addCss('css/index.css');
        $this->view->currentOrder = $order;
        $this->view->offset       = $offset;
        $this->view->paginatorUri = 'discussions/' . $order;
        $this->view->canonical    = '';

    }

    /**
     * Shows latest posts by category
     */
    public function categoryAction($categoryId, $slug, $offset = 0)
    {
        $this->assets->addCss('css/index.css');
        $category = Categories::findFirstById($categoryId);
        if (!$category) {
            $this->flashSession->notice('The category doesn\'t exist');
            return $this->response->redirect();
        }
        $this->tag->setTitle($category->name);

        /** @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')
{'fraction_non_alphanumeric': 0.16722884341503635, 'fraction_numerical': 0.0031481897908702495, 'mean_word_length': 1.67374749498998, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 148, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7236739', 'n_tokens_mistral': 3693, 'n_tokens_neox': 3472, 'n_words': 721}