/** * Allows user to delete a discussion. * * This is a "hard" delete - it is removed from the database. * * @since 2.0.0 * @access public * * @param int $DiscussionID Unique discussion ID. */ public function delete($DiscussionID, $Target = '') { $Discussion = $this->DiscussionModel->getID($DiscussionID); if (!$Discussion) { throw notFoundException('Discussion'); } $this->permission('Vanilla.Discussions.Delete', true, 'Category', $Discussion->PermissionCategoryID); if ($this->Form->authenticatedPostBack()) { if (!$this->DiscussionModel->delete($DiscussionID)) { $this->Form->addError('Failed to delete discussion'); } if ($this->Form->errorCount() == 0) { if ($this->_DeliveryType === DELIVERY_TYPE_ALL) { safeRedirect($Target); } if ($Target) { $this->RedirectUrl = url($Target); } $this->jsonTarget(".Section-DiscussionList #Discussion_{$DiscussionID}", null, 'SlideUp'); } } $this->setData('Title', t('Delete Discussion')); $this->render(); }
/** * Add a method to the ModerationController to handle merging discussions. * * @param Gdn_Controller $Sender */ public function moderationController_mergeDiscussions_create($Sender) { $Session = Gdn::session(); $Sender->Form = new Gdn_Form(); $Sender->title(t('Merge Discussions')); $DiscussionModel = new DiscussionModel(); $CheckedDiscussions = Gdn::userModel()->getAttribute($Session->User->UserID, 'CheckedDiscussions', array()); if (!is_array($CheckedDiscussions)) { $CheckedDiscussions = array(); } $DiscussionIDs = $CheckedDiscussions; $Sender->setData('DiscussionIDs', $DiscussionIDs); $CountCheckedDiscussions = count($DiscussionIDs); $Sender->setData('CountCheckedDiscussions', $CountCheckedDiscussions); $Discussions = $DiscussionModel->SQL->whereIn('DiscussionID', $DiscussionIDs)->get('Discussion')->resultArray(); $Sender->setData('Discussions', $Discussions); // Make sure none of the selected discussions are ghost redirects. $discussionTypes = array_column($Discussions, 'Type'); if (in_array('redirect', $discussionTypes)) { throw Gdn_UserException('You cannot merge redirects.', 400); } // Perform the merge if ($Sender->Form->authenticatedPostBack()) { // Create a new discussion record $MergeDiscussion = false; $MergeDiscussionID = $Sender->Form->getFormValue('MergeDiscussionID'); foreach ($Discussions as $Discussion) { if ($Discussion['DiscussionID'] == $MergeDiscussionID) { $MergeDiscussion = $Discussion; break; } } $RedirectLink = $Sender->Form->getFormValue('RedirectLink'); if ($MergeDiscussion) { $ErrorCount = 0; // Verify that the user has permission to perform the merge. $Category = CategoryModel::categories($MergeDiscussion['CategoryID']); if ($Category && !$Category['PermsDiscussionsEdit']) { throw permissionException('Vanilla.Discussions.Edit'); } $DiscussionModel->defineSchema(); $MaxNameLength = val('Length', $DiscussionModel->Schema->getField('Name')); // Assign the comments to the new discussion record $DiscussionModel->SQL->update('Comment')->set('DiscussionID', $MergeDiscussionID)->whereIn('DiscussionID', $DiscussionIDs)->put(); $CommentModel = new CommentModel(); foreach ($Discussions as $Discussion) { if ($Discussion['DiscussionID'] == $MergeDiscussionID) { continue; } // Create a comment out of the discussion. $Comment = arrayTranslate($Discussion, array('Body', 'Format', 'DateInserted', 'InsertUserID', 'InsertIPAddress', 'DateUpdated', 'UpdateUserID', 'UpdateIPAddress', 'Attributes', 'Spam', 'Likes', 'Abuse')); $Comment['DiscussionID'] = $MergeDiscussionID; $CommentModel->Validation->results(true); $CommentID = $CommentModel->save($Comment); if ($CommentID) { // Move any attachments (FileUpload plugin awareness) if (class_exists('MediaModel')) { $MediaModel = new MediaModel(); $MediaModel->reassign($Discussion['DiscussionID'], 'discussion', $CommentID, 'comment'); } if ($RedirectLink) { // The discussion needs to be changed to a moved link. $RedirectDiscussion = array('Name' => SliceString(sprintf(t('Merged: %s'), $Discussion['Name']), $MaxNameLength), 'Type' => 'redirect', 'Body' => formatString(t('This discussion has been <a href="{url,html}">merged</a>.'), array('url' => DiscussionUrl($MergeDiscussion))), 'Format' => 'Html'); $DiscussionModel->setField($Discussion['DiscussionID'], $RedirectDiscussion); $CommentModel->updateCommentCount($Discussion['DiscussionID']); $CommentModel->removePageCache($Discussion['DiscussionID']); } else { // Delete discussion that was merged. $DiscussionModel->delete($Discussion['DiscussionID']); } } else { $Sender->informMessage($CommentModel->Validation->resultsText()); $ErrorCount++; } } // Update counts on all affected discussions. $CommentModel->updateCommentCount($MergeDiscussionID); $CommentModel->removePageCache($MergeDiscussionID); // Clear selections Gdn::userModel()->saveAttribute($Session->UserID, 'CheckedDiscussions', false); ModerationController::informCheckedDiscussions($Sender); if ($ErrorCount == 0) { $Sender->jsonTarget('', '', 'Refresh'); } } } $Sender->render('MergeDiscussions', '', 'plugins/SplitMerge'); }
/** * Form to confirm that the administrator wants to delete the selected * discussions (and has permission to do so). */ public function confirmDiscussionDeletes() { $Session = Gdn::session(); $this->Form = new Gdn_Form(); $DiscussionModel = new DiscussionModel(); // Verify that the user has permission to perform the deletes $this->permission('Vanilla.Discussions.Delete', true, 'Category', 'any'); $this->title(t('Confirm')); $CheckedDiscussions = Gdn::userModel()->getAttribute($Session->User->UserID, 'CheckedDiscussions', array()); if (!is_array($CheckedDiscussions)) { $CheckedDiscussions = array(); } $DiscussionIDs = $CheckedDiscussions; $CountCheckedDiscussions = count($DiscussionIDs); $this->setData('CountCheckedDiscussions', $CountCheckedDiscussions); // Check permissions on each discussion to make sure the user has permission to delete them $AllowedDiscussions = array(); $DiscussionData = $DiscussionModel->SQL->select('DiscussionID, CategoryID')->from('Discussion')->whereIn('DiscussionID', $DiscussionIDs)->get(); foreach ($DiscussionData->result() as $Discussion) { $PermissionCategory = CategoryModel::categories(val('CategoryID', $Discussion)); $CountCheckedDiscussions = $DiscussionData->numRows(); if ($Session->checkPermission('Vanilla.Discussions.Delete', true, 'Category', val('PermissionCategoryID', $PermissionCategory))) { $AllowedDiscussions[] = $Discussion->DiscussionID; } } $this->setData('CountAllowed', count($AllowedDiscussions)); $CountNotAllowed = $CountCheckedDiscussions - count($AllowedDiscussions); $this->setData('CountNotAllowed', $CountNotAllowed); if ($this->Form->authenticatedPostBack()) { // Delete the selected discussions (that the user has permission to delete). foreach ($AllowedDiscussions as $DiscussionID) { $Deleted = $DiscussionModel->delete($DiscussionID); if ($Deleted) { $this->jsonTarget("#Discussion_{$DiscussionID}", '', 'SlideUp'); } } // Clear selections Gdn::userModel()->saveAttribute($Session->UserID, 'CheckedDiscussions', null); ModerationController::InformCheckedDiscussions($this, true); } $this->render(); }
/** * Insert a SPAM Queue entry for the specified record and delete the record, if possible. * * @param string $recordType The type of record we're flagging: Discussion or Comment. * @param int $id ID of the record we're flagging. * @param object|array $data Properties used for updating/overriding the record's current values. * * @throws Exception If an invalid record type is specified, throw an exception. */ protected static function flagForReview($recordType, $id, $data) { // We're planning to purge the spammy record. $deleteRow = true; /** * We're only handling two types of content: discussions and comments. Both need some special setup. * Error out if we're not dealing with a discussion or comment. */ switch ($recordType) { case 'Comment': $model = new CommentModel(); $row = $model->getID($id, DATASET_TYPE_ARRAY); break; case 'Discussion': $model = new DiscussionModel(); $row = $model->getID($id, DATASET_TYPE_ARRAY); /** * If our discussion has more than three comments, it might be worth saving. Hold off on deleting and * just flag it. If we have between 0 and 3 comments, save them along with the discussion. */ if ($row['CountComments'] > 3) { $deleteRow = false; } elseif ($row['CountComments'] > 0) { $comments = Gdn::database()->sql()->getWhere('Comment', array('DiscussionID' => $id))->resultArray(); if (!array_key_exists('_Data', $row)) { $row['_Data'] = array(); } $row['_Data']['Comment'] = $comments; } break; default: throw notFoundException($recordType); } $overrideFields = array('Name', 'Body'); foreach ($overrideFields as $fieldName) { if (($fieldValue = val($fieldName, $data, false)) !== false) { $row[$fieldName] = $fieldValue; } } $logOptions = array('GroupBy' => array('RecordID')); if ($deleteRow) { // Remove the record to the log. $model->delete($id); } LogModel::insert('Spam', $recordType, $row, $logOptions); }