Get single comment by ID. Allows you to pick data format of return value.
public getID ( integer $CommentID, string $ResultType = DATASET_TYPE_OBJECT, array $Options = [] ) : mixed | ||
$CommentID | integer | Unique ID of the comment. |
$ResultType | string | Format to return comment in. |
$Options | array | options to pass to the database. |
Результат | mixed | SQL result in format specified by $ResultType. |
/** * Allows user to delete a comment. * * If the comment is the only one in the discussion, the discussion will * be deleted as well. Users without administrative delete abilities * should not be able to delete a comment unless it is a draft. This is * a "hard" delete - it is removed from the database. * * @since 2.0.0 * @access public * * @param int $CommentID Unique comment ID. * @param string $TransientKey Single-use hash to prove intent. */ public function deleteComment($CommentID = '', $TransientKey = '') { $Session = Gdn::session(); $DefaultTarget = '/discussions/'; $ValidCommentID = is_numeric($CommentID) && $CommentID > 0; $ValidUser = $Session->UserID > 0 && $Session->validateTransientKey($TransientKey); if ($ValidCommentID && $ValidUser) { // Get comment and discussion data $Comment = $this->CommentModel->getID($CommentID); $DiscussionID = val('DiscussionID', $Comment); $Discussion = $this->DiscussionModel->getID($DiscussionID); if ($Comment && $Discussion) { $DefaultTarget = discussionUrl($Discussion); // Make sure comment is this user's or they have Delete permission if ($Comment->InsertUserID != $Session->UserID || !c('Vanilla.Comments.AllowSelfDelete')) { $this->permission('Vanilla.Comments.Delete', true, 'Category', $Discussion->PermissionCategoryID); } // Make sure that content can (still) be edited $EditContentTimeout = c('Garden.EditContentTimeout', -1); $CanEdit = $EditContentTimeout == -1 || strtotime($Comment->DateInserted) + $EditContentTimeout > time(); if (!$CanEdit) { $this->permission('Vanilla.Comments.Delete', true, 'Category', $Discussion->PermissionCategoryID); } // Delete the comment if (!$this->CommentModel->deleteID($CommentID)) { $this->Form->addError('Failed to delete comment'); } } else { $this->Form->addError('Invalid comment'); } } else { $this->Form->addError('ErrPermission'); } // Redirect if ($this->_DeliveryType == DELIVERY_TYPE_ALL) { $Target = GetIncomingValue('Target', $DefaultTarget); SafeRedirect($Target); } if ($this->Form->errorCount() > 0) { $this->setJson('ErrorMessage', $this->Form->errors()); } else { $this->jsonTarget("#Comment_{$CommentID}", '', 'SlideUp'); } $this->render(); }
/** * Get a record from the database. * * @param string $recordType The type of record to get. This is usually the un-prefixed table name of the record. * @param int $id The ID of the record. * @param bool $throw Whether or not to throw an exception if the record isn't found. * @return array|false Returns an array representation of the record or false if the record isn't found. * @throws Exception Throws an exception with a 404 code if the record isn't found and {@link $throw} is true. * @throws Gdn_UserException Throws an exception when {@link $recordType} is unknown. */ function getRecord($recordType, $id, $throw = false) { $Row = false; switch (strtolower($recordType)) { case 'discussion': $Model = new DiscussionModel(); $Row = $Model->getID($id); $Row->Url = DiscussionUrl($Row); $Row->ShareUrl = $Row->Url; if ($Row) { return (array) $Row; } break; case 'comment': $Model = new CommentModel(); $Row = $Model->getID($id, DATASET_TYPE_ARRAY); if ($Row) { $Row['Url'] = Url("/discussion/comment/{$id}#Comment_{$id}", true); $Model = new DiscussionModel(); $Discussion = $Model->getID($Row['DiscussionID']); if ($Discussion) { $Discussion->Url = DiscussionUrl($Discussion); $Row['ShareUrl'] = $Discussion->Url; $Row['Name'] = $Discussion->Name; $Row['Discussion'] = (array) $Discussion; } return $Row; } break; case 'activity': $Model = new ActivityModel(); $Row = $Model->getID($id, DATASET_TYPE_ARRAY); if ($Row) { $Row['Name'] = formatString($Row['HeadlineFormat'], $Row); $Row['Body'] = $Row['Story']; return $Row; } break; default: throw new Gdn_UserException('Unknown record type requested.'); } if ($throw) { throw NotFoundException(); } else { return false; } }
/** * Discussion view counter. * * @param $Sender * @param $Args */ public function gdn_statistics_tick_handler($Sender, $Args) { $Path = Gdn::request()->post('Path'); $Args = Gdn::request()->post('Args'); parse_str($Args, $Args); $ResolvedPath = trim(Gdn::request()->post('ResolvedPath'), '/'); $ResolvedArgs = Gdn::request()->post('ResolvedArgs'); $DiscussionID = null; $DiscussionModel = new DiscussionModel(); // Comment permalink if ($ResolvedPath == 'vanilla/discussion/comment') { $CommentID = val('CommentID', $ResolvedArgs); $CommentModel = new CommentModel(); $Comment = $CommentModel->getID($CommentID); $DiscussionID = val('DiscussionID', $Comment); } elseif ($ResolvedPath == 'vanilla/discussion/index') { $DiscussionID = val('DiscussionID', $ResolvedArgs, null); } elseif ($ResolvedPath == 'vanilla/discussion/embed') { $ForeignID = val('vanilla_identifier', $Args); if ($ForeignID) { // This will be hit a lot so let's try caching it... $Key = "DiscussionID.ForeignID.page.{$ForeignID}"; $DiscussionID = Gdn::cache()->get($Key); if (!$DiscussionID) { $Discussion = $DiscussionModel->getForeignID($ForeignID, 'page'); $DiscussionID = val('DiscussionID', $Discussion); Gdn::cache()->store($Key, $DiscussionID, array(Gdn_Cache::FEATURE_EXPIRY, 1800)); } } } if ($DiscussionID) { $DiscussionModel->addView($DiscussionID); } }
/** * Insert a SPAM Queue entry for the specified record and delete the record, if possible. * * @param string $recordType The type of record we're flagging: Discussion or Comment. * @param int $id ID of the record we're flagging. * @param object|array $data Properties used for updating/overriding the record's current values. * * @throws Exception If an invalid record type is specified, throw an exception. */ protected static function flagForReview($recordType, $id, $data) { // We're planning to purge the spammy record. $deleteRow = true; /** * We're only handling two types of content: discussions and comments. Both need some special setup. * Error out if we're not dealing with a discussion or comment. */ switch ($recordType) { case 'Comment': $model = new CommentModel(); $row = $model->getID($id, DATASET_TYPE_ARRAY); break; case 'Discussion': $model = new DiscussionModel(); $row = $model->getID($id, DATASET_TYPE_ARRAY); /** * If our discussion has more than three comments, it might be worth saving. Hold off on deleting and * just flag it. If we have between 0 and 3 comments, save them along with the discussion. */ if ($row['CountComments'] > 3) { $deleteRow = false; } elseif ($row['CountComments'] > 0) { $comments = Gdn::database()->sql()->getWhere('Comment', array('DiscussionID' => $id))->resultArray(); if (!array_key_exists('_Data', $row)) { $row['_Data'] = array(); } $row['_Data']['Comment'] = $comments; } break; default: throw notFoundException($recordType); } $overrideFields = array('Name', 'Body'); foreach ($overrideFields as $fieldName) { if (($fieldValue = val($fieldName, $data, false)) !== false) { $row[$fieldName] = $fieldValue; } } $logOptions = array('GroupBy' => array('RecordID')); if ($deleteRow) { // Remove the record to the log. $model->deleteID($id); } LogModel::insert('Spam', $recordType, $row, $logOptions); }