/** * Post every new discussion to HipChat. * * @param PostController $sender * @param array $args */ public function discussionModel_afterSaveDiscussion_handler($sender, $args) { // Make sure we have a valid discussion. if (!$args['Discussion'] || !val('DiscussionID', $args['Discussion'])) { return; } // Only trigger for new discussions. if (!$args['Insert']) { return; } // Prep HipChat message. $author = Gdn::userModel()->getID(val('InsertUserID', $args['Discussion'])); $message = sprintf('%1$s: %2$s', userAnchor($author), anchor(val('Name', $args['Discussion']), discussionUrl($args['Discussion']))); // Say it. self::sayInHipChat($message); }
/** * Handle discussion option menu Change Author action. */ public function discussionController_author_create($Sender) { $DiscussionID = $Sender->Request->get('discussionid'); $Discussion = $Sender->DiscussionModel->getID($DiscussionID); if (!$Discussion) { throw NotFoundException('Discussion'); } // Check edit permission $Sender->permission('Vanilla.Discussions.Edit', true, 'Category', $Discussion->PermissionCategoryID); if ($Sender->Form->authenticatedPostBack()) { // Change the author $Name = $Sender->Form->getFormValue('Author', ''); $UserModel = new UserModel(); if (trim($Name) != '') { $User = $UserModel->getByUsername(trim($Name)); if (is_object($User)) { if ($Discussion->InsertUserID == $User->UserID) { $Sender->Form->addError('That user is already the discussion author.'); } else { // Change discussion InsertUserID $Sender->DiscussionModel->setField($DiscussionID, 'InsertUserID', $User->UserID); // Update users' discussion counts $Sender->DiscussionModel->updateUserDiscussionCount($Discussion->InsertUserID); $Sender->DiscussionModel->updateUserDiscussionCount($User->UserID, true); // Increment // Go to the updated discussion redirect(discussionUrl($Discussion)); } } else { $Sender->Form->addError('No user with that name was found.'); } } } else { // Form to change the author $Sender->setData('Title', $Discussion->Name); } $Sender->render('changeauthor', '', 'plugins/AuthorSelector'); }
/** * * * @param $Sender * @param $Args * @throws Gdn_UserException */ public function discussionModel_afterSaveDiscussion_handler($Sender, $Args) { if (!$this->socialSharing() || !$this->accessToken()) { return; } $Share = valr('FormPostValues.ShareTwitter', $Args); if ($Share && $this->accessToken()) { $Row = $Args['Fields']; $Url = discussionUrl($Row, '', true); $Message = sliceTwitter(Gdn_Format::plainText($Row['Body'], $Row['Format'])) . ' ' . $Url; $R = $this->api('/statuses/update.json', array('status' => $Message), 'POST'); } }
/** * Alternate version of Index that uses the embed master view. * * @param int $DiscussionID Unique identifier, if discussion has been created. * @param string $DiscussionStub Deprecated. * @param int $Offset * @param int $Limit */ public function embed($DiscussionID = '', $DiscussionStub = '', $Offset = '', $Limit = '') { $this->title(t('Comments')); // Add theme data $this->Theme = c('Garden.CommentsTheme', $this->Theme); Gdn_Theme::section('Comments'); // Force view options $this->MasterView = 'empty'; $this->CanEditComments = false; // Don't show the comment checkboxes on the embed comments page // Add some css to help with the transparent bg on embedded comments if ($this->Head) { $this->Head->addString('<style type="text/css"> body { background: transparent !important; } </style>'); } // Javascript files & options $this->addJsFile('jquery.gardenmorepager.js'); $this->addJsFile('jquery.autosize.min.js'); $this->addJsFile('discussion.js'); $this->removeJsFile('autosave.js'); $this->addDefinition('DoInform', '0'); // Suppress inform messages on embedded page. $this->addDefinition('SelfUrl', Gdn::request()->PathAndQuery()); $this->addDefinition('Embedded', true); // Define incoming variables (prefer querystring parameters over method parameters) $DiscussionID = is_numeric($DiscussionID) && $DiscussionID > 0 ? $DiscussionID : 0; $DiscussionID = getIncomingValue('vanilla_discussion_id', $DiscussionID); $Offset = getIncomingValue('Offset', $Offset); $Limit = getIncomingValue('Limit', $Limit); $vanilla_identifier = getIncomingValue('vanilla_identifier', ''); // Only allow vanilla identifiers of 32 chars or less - md5 if larger if (strlen($vanilla_identifier) > 32) { $vanilla_identifier = md5($vanilla_identifier); } $vanilla_type = getIncomingValue('vanilla_type', 'page'); $vanilla_url = getIncomingValue('vanilla_url', ''); $vanilla_category_id = getIncomingValue('vanilla_category_id', ''); $ForeignSource = array('vanilla_identifier' => $vanilla_identifier, 'vanilla_type' => $vanilla_type, 'vanilla_url' => $vanilla_url, 'vanilla_category_id' => $vanilla_category_id); $this->setData('ForeignSource', $ForeignSource); // Set comment sorting $SortComments = c('Garden.Embed.SortComments') == 'desc' ? 'desc' : 'asc'; $this->setData('SortComments', $SortComments); // Retrieve the discussion record $Discussion = false; if ($DiscussionID > 0) { $Discussion = $this->DiscussionModel->getID($DiscussionID); } elseif ($vanilla_identifier != '' && $vanilla_type != '') { $Discussion = $this->DiscussionModel->GetForeignID($vanilla_identifier, $vanilla_type); } // Set discussion data if we have one for this page if ($Discussion) { // Allow Vanilla.Comments.View to be defined to limit access to embedded comments only. // Otherwise, go with normal discussion view permissions. Either will do. $this->permission(array('Vanilla.Discussions.View', 'Vanilla.Comments.View'), false, 'Category', $Discussion->PermissionCategoryID); $this->setData('Discussion', $Discussion, true); $this->setData('DiscussionID', $Discussion->DiscussionID, true); $this->title($Discussion->Name); // Actual number of comments, excluding the discussion itself $ActualResponses = $Discussion->CountComments; // Define the query offset & limit if (!is_numeric($Limit) || $Limit < 0) { $Limit = c('Garden.Embed.CommentsPerPage', 30); } $OffsetProvided = $Offset != ''; list($Offset, $Limit) = offsetLimit($Offset, $Limit); $this->Offset = $Offset; if (c('Vanilla.Comments.AutoOffset')) { if ($ActualResponses <= $Limit) { $this->Offset = 0; } if ($this->Offset == $ActualResponses) { $this->Offset -= $Limit; } } elseif ($this->Offset == '') { $this->Offset = 0; } if ($this->Offset < 0) { $this->Offset = 0; } // Set the canonical url to have the proper page title. $this->canonicalUrl(discussionUrl($Discussion, pageNumber($this->Offset, $Limit))); // Load the comments. $CurrentOrderBy = $this->CommentModel->orderBy(); if (stringBeginsWith(GetValueR('0.0', $CurrentOrderBy), 'c.DateInserted')) { $this->CommentModel->orderBy('c.DateInserted ' . $SortComments); // allow custom sort } $this->setData('Comments', $this->CommentModel->get($Discussion->DiscussionID, $Limit, $this->Offset), true); if (count($this->CommentModel->where()) > 0) { $ActualResponses = false; } $this->setData('_Count', $ActualResponses); // Build a pager $PagerFactory = new Gdn_PagerFactory(); $this->EventArguments['PagerType'] = 'MorePager'; $this->fireEvent('BeforeBuildPager'); $this->Pager = $PagerFactory->getPager($this->EventArguments['PagerType'], $this); $this->Pager->ClientID = 'Pager'; $this->Pager->MoreCode = 'More Comments'; $this->Pager->configure($this->Offset, $Limit, $ActualResponses, 'discussion/embed/' . $Discussion->DiscussionID . '/' . Gdn_Format::url($Discussion->Name) . '/%1$s'); $this->Pager->CurrentRecords = $this->Comments->numRows(); $this->fireEvent('AfterBuildPager'); } // Define the form for the comment input $this->Form = Gdn::Factory('Form', 'Comment'); $this->Form->Action = url('/post/comment/'); $this->Form->addHidden('CommentID', ''); $this->Form->addHidden('Embedded', 'true'); // Tell the post controller that this is an embedded page (in case there are custom views it needs to pick up from a theme). $this->Form->addHidden('DisplayNewCommentOnly', 'true'); // Only load/display the new comment after posting (don't load all new comments since the page last loaded). // Grab the page title if ($this->Request->get('title')) { $this->Form->setValue('Name', $this->Request->get('title')); } // Set existing DiscussionID for comment form if ($Discussion) { $this->Form->addHidden('DiscussionID', $Discussion->DiscussionID); } foreach ($ForeignSource as $Key => $Val) { // Drop the foreign source information into the form so it can be used if creating a discussion $this->Form->addHidden($Key, $Val); // Also drop it into the definitions so it can be picked up for stashing comments $this->addDefinition($Key, $Val); } // Retrieve & apply the draft if there is one: $Draft = false; if (Gdn::session()->UserID && $Discussion) { $DraftModel = new DraftModel(); $Draft = $DraftModel->get(Gdn::session()->UserID, 0, 1, $Discussion->DiscussionID)->firstRow(); $this->Form->addHidden('DraftID', $Draft ? $Draft->DraftID : ''); } if ($Draft) { $this->Form->setFormValue('Body', $Draft->Body); } else { // Look in the session stash for a comment $StashComment = Gdn::session()->getPublicStash('CommentForForeignID_' . $ForeignSource['vanilla_identifier']); if ($StashComment) { $this->Form->setValue('Body', $StashComment); $this->Form->setFormValue('Body', $StashComment); } } // Deliver JSON data if necessary if ($this->_DeliveryType != DELIVERY_TYPE_ALL) { if ($this->Discussion) { $this->setJson('LessRow', $this->Pager->toString('less')); $this->setJson('MoreRow', $this->Pager->toString('more')); } $this->View = 'comments'; } // Ordering note for JS if ($SortComments == 'desc') { $this->addDefinition('PrependNewComments', '1'); } // Report the discussion id so js can use it. if ($Discussion) { $this->addDefinition('DiscussionID', $Discussion->DiscussionID); } $this->fireEvent('BeforeDiscussionRender'); $this->render(); }
/** * Edit a discussion (wrapper for PostController::Discussion). * * Will throw an error if both params are blank. * * @since 2.0.0 * @access public * * @param int $DiscussionID Unique ID of the discussion to edit. * @param int $DraftID Unique ID of draft discussion to edit. */ public function editDiscussion($DiscussionID = '', $DraftID = '') { if ($DraftID != '') { $this->Draft = $this->DraftModel->getID($DraftID); $this->CategoryID = $this->Draft->CategoryID; // Verify this is their draft if (val('InsertUserID', $this->Draft) != Gdn::session()->UserID) { throw permissionException(); } } else { $this->setData('Discussion', $this->DiscussionModel->getID($DiscussionID), true); $this->CategoryID = $this->Discussion->CategoryID; } if (c('Garden.ForceInputFormatter')) { $this->Form->removeFormValue('Format'); } $this->setData('_CancelUrl', discussionUrl($this->data('Discussion'))); // Set view and render $this->View = 'Discussion'; $this->discussion($this->CategoryID); }
/** * Add numbering index to discussion. * * @param DiscussionController $sender Sending controller instance. * @param array $args Event arguments. */ public function discussionController_discussionInfo_handler($sender, $args) { echo wrap(anchor('#1', discussionUrl($args['Discussion'])), 'span', ['Class' => 'MItem PostNumbering Num-1']); }
/** * * * @param $Search * @param int $Offset * @param int $Limit * @return array|null * @throws Exception */ public function search($Search, $Offset = 0, $Limit = 20) { // If there are no searches then return an empty array. if (trim($Search) == '') { return array(); } // Figure out the exact search mode. if ($this->ForceSearchMode) { $SearchMode = $this->ForceSearchMode; } else { $SearchMode = strtolower(c('Garden.Search.Mode', 'matchboolean')); } if ($SearchMode == 'matchboolean') { if (strpos($Search, '+') !== false || strpos($Search, '-') !== false) { $SearchMode = 'boolean'; } else { $SearchMode = 'match'; } } else { $this->_SearchMode = $SearchMode; } if ($ForceDatabaseEngine = c('Database.ForceStorageEngine')) { if (strcasecmp($ForceDatabaseEngine, 'myisam') != 0) { $SearchMode = 'like'; } } if (strlen($Search) <= 4) { $SearchMode = 'like'; } $this->_SearchMode = $SearchMode; $this->EventArguments['Search'] = $Search; $this->fireEvent('Search'); if (count($this->_SearchSql) == 0) { return array(); } // Perform the search by unioning all of the sql together. $Sql = $this->SQL->select()->from('_TBL_ s')->orderBy('s.DateInserted', 'desc')->limit($Limit, $Offset)->getSelect(); $Sql = str_replace($this->Database->DatabasePrefix . '_TBL_', "(\n" . implode("\nunion all\n", $this->_SearchSql) . "\n)", $Sql); $this->fireEvent('AfterBuildSearchQuery'); if ($this->_SearchMode == 'like') { $Search = '%' . $Search . '%'; } foreach ($this->_Parameters as $Key => $Value) { $this->_Parameters[$Key] = $Search; } $Parameters = $this->_Parameters; $this->reset(); $this->SQL->reset(); $Result = $this->Database->query($Sql, $Parameters)->resultArray(); foreach ($Result as $Key => $Value) { if (isset($Value['Summary'])) { $Value['Summary'] = condense(Gdn_Format::to($Value['Summary'], $Value['Format'])); $Result[$Key] = $Value; } switch ($Value['RecordType']) { case 'Discussion': $Discussion = arrayTranslate($Value, array('PrimaryID' => 'DiscussionID', 'Title' => 'Name', 'CategoryID')); $Result[$Key]['Url'] = discussionUrl($Discussion, 1); break; } } return $Result; }
/** * 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('autosave.js'); $this->addJsFile('post.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; $CategoryModel = new CategoryModel(); if (isset($this->Discussion)) { $this->CategoryID = $this->Discussion->CategoryID; $Category = CategoryModel::categories($this->CategoryID); } elseif ($CategoryUrlCode != '') { $Category = CategoryModel::categories($CategoryUrlCode); if ($Category) { $this->CategoryID = val('CategoryID', $Category); } } if ($Category) { $this->Category = (object) $Category; $this->setData('Category', $Category); $this->ShowCategorySelector = false; $this->Form->addHidden('CategoryID', $this->Category->CategoryID); } else { $this->CategoryID = 0; $this->Category = null; } $CategoryData = $this->ShowCategorySelector ? CategoryModel::categories() : false; // Check permission if (isset($this->Discussion)) { // Make sure that content can (still) be edited. $CanEdit = DiscussionModel::canEdit($this->Discussion); if (!$CanEdit) { throw permissionException('Vanilla.Discussions.Edit'); } // 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. if ($this->Category) { $this->permission('Vanilla.Discussions.Add', true, 'Category', $this->Category->PermissionCategoryID); } else { $this->permission('Vanilla.Discussions.Add'); } $this->title(t('New Discussion')); } touchValue('Type', $this->Data, 'Discussion'); // See if we should hide the category dropdown. if ($this->ShowCategorySelector) { $AllowedCategories = CategoryModel::getByPermission('Discussions.Add', $this->Form->getValue('CategoryID', $this->CategoryID), ['Archived' => 0, 'AllowDiscussions' => 1], ['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()) { // 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); } } elseif ($this->Form->authenticatedPostBack()) { // Form was submitted // Save as a draft? $FormValues = $this->Form->formValues(); $FormValues = $this->DiscussionModel->filterForm($FormValues); $this->deliveryType(Gdn::request()->getValue('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 = val($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.'); // Clear out the form so that a draft won't save. $this->Form->formValues(array()); $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 = val('Body', $FormValues, ''); $this->Comment->Format = val('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()); } elseif ($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, 1)) . '?new=1'; } else { $this->RedirectUrl = discussionUrl($Discussion, 1, 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' => val('AddUrl', val($this->data('Type'), DiscussionModel::discussionTypes()), '/post/discussion')); $this->setData('Breadcrumbs', $Breadcrumbs); $this->setData('_AnnounceOptions', $this->announceOptions()); // Render view (posts/discussion.php or post/preview.php) $this->render(); }
/** * This method supports {@link CategoryModel::joinRecent()}. * * @param array &$categoryTree The array of categories in tree format. * @param array $discussions An array of discussions indexed by discussion ID. * @param array $comments An array of comments indexed by comment ID. */ private function joinRecentInternal(&$categoryTree, $discussions, $comments) { foreach ($categoryTree as &$category) { $discussion = val($category['LastDiscussionID'], $discussions, null); $comment = val($category['LastCommentID'], $comments, null); if (!empty($discussion)) { $category['LastTitle'] = $discussion['Name']; $category['LastUrl'] = discussionUrl($discussion, false, '/') . '#latest'; $category['LastDiscussionUserID'] = $discussion['InsertUserID']; } if (!empty($comment)) { $category['LastUserID'] = $comment['InsertUserID']; } elseif (!empty($discussion)) { $category['LastUserID'] = $discussion['InsertUserID']; } else { $category['LastTitle'] = ''; $category['LastUserID'] = null; } $user = Gdn::userModel()->getID($category['LastUserID']); foreach (['Name', 'Email', 'Photo'] as $field) { $category['Last' . $field] = val($field, $user); } if (!empty($category['Children'])) { $this->joinRecentInternal($category['Children'], $discussions, $comments); } } }