/** * Create or update a discussion. * * @since 2.0.0 * @access public * * @param int $CategoryID Unique ID of the category to add the discussion to. */ public function Discussion($CategoryUrlCode = '') { // Override CategoryID if categories are disabled $UseCategories = $this->ShowCategorySelector = (bool) C('Vanilla.Categories.Use'); if (!$UseCategories) { $CategoryUrlCode = ''; } // Setup head $this->AddJsFile('jquery.autosize.min.js'); $this->AddJsFile('post.js'); $this->AddJsFile('autosave.js'); $Session = Gdn::Session(); Gdn_Theme::Section('PostDiscussion'); // Set discussion, draft, and category data $DiscussionID = isset($this->Discussion) ? $this->Discussion->DiscussionID : ''; $DraftID = isset($this->Draft) ? $this->Draft->DraftID : 0; $Category = FALSE; if (isset($this->Discussion)) { $this->CategoryID = $this->Discussion->CategoryID; $Category = CategoryModel::Categories($this->CategoryID); } else { if ($CategoryUrlCode != '') { $CategoryModel = new CategoryModel(); $Category = $CategoryModel->GetByCode($CategoryUrlCode); $this->CategoryID = $Category->CategoryID; } } if ($Category) { $this->Category = (object) $Category; $this->SetData('Category', $Category); } else { $this->CategoryID = 0; $this->Category = NULL; } $CategoryData = $UseCategories ? CategoryModel::Categories() : FALSE; // Check permission if (isset($this->Discussion)) { // Permission to edit if ($this->Discussion->InsertUserID != $Session->UserID) { $this->Permission('Vanilla.Discussions.Edit', TRUE, 'Category', $this->Category->PermissionCategoryID); } // Make sure that content can (still) be edited. $EditContentTimeout = C('Garden.EditContentTimeout', -1); $CanEdit = $EditContentTimeout == -1 || strtotime($this->Discussion->DateInserted) + $EditContentTimeout > time(); if (!$CanEdit) { $this->Permission('Vanilla.Discussions.Edit', TRUE, 'Category', $this->Category->PermissionCategoryID); } // Make sure only moderators can edit closed things if ($this->Discussion->Closed) { $this->Permission('Vanilla.Discussions.Edit', TRUE, 'Category', $this->Category->PermissionCategoryID); } $this->Form->SetFormValue('DiscussionID', $this->Discussion->DiscussionID); $this->Title(T('Edit Discussion')); if ($this->Discussion->Type) { $this->SetData('Type', $this->Discussion->Type); } else { $this->SetData('Type', 'Discussion'); } } else { // Permission to add $this->Permission('Vanilla.Discussions.Add'); $this->Title(T('New Discussion')); } TouchValue('Type', $this->Data, 'Discussion'); // See if we should hide the category dropdown. $AllowedCategories = CategoryModel::GetByPermission('Discussions.Add', $this->Form->GetValue('CategoryID', $this->CategoryID), array('Archived' => 0, 'AllowDiscussions' => 1), array('AllowedDiscussionTypes' => $this->Data['Type'])); if (count($AllowedCategories) == 1) { $AllowedCategory = array_pop($AllowedCategories); $this->ShowCategorySelector = FALSE; $this->Form->AddHidden('CategoryID', $AllowedCategory['CategoryID']); if ($this->Form->IsPostBack() && !$this->Form->GetFormValue('CategoryID')) { $this->Form->SetFormValue('CategoryID', $AllowedCategory['CategoryID']); } } // Set the model on the form $this->Form->SetModel($this->DiscussionModel); if ($this->Form->IsPostBack() == FALSE) { // Prep form with current data for editing if (isset($this->Discussion)) { $this->Form->SetData($this->Discussion); } elseif (isset($this->Draft)) { $this->Form->SetData($this->Draft); } else { if ($this->Category !== NULL) { $this->Form->SetData(array('CategoryID' => $this->Category->CategoryID)); } $this->PopulateForm($this->Form); } } else { // Form was submitted // Save as a draft? $FormValues = $this->Form->FormValues(); $FormValues = $this->DiscussionModel->FilterForm($FormValues); $this->DeliveryType(GetIncomingValue('DeliveryType', $this->_DeliveryType)); if ($DraftID == 0) { $DraftID = $this->Form->GetFormValue('DraftID', 0); } $Draft = $this->Form->ButtonExists('Save Draft') ? TRUE : FALSE; $Preview = $this->Form->ButtonExists('Preview') ? TRUE : FALSE; if (!$Preview) { if (!is_object($this->Category) && is_array($CategoryData) && isset($FormValues['CategoryID'])) { $this->Category = GetValue($FormValues['CategoryID'], $CategoryData); } if (is_object($this->Category)) { // Check category permissions. if ($this->Form->GetFormValue('Announce', '') && !$Session->CheckPermission('Vanilla.Discussions.Announce', TRUE, 'Category', $this->Category->PermissionCategoryID)) { $this->Form->AddError('You do not have permission to announce in this category', 'Announce'); } if ($this->Form->GetFormValue('Close', '') && !$Session->CheckPermission('Vanilla.Discussions.Close', TRUE, 'Category', $this->Category->PermissionCategoryID)) { $this->Form->AddError('You do not have permission to close in this category', 'Close'); } if ($this->Form->GetFormValue('Sink', '') && !$Session->CheckPermission('Vanilla.Discussions.Sink', TRUE, 'Category', $this->Category->PermissionCategoryID)) { $this->Form->AddError('You do not have permission to sink in this category', 'Sink'); } if (!isset($this->Discussion) && (!$Session->CheckPermission('Vanilla.Discussions.Add', TRUE, 'Category', $this->Category->PermissionCategoryID) || !$this->Category->AllowDiscussions)) { $this->Form->AddError('You do not have permission to start discussions in this category', 'CategoryID'); } } // Make sure that the title will not be invisible after rendering $Name = trim($this->Form->GetFormValue('Name', '')); if ($Name != '' && Gdn_Format::Text($Name) == '') { $this->Form->AddError(T('You have entered an invalid discussion title'), 'Name'); } else { // Trim the name. $FormValues['Name'] = $Name; $this->Form->SetFormValue('Name', $Name); } if ($this->Form->ErrorCount() == 0) { if ($Draft) { $DraftID = $this->DraftModel->Save($FormValues); $this->Form->SetValidationResults($this->DraftModel->ValidationResults()); } else { $DiscussionID = $this->DiscussionModel->Save($FormValues, $this->CommentModel); $this->Form->SetValidationResults($this->DiscussionModel->ValidationResults()); if ($DiscussionID > 0) { if ($DraftID > 0) { $this->DraftModel->Delete($DraftID); } } if ($DiscussionID == SPAM || $DiscussionID == UNAPPROVED) { $this->StatusMessage = T('DiscussionRequiresApprovalStatus', 'Your discussion will appear after it is approved.'); $this->Render('Spam'); return; } } } } else { // If this was a preview click, create a discussion/comment shell with the values for this comment $this->Discussion = new stdClass(); $this->Discussion->Name = $this->Form->GetValue('Name', ''); $this->Comment = new stdClass(); $this->Comment->InsertUserID = $Session->User->UserID; $this->Comment->InsertName = $Session->User->Name; $this->Comment->InsertPhoto = $Session->User->Photo; $this->Comment->DateInserted = Gdn_Format::Date(); $this->Comment->Body = ArrayValue('Body', $FormValues, ''); $this->Comment->Format = GetValue('Format', $FormValues, C('Garden.InputFormatter')); $this->EventArguments['Discussion'] =& $this->Discussion; $this->EventArguments['Comment'] =& $this->Comment; $this->FireEvent('BeforeDiscussionPreview'); if ($this->_DeliveryType == DELIVERY_TYPE_ALL) { $this->AddAsset('Content', $this->FetchView('preview')); } else { $this->View = 'preview'; } } if ($this->Form->ErrorCount() > 0) { // Return the form errors $this->ErrorMessage($this->Form->Errors()); } else { if ($DiscussionID > 0 || $DraftID > 0) { // Make sure that the ajax request form knows about the newly created discussion or draft id $this->SetJson('DiscussionID', $DiscussionID); $this->SetJson('DraftID', $DraftID); if (!$Preview) { // If the discussion was not a draft if (!$Draft) { // Redirect to the new discussion $Discussion = $this->DiscussionModel->GetID($DiscussionID, DATASET_TYPE_OBJECT, array('Slave' => FALSE)); $this->SetData('Discussion', $Discussion); $this->EventArguments['Discussion'] = $Discussion; $this->FireEvent('AfterDiscussionSave'); if ($this->_DeliveryType == DELIVERY_TYPE_ALL) { Redirect(DiscussionUrl($Discussion)) . '?new=1'; } else { $this->RedirectUrl = DiscussionUrl($Discussion, '', TRUE) . '?new=1'; } } else { // If this was a draft save, notify the user about the save $this->InformMessage(sprintf(T('Draft saved at %s'), Gdn_Format::Date())); } } } } } // Add hidden fields for editing $this->Form->AddHidden('DiscussionID', $DiscussionID); $this->Form->AddHidden('DraftID', $DraftID, TRUE); $this->FireEvent('BeforeDiscussionRender'); if ($this->CategoryID) { $Breadcrumbs = CategoryModel::GetAncestors($this->CategoryID); } else { $Breadcrumbs = array(); } $Breadcrumbs[] = array('Name' => $this->Data('Title'), 'Url' => '/post/discussion'); $this->SetData('Breadcrumbs', $Breadcrumbs); $this->SetData('_AnnounceOptions', $this->AnnounceOptions()); // Render view (posts/discussion.php or post/preview.php) $this->Render(); }