public function save($FormPostValues, $Settings = false)
{
$Session = Gdn::session();
// Define the primary key in this model's table.
$this->defineSchema();
// Add & apply any extra validation rules:
$this->Validation->applyRule('Body', 'Required');
$this->Validation->addRule('MeAction', 'function:ValidateMeAction');
$this->Validation->applyRule('Body', 'MeAction');
$MaxCommentLength = Gdn::config('Vanilla.Comment.MaxLength');
if (is_numeric($MaxCommentLength) && $MaxCommentLength > 0) {
$this->Validation->SetSchemaProperty('Body', 'Length', $MaxCommentLength);
$this->Validation->applyRule('Body', 'Length');
}
$MinCommentLength = c('Vanilla.Comment.MinLength');
if ($MinCommentLength && is_numeric($MinCommentLength)) {
$this->Validation->SetSchemaProperty('Body', 'MinLength', $MinCommentLength);
$this->Validation->addRule('MinTextLength', 'function:ValidateMinTextLength');
$this->Validation->applyRule('Body', 'MinTextLength');
}
// Validate $CommentID and whether this is an insert
$CommentID = val('CommentID', $FormPostValues);
$CommentID = is_numeric($CommentID) && $CommentID > 0 ? $CommentID : false;
$Insert = $CommentID === false;
if ($Insert) {
$this->AddInsertFields($FormPostValues);
} else {
$this->AddUpdateFields($FormPostValues);
}
// Prep and fire event
$this->EventArguments['FormPostValues'] =& $FormPostValues;
$this->EventArguments['CommentID'] = $CommentID;
$this->fireEvent('BeforeSaveComment');
// Validate the form posted values
if ($this->validate($FormPostValues, $Insert)) {
// If the post is new and it validates, check for spam
if (!$Insert || !$this->CheckForSpam('Comment')) {
$Fields = $this->Validation->SchemaValidationFields();
unset($Fields[$this->PrimaryKey]);
$CommentData = $CommentID ? array_merge($Fields, ['CommentID' => $CommentID]) : $Fields;
// Check for spam
$spam = SpamModel::isSpam('Comment', $CommentData);
if ($spam) {
return SPAM;
}
$isValid = true;
$invalidReturnType = false;
$this->EventArguments['CommentData'] = $CommentData;
$this->EventArguments['IsValid'] =& $isValid;
$this->EventArguments['InvalidReturnType'] =& $invalidReturnType;
$this->fireEvent('AfterValidateComment');
if (!$isValid) {
return $invalidReturnType;
}
if ($Insert === false) {
// Log the save.
LogModel::LogChange('Edit', 'Comment', array_merge($Fields, array('CommentID' => $CommentID)));
// Save the new value.
$this->SerializeRow($Fields);
$this->SQL->put($this->Name, $Fields, array('CommentID' => $CommentID));
} else {
// Make sure that the comments get formatted in the method defined by Garden.
if (!val('Format', $Fields) || c('Garden.ForceInputFormatter')) {
$Fields['Format'] = Gdn::config('Garden.InputFormatter', '');
}
// Check for approval
$ApprovalRequired = CheckRestriction('Vanilla.Approval.Require');
if ($ApprovalRequired && !val('Verified', Gdn::session()->User)) {
$DiscussionModel = new DiscussionModel();
$Discussion = $DiscussionModel->getID(val('DiscussionID', $Fields));
$Fields['CategoryID'] = val('CategoryID', $Discussion);
LogModel::insert('Pending', 'Comment', $Fields);
return UNAPPROVED;
}
// Create comment.
$this->SerializeRow($Fields);
$CommentID = $this->SQL->insert($this->Name, $Fields);
}
if ($CommentID) {
$this->EventArguments['CommentID'] = $CommentID;
$this->EventArguments['Insert'] = $Insert;
// IsNewDiscussion is passed when the first comment for new discussions are created.
$this->EventArguments['IsNewDiscussion'] = val('IsNewDiscussion', $FormPostValues);
$this->fireEvent('AfterSaveComment');
}
}
}
// Update discussion's comment count
$DiscussionID = val('DiscussionID', $FormPostValues);
$this->UpdateCommentCount($DiscussionID, array('Slave' => false));
return $CommentID;
}