Advertisement
Guest User

class.categoriescontroller.php

a guest
Dec 16th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.10 KB | None | 0 0
  1. <?php if (!defined('APPLICATION')) exit();
  2. /*
  3. Copyright 2008, 2009 Vanilla Forums Inc.
  4. This file is part of Garden.
  5. Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. You should have received a copy of the GNU General Public License along with Garden.  If not, see <http://www.gnu.org/licenses/>.
  8. Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
  9. */
  10. /**
  11.  * Categories Controller
  12.  *
  13.  * @package Vanilla
  14.  */
  15.  
  16. /**
  17.  * Handles displaying categories.
  18.  *
  19.  * @since 2.0.0
  20.  * @package Vanilla
  21.  */
  22. class CategoriesController extends VanillaController {
  23.    /**
  24.     * Models to include.
  25.     *
  26.     * @since 2.0.0
  27.     * @access public
  28.     * @var array
  29.     */
  30.    public $Uses = array('Database', 'Form', 'CategoryModel');
  31.    
  32.    /**
  33.     * Should the discussions have their options available.
  34.     *
  35.     * @since 2.0.0
  36.     * @access public
  37.     * @var bool
  38.     */
  39.    public $ShowOptions = TRUE;
  40.    
  41.    /**
  42.     * Unique identifier.
  43.     *
  44.     * @since 2.0.0
  45.     * @access public
  46.     * @var int
  47.     */
  48.    public $CategoryID;
  49.    
  50.    /**
  51.     * Category object.
  52.     *
  53.     * @since 2.0.0
  54.     * @access public
  55.     * @var object
  56.     */
  57.    public $Category;
  58.    
  59.    /**
  60.     * Show all discussions in a particular category.
  61.     *
  62.     * @since 2.0.0
  63.     * @access public
  64.     *
  65.     * @param string $CategoryIdentifier Unique category slug or ID.
  66.     * @param int $Offset Number of discussions to skip.
  67.     */
  68.    public function Index($CategoryIdentifier = '', $Page = '0') {
  69.       $Category = CategoryModel::Categories($CategoryIdentifier);
  70.      
  71.       if (empty($Category)) {
  72.          if ($CategoryIdentifier)
  73.             throw NotFoundException();
  74.       }
  75.       $Category = (object)$Category;
  76.            
  77.         // Load the breadcrumbs.
  78.       $this->SetData('Breadcrumbs', CategoryModel::GetAncestors(GetValue('CategoryID', $Category)));
  79.      
  80.       $this->SetData('Category', $Category, TRUE);
  81.  
  82.       // Setup head
  83.       $this->AddCssFile('vanilla.css');
  84.       $this->Menu->HighlightRoute('/discussions');      
  85.       if ($this->Head) {
  86.          $this->Title(GetValue('Name', $Category, ''));
  87.          $this->AddJsFile('discussions.js');
  88.          $this->AddJsFile('bookmark.js');
  89.          $this->AddJsFile('options.js');
  90.          $this->AddJsFile('jquery.gardenmorepager.js');
  91.          $this->Head->AddRss($this->SelfUrl.'/feed.rss', $this->Head->Title());
  92.       }
  93.      
  94.       // Set CategoryID
  95.       $this->SetData('CategoryID', GetValue('CategoryID', $Category), TRUE);
  96.      
  97.       // Add modules
  98.       $this->AddModule('NewDiscussionModule');
  99.       $this->AddModule('CategoriesModule');
  100.       $this->AddModule('BookmarkedModule');
  101.      
  102.       // Get a DiscussionModel
  103.       $DiscussionModel = new DiscussionModel();
  104.       if(C('Vanilla.Categories.cascadeDiscussions')){
  105.         $catTree = $this->CategoryModel->GetSubTreeIds($this->CategoryID);
  106.         $Wheres = array('d.CategoryID' => $catTree);
  107.       }
  108.       else {
  109.         $Wheres = array('d.CategoryID' => $this->CategoryID);
  110.       }
  111.      
  112.       // Check permission
  113.       $this->Permission('Vanilla.Discussions.View', TRUE, 'Category', GetValue('PermissionCategoryID', $Category));
  114.      
  115.       // Set discussion meta data.
  116.       $this->EventArguments['PerPage'] = C('Vanilla.Discussions.PerPage', 30);
  117.       $this->FireEvent('BeforeGetDiscussions');
  118.       list($Offset, $Limit) = OffsetLimit($Page, $this->EventArguments['PerPage']);
  119.      
  120.       if (!is_numeric($Offset) || $Offset < 0)
  121.          $Offset = 0;
  122.          
  123.       $CountDiscussions = $DiscussionModel->GetCount($Wheres);
  124.       $this->SetData('CountDiscussions', $CountDiscussions);
  125.       $this->SetData('_Limit', $Limit);
  126.       $AnnounceData = $Offset == 0 ? $DiscussionModel->GetAnnouncements($Wheres) : new Gdn_DataSet();
  127.       $this->SetData('AnnounceData', $AnnounceData, TRUE);
  128.       $this->DiscussionData = $this->SetData('Discussions', $DiscussionModel->Get($Offset, $Limit, $Wheres));
  129.  
  130.       // Build a pager
  131.       $PagerFactory = new Gdn_PagerFactory();
  132.       $this->Pager = $PagerFactory->GetPager('Pager', $this);
  133.       $this->Pager->ClientID = 'Pager';
  134.       $this->Pager->Configure(
  135.          $Offset,
  136.          $Limit,
  137.          $CountDiscussions,
  138.          'categories/'.$CategoryIdentifier.'/%1$s'
  139.       );
  140.       $this->SetData('_PagerUrl', 'categories/'.rawurlencode($CategoryIdentifier).'/{Page}');
  141.       $this->SetData('_Page', $Page);
  142.  
  143.       // Set the canonical Url.
  144.       $this->CanonicalUrl(Url(ConcatSep('/', 'categories/'.GetValue('UrlCode', $Category, $CategoryIdentifier), PageNumber($Offset, $Limit, TRUE)), TRUE));
  145.      
  146.       // Change the controller name so that it knows to grab the discussion views
  147.       $this->ControllerName = 'DiscussionsController';
  148.       // Pick up the discussions class
  149.       $this->CssClass = 'Discussions';
  150.      
  151.       // Deliver JSON data if necessary
  152.       if ($this->_DeliveryType != DELIVERY_TYPE_ALL) {
  153.          $this->SetJson('LessRow', $this->Pager->ToString('less'));
  154.          $this->SetJson('MoreRow', $this->Pager->ToString('more'));
  155.          $this->View = 'discussions';
  156.       }
  157.  
  158.       $this->CanonicalUrl(Url('/categories', TRUE));
  159.  
  160.       // Render default view
  161.       $this->Render();
  162.    }
  163.    
  164.     /**
  165.      * Show all (nested) categories.
  166.      *
  167.      * @since 2.0.17
  168.      * @access public
  169.      */
  170.     public function All() {
  171.       // Setup head
  172.       $this->AddCssFile('vanilla.css');
  173.       $this->Menu->HighlightRoute('/discussions');
  174.       $this->Title(T('All Categories'));
  175.      
  176.         // Set the category follow toggle before we load category data so that it affects the category query appropriately.
  177.         $CategoryFollowToggleModule = new CategoryFollowToggleModule($this);
  178.         $CategoryFollowToggleModule->SetToggle();
  179.        
  180.       // Get category data
  181.       $CategoryModel = new CategoryModel();
  182.       $this->CategoryModel->Watching = !Gdn::Session()->GetPreference('ShowAllCategories');
  183.       $this->CategoryData = $this->CategoryModel->GetFull();
  184.         $this->SetData('Categories', $this->CategoryData);
  185.      
  186.       // Add modules
  187.       $this->AddModule('NewDiscussionModule');
  188.       $this->AddModule('BookmarkedModule');
  189.         $this->AddModule($CategoryFollowToggleModule);
  190.  
  191.       $this->CanonicalUrl(Url('/categories/all', TRUE));
  192.      
  193.       // Set a definition of the user's current timezone from the db. jQuery
  194.       // will pick this up, compare to the browser, and update the user's
  195.       // timezone if necessary.
  196.       $CurrentUser = Gdn::Session()->User;
  197.       if (is_object($CurrentUser)) {
  198.          $ClientHour = $CurrentUser->HourOffset + date('G', time());
  199.          $this->AddDefinition('SetClientHour', $ClientHour);
  200.       }
  201.  
  202.       $this->Render();
  203.     }
  204.  
  205.    /**
  206.     * Show all categories and few discussions from each.
  207.     *
  208.     * @since 2.0.0
  209.     * @access public
  210.     */
  211.    public function Discussions() {
  212.       // Setup head
  213.       $this->AddCssFile('vanilla.css');
  214.       $this->Menu->HighlightRoute('/discussions');
  215.       $this->AddJsFile('bookmark.js');
  216.       $this->AddJsFile('discussions.js');
  217.       $this->Title(T('All Categories'));
  218.      
  219.         // Set the category follow toggle before we load category data so that it affects the category query appropriately.
  220.         $CategoryFollowToggleModule = new CategoryFollowToggleModule($this);
  221.         $CategoryFollowToggleModule->SetToggle();
  222.        
  223.       // Get category data and discussions
  224.       $this->DiscussionsPerCategory = C('Vanilla.Discussions.PerCategory', 5);
  225.       $DiscussionModel = new DiscussionModel();
  226.       $this->CategoryModel->Watching = !Gdn::Session()->GetPreference('ShowAllCategories');
  227.       $this->CategoryData = $this->CategoryModel->GetFull();
  228.         $this->SetData('Categories', $this->CategoryData);
  229.       $this->CategoryDiscussionData = array();
  230.       foreach ($this->CategoryData->Result() as $Category) {
  231.             if ($Category->CategoryID > 0)
  232.                 $this->CategoryDiscussionData[$Category->CategoryID] = $DiscussionModel->Get(0, $this->DiscussionsPerCategory, array('d.CategoryID' => $Category->CategoryID, 'Announce' => 'all'));
  233.       }
  234.      
  235.       // Add modules
  236.       $this->AddModule('NewDiscussionModule');
  237.       $this->AddModule('CategoriesModule');
  238.       $this->AddModule('BookmarkedModule');
  239.         $this->AddModule($CategoryFollowToggleModule);
  240.      
  241.       // Set view and render
  242.       $this->View = 'discussions';
  243.  
  244.       $this->CanonicalUrl(Url('/categories', TRUE));
  245.      
  246.       $this->Render();
  247.    }
  248.    
  249.    /**
  250.     * Highlight route.
  251.     *
  252.     * Always called by dispatcher before controller's requested method.
  253.     *
  254.     * @since 2.0.0
  255.     * @access public
  256.     */
  257.    public function Initialize() {
  258.       parent::Initialize();
  259.       if ($this->Menu)
  260.          $this->Menu->HighlightRoute('/categories');
  261.      
  262.    }      
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement