/**
  * Delete an activity item.
  * 
  * @since 2.0.0
  * @access public
  * 
  * @param int $ActivityID Unique ID of item to delete.
  * @param string $TransientKey Verify intent.
  */
 public function Delete($ActivityID = '', $TransientKey = '')
 {
     $Session = Gdn::Session();
     if (!$Session->ValidateTransientKey($TransientKey)) {
         throw PermissionException();
     }
     if (!is_numeric($ActivityID)) {
         throw Gdn_UserException('Invalid activity ID');
     }
     $HasPermission = $Session->CheckPermission('Garden.Activity.Delete');
     if (!$HasPermission) {
         $Activity = $this->ActivityModel->GetID($ActivityID);
         if (!$Activity) {
             throw NotFoundException('Activity');
         }
         $HasPermission = $Activity['InsertUserID'] == $Session->UserID;
     }
     if (!$HasPermission) {
         throw PermissionException();
     }
     $this->ActivityModel->Delete($ActivityID);
     if ($this->_DeliveryType === DELIVERY_TYPE_ALL) {
         Redirect(GetIncomingValue('Target', $this->SelfUrl));
     }
     // Still here? Getting a 404.
     $this->ControllerName = 'Home';
     $this->View = 'FileNotFound';
     $this->Render();
 }
 /**
  * Delete an activity item.
  *
  * @since 2.0.0
  * @access public
  *
  * @param int $ActivityID Unique ID of item to delete.
  * @param string $TransientKey Verify intent.
  */
 public function delete($ActivityID = '', $TransientKey = '')
 {
     $session = Gdn::session();
     if (!$session->validateTransientKey($TransientKey)) {
         throw permissionException();
     }
     if (!is_numeric($ActivityID)) {
         throw Gdn_UserException('Invalid ID');
     }
     if (!$this->ActivityModel->canDelete($this->ActivityModel->getID($ActivityID))) {
         throw permissionException();
     }
     $this->ActivityModel->delete($ActivityID);
     if ($this->_DeliveryType === DELIVERY_TYPE_ALL) {
         redirect(GetIncomingValue('Target', $this->SelfUrl));
     }
     // Still here? Getting a 404.
     $this->ControllerName = 'Home';
     $this->View = 'FileNotFound';
     $this->render();
 }
 /**
  * Delete an activity item.
  *
  * @since 2.0.0
  * @access public
  *
  * @param int $ActivityID Unique ID of item to delete.
  * @param string $TransientKey Verify intent.
  */
 public function delete($ActivityID = '', $TransientKey = '')
 {
     $session = Gdn::session();
     if (!$session->validateTransientKey($TransientKey)) {
         throw permissionException();
     }
     if (!is_numeric($ActivityID)) {
         throw Gdn_UserException('Invalid ID');
     }
     if (!$this->ActivityModel->canDelete($this->ActivityModel->getID($ActivityID))) {
         throw permissionException();
     }
     $this->ActivityModel->deleteID($ActivityID);
     if ($this->_DeliveryType === DELIVERY_TYPE_ALL) {
         $target = Gdn::request()->get('Target');
         if ($target) {
             // Bail with a redirect if we got one.
             redirect($target);
         } else {
             // We got this as a full page somehow, so send them back to /activity.
             $this->RedirectUrl = url('activity');
         }
     }
     $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');
 }