public static joinCategories ( &$Data, string $Column = 'CategoryID', array $Options = [] ) | ||
$Column | string | Name of database column. |
$Options | array | The 'Join' key may contain array of columns to join on. |
/** * * Get discussions for a user. * * Events: BeforeGetByUser * * @since 2.1 * @access public * * @param int $UserID Which user to get discussions for. * @param int $Limit Max number to get. * @param int $Offset Number to skip. * @param int $LastDiscussionID A hint for quicker paging. * @param int $WatchUserID User to use for read/unread data. * @return Gdn_DataSet SQL results. */ public function getByUser($UserID, $Limit, $Offset, $LastDiscussionID = false, $WatchUserID = false) { $Perms = DiscussionModel::categoryPermissions(); if (is_array($Perms) && empty($Perms)) { return new Gdn_DataSet([]); } // Allow us to set perspective of a different user. if (empty($WatchUserID)) { $WatchUserID = $UserID; } // The point of this query is to select from one comment table, but filter and sort on another. // This puts the paging into an index scan rather than a table scan. $this->SQL->select('d2.*')->select('d2.InsertUserID', '', 'FirstUserID')->select('d2.DateInserted', '', 'FirstDate')->select('d2.DateLastComment', '', 'LastDate')->select('d2.LastCommentUserID', '', 'LastUserID')->from('Discussion d')->join('Discussion d2', 'd.DiscussionID = d2.DiscussionID')->where('d.InsertUserID', $UserID)->orderBy('d.DiscussionID', 'desc'); // Join in the watch data. if ($WatchUserID > 0) { $this->SQL->select('w.UserID', '', 'WatchUserID')->select('w.DateLastViewed, w.Dismissed, w.Bookmarked')->select('w.CountComments', '', 'CountCommentWatch')->select('w.Participated')->join('UserDiscussion w', 'd2.DiscussionID = w.DiscussionID and w.UserID = ' . $WatchUserID, 'left'); } else { $this->SQL->select('0', '', 'WatchUserID')->select('now()', '', 'DateLastViewed')->select('0', '', 'Dismissed')->select('0', '', 'Bookmarked')->select('0', '', 'CountCommentWatch')->select('d.Announce', '', 'IsAnnounce'); } if (!empty($LastDiscussionID)) { // The last comment id from the last page was given and can be used as a hint to speed up the query. $this->SQL->where('d.DiscussionID <', $LastDiscussionID)->limit($Limit); } else { $this->SQL->limit($Limit, $Offset); } $this->fireEvent('BeforeGetByUser'); $Data = $this->SQL->get(); $Result =& $Data->result(); $this->LastDiscussionCount = $Data->numRows(); if (count($Result) > 0) { $this->LastDiscussionID = $Result[count($Result) - 1]->DiscussionID; } else { $this->LastDiscussionID = null; } // Now that we have th comments we can filter out the ones we don't have permission to. if ($Perms !== true) { $Remove = []; foreach ($Data->result() as $Index => $Row) { if (!in_array($Row->CategoryID, $Perms)) { $Remove[] = $Index; } } if (count($Remove) > 0) { foreach ($Remove as $Index) { unset($Result[$Index]); } $Result = array_values($Result); } } // Change discussions returned based on additional criteria $this->addDiscussionColumns($Data); // Join in the users. Gdn::userModel()->joinUsers($Data, ['FirstUserID', 'LastUserID']); CategoryModel::joinCategories($Data); if (c('Vanilla.Views.Denormalize', false)) { $this->addDenormalizedViews($Data); } $this->EventArguments['Data'] =& $Data; $this->fireEvent('AfterAddColumns'); return $Data; }