ActivityModel::comment PHP Method

comment() public method

Save a comment on an activity.
Since: 2.1
public comment ( array $Comment ) : integer | boolean | string
$Comment array
return integer | boolean | string
    public function comment($Comment)
    {
        $Comment['InsertUserID'] = Gdn::session()->UserID;
        $Comment['DateInserted'] = Gdn_Format::toDateTime();
        $Comment['InsertIPAddress'] = ipEncode(Gdn::request()->ipAddress());
        $this->Validation->applyRule('ActivityID', 'Required');
        $this->Validation->applyRule('Body', 'Required');
        $this->Validation->applyRule('DateInserted', 'Required');
        $this->Validation->applyRule('InsertUserID', 'Required');
        $this->EventArguments['Comment'] =& $Comment;
        $this->fireEvent('BeforeSaveComment');
        if ($this->validate($Comment)) {
            $Activity = $this->getID($Comment['ActivityID'], DATASET_TYPE_ARRAY);
            Gdn::controller()->json('Activity', $Activity);
            $_ActivityID = $Comment['ActivityID'];
            // Check to see if this is a shared activity/notification.
            if ($CommentActivityID = val('CommentActivityID', $Activity['Data'])) {
                Gdn::controller()->json('CommentActivityID', $CommentActivityID);
                $Comment['ActivityID'] = $CommentActivityID;
            }
            // Check for spam.
            $Spam = SpamModel::isSpam('ActivityComment', $Comment);
            if ($Spam) {
                return SPAM;
            }
            // Check for approval
            $ApprovalRequired = checkRestriction('Vanilla.Approval.Require');
            if ($ApprovalRequired && !val('Verified', Gdn::session()->User)) {
                LogModel::insert('Pending', 'ActivityComment', $Comment);
                return UNAPPROVED;
            }
            $ID = $this->SQL->insert('ActivityComment', $Comment);
            if ($ID) {
                // Check to see if this comment bumps the activity.
                if ($Activity && val('Bump', $Activity['Data'])) {
                    $this->SQL->put('Activity', ['DateUpdated' => $Comment['DateInserted']], ['ActivityID' => $Activity['ActivityID']]);
                    if ($_ActivityID != $Comment['ActivityID']) {
                        $this->SQL->put('Activity', ['DateUpdated' => $Comment['DateInserted']], ['ActivityID' => $_ActivityID]);
                    }
                }
                // Send a notification to the original person.
                if (val('ActivityType', $Activity) === 'WallPost') {
                    $this->notifyWallComment($Comment, $Activity);
                }
            }
            return $ID;
        }
        return false;
    }

Usage Example

 /**
  * Comment on an activity item.
  *
  * @since 2.0.0
  * @access public
  */
 public function comment()
 {
     $this->permission('Garden.Profiles.Edit');
     $Session = Gdn::session();
     $this->Form->setModel($this->ActivityModel);
     $NewActivityID = 0;
     // Form submitted
     if ($this->Form->authenticatedPostBack()) {
         $Body = $this->Form->getValue('Body', '');
         $ActivityID = $this->Form->getValue('ActivityID', '');
         if (is_numeric($ActivityID) && $ActivityID > 0) {
             $activity = $this->ActivityModel->getID($ActivityID);
             if ($activity) {
                 if ($activity['NotifyUserID'] == ActivityModel::NOTIFY_ADMINS) {
                     $this->permission('Garden.Settings.Manage');
                 } elseif ($activity['NotifyUserID'] == ActivityModel::NOTIFY_MODS) {
                     $this->permission('Garden.Moderation.Manage');
                 }
             } else {
                 throw new Exception(t('Invalid activity'));
             }
             $ActivityComment = array('ActivityID' => $ActivityID, 'Body' => $Body, 'Format' => 'Text');
             $ID = $this->ActivityModel->comment($ActivityComment);
             if ($ID == SPAM || $ID == UNAPPROVED) {
                 $this->StatusMessage = t('ActivityCommentRequiresApproval', 'Your comment will appear after it is approved.');
                 $this->render('Blank', 'Utility');
                 return;
             }
             $this->Form->setValidationResults($this->ActivityModel->validationResults());
             if ($this->Form->errorCount() > 0) {
                 throw new Exception($this->ActivityModel->Validation->resultsText());
                 $this->errorMessage($this->Form->errors());
             }
         }
     }
     // Redirect back to the sending location if this isn't an ajax request
     if ($this->_DeliveryType === DELIVERY_TYPE_ALL) {
         $Target = $this->Form->getValue('Return');
         if (!$Target) {
             $Target = '/activity';
         }
         redirect($Target);
     } else {
         // Load the newly added comment.
         $this->setData('Comment', $this->ActivityModel->getComment($ID));
         // Set it in the appropriate view.
         $this->View = 'comment';
     }
     // And render
     $this->render();
 }