/** * Create a discussion. * * @param int The CategoryID to add the discussion to. */ public function Discussion($CategoryID = '') { $Session = Gdn::Session(); $DiscussionID = isset($this->Discussion) ? $this->Discussion->DiscussionID : ''; $DraftID = isset($this->Draft) ? $this->Draft->DraftID : 0; $this->CategoryID = isset($this->Discussion) ? $this->Discussion->CategoryID : $CategoryID; if (Gdn::Config('Vanilla.Categories.Use') === TRUE) { $CategoryModel = new Gdn_CategoryModel(); // Filter to categories that this user can add to $CategoryModel->SQL->Distinct()->Join('Permission _p2', '_p2.JunctionID = c.CategoryID', 'inner')->Join('UserRole _ur2', '_p2.RoleID = _ur2.RoleID', 'inner')->BeginWhereGroup()->Where('_ur2.UserID', $Session->UserID)->Where('_p2.`Vanilla.Discussions.Add`', 1)->EndWhereGroup(); $this->CategoryData = $CategoryModel->GetFull(); } $this->AddJsFile('js/library/jquery.autogrow.js'); $this->AddJsFile('post.js'); $this->AddJsFile('autosave.js'); $this->Title(Translate('Start a New Discussion')); if (isset($this->Discussion)) { if ($this->Discussion->InsertUserID != $Session->UserID) { $this->Permission('Vanilla.Discussions.Edit', $this->Discussion->CategoryID); } } else { $this->Permission('Vanilla.Discussions.Add'); } // Set the model on the form. $this->Form->SetModel($this->DiscussionModel); if ($this->Form->AuthenticatedPostBack() === FALSE) { if (isset($this->Discussion)) { $this->Form->SetData($this->Discussion); } else { if (isset($this->Draft)) { $this->Form->SetData($this->Draft); } else { $this->Form->SetData(array('CategoryID' => $CategoryID)); } } } else { // Save as a draft? $FormValues = $this->Form->FormValues(); 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) { // Check category permissions if ($this->Form->GetFormValue('Announce', '') != '' && !$Session->CheckPermission('Vanilla.Discussions.Announce', $this->CategoryID)) { $this->Form->AddError('You do not have permission to announce in this category', 'Announce'); } if ($this->Form->GetFormValue('Close', '') != '' && !$Session->CheckPermission('Vanilla.Discussions.Close', $this->CategoryID)) { $this->Form->AddError('You do not have permission to close in this category', 'Close'); } if ($this->Form->GetFormValue('Sink', '') != '' && !$Session->CheckPermission('Vanilla.Discussions.Sink', $this->CategoryID)) { $this->Form->AddError('You do not have permission to sink in this category', 'Sink'); } if (!$Session->CheckPermission('Vanilla.Discussions.Add', $this->CategoryID)) { $this->Form->AddError('You do not have permission to start discussions in this category', 'CategoryID'); } 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 && $DraftID > 0) { $this->DraftModel->Delete($DraftID); } } } } 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 = Format::Date(); $this->Comment->Body = ArrayValue('Body', $FormValues, ''); 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->StatusMessage = $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); if ($this->_DeliveryType == DELIVERY_TYPE_ALL) { Redirect('/vanilla/discussion/' . $DiscussionID . '/' . Format::Url($Discussion->Name)); } else { $this->RedirectUrl = Url('/vanilla/discussion/' . $DiscussionID . '/' . Format::Url($Discussion->Name)); } } else { // If this was a draft save, notify the user about the save $this->StatusMessage = sprintf(Gdn::Translate('Draft saved at %s'), Format::Date()); } } } } } $this->Form->AddHidden('DiscussionID', $DiscussionID); $this->Form->AddHidden('DraftID', $DraftID, TRUE); $this->Render(); }