PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/SplitMerge/class.splitmerge.plugin.php

https://bitbucket.org/gm112/zfgc3
PHP | 195 lines | 130 code | 26 blank | 39 comment | 20 complexity | 1434cd2c3d1cb900dcb0f5a401b62f32 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php if (!defined('APPLICATION')) exit();
  2. /*
  3. Copyright 2008, 2009 Vanilla Forums Inc.
  4. This file is part of Garden.
  5. Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  6. Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
  8. Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
  9. */
  10. // Define the plugin:
  11. $PluginInfo['SplitMerge'] = array(
  12. 'Name' => 'Split / Merge',
  13. 'Description' => 'Allows moderators with discussion edit permission to split & merge discussions.',
  14. 'Version' => '1',
  15. 'HasLocale' => TRUE,
  16. 'Author' => "Mark O'Sullivan",
  17. 'AuthorEmail' => 'mark@vanillaforums.com',
  18. 'AuthorUrl' => 'http://www.vanillaforums.com'
  19. );
  20. class SplitMergePlugin extends Gdn_Plugin {
  21. /**
  22. * Add "split" action link.
  23. */
  24. public function Base_BeforeCheckComments_Handler($Sender) {
  25. $ActionMessage = &$Sender->EventArguments['ActionMessage'];
  26. $Discussion = $Sender->EventArguments['Discussion'];
  27. if (Gdn::Session()->CheckPermission('Vanilla.Discussions.Edit', TRUE, 'Category', $Discussion->PermissionCategoryID))
  28. $ActionMessage .= ' '.Anchor(T('Split'), 'vanilla/moderation/splitcomments/'.$Discussion->DiscussionID.'/', 'Split Popup');
  29. }
  30. /**
  31. * Add "merge" action link.
  32. */
  33. public function Base_BeforeCheckDiscussions_Handler($Sender) {
  34. $ActionMessage = &$Sender->EventArguments['ActionMessage'];
  35. if (Gdn::Session()->CheckPermission('Vanilla.Discussions.Edit', TRUE, 'Category', 'any'))
  36. $ActionMessage .= ' '.Anchor(T('Merge'), 'vanilla/moderation/mergediscussions/', 'Merge Popup');
  37. }
  38. /**
  39. * Add a method to the ModerationController to handle splitting comments out to a new discussion.
  40. */
  41. public function ModerationController_SplitComments_Create($Sender) {
  42. $Session = Gdn::Session();
  43. $Sender->Form = new Gdn_Form();
  44. $Sender->Title(T('Split Comments'));
  45. $Sender->Category = FALSE;
  46. $DiscussionID = GetValue('0', $Sender->RequestArgs, '');
  47. if (!is_numeric($DiscussionID))
  48. return;
  49. $DiscussionModel = new DiscussionModel();
  50. $Discussion = $DiscussionModel->GetID($DiscussionID);
  51. if (!$Discussion)
  52. return;
  53. // Verify that the user has permission to perform the split
  54. $Sender->Permission('Vanilla.Discussions.Edit', TRUE, 'Category', $Discussion->PermissionCategoryID);
  55. $CheckedComments = Gdn::UserModel()->GetAttribute($Session->User->UserID, 'CheckedComments', array());
  56. if (!is_array($CheckedComments))
  57. $CheckedComments = array();
  58. $CommentIDs = array();
  59. foreach ($CheckedComments as $DiscID => $Comments) {
  60. foreach ($Comments as $Comment) {
  61. if ($DiscID == $DiscussionID)
  62. $CommentIDs[] = str_replace('Comment_', '', $Comment);
  63. }
  64. }
  65. // Load category data.
  66. $Sender->ShowCategorySelector = (bool)C('Vanilla.Categories.Use');
  67. $CountCheckedComments = count($CommentIDs);
  68. $Sender->SetData('CountCheckedComments', $CountCheckedComments);
  69. // Perform the split
  70. if ($Sender->Form->AuthenticatedPostBack()) {
  71. // Create a new discussion record
  72. $Data = $Sender->Form->FormValues();
  73. $Data['Body'] = sprintf(T('This discussion was created from comments split from: %s.'), Anchor(Gdn_Format::Text($Discussion->Name), 'discussion/'.$Discussion->DiscussionID.'/'.Gdn_Format::Url($Discussion->Name).'/'));
  74. $Data['Format'] = 'Html';
  75. $NewDiscussionID = $DiscussionModel->Save($Data);
  76. $Sender->Form->SetValidationResults($DiscussionModel->ValidationResults());
  77. if ($Sender->Form->ErrorCount() == 0 && $NewDiscussionID > 0) {
  78. // Re-assign the comments to the new discussion record
  79. $DiscussionModel->SQL
  80. ->Update('Comment')
  81. ->Set('DiscussionID', $NewDiscussionID)
  82. ->WhereIn('CommentID', $CommentIDs)
  83. ->Put();
  84. // Update counts on both discussions
  85. $CommentModel = new CommentModel();
  86. $CommentModel->UpdateCommentCount($DiscussionID);
  87. // $CommentModel->UpdateUserCommentCounts($DiscussionID);
  88. $CommentModel->UpdateCommentCount($NewDiscussionID);
  89. // Clear selections
  90. unset($CheckedComments[$DiscussionID]);
  91. Gdn::UserModel()->SaveAttribute($Session->UserID, 'CheckedComments', $CheckedComments);
  92. ModerationController::InformCheckedComments($Sender);
  93. $Sender->RedirectUrl = Url('discussion/'.$NewDiscussionID.'/'.Gdn_Format::Url($Data['Name']));
  94. }
  95. } else {
  96. $Sender->Form->SetValue('CategoryID', GetValue('CategoryID', $Discussion));
  97. }
  98. $Sender->Render($this->GetView('splitcomments.php'));
  99. }
  100. /**
  101. * Add a method to the ModerationController to handle merging discussions.
  102. */
  103. public function ModerationController_MergeDiscussions_Create($Sender) {
  104. $Session = Gdn::Session();
  105. $Sender->Form = new Gdn_Form();
  106. $Sender->Title(T('Merge Discussions'));
  107. $DiscussionModel = new DiscussionModel();
  108. $CheckedDiscussions = Gdn::UserModel()->GetAttribute($Session->User->UserID, 'CheckedDiscussions', array());
  109. if (!is_array($CheckedDiscussions))
  110. $CheckedDiscussions = array();
  111. $DiscussionIDs = $CheckedDiscussions;
  112. $Sender->SetData('DiscussionIDs', $DiscussionIDs);
  113. $CountCheckedDiscussions = count($DiscussionIDs);
  114. $Sender->SetData('CountCheckedDiscussions', $CountCheckedDiscussions);
  115. $DiscussionData = $DiscussionModel->GetIn($DiscussionIDs);
  116. $Sender->SetData('DiscussionData', $DiscussionData);
  117. // Perform the merge
  118. if ($Sender->Form->AuthenticatedPostBack()) {
  119. // Create a new discussion record
  120. $MergeDiscussion = FALSE;
  121. $MergeDiscussionID = $Sender->Form->GetFormValue('MergeDiscussionID');
  122. foreach ($DiscussionData->Result() as $Discussion) {
  123. if ($Discussion->DiscussionID == $MergeDiscussionID) {
  124. $MergeDiscussion = $Discussion;
  125. break;
  126. }
  127. }
  128. if ($MergeDiscussion) {
  129. // Verify that the user has permission to perform the merge
  130. $Sender->Permission('Vanilla.Discussions.Edit', TRUE, 'Category', $MergeDiscussion->PermissionCategoryID);
  131. // Assign the comments to the new discussion record
  132. $DiscussionModel->SQL
  133. ->Update('Comment')
  134. ->Set('DiscussionID', $MergeDiscussionID)
  135. ->WhereIn('DiscussionID', $DiscussionIDs)
  136. ->Put();
  137. $CommentModel = new CommentModel();
  138. foreach ($DiscussionIDs as $DiscussionID) {
  139. // Add a new comment to each empty discussion
  140. if ($DiscussionID != $MergeDiscussionID) {
  141. // Add a comment to each one explaining the merge
  142. $DiscussionAnchor = Anchor(
  143. Gdn_Format::Text($MergeDiscussion->Name),
  144. 'discussion/'.$MergeDiscussionID.'/'.Gdn_Format::Url($MergeDiscussion->Name)
  145. );
  146. $CommentModel->Save(array(
  147. 'DiscussionID' => $DiscussionID,
  148. 'Body' => sprintf(T('This discussion was merged into %s'), $DiscussionAnchor),
  149. 'Format' => 'Html'
  150. ));
  151. // Close non-merge discussions
  152. $CommentModel->SQL->Update('Discussion')->Set('Closed', '1')->Where('DiscussionID', $DiscussionID)->Put();
  153. }
  154. // Update counts on all affected discussions
  155. $CommentModel->UpdateCommentCount($DiscussionID);
  156. // $CommentModel->UpdateUserCommentCounts($DiscussionID);
  157. }
  158. // Clear selections
  159. Gdn::UserModel()->SaveAttribute($Session->UserID, 'CheckedDiscussions', FALSE);
  160. ModerationController::InformCheckedDiscussions($Sender);
  161. $Sender->RedirectUrl = Url('discussion/'.$MergeDiscussionID.'/'.Gdn_Format::Url($MergeDiscussion->Name));
  162. }
  163. }
  164. $Sender->Render($this->GetView('mergediscussions.php'));
  165. }
  166. public function Setup() {
  167. SaveToConfig('Vanilla.AdminCheckboxes.Use', TRUE);
  168. }
  169. }