public function getUnreadCount($Wheres = '')
{
if (is_array($Wheres) && count($Wheres) == 0) {
$Wheres = '';
}
// Check permission and limit to categories as necessary
if ($this->Watching) {
$Perms = CategoryModel::categoryWatch();
} else {
$Perms = self::categoryPermissions();
}
if (!$Wheres || count($Wheres) == 1 && isset($Wheres['d.CategoryID'])) {
// Grab the counts from the faster category cache.
if (isset($Wheres['d.CategoryID'])) {
$CategoryIDs = (array) $Wheres['d.CategoryID'];
if ($Perms === false) {
$CategoryIDs = [];
} elseif (is_array($Perms)) {
$CategoryIDs = array_intersect($CategoryIDs, $Perms);
}
if (count($CategoryIDs) == 0) {
return 0;
} else {
$Perms = $CategoryIDs;
}
}
$Categories = CategoryModel::categories();
$Count = 0;
foreach ($Categories as $Cat) {
if (is_array($Perms) && !in_array($Cat['CategoryID'], $Perms)) {
continue;
}
$Count += (int) $Cat['CountDiscussions'];
}
return $Count;
}
if ($Perms !== true) {
$this->SQL->whereIn('c.CategoryID', $Perms);
}
$this->EventArguments['Wheres'] =& $Wheres;
$this->fireEvent('BeforeGetUnreadCount');
// @see 'BeforeGet' for consistency in count vs. results
$this->SQL->select('d.DiscussionID', 'count', 'CountDiscussions')->from('Discussion d')->join('Category c', 'd.CategoryID = c.CategoryID')->join('UserDiscussion w', 'd.DiscussionID = w.DiscussionID and w.UserID = ' . Gdn::session()->UserID, 'left')->where('d.CountComments >', 'COALESCE(w.CountComments, 0)', true, false)->where($Wheres);
$Result = $this->SQL->get()->firstRow()->CountDiscussions;
return $Result;
}