DiscussionModel::getWhere PHP Method

getWhere() public method

This method call will remove announcements and may not return exactly {@link $Limit} records for optimization. You can set $Where['d.Announce'] = 'all' to return announcements.
public getWhere ( array | false $Where = false, string $OrderFields = '', string $OrderDirection = '', integer | false $Limit = false, integer | false $Offset = false ) : Gdn_DataSet
$Where array | false The where condition of the get.
$OrderFields string The field to order the discussions by.
$OrderDirection string The order, either **asc** or **desc**.
$Limit integer | false The number of discussion to return.
$Offset integer | false The offset within the total set.
return Gdn_DataSet Returns a {@link Gdn_DataSet} of discussions.
    public function getWhere($Where = false, $OrderFields = '', $OrderDirection = '', $Limit = false, $Offset = false)
    {
        // Add backwards compatibility for the old way getWhere() was called.
        if (is_numeric($OrderFields)) {
            deprecated('DiscussionModel->getWhere($where, $limit, ...)', 'DiscussionModel->getWhereRecent()');
            $Limit = $OrderFields;
            $OrderFields = '';
        }
        if (is_numeric($OrderDirection)) {
            deprecated('DiscussionModel->getWhere($where, $limit, $offset)', 'DiscussionModel->getWhereRecent()');
            $Offset = $OrderDirection;
            $OrderDirection = '';
        }
        if ($Limit === 0) {
            trigger_error("You should not supply 0 to for {$Limit} in DiscussionModel->getWhere()", E_USER_NOTICE);
        }
        if (empty($Limit)) {
            $Limit = c('Vanilla.Discussions.PerPage', 30);
        }
        if (empty($Offset)) {
            $Offset = 0;
        }
        if (!is_array($Where)) {
            $Where = [];
        }
        $Sql = $this->SQL;
        // Determine category watching
        if ($this->Watching && !isset($Where['d.CategoryID'])) {
            $Watch = CategoryModel::categoryWatch();
            if ($Watch !== true) {
                $Where['d.CategoryID'] = $Watch;
            }
        }
        $Where = $this->combineWheres($this->getWheres(), $Where);
        $orderBy = [];
        if (empty($OrderFields)) {
            $orderBy = $this->getOrderBy();
        } elseif (is_string($OrderFields)) {
            if ($OrderDirection != 'asc') {
                $OrderDirection = 'desc';
            }
            $orderBy = [$OrderFields => $OrderDirection];
        }
        $this->EventArguments['OrderBy'] =& $orderBy;
        $this->EventArguments['Wheres'] =& $Where;
        $this->fireEvent('BeforeGet');
        // Build up the base query. Self-join for optimization.
        $Sql->select('d2.*')->from('Discussion d')->join('Discussion d2', 'd.DiscussionID = d2.DiscussionID')->limit($Limit, $Offset);
        foreach ($orderBy as $field => $direction) {
            $Sql->orderBy($this->addFieldPrefix($field), $direction);
        }
        // Verify permissions (restricting by category if necessary)
        $Perms = self::categoryPermissions();
        if ($Perms !== true) {
            if (isset($Where['d.CategoryID'])) {
                $Where['d.CategoryID'] = array_values(array_intersect((array) $Where['d.CategoryID'], $Perms));
            } else {
                $Where['d.CategoryID'] = $Perms;
            }
        }
        // Check to see whether or not we are removing announcements.
        if (strtolower(val('Announce', $Where)) == 'all') {
            $RemoveAnnouncements = false;
            unset($Where['Announce']);
        } elseif (strtolower(val('d.Announce', $Where)) == 'all') {
            $RemoveAnnouncements = false;
            unset($Where['d.Announce']);
        } else {
            $RemoveAnnouncements = true;
        }
        // Make sure there aren't any ambiguous discussion references.
        $safeWheres = [];
        foreach ($Where as $Key => $Value) {
            $safeWheres[$this->addFieldPrefix($Key)] = $Value;
        }
        $Sql->where($safeWheres);
        // Add the UserDiscussion query.
        if (($UserID = Gdn::session()->UserID) > 0) {
            $Sql->join('UserDiscussion w', "w.DiscussionID = d2.DiscussionID and w.UserID = {$UserID}", 'left')->select('w.UserID', '', 'WatchUserID')->select('w.DateLastViewed, w.Dismissed, w.Bookmarked')->select('w.CountComments', '', 'CountCommentWatch')->select('w.Participated');
        }
        $Data = $Sql->get();
        // Change discussions returned based on additional criteria
        $this->addDiscussionColumns($Data);
        // If not looking at discussions filtered by bookmarks or user, filter announcements out.
        if ($RemoveAnnouncements && !isset($Where['w.Bookmarked']) && !isset($Where['d.InsertUserID'])) {
            $this->removeAnnouncements($Data);
        }
        // Join in the users.
        Gdn::userModel()->joinUsers($Data, ['FirstUserID', 'LastUserID']);
        CategoryModel::joinCategories($Data);
        if (c('Vanilla.Views.Denormalize', false)) {
            $this->addDenormalizedViews($Data);
        }
        // Prep and fire event
        $this->EventArguments['Data'] = $Data;
        $this->fireEvent('AfterAddColumns');
        return $Data;
    }

Usage Example

コード例 #1
0
 /**
  * Default all discussions view: chronological by most recent comment.
  *
  * @since 2.0.0
  * @access public
  *
  * @param int $Page Multiplied by PerPage option to determine offset.
  */
 public function index($Page = false)
 {
     // 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');
     // 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();
     // 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);
     // Get Discussions
     $this->DiscussionData = $DiscussionModel->getWhere($where, $Offset, $Limit);
     $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}');
     }
     $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();
 }
All Usage Examples Of DiscussionModel::getWhere