public static function joinRecentPosts(&$Data, $CategoryID = null)
{
$DiscussionIDs = array();
$CommentIDs = array();
$Joined = false;
foreach ($Data as &$Row) {
if (!is_null($CategoryID) && $Row['CategoryID'] != $CategoryID) {
continue;
}
if (isset($Row['LastTitle']) && $Row['LastTitle']) {
continue;
}
if ($Row['LastDiscussionID']) {
$DiscussionIDs[] = $Row['LastDiscussionID'];
}
if ($Row['LastCommentID']) {
$CommentIDs[] = $Row['LastCommentID'];
}
$Joined = true;
}
// Create a fresh copy of the Sql object so as not to pollute.
$Sql = clone Gdn::sql();
$Sql->reset();
$Discussions = null;
// Grab the discussions.
if (count($DiscussionIDs) > 0) {
$Discussions = $Sql->whereIn('DiscussionID', $DiscussionIDs)->get('Discussion')->resultArray();
$Discussions = Gdn_DataSet::Index($Discussions, array('DiscussionID'));
}
if (count($CommentIDs) > 0) {
$Comments = $Sql->whereIn('CommentID', $CommentIDs)->get('Comment')->resultArray();
$Comments = Gdn_DataSet::Index($Comments, array('CommentID'));
}
foreach ($Data as &$Row) {
if (!is_null($CategoryID) && $Row['CategoryID'] != $CategoryID) {
continue;
}
$Discussion = val($Row['LastDiscussionID'], $Discussions);
$NameUrl = 'x';
if ($Discussion) {
$Row['LastTitle'] = Gdn_Format::text($Discussion['Name']);
$Row['LastUserID'] = $Discussion['InsertUserID'];
$Row['LastDiscussionUserID'] = $Discussion['InsertUserID'];
$Row['LastDateInserted'] = $Discussion['DateInserted'];
$NameUrl = Gdn_Format::text($Discussion['Name'], true);
$Row['LastUrl'] = DiscussionUrl($Discussion, false, '/') . '#latest';
}
if (!empty($Comments) && ($Comment = val($Row['LastCommentID'], $Comments))) {
$Row['LastUserID'] = $Comment['InsertUserID'];
$Row['LastDateInserted'] = $Comment['DateInserted'];
$Row['DateLastComment'] = $Comment['DateInserted'];
} else {
$Row['NoComment'] = true;
}
touchValue('LastTitle', $Row, '');
touchValue('LastUserID', $Row, null);
touchValue('LastDiscussionUserID', $Row, null);
touchValue('LastDateInserted', $Row, null);
touchValue('LastUrl', $Row, null);
}
return $Joined;
}