PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/library/Vanilla/Vanilla.Control.DiscussionForm.php

http://lussumo-vanilla.googlecode.com/
PHP | 287 lines | 212 code | 37 blank | 38 comment | 67 complexity | 9f1c5e276b2dccf816ec7a45e1548f19 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0
  1. <?php
  2. /**
  3. * The DiscussionForm control is used for adding and editing discussions and comments in Vanilla.
  4. *
  5. * Copyright 2003 Mark O'Sullivan
  6. * This file is part of Lussumo's Software Library.
  7. * Lussumo's Software Library 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 2 of the License, or (at your option) any later version.
  8. * Lussumo's Software Library 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.
  9. * You should have received a copy of the GNU General Public License along with Vanilla; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  10. * The latest source code is available at www.vanilla1forums.com
  11. * Contact Mark O'Sullivan at mark [at] lussumo [dot] com
  12. *
  13. * @author Mark O'Sullivan
  14. * @copyright 2003 Mark O'Sullivan
  15. * @license http://www.gnu.org/licenses/gpl-2.0.html GPL 2
  16. * @package Vanilla
  17. */
  18. /**
  19. * The DiscussionForm control is used for adding and editing discussions and comments in Vanilla.
  20. * @package Vanilla
  21. */
  22. class DiscussionForm extends PostBackControl {
  23. var $FatalError; // If a fatal error occurs, only the warning messages should be displayed
  24. var $EditDiscussionID;
  25. var $Discussion;
  26. var $DiscussionFormattedForDisplay;
  27. var $DiscussionID;
  28. var $Comment;
  29. var $CommentID;
  30. var $Form;
  31. var $Title; // The title of the form
  32. var $CommentFormAttributes; // Attributes added to the comment form's textarea input
  33. var $DiscussionFormAttributes; // Attributes added to the discussion form's textarea input
  34. function DiscussionForm(&$Context) {
  35. $this->Name = 'DiscussionForm';
  36. $this->CommentFormAttributes = '';
  37. $this->DiscussionFormAttributes = '';
  38. $this->Constructor($Context);
  39. $this->FatalError = 0;
  40. $this->EditDiscussionID = 0;
  41. $this->CommentID = ForceIncomingInt('CommentID', 0);
  42. $this->DiscussionID = ForceIncomingInt('DiscussionID', 0);
  43. $this->DiscussionFormattedForDisplay = 0;
  44. $this->ValidActions = array('SaveDiscussion', 'SaveComment', 'Reply');
  45. $this->CallDelegate('PreLoadData');
  46. // Check permissions and make sure that the user can add comments/discussions
  47. // Make sure user can post
  48. if ($this->DiscussionID == 0 && $this->Context->Session->UserID == 0) {
  49. $this->Context->WarningCollector->Add($this->Context->GetDefinition('NoDiscussionsNotSignedIn'));
  50. $this->FatalError = 1;
  51. }
  52. $this->Comment = $this->Context->ObjectFactory->NewContextObject($this->Context, 'Comment');
  53. $this->Discussion = $this->Context->ObjectFactory->NewContextObject($this->Context, 'Discussion');
  54. $cm = $this->Context->ObjectFactory->NewContextObject($this->Context, 'CommentManager');
  55. $dm = $this->Context->ObjectFactory->NewContextObject($this->Context, 'DiscussionManager');
  56. $this->DelegateParameters['CommentManager'] = &$cm;
  57. $this->DelegateParameters['DiscussionManager'] = &$dm;
  58. // If editing a comment, define it and validate the user's permissions
  59. if ($this->CommentID > 0) {
  60. $this->Comment = $cm->GetCommentById($this->CommentID, $this->Context->Session->UserID);
  61. if (!$this->Comment) {
  62. $this->FatalError = 1;
  63. } else {
  64. $this->DiscussionID = $this->Comment->DiscussionID;
  65. $this->Discussion = $dm->GetDiscussionById($this->Comment->DiscussionID);
  66. if (!$this->Discussion) {
  67. $this->FatalError = 1;
  68. } else {
  69. // if editing a discussion
  70. if (($this->Context->Session->UserID == $this->Discussion->AuthUserID || $this->Context->Session->User->Permission('PERMISSION_EDIT_DISCUSSIONS')) && $this->Discussion->FirstCommentID == $this->CommentID) {
  71. $this->EditDiscussionID = $this->Discussion->DiscussionID;
  72. $this->Discussion->Comment = $this->Comment;
  73. }
  74. // Set the page title
  75. $this->DiscussionFormattedForDisplay = 1;
  76. $this->Discussion->FormatPropertiesForDisplay();
  77. $this->Context->PageTitle = $this->Discussion->Name;
  78. }
  79. }
  80. // Ensure that this user has sufficient priviledges to edit the comment
  81. if ($this->Comment
  82. && $this->Discussion
  83. && !$this->Context->Session->User->Permission('PERMISSION_EDIT_COMMENTS')
  84. && $this->Context->Session->UserID != $this->Comment->AuthUserID
  85. && !($this->Discussion->FirstCommentID == $this->CommentID && $this->Context->Session->User->Permission('PERMISSION_EDIT_DISCUSSIONS'))) {
  86. $this->Context->WarningCollector->Add($this->Context->GetDefinition('ErrPermissionCommentEdit'));
  87. $this->FatalError = 1;
  88. }
  89. }
  90. $this->CallDelegate('PostLoadData');
  91. // If saving a discussion
  92. if ($this->PostBackAction == 'SaveDiscussion') {
  93. $FirstCommentID = $this->Discussion->FirstCommentID;
  94. $AuthUserID = $this->Discussion->AuthUserID;
  95. $this->Discussion->Clear();
  96. $this->Discussion->GetPropertiesFromForm($this->Context);
  97. $this->Discussion->FirstCommentID = $FirstCommentID;
  98. $this->Discussion->AuthUserID = $AuthUserID;
  99. // If we are editing a discussion, the following line
  100. // will make sure we save the proper discussion topic & message
  101. $this->Discussion->DiscussionID = $this->EditDiscussionID;
  102. if ($this->IsValidFormPostBack()) {
  103. $this->DelegateParameters['SaveDiscussion'] = &$this->Discussion;
  104. $this->CallDelegate('PreSaveDiscussion');
  105. $ResultDiscussion = $dm->SaveDiscussion($this->Discussion);
  106. $this->DelegateParameters['ResultDiscussion'] = &$ResultDiscussion;
  107. $this->CallDelegate('PostSaveDiscussion');
  108. if ($ResultDiscussion && ($ResultDiscussion->DiscussionID > 0)) {
  109. // Saved successfully, so send back to the discussion
  110. $Suffix = CleanupString($this->Discussion->Name).'/';
  111. $Redirect = GetUrl(
  112. $this->Context->Configuration, 'comments.php', '', 'DiscussionID',
  113. $ResultDiscussion->DiscussionID, '', '', $Suffix);
  114. Redirect($Redirect);
  115. }
  116. }
  117. // If saving a comment
  118. } elseif ($this->PostBackAction == 'SaveComment') {
  119. $this->Comment->Clear();
  120. $this->Comment->GetPropertiesFromForm();
  121. $this->Comment->DiscussionID = $this->DiscussionID;
  122. $this->Discussion = $dm->GetDiscussionById($this->Comment->DiscussionID);
  123. if ($this->IsValidFormPostBack()) {
  124. // Make sure to prevent saving comments if the discussion is closed and
  125. // the user doesn't have permission to manage closed discussions
  126. if ($this->Discussion->Closed && !$this->Context->Session->User->Permission('PERMISSION_CLOSE_DISCUSSIONS')) {
  127. $ResultComment = false;
  128. $this->Context->WarningCollector->Add($this->Context->GetDefinition('ErrPermissionInsufficient'));
  129. } else {
  130. $this->DelegateParameters['SaveComment'] = &$this->Comment;
  131. $this->CallDelegate('PreSaveComment');
  132. $ResultComment = $cm->SaveComment($this->Comment);
  133. }
  134. $this->DelegateParameters['ResultComment'] = &$ResultComment;
  135. $this->CallDelegate('PostSaveComment');
  136. if ($ResultComment) {
  137. // Reload the discussion so the "lastpage" property is recalculated with the new comment in the math.
  138. $this->Discussion = $dm->GetDiscussionById($this->Comment->DiscussionID);
  139. // Saved successfully, so send back to the discussion
  140. // print_r($this->Discussion);
  141. $Suffix = CleanupString($this->Discussion->Name).'/';
  142. $Url = GetUrl($this->Context->Configuration, 'comments.php', '', 'DiscussionID', $ResultComment->DiscussionID, $this->Discussion->LastPage, ($ResultComment->CommentID > 0 ? '#Comment_'.$ResultComment->CommentID:'#pgbottom'), $Suffix);
  143. $UrlParts = explode("?", $Url);
  144. $QS = "";
  145. if (array_key_exists(1, $UrlParts)) $QS = str_replace("&amp;", "&", $UrlParts[1]);
  146. $Url = $UrlParts[0];
  147. if ($QS != "") $Url .= "?".str_replace("&amp;", "&", $UrlParts[1]);
  148. Redirect($Url);
  149. }
  150. }
  151. } elseif ($this->PostBackAction == 'Reply') {
  152. if ($this->Comment) $this->Comment->GetPropertiesFromForm();
  153. }
  154. if (!$this->IsPostBack && $this->Comment->DiscussionID == 0 && $this->Comment->CommentID == 0) {
  155. if (!$this->Discussion->Comment) $this->Discussion->Comment = $this->Context->ObjectFactory->NewContextObject($this->Context, 'Comment');
  156. $this->Discussion->Comment->FormatType = $this->Context->Session->User->DefaultFormatType;
  157. if ($this->Comment) $this->Comment->FormatType = $this->Context->Session->User->DefaultFormatType;
  158. }
  159. if ($this->Comment) $this->PostBackParams->Set('CommentID', $this->Comment->CommentID);
  160. $this->PostBackParams->Set('DiscussionID', $this->DiscussionID);
  161. $this->Title = ($this->EditDiscussionID
  162. ? $this->Context->GetDefinition('EditYourDiscussion')
  163. : $this->Context->GetDefinition('StartANewDiscussion'));
  164. if ($this->EditDiscussionID > 0 || ($this->CommentID == 0 && $this->DiscussionID == 0)) {
  165. $this->Form = 'DiscussionForm';
  166. } else {
  167. $this->Form = 'CommentForm';
  168. if ($this->Comment && $this->Comment->CommentID > 0) {
  169. $this->Title = $this->Context->GetDefinition('EditYourComments');
  170. } else {
  171. $this->Title = $this->Context->GetDefinition('AddYourComments');
  172. }
  173. }
  174. $this->Context->PageTitle = $this->Title;
  175. $this->CallDelegate('PostSaveData');
  176. }
  177. function GetCommentForm($Comment) {
  178. $this->DelegateParameters['Comment'] = &$Comment;
  179. $this->CallDelegate('CommentForm_PreRender');
  180. // Encode everything properly
  181. $Comment->FormatPropertiesForDisplay(1);
  182. $this->PostBackParams->Set('PostBackAction', 'SaveComment');
  183. $this->PostBackParams->Set('UserCommentCount', $this->Context->Session->User->CountComments);
  184. $this->PostBackParams->Set('AuthUserID', $Comment->AuthUserID);
  185. include(ThemeFilePath($this->Context->Configuration, 'comment_form.php'));
  186. $this->CallDelegate('CommentForm_PostRender');
  187. }
  188. function GetDiscussionForm($Discussion) {
  189. $this->DelegateParameters['Discussion'] = &$Discussion;
  190. $this->CallDelegate('DiscussionForm_PreRender');
  191. if (!$this->DiscussionFormattedForDisplay) $Discussion->FormatPropertiesForDisplay();
  192. $Discussion->Comment->FormatPropertiesForDisplay(1);
  193. // Load the category selector
  194. $cm = $this->Context->ObjectFactory->NewContextObject($this->Context, 'CategoryManager');
  195. $CategoryData = $cm->GetCategories(0, 1);
  196. $cs = $this->Context->ObjectFactory->NewObject($this->Context, 'Select');
  197. $cs->Name = 'CategoryID';
  198. $cs->CssClass = 'CategorySelect';
  199. $cs->SelectedValue = ForceIncomingInt('CategoryID', $Discussion->CategoryID);
  200. $cat = $this->Context->ObjectFactory->NewObject($this->Context, 'Category');
  201. $LastBlocked = -1;
  202. while ($Row = $this->Context->Database->GetRow($CategoryData)) {
  203. $cat->Clear();
  204. $cat->GetPropertiesFromDataSet($Row);
  205. if ($cat->Blocked != $LastBlocked && $LastBlocked != -1) {
  206. $cs->AddOption("-1", "---", " disabled=\"true\"");
  207. }
  208. $cs->AddOption($cat->CategoryID, $cat->Name);
  209. $LastBlocked = $cat->Blocked;
  210. }
  211. $this->PostBackParams->Set('CommentID', $Discussion->FirstCommentID);
  212. $this->PostBackParams->Set('AuthUserID', $Discussion->AuthUserID);
  213. $this->PostBackParams->Set('UserDiscussionCount', $this->Context->Session->User->CountDiscussions);
  214. $this->PostBackParams->Set('PostBackAction', 'SaveDiscussion');
  215. include(ThemeFilePath($this->Context->Configuration, 'discussion_form.php'));
  216. $this->CallDelegate('DiscussionForm_PostRender');
  217. }
  218. function GetPostFormatting($SelectedFormatType) {
  219. $FormatCount = count($this->Context->StringManipulator->Formatters);
  220. $f = $this->Context->ObjectFactory->NewObject($this->Context, 'Radio');
  221. $f->Name = 'FormatType';
  222. $f->CssClass = 'FormatTypeRadio';
  223. $f->SelectedID = $SelectedFormatType;
  224. $this->DelegateParameters['FormatRadio'] = &$f;
  225. $ItemAppend = '';
  226. while (list($Name, $Object) = each($this->Context->StringManipulator->Formatters)) {
  227. $this->DelegateParameters['RadioItemName'] = &$Name;
  228. $this->DelegateParameters['RadioItemAppend'] = &$ItemAppend;
  229. $this->CallDelegate('PreFormatRadioItemAdd');
  230. $f->AddOption($Name, $this->Context->GetDefinition($Name), $ItemAppend);
  231. $ItemAppend = '';
  232. }
  233. $this->CallDelegate('PreFormatRadioRender');
  234. $sReturn = '';
  235. include(ThemeFilePath($this->Context->Configuration, 'post_formatter.php'));
  236. return $sReturn;
  237. }
  238. function Render() {
  239. if ($this->FatalError) {
  240. $this->Render_Warnings();
  241. } else {
  242. if ($this->Form == 'DiscussionForm') {
  243. $this->GetDiscussionForm($this->Discussion);
  244. } elseif ($this->Form == 'CommentForm') {
  245. $this->GetCommentForm($this->Comment);
  246. }
  247. }
  248. }
  249. }
  250. ?>