/** * Handle flagging process in a discussion. */ public function discussionController_flag_create($Sender) { // Signed in users only. if (!($UserID = Gdn::session()->UserID)) { return; } $UserName = Gdn::session()->User->Name; $Arguments = $Sender->RequestArgs; if (sizeof($Arguments) != 5) { return; } list($Context, $ElementID, $ElementAuthorID, $ElementAuthor, $EncodedURL) = $Arguments; $URL = htmlspecialchars(base64_decode(str_replace('-', '=', $EncodedURL))); $Sender->setData('Plugin.Flagging.Data', array('Context' => $Context, 'ElementID' => $ElementID, 'ElementAuthorID' => $ElementAuthorID, 'ElementAuthor' => $ElementAuthor, 'URL' => $URL, 'UserID' => $UserID, 'UserName' => $UserName)); if ($Sender->Form->authenticatedPostBack()) { $SQL = Gdn::sql(); $Comment = $Sender->Form->getValue('Plugin.Flagging.Reason'); $Sender->setData('Plugin.Flagging.Reason', $Comment); $CreateDiscussion = c('Plugins.Flagging.UseDiscussions'); if ($CreateDiscussion) { // Category $CategoryID = c('Plugins.Flagging.CategoryID'); // New discussion name if ($Context == 'comment') { $Result = $SQL->select('d.Name')->select('c.Body')->from('Comment c')->join('Discussion d', 'd.DiscussionID = c.DiscussionID', 'left')->where('c.CommentID', $ElementID)->get()->firstRow(); } elseif ($Context == 'discussion') { $DiscussionModel = new DiscussionModel(); $Result = $DiscussionModel->getID($ElementID); } $DiscussionName = val('Name', $Result); $PrefixedDiscussionName = t('FlagPrefix', 'FLAG: ') . $DiscussionName; // Prep data for the template $Sender->setData('Plugin.Flagging.Report', array('DiscussionName' => $DiscussionName, 'FlaggedContent' => val('Body', $Result))); // Assume no discussion exists $this->DiscussionID = null; // Get discussion ID if already flagged $FlagResult = Gdn::sql()->select('DiscussionID')->from('Flag fl')->where('ForeignType', $Context)->where('ForeignID', $ElementID)->get()->firstRow(); if ($FlagResult) { // New comment in existing discussion $DiscussionID = $FlagResult->DiscussionID; $ReportBody = $Sender->fetchView($this->getView('reportcomment.php')); $SQL->insert('Comment', array('DiscussionID' => $DiscussionID, 'InsertUserID' => $UserID, 'Body' => $ReportBody, 'Format' => 'Html', 'DateInserted' => date('Y-m-d H:i:s'))); $CommentModel = new CommentModel(); $CommentModel->updateCommentCount($DiscussionID); } else { // New discussion body $ReportBody = $Sender->fetchView($this->getView('report.php')); $DiscussionID = $SQL->insert('Discussion', array('InsertUserID' => $UserID, 'UpdateUserID' => $UserID, 'CategoryID' => $CategoryID, 'Name' => $PrefixedDiscussionName, 'Body' => $ReportBody, 'Format' => 'Html', 'CountComments' => 1, 'DateInserted' => date('Y-m-d H:i:s'), 'DateUpdated' => date('Y-m-d H:i:s'), 'DateLastComment' => date('Y-m-d H:i:s'))); // Update discussion count $DiscussionModel = new DiscussionModel(); $DiscussionModel->updateDiscussionCount($CategoryID); } } try { // Insert the flag $SQL->insert('Flag', array('DiscussionID' => $DiscussionID, 'InsertUserID' => $UserID, 'InsertName' => $UserName, 'AuthorID' => $ElementAuthorID, 'AuthorName' => $ElementAuthor, 'ForeignURL' => $URL, 'ForeignID' => $ElementID, 'ForeignType' => $Context, 'Comment' => $Comment, 'DateInserted' => date('Y-m-d H:i:s'))); } catch (Exception $e) { } // Notify users with permission who've chosen to be notified if (!$FlagResult) { // Only send if this is first time it's being flagged. $Sender->setData('Plugin.Flagging.DiscussionID', $DiscussionID); $Subject = isset($PrefixedDiscussionName) ? $PrefixedDiscussionName : t('FlagDiscussion', 'A discussion was flagged'); $EmailBody = $Sender->fetchView($this->getView('reportemail.php')); $NotifyUsers = c('Plugins.Flagging.NotifyUsers', array()); // Send emails $UserModel = new UserModel(); foreach ($NotifyUsers as $UserID) { $User = $UserModel->getID($UserID); $Email = new Gdn_Email(); $Email->to($User->Email)->subject(sprintf(t('[%1$s] %2$s'), Gdn::config('Garden.Title'), $Subject))->message($EmailBody); try { $Email->send(); } catch (Exception $e) { if (debug()) { throw $e; } } } } $Sender->informMessage(t('FlagSent', "Your complaint has been registered.")); } $Sender->render($this->getView('flag.php')); }
/** * 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'); }