getAncestors() public static method

Get all of the ancestor categories above this one.
public static getAncestors ( $categoryID, boolean $checkPermissions = true, boolean $includeHeadings = false ) : array
$checkPermissions boolean Whether or not to only return the categories with view permission.
$includeHeadings boolean Whether or not to include heading categories.
return array
 /**
  * Default single discussion display.
  *
  * @since 2.0.0
  * @access public
  *
  * @param int $DiscussionID Unique discussion ID
  * @param string $DiscussionStub URL-safe title slug
  * @param int $Page The current page of comments
  */
 public function index($DiscussionID = '', $DiscussionStub = '', $Page = '')
 {
     // Setup head
     $Session = Gdn::session();
     $this->addJsFile('jquery.autosize.min.js');
     $this->addJsFile('autosave.js');
     $this->addJsFile('discussion.js');
     Gdn_Theme::section('Discussion');
     // Load the discussion record
     $DiscussionID = is_numeric($DiscussionID) && $DiscussionID > 0 ? $DiscussionID : 0;
     if (!array_key_exists('Discussion', $this->Data)) {
         $this->setData('Discussion', $this->DiscussionModel->getID($DiscussionID), true);
     }
     if (!is_object($this->Discussion)) {
         $this->EventArguments['DiscussionID'] = $DiscussionID;
         $this->fireEvent('DiscussionNotFound');
         throw notFoundException('Discussion');
     }
     // Define the query offset & limit.
     $Limit = c('Vanilla.Comments.PerPage', 30);
     $OffsetProvided = $Page != '';
     list($Offset, $Limit) = offsetLimit($Page, $Limit);
     // Check permissions
     $this->permission('Vanilla.Discussions.View', true, 'Category', $this->Discussion->PermissionCategoryID);
     $this->setData('CategoryID', $this->CategoryID = $this->Discussion->CategoryID, true);
     if (strcasecmp(val('Type', $this->Discussion), 'redirect') === 0) {
         $this->redirectDiscussion($this->Discussion);
     }
     $Category = CategoryModel::categories($this->Discussion->CategoryID);
     $this->setData('Category', $Category);
     if ($CategoryCssClass = val('CssClass', $Category)) {
         Gdn_Theme::section($CategoryCssClass);
     }
     $this->setData('Breadcrumbs', CategoryModel::getAncestors($this->CategoryID));
     // Setup
     $this->title($this->Discussion->Name);
     // Actual number of comments, excluding the discussion itself.
     $ActualResponses = $this->Discussion->CountComments;
     $this->Offset = $Offset;
     if (c('Vanilla.Comments.AutoOffset')) {
         //         if ($this->Discussion->CountCommentWatch > 1 && $OffsetProvided == '')
         //            $this->addDefinition('ScrollTo', 'a[name=Item_'.$this->Discussion->CountCommentWatch.']');
         if (!is_numeric($this->Offset) || $this->Offset < 0 || !$OffsetProvided) {
             // Round down to the appropriate offset based on the user's read comments & comments per page
             $CountCommentWatch = $this->Discussion->CountCommentWatch > 0 ? $this->Discussion->CountCommentWatch : 0;
             if ($CountCommentWatch > $ActualResponses) {
                 $CountCommentWatch = $ActualResponses;
             }
             // (((67 comments / 10 perpage) = 6.7) rounded down = 6) * 10 perpage = offset 60;
             $this->Offset = floor($CountCommentWatch / $Limit) * $Limit;
         }
         if ($ActualResponses <= $Limit) {
             $this->Offset = 0;
         }
         if ($this->Offset == $ActualResponses) {
             $this->Offset -= $Limit;
         }
     } else {
         if ($this->Offset == '') {
             $this->Offset = 0;
         }
     }
     if ($this->Offset < 0) {
         $this->Offset = 0;
     }
     $LatestItem = $this->Discussion->CountCommentWatch;
     if ($LatestItem === null) {
         $LatestItem = 0;
     } elseif ($LatestItem < $this->Discussion->CountComments) {
         $LatestItem += 1;
     }
     $this->setData('_LatestItem', $LatestItem);
     // Set the canonical url to have the proper page title.
     $this->canonicalUrl(discussionUrl($this->Discussion, pageNumber($this->Offset, $Limit, 0, false)));
     //      url(ConcatSep('/', 'discussion/'.$this->Discussion->DiscussionID.'/'. Gdn_Format::url($this->Discussion->Name), PageNumber($this->Offset, $Limit, TRUE, Gdn::session()->UserID != 0)), true), Gdn::session()->UserID == 0);
     // Load the comments
     $this->setData('Comments', $this->CommentModel->get($DiscussionID, $Limit, $this->Offset));
     $PageNumber = PageNumber($this->Offset, $Limit);
     $this->setData('Page', $PageNumber);
     $this->_SetOpenGraph();
     include_once PATH_LIBRARY . '/vendors/simplehtmldom/simple_html_dom.php';
     if ($PageNumber == 1) {
         $this->description(sliceParagraph(Gdn_Format::plainText($this->Discussion->Body, $this->Discussion->Format), 160));
         // Add images to head for open graph
         $Dom = str_get_html(Gdn_Format::to($this->Discussion->Body, $this->Discussion->Format));
     } else {
         $this->Data['Title'] .= sprintf(t(' - Page %s'), PageNumber($this->Offset, $Limit));
         $FirstComment = $this->data('Comments')->firstRow();
         $FirstBody = val('Body', $FirstComment);
         $FirstFormat = val('Format', $FirstComment);
         $this->description(sliceParagraph(Gdn_Format::plainText($FirstBody, $FirstFormat), 160));
         // Add images to head for open graph
         $Dom = str_get_html(Gdn_Format::to($FirstBody, $FirstFormat));
     }
     if ($Dom) {
         foreach ($Dom->find('img') as $img) {
             if (isset($img->src)) {
                 $this->image($img->src);
             }
         }
     }
     // Queue notification.
     if ($this->Request->get('new') && c('Vanilla.QueueNotifications')) {
         $this->addDefinition('NotifyNewDiscussion', 1);
     }
     // Make sure to set the user's discussion watch records if this is not an API request.
     if ($this->deliveryType() !== DELIVERY_TYPE_DATA) {
         $this->CommentModel->SetWatch($this->Discussion, $Limit, $this->Offset, $this->Discussion->CountComments);
     }
     // Build a pager
     $PagerFactory = new Gdn_PagerFactory();
     $this->EventArguments['PagerType'] = 'Pager';
     $this->fireEvent('BeforeBuildPager');
     $this->Pager = $PagerFactory->getPager($this->EventArguments['PagerType'], $this);
     $this->Pager->ClientID = 'Pager';
     $this->Pager->configure($this->Offset, $Limit, $ActualResponses, array('DiscussionUrl'));
     $this->Pager->Record = $this->Discussion;
     PagerModule::current($this->Pager);
     $this->fireEvent('AfterBuildPager');
     // Define the form for the comment input
     $this->Form = Gdn::Factory('Form', 'Comment');
     $this->Form->Action = url('/post/comment/');
     $this->DiscussionID = $this->Discussion->DiscussionID;
     $this->Form->addHidden('DiscussionID', $this->DiscussionID);
     $this->Form->addHidden('CommentID', '');
     // Look in the session stash for a comment
     $StashComment = $Session->getPublicStash('CommentForDiscussionID_' . $this->Discussion->DiscussionID);
     if ($StashComment) {
         $this->Form->setValue('Body', $StashComment);
         $this->Form->setFormValue('Body', $StashComment);
     }
     // Retrieve & apply the draft if there is one:
     if (Gdn::session()->UserID) {
         $DraftModel = new DraftModel();
         $Draft = $DraftModel->get($Session->UserID, 0, 1, $this->Discussion->DiscussionID)->firstRow();
         $this->Form->addHidden('DraftID', $Draft ? $Draft->DraftID : '');
         if ($Draft && !$this->Form->isPostBack()) {
             $this->Form->setValue('Body', $Draft->Body);
             $this->Form->setValue('Format', $Draft->Format);
         }
     }
     // 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 = 'comments';
     }
     // Inform moderator of checked comments in this discussion
     $CheckedComments = $Session->getAttribute('CheckedComments', array());
     if (count($CheckedComments) > 0) {
         ModerationController::informCheckedComments($this);
     }
     // Add modules
     $this->addModule('DiscussionFilterModule');
     $this->addModule('NewDiscussionModule');
     $this->addModule('CategoriesModule');
     $this->addModule('BookmarkedModule');
     $this->CanEditComments = Gdn::session()->checkPermission('Vanilla.Comments.Edit', true, 'Category', 'any') && c('Vanilla.AdminCheckboxes.Use');
     // Report the discussion id so js can use it.
     $this->addDefinition('DiscussionID', $DiscussionID);
     $this->addDefinition('Category', $this->data('Category.Name'));
     $this->fireEvent('BeforeDiscussionRender');
     $AttachmentModel = AttachmentModel::instance();
     if (AttachmentModel::enabled()) {
         $AttachmentModel->joinAttachments($this->Data['Discussion'], $this->Data['Comments']);
         $this->fireEvent('FetchAttachmentViews');
         if ($this->deliveryMethod() === DELIVERY_METHOD_XHTML) {
             require_once $this->fetchViewLocation('attachment', 'attachments', 'dashboard');
         }
     }
     $this->render();
 }
 /**
  * Move a category to a different parent.
  *
  * @param int $categoryID Unique ID for the category to move.
  * @throws Exception if category is not found.
  */
 public function moveCategory($categoryID)
 {
     // Check permission
     $this->permission(['Garden.Community.Manage', 'Garden.Settings.Manage'], false);
     $category = CategoryModel::categories($categoryID);
     if (!$category) {
         throw notFoundException();
     }
     $this->Form->setModel($this->CategoryModel);
     $this->Form->addHidden('CategoryID', $categoryID);
     $this->setData('Category', $category);
     $parentCategories = CategoryModel::getAncestors($categoryID);
     array_pop($parentCategories);
     if (!empty($parentCategories)) {
         $this->setData('ParentCategories', array_column($parentCategories, 'Name', 'CategoryID'));
     }
     if ($this->Form->authenticatedPostBack()) {
         // Verify we're only attempting to save specific values.
         $this->Form->formValues(['CategoryID' => $this->Form->getValue('CategoryID'), 'ParentCategoryID' => $this->Form->getValue('ParentCategoryID')]);
         $this->Form->save();
     } else {
         $this->Form->setData($category);
     }
     $this->render();
 }
Example #3
0
 /**
  * Create or update a discussion.
  *
  * @since 2.0.0
  * @access public
  *
  * @param int $CategoryID Unique ID of the category to add the discussion to.
  */
 public function discussion($CategoryUrlCode = '')
 {
     // Override CategoryID if categories are disabled
     $UseCategories = $this->ShowCategorySelector = (bool) c('Vanilla.Categories.Use');
     if (!$UseCategories) {
         $CategoryUrlCode = '';
     }
     // Setup head
     $this->addJsFile('jquery.autosize.min.js');
     $this->addJsFile('autosave.js');
     $this->addJsFile('post.js');
     $Session = Gdn::session();
     Gdn_Theme::section('PostDiscussion');
     // Set discussion, draft, and category data
     $DiscussionID = isset($this->Discussion) ? $this->Discussion->DiscussionID : '';
     $DraftID = isset($this->Draft) ? $this->Draft->DraftID : 0;
     $Category = false;
     $CategoryModel = new CategoryModel();
     if (isset($this->Discussion)) {
         $this->CategoryID = $this->Discussion->CategoryID;
         $Category = CategoryModel::categories($this->CategoryID);
     } elseif ($CategoryUrlCode != '') {
         $Category = CategoryModel::categories($CategoryUrlCode);
         if ($Category) {
             $this->CategoryID = val('CategoryID', $Category);
         }
     }
     if ($Category) {
         $this->Category = (object) $Category;
         $this->setData('Category', $Category);
         $this->Form->addHidden('CategoryID', $this->Category->CategoryID);
         if (val('DisplayAs', $this->Category) == 'Discussions' && !$DraftID) {
             $this->ShowCategorySelector = false;
         } else {
             // Get all our subcategories to add to the category if we are in a Header or Categories category.
             $this->Context = CategoryModel::getSubtree($this->CategoryID);
         }
     } else {
         $this->CategoryID = 0;
         $this->Category = null;
     }
     $CategoryData = $this->ShowCategorySelector ? CategoryModel::categories() : false;
     // Check permission
     if (isset($this->Discussion)) {
         // Make sure that content can (still) be edited.
         $CanEdit = DiscussionModel::canEdit($this->Discussion);
         if (!$CanEdit) {
             throw permissionException('Vanilla.Discussions.Edit');
         }
         // Make sure only moderators can edit closed things
         if ($this->Discussion->Closed) {
             $this->permission('Vanilla.Discussions.Edit', true, 'Category', $this->Category->PermissionCategoryID);
         }
         $this->Form->setFormValue('DiscussionID', $this->Discussion->DiscussionID);
         $this->title(t('Edit Discussion'));
         if ($this->Discussion->Type) {
             $this->setData('Type', $this->Discussion->Type);
         } else {
             $this->setData('Type', 'Discussion');
         }
     } else {
         // Permission to add.
         if ($this->Category) {
             $this->permission('Vanilla.Discussions.Add', true, 'Category', $this->Category->PermissionCategoryID);
         } else {
             $this->permission('Vanilla.Discussions.Add');
         }
         $this->title(t('New Discussion'));
     }
     touchValue('Type', $this->Data, 'Discussion');
     // See if we should hide the category dropdown.
     if ($this->ShowCategorySelector) {
         $AllowedCategories = CategoryModel::getByPermission('Discussions.Add', $this->Form->getValue('CategoryID', $this->CategoryID), ['Archived' => 0, 'AllowDiscussions' => 1], ['AllowedDiscussionTypes' => $this->Data['Type']]);
         if (count($AllowedCategories) == 1) {
             $AllowedCategory = array_pop($AllowedCategories);
             $this->ShowCategorySelector = false;
             $this->Form->addHidden('CategoryID', $AllowedCategory['CategoryID']);
             if ($this->Form->isPostBack() && !$this->Form->getFormValue('CategoryID')) {
                 $this->Form->setFormValue('CategoryID', $AllowedCategory['CategoryID']);
             }
         }
     }
     // Set the model on the form
     $this->Form->setModel($this->DiscussionModel);
     if (!$this->Form->isPostBack()) {
         // Prep form with current data for editing
         if (isset($this->Discussion)) {
             $this->Form->setData($this->Discussion);
         } elseif (isset($this->Draft)) {
             $this->Form->setData($this->Draft);
         } else {
             if ($this->Category !== null) {
                 $this->Form->setData(array('CategoryID' => $this->Category->CategoryID));
             }
             $this->populateForm($this->Form);
         }
     } elseif ($this->Form->authenticatedPostBack()) {
         // Form was submitted
         // Save as a draft?
         $FormValues = $this->Form->formValues();
         $FormValues = $this->DiscussionModel->filterForm($FormValues);
         $this->deliveryType(Gdn::request()->getValue('DeliveryType', $this->_DeliveryType));
         if ($DraftID == 0) {
             $DraftID = $this->Form->getFormValue('DraftID', 0);
         }
         $Draft = $this->Form->buttonExists('Save_Draft') ? true : false;
         $Preview = $this->Form->buttonExists('Preview') ? true : false;
         if (!$Preview) {
             if (!is_object($this->Category) && is_array($CategoryData) && isset($FormValues['CategoryID'])) {
                 $this->Category = val($FormValues['CategoryID'], $CategoryData);
             }
             if (is_object($this->Category)) {
                 // Check category permissions.
                 if ($this->Form->getFormValue('Announce', '') && !$Session->checkPermission('Vanilla.Discussions.Announce', true, 'Category', $this->Category->PermissionCategoryID)) {
                     $this->Form->addError('You do not have permission to announce in this category', 'Announce');
                 }
                 if ($this->Form->getFormValue('Close', '') && !$Session->checkPermission('Vanilla.Discussions.Close', true, 'Category', $this->Category->PermissionCategoryID)) {
                     $this->Form->addError('You do not have permission to close in this category', 'Close');
                 }
                 if ($this->Form->getFormValue('Sink', '') && !$Session->checkPermission('Vanilla.Discussions.Sink', true, 'Category', $this->Category->PermissionCategoryID)) {
                     $this->Form->addError('You do not have permission to sink in this category', 'Sink');
                 }
                 if (!isset($this->Discussion) && (!$Session->checkPermission('Vanilla.Discussions.Add', true, 'Category', $this->Category->PermissionCategoryID) || !$this->Category->AllowDiscussions)) {
                     $this->Form->addError('You do not have permission to start discussions in this category', 'CategoryID');
                 }
             }
             $isTitleValid = true;
             $Name = trim($this->Form->getFormValue('Name', ''));
             if (!$Draft) {
                 // Let's be super aggressive and disallow titles with no word characters in them!
                 $hasWordCharacter = preg_match('/\\w/u', $Name) === 1;
                 if (!$hasWordCharacter || $Name != '' && Gdn_Format::text($Name) == '') {
                     $this->Form->addError(t('You have entered an invalid discussion title'), 'Name');
                     $isTitleValid = false;
                 }
             }
             if ($isTitleValid) {
                 // Trim the name.
                 $FormValues['Name'] = $Name;
                 $this->Form->setFormValue('Name', $Name);
             }
             if ($this->Form->errorCount() == 0) {
                 if ($Draft) {
                     $DraftID = $this->DraftModel->save($FormValues);
                     $this->Form->setValidationResults($this->DraftModel->validationResults());
                 } else {
                     $DiscussionID = $this->DiscussionModel->save($FormValues);
                     $this->Form->setValidationResults($this->DiscussionModel->validationResults());
                     if ($DiscussionID > 0) {
                         if ($DraftID > 0) {
                             $this->DraftModel->delete($DraftID);
                         }
                     }
                     if ($DiscussionID == SPAM || $DiscussionID == UNAPPROVED) {
                         $this->StatusMessage = t('DiscussionRequiresApprovalStatus', 'Your discussion will appear after it is approved.');
                         // Clear out the form so that a draft won't save.
                         $this->Form->formValues(array());
                         $this->render('Spam');
                         return;
                     }
                 }
             }
         } else {
             // If this was a preview click, create a discussion/comment shell with the values for this comment
             $this->Discussion = new stdClass();
             $this->Discussion->Name = $this->Form->getValue('Name', '');
             $this->Comment = new stdClass();
             $this->Comment->InsertUserID = $Session->User->UserID;
             $this->Comment->InsertName = $Session->User->Name;
             $this->Comment->InsertPhoto = $Session->User->Photo;
             $this->Comment->DateInserted = Gdn_Format::date();
             $this->Comment->Body = val('Body', $FormValues, '');
             $this->Comment->Format = val('Format', $FormValues, c('Garden.InputFormatter'));
             $this->EventArguments['Discussion'] =& $this->Discussion;
             $this->EventArguments['Comment'] =& $this->Comment;
             $this->fireEvent('BeforeDiscussionPreview');
             if ($this->_DeliveryType == DELIVERY_TYPE_ALL) {
                 $this->addAsset('Content', $this->fetchView('preview'));
             } else {
                 $this->View = 'preview';
             }
         }
         if ($this->Form->errorCount() > 0) {
             // Return the form errors
             $this->errorMessage($this->Form->errors());
         } elseif ($DiscussionID > 0 || $DraftID > 0) {
             // Make sure that the ajax request form knows about the newly created discussion or draft id
             $this->setJson('DiscussionID', $DiscussionID);
             $this->setJson('DraftID', $DraftID);
             if (!$Preview) {
                 // If the discussion was not a draft
                 if (!$Draft) {
                     // Redirect to the new discussion
                     $Discussion = $this->DiscussionModel->getID($DiscussionID, DATASET_TYPE_OBJECT, array('Slave' => false));
                     $this->setData('Discussion', $Discussion);
                     $this->EventArguments['Discussion'] = $Discussion;
                     $this->fireEvent('AfterDiscussionSave');
                     if ($this->_DeliveryType == DELIVERY_TYPE_ALL) {
                         redirect(discussionUrl($Discussion, 1)) . '?new=1';
                     } else {
                         $this->RedirectUrl = discussionUrl($Discussion, 1, true) . '?new=1';
                     }
                 } else {
                     // If this was a draft save, notify the user about the save
                     $this->informMessage(sprintf(t('Draft saved at %s'), Gdn_Format::date()));
                 }
             }
         }
     }
     // Add hidden fields for editing
     $this->Form->addHidden('DiscussionID', $DiscussionID);
     $this->Form->addHidden('DraftID', $DraftID, true);
     $this->fireEvent('BeforeDiscussionRender');
     if ($this->CategoryID) {
         $Breadcrumbs = CategoryModel::getAncestors($this->CategoryID);
     } else {
         $Breadcrumbs = array();
     }
     $Breadcrumbs[] = array('Name' => $this->data('Title'), 'Url' => val('AddUrl', val($this->data('Type'), DiscussionModel::discussionTypes()), '/post/discussion'));
     $this->setData('Breadcrumbs', $Breadcrumbs);
     $this->setData('_AnnounceOptions', $this->announceOptions());
     // Render view (posts/discussion.php or post/preview.php)
     $this->render();
 }
 /**
  * Show all discussions in a particular category.
  *
  * @since 2.0.0
  * @access public
  *
  * @param string $CategoryIdentifier Unique category slug or ID.
  * @param int $Offset Number of discussions to skip.
  */
 public function index($CategoryIdentifier = '', $Page = '0')
 {
     // Figure out which category layout to choose (Defined on "Homepage" settings page).
     $Layout = c('Vanilla.Categories.Layout');
     if ($CategoryIdentifier == '') {
         switch ($Layout) {
             case 'mixed':
                 $this->View = 'discussions';
                 $this->discussions();
                 break;
             case 'table':
                 $this->table();
                 break;
             default:
                 $this->View = 'all';
                 $this->all('', CategoryModel::getRootDisplayAs());
                 break;
         }
         return;
     } else {
         $Category = CategoryModel::categories($CategoryIdentifier);
         if (empty($Category)) {
             throw notFoundException();
         }
         $Category = (object) $Category;
         Gdn_Theme::section($Category->CssClass);
         // Load the breadcrumbs.
         $this->setData('Breadcrumbs', CategoryModel::getAncestors(val('CategoryID', $Category)));
         $this->setData('Category', $Category, true);
         $this->title(htmlspecialchars(val('Name', $Category, '')));
         $this->description(val('Description', $Category), true);
         switch ($Category->DisplayAs) {
             case 'Flat':
             case 'Heading':
             case 'Categories':
                 $stopHeadings = val('Depth', $Category) > CategoryModel::instance()->getNavDepth();
                 CategoryModel::instance()->setStopHeadingsCalculation($stopHeadings);
                 if ($this->SyndicationMethod != SYNDICATION_NONE) {
                     // RSS can't show a category list so just tell it to expand all categories.
                     saveToConfig('Vanilla.ExpandCategories', true, false);
                 } else {
                     // This category is an overview style category and displays as a category list.
                     switch ($Layout) {
                         case 'mixed':
                             $this->View = 'discussions';
                             $this->discussions($CategoryIdentifier);
                             break;
                         case 'table':
                             $this->table($CategoryIdentifier, $Category->DisplayAs);
                             break;
                         default:
                             $this->View = 'all';
                             $this->All($CategoryIdentifier, $Category->DisplayAs);
                             break;
                     }
                     return;
                 }
                 break;
         }
         Gdn_Theme::section('DiscussionList');
         // 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;
         }
         $this->setData('CategoryTree', $this->getCategoryTree($CategoryIdentifier, val('DisplayAs', $Category)));
         // Add a backwards-compatibility shim for the old categories.
         $this->categoriesCompatibilityCallback = function () use($CategoryIdentifier) {
             $categories = CategoryModel::getSubtree($CategoryIdentifier, false);
             return $categories;
         };
         // Setup head
         $this->Menu->highlightRoute('/discussions');
         if ($this->Head) {
             $this->addJsFile('discussions.js');
             $this->Head->addRss(categoryUrl($Category) . '/feed.rss', $this->Head->title());
         }
         // Set CategoryID
         $CategoryID = val('CategoryID', $Category);
         $this->setData('CategoryID', $CategoryID, true);
         // Add modules
         $this->addModule('NewDiscussionModule');
         $this->addModule('DiscussionFilterModule');
         $this->addModule('CategoriesModule');
         $this->addModule('BookmarkedModule');
         // Get a DiscussionModel
         $DiscussionModel = new DiscussionModel();
         $DiscussionModel->setSort(Gdn::request()->get());
         $DiscussionModel->setFilters(Gdn::request()->get());
         $this->setData('Sort', $DiscussionModel->getSort());
         $this->setData('Filters', $DiscussionModel->getFilters());
         $CategoryIDs = array($CategoryID);
         if (c('Vanilla.ExpandCategories')) {
             $CategoryIDs = array_merge($CategoryIDs, array_column($this->data('Categories'), 'CategoryID'));
         }
         $Wheres = array('d.CategoryID' => $CategoryIDs);
         $this->setData('_ShowCategoryLink', count($CategoryIDs) > 1);
         // Check permission
         $this->permission('Vanilla.Discussions.View', true, 'Category', val('PermissionCategoryID', $Category));
         // Set discussion meta data.
         $this->EventArguments['PerPage'] = c('Vanilla.Discussions.PerPage', 30);
         $this->fireEvent('BeforeGetDiscussions');
         list($Offset, $Limit) = offsetLimit($Page, $this->EventArguments['PerPage']);
         if (!is_numeric($Offset) || $Offset < 0) {
             $Offset = 0;
         }
         $Page = PageNumber($Offset, $Limit);
         // Allow page manipulation
         $this->EventArguments['Page'] =& $Page;
         $this->EventArguments['Offset'] =& $Offset;
         $this->EventArguments['Limit'] =& $Limit;
         $this->fireEvent('AfterPageCalculation');
         // We want to limit the number of pages on large databases because requesting a super-high page can kill the db.
         $MaxPages = c('Vanilla.Categories.MaxPages');
         if ($MaxPages && $Page > $MaxPages) {
             throw notFoundException();
         }
         $CountDiscussions = $DiscussionModel->getCount($Wheres);
         if ($MaxPages && $MaxPages * $Limit < $CountDiscussions) {
             $CountDiscussions = $MaxPages * $Limit;
         }
         $this->setData('CountDiscussions', $CountDiscussions);
         $this->setData('_Limit', $Limit);
         // We don't wan't child categories in announcements.
         $Wheres['d.CategoryID'] = $CategoryID;
         $AnnounceData = $DiscussionModel->getAnnouncements($Wheres, $Offset, $Limit);
         $this->AnnounceData = $this->setData('Announcements', $AnnounceData);
         $Wheres['d.CategoryID'] = $CategoryIDs;
         // RSS should include announcements.
         if ($this->SyndicationMethod !== SYNDICATION_NONE) {
             $Wheres['Announce'] = 'all';
         }
         $this->DiscussionData = $this->setData('Discussions', $DiscussionModel->getWhereRecent($Wheres, $Limit, $Offset));
         // Build a pager
         $PagerFactory = new Gdn_PagerFactory();
         $url = CategoryUrl($CategoryIdentifier);
         $this->EventArguments['PagerType'] = 'Pager';
         $this->fireEvent('BeforeBuildPager');
         if (!$this->data('_PagerUrl')) {
             $this->setData('_PagerUrl', $url . '/{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, $CountDiscussions, $this->data('_PagerUrl'));
         $this->Pager->Record = $Category;
         PagerModule::current($this->Pager);
         $this->setData('_Page', $Page);
         $this->setData('_Limit', $Limit);
         $this->fireEvent('AfterBuildPager');
         // Set the canonical Url.
         $this->canonicalUrl(categoryUrl($Category, pageNumber($Offset, $Limit)));
         // Change the controller name so that it knows to grab the discussion views
         $this->ControllerName = 'DiscussionsController';
         // Pick up the discussions class
         $this->CssClass = 'Discussions Category-' . val('UrlCode', $Category);
         // 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';
         }
         // Render default view.
         $this->fireEvent('BeforeCategoriesRender');
         $this->render();
     }
 }