/**
* Delete a discussion. Update and/or delete all related data.
*
* Events: DeleteDiscussion.
*
* @param int $discussionID Unique ID of discussion to delete.
* @param array $options Additional options to control the delete behavior. Not used for discussions.
* @return bool Always returns **true**.
*/
public function deleteID($discussionID, $options = [])
{
// Retrieve the users who have bookmarked this discussion.
$BookmarkData = $this->getBookmarkUsers($discussionID);
$Data = $this->SQL->select('*')->from('Discussion')->where('DiscussionID', $discussionID)->get()->firstRow(DATASET_TYPE_ARRAY);
$UserID = false;
$CategoryID = false;
if ($Data) {
$UserID = $Data['InsertUserID'];
$CategoryID = $Data['CategoryID'];
}
// Prep and fire event
$this->EventArguments['DiscussionID'] = $discussionID;
$this->EventArguments['Discussion'] = $Data;
$this->fireEvent('DeleteDiscussion');
// Setup logging.
$Log = val('Log', $options, true);
$LogOptions = val('LogOptions', $options, []);
if ($Log === true) {
$Log = 'Delete';
}
LogModel::beginTransaction();
// Log all of the comment deletes.
$Comments = $this->SQL->getWhere('Comment', ['DiscussionID' => $discussionID])->resultArray();
if (count($Comments) > 0 && count($Comments) < 50) {
// A smaller number of comments should just be stored with the record.
$Data['_Data']['Comment'] = $Comments;
LogModel::insert($Log, 'Discussion', $Data, $LogOptions);
} else {
LogModel::insert($Log, 'Discussion', $Data, $LogOptions);
foreach ($Comments as $Comment) {
LogModel::insert($Log, 'Comment', $Comment, $LogOptions);
}
}
LogModel::endTransaction();
$this->SQL->delete('Comment', ['DiscussionID' => $discussionID]);
$this->SQL->delete('Discussion', ['DiscussionID' => $discussionID]);
$this->SQL->delete('UserDiscussion', ['DiscussionID' => $discussionID]);
$this->updateDiscussionCount($CategoryID);
// Get the user's discussion count.
$this->updateUserDiscussionCount($UserID);
// Update bookmark counts for users who had bookmarked this discussion
foreach ($BookmarkData->result() as $User) {
$this->setUserBookmarkCount($User->UserID);
}
return true;
}