DiscussionsController::index PHP Method

index() public method

Default all discussions view: chronological by most recent comment.
Since: 2.0.0
public index ( integer $Page = false )
$Page integer Multiplied by PerPage option to determine offset.
    public function index($Page = false)
    {
        $this->allowJSONP(true);
        // Figure out which discussions layout to choose (Defined on "Homepage" settings page).
        $Layout = c('Vanilla.Discussions.Layout');
        switch ($Layout) {
            case 'table':
                if ($this->SyndicationMethod == SYNDICATION_NONE) {
                    $this->View = 'table';
                }
                break;
            default:
                // $this->View = 'index';
                break;
        }
        Gdn_Theme::section('DiscussionList');
        // Remove score sort
        DiscussionModel::removeSort('top');
        // Check for the feed keyword.
        if ($Page === 'feed' && $this->SyndicationMethod != SYNDICATION_NONE) {
            $Page = 'p1';
        }
        // Determine offset from $Page
        list($Offset, $Limit) = offsetLimit($Page, c('Vanilla.Discussions.PerPage', 30), true);
        $Page = PageNumber($Offset, $Limit);
        // Allow page manipulation
        $this->EventArguments['Page'] =& $Page;
        $this->EventArguments['Offset'] =& $Offset;
        $this->EventArguments['Limit'] =& $Limit;
        $this->fireEvent('AfterPageCalculation');
        // Set canonical URL
        $this->canonicalUrl(url(ConcatSep('/', 'discussions', PageNumber($Offset, $Limit, true, false)), true));
        // We want to limit the number of pages on large databases because requesting a super-high page can kill the db.
        $MaxPages = c('Vanilla.Discussions.MaxPages');
        if ($MaxPages && $Page > $MaxPages) {
            throw notFoundException();
        }
        // Setup head.
        if (!$this->data('Title')) {
            $Title = c('Garden.HomepageTitle');
            $DefaultControllerRoute = val('Destination', Gdn::router()->GetRoute('DefaultController'));
            if ($Title && $DefaultControllerRoute == 'discussions') {
                $this->title($Title, '');
            } else {
                $this->title(t('Recent Discussions'));
            }
        }
        if (!$this->Description()) {
            $this->Description(c('Garden.Description', null));
        }
        if ($this->Head) {
            $this->Head->AddRss(url('/discussions/feed.rss', true), $this->Head->title());
        }
        // Add modules
        $this->addModule('DiscussionFilterModule');
        $this->addModule('NewDiscussionModule');
        $this->addModule('CategoriesModule');
        $this->addModule('BookmarkedModule');
        $this->setData('Breadcrumbs', array(array('Name' => t('Recent Discussions'), 'Url' => '/discussions')));
        // Set criteria & get discussions data
        $this->setData('Category', false, true);
        $DiscussionModel = new DiscussionModel();
        $DiscussionModel->setSort(Gdn::request()->get());
        $DiscussionModel->setFilters(Gdn::request()->get());
        $this->setData('Sort', $DiscussionModel->getSort());
        $this->setData('Filters', $DiscussionModel->getFilters());
        // Check for individual categories.
        $categoryIDs = $this->getCategoryIDs();
        $where = array();
        if ($categoryIDs) {
            $where['d.CategoryID'] = CategoryModel::filterCategoryPermissions($categoryIDs);
        } else {
            $DiscussionModel->Watching = true;
        }
        // Get Discussion Count
        $CountDiscussions = $DiscussionModel->getCount($where);
        if ($MaxPages) {
            $CountDiscussions = min($MaxPages * $Limit, $CountDiscussions);
        }
        $this->setData('CountDiscussions', $CountDiscussions);
        // Get Announcements
        $this->AnnounceData = $Offset == 0 ? $DiscussionModel->GetAnnouncements($where) : false;
        $this->setData('Announcements', $this->AnnounceData !== false ? $this->AnnounceData : array(), true);
        // RSS should include announcements.
        if ($this->SyndicationMethod !== SYNDICATION_NONE) {
            $Where['Announce'] = 'all';
        }
        // Get Discussions
        $this->DiscussionData = $DiscussionModel->getWhereRecent($where, $Limit, $Offset);
        $this->setData('Discussions', $this->DiscussionData, true);
        $this->setJson('Loading', $Offset . ' to ' . $Limit);
        // Build a pager
        $PagerFactory = new Gdn_PagerFactory();
        $this->EventArguments['PagerType'] = 'Pager';
        $this->fireEvent('BeforeBuildPager');
        if (!$this->data('_PagerUrl')) {
            $this->setData('_PagerUrl', 'discussions/{Page}');
        }
        $queryString = DiscussionModel::getSortFilterQueryString($DiscussionModel->getSort(), $DiscussionModel->getFilters());
        $this->setData('_PagerUrl', $this->data('_PagerUrl') . $queryString);
        $this->Pager = $PagerFactory->GetPager($this->EventArguments['PagerType'], $this);
        $this->Pager->ClientID = 'Pager';
        $this->Pager->configure($Offset, $Limit, $this->data('CountDiscussions'), $this->data('_PagerUrl'));
        PagerModule::Current($this->Pager);
        $this->setData('_Page', $Page);
        $this->setData('_Limit', $Limit);
        $this->fireEvent('AfterBuildPager');
        // Deliver JSON data if necessary
        if ($this->_DeliveryType != DELIVERY_TYPE_ALL) {
            $this->setJson('LessRow', $this->Pager->toString('less'));
            $this->setJson('MoreRow', $this->Pager->toString('more'));
            $this->View = 'discussions';
        }
        $this->render();
    }

Usage Example

コード例 #1
0
ファイル: class.qna.plugin.php プロジェクト: vanilla/addons
 /**
  *
  *
  * @param DiscussionsController $sender Sending controller instance.
  * @param array $args Event arguments.
  */
 public function discussionsController_unanswered_create($sender, $args)
 {
     $sender->View = 'Index';
     $sender->setData('_PagerUrl', 'discussions/unanswered/{Page}');
     // Be sure to display every unanswered question (ie from groups)
     $categories = CategoryModel::categories();
     $this->EventArguments['Categories'] =& $categories;
     $this->fireEvent('UnansweredBeforeSetCategories');
     $sender->setCategoryIDs(array_keys($categories));
     $sender->index(val(0, $args, 'p1'));
     $this->InUnanswered = true;
 }