예제 #1
0
 public function Save($FormPostValues)
 {
     $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');
     $MaxCommentLength = Gdn::Config('Vanilla.Comment.MaxLength');
     if (is_numeric($MaxCommentLength) && $MaxCommentLength > 0) {
         $this->Validation->SetSchemaProperty('Body', 'Length', $MaxCommentLength);
         $this->Validation->ApplyRule('Body', 'Length');
     }
     $CommentID = ArrayValue('CommentID', $FormPostValues);
     $CommentID = is_numeric($CommentID) && $CommentID > 0 ? $CommentID : FALSE;
     $Insert = $CommentID === FALSE;
     if ($Insert) {
         $this->AddInsertFields($FormPostValues);
     } else {
         $this->AddUpdateFields($FormPostValues);
     }
     // 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();
             $Fields = RemoveKeyFromArray($Fields, $this->PrimaryKey);
             $DiscussionModel = new Gdn_DiscussionModel();
             $DiscussionID = ArrayValue('DiscussionID', $Fields);
             $Discussion = $DiscussionModel->GetID($DiscussionID);
             if ($Insert === FALSE) {
                 $this->SQL->Put($this->Name, $Fields, array('CommentID' => $CommentID));
             } else {
                 // Make sure that the comments get formatted in the method defined by Garden
                 $Fields['Format'] = Gdn::Config('Garden.InputFormatter', '');
                 $CommentID = $this->SQL->Insert($this->Name, $Fields);
                 $this->EventArguments['CommentID'] = $CommentID;
                 // IsNewDiscussion is passed when the first comment for new discussions are created.
                 $this->EventArguments['IsNewDiscussion'] = ArrayValue('IsNewDiscussion', $FormPostValues);
                 $this->FireEvent('AfterSaveComment');
                 // Notify any users who were mentioned in the comment
                 $Usernames = GetMentions($Fields['Body']);
                 $UserModel = Gdn::UserModel();
                 $Story = ArrayValue('Body', $Fields, '');
                 $NotifiedUsers = array();
                 foreach ($Usernames as $Username) {
                     $User = $UserModel->GetWhere(array('Name' => $Username))->FirstRow();
                     if ($User && $User->UserID != $Session->UserID) {
                         $NotifiedUsers[] = $User->UserID;
                         $ActivityModel = new Gdn_ActivityModel();
                         $ActivityID = $ActivityModel->Add($Session->UserID, 'CommentMention', Anchor(Format::Text($Discussion->Name), 'discussion/comment/' . $CommentID . '/#Comment_' . $CommentID), $User->UserID, '', 'discussion/comment/' . $CommentID . '/#Comment_' . $CommentID, FALSE);
                         $ActivityModel->SendNotification($ActivityID, $Story);
                     }
                 }
                 // Notify users who have bookmarked the discussion
                 $BookmarkData = $DiscussionModel->GetBookmarkUsers($DiscussionID);
                 foreach ($BookmarkData->Result() as $Bookmark) {
                     if (!in_array($Bookmark->UserID, $NotifiedUsers) && $Bookmark->UserID != $Session->UserID) {
                         $NotifiedUsers[] = $Bookmark->UserID;
                         $ActivityModel = new Gdn_ActivityModel();
                         $ActivityID = $ActivityModel->Add($Session->UserID, 'BookmarkComment', Anchor(Format::Text($Discussion->Name), 'discussion/comment/' . $CommentID . '/#Comment_' . $CommentID), $Bookmark->UserID, '', 'discussion/comment/' . $CommentID . '/#Comment_' . $CommentID, FALSE);
                         $ActivityModel->SendNotification($ActivityID, $Story);
                     }
                 }
             }
             // Record user-comment activity
             if ($Insert === TRUE && $Discussion !== FALSE && !in_array($Session->UserID, $NotifiedUsers)) {
                 $this->RecordActivity($Discussion, $Session->UserID, $CommentID);
             }
             $this->UpdateCommentCount($DiscussionID);
             // Update the discussion author's CountUnreadDiscussions (ie.
             // the number of discussions created by the user that s/he has
             // unread messages in) if this comment was not added by the
             // discussion author.
             $Data = $this->SQL->Select('d.InsertUserID')->Select('d.DiscussionID', 'count', 'CountDiscussions')->From('Discussion d')->Join('Comment c', 'd.DiscussionID = c.DiscussionID')->Join('UserDiscussion w', 'd.DiscussionID = w.DiscussionID and w.UserID = d.InsertUserID')->Where('w.CountComments >', 0)->Where('c.InsertUserID', $Session->UserID)->Where('c.InsertUserID <>', 'd.InsertUserID', TRUE, FALSE)->GroupBy('d.InsertUserID')->Get();
             if ($Data->NumRows() > 0) {
                 $UserData = $Data->FirstRow();
                 $this->SQL->Update('User')->Set('CountUnreadDiscussions', $UserData->CountDiscussions)->Where('UserID', $UserData->InsertUserID)->Put();
             }
             $this->UpdateUser($Session->UserID);
         }
     }
     return $CommentID;
 }
예제 #2
0
 public function Save($FormPostValues)
 {
     $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');
     $MaxCommentLength = Gdn::Config('Vanilla.Comment.MaxLength');
     if (is_numeric($MaxCommentLength) && $MaxCommentLength > 0) {
         $this->Validation->SetSchemaProperty('Body', 'Length', $MaxCommentLength);
         $this->Validation->ApplyRule('Body', 'Length');
     }
     $CommentID = ArrayValue('CommentID', $FormPostValues);
     $CommentID = is_numeric($CommentID) && $CommentID > 0 ? $CommentID : FALSE;
     $Insert = $CommentID === FALSE;
     if ($Insert) {
         $this->AddInsertFields($FormPostValues);
     } else {
         $this->AddUpdateFields($FormPostValues);
     }
     // 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();
             $Fields = RemoveKeyFromArray($Fields, $this->PrimaryKey);
             $DiscussionModel = new Gdn_DiscussionModel();
             $DiscussionID = ArrayValue('DiscussionID', $Fields);
             $Discussion = $DiscussionModel->GetID($DiscussionID);
             $DiscussionAuthorMentioned = FALSE;
             if ($Insert === FALSE) {
                 $this->SQL->Put($this->Name, $Fields, array('CommentID' => $CommentID));
             } else {
                 // Make sure that the comments get formatted in the method defined by Garden
                 $Fields['Format'] = Gdn::Config('Garden.InputFormatter', '');
                 $CommentID = $this->SQL->Insert($this->Name, $Fields);
                 $this->EventArguments['CommentID'] = $CommentID;
                 $this->FireEvent('SaveCommentAfter');
                 // Notify any users who were mentioned in the comment
                 $Usernames = GetMentions($Fields['Body']);
                 $UserModel = Gdn::UserModel();
                 $DiscussionName = '';
                 foreach ($Usernames as $Username) {
                     $User = $UserModel->GetWhere(array('Name' => $Username))->FirstRow();
                     if ($User && $User->UserID != $Session->UserID) {
                         if ($User->UserID == $Discussion->InsertUserID) {
                             $DiscussionAuthorMentioned = TRUE;
                         }
                         AddActivity($Session->UserID, 'CommentMention', Anchor(Format::Text($Discussion->Name), 'discussion/comment/' . $CommentID . '/#Comment_' . $CommentID), $User->UserID, 'discussion/comment/' . $CommentID . '/#Comment_' . $CommentID);
                     }
                 }
             }
             // Record user-comment activity
             if ($Insert === TRUE && $Discussion !== FALSE && $DiscussionAuthorMentioned === FALSE) {
                 $this->RecordActivity($Discussion, $Session->UserID, $CommentID);
             }
             // Only record activity if inserting a comment, not on edit.
             $this->UpdateCommentCount($DiscussionID);
             // Update the discussion author's CountUnreadDiscussions (ie.
             // the number of discussions created by the user that s/he has
             // unread messages in) if this comment was not added by the
             // discussion author.
             $Data = $this->SQL->Select('d.InsertUserID')->Select('d.DiscussionID', 'count', 'CountDiscussions')->From('Discussion d')->Join('Comment c', 'd.DiscussionID = c.DiscussionID')->Join('UserDiscussion w', 'd.DiscussionID = w.DiscussionID and w.UserID = d.InsertUserID')->Where('w.CountComments >', 0)->Where('c.InsertUserID', $Session->UserID)->Where('c.InsertUserID <>', 'd.InsertUserID', TRUE, FALSE)->GroupBy('d.InsertUserID')->Get();
             if ($Data->NumRows() > 0) {
                 $UserData = $Data->FirstRow();
                 $this->SQL->Update('User')->Set('CountUnreadDiscussions', $UserData->CountDiscussions)->Where('UserID', $UserData->InsertUserID)->Put();
             }
             // Index the post.
             $Search = Gdn::Factory('SearchModel');
             if (!is_null($Search)) {
                 if (array_key_exists('Name', $FormPostValues) && array_key_exists('CategoryID', $FormPostValues)) {
                     $Title = $FormPostValues['Name'];
                     $CategoryID = $FormPostValues['CategoryID'];
                 } else {
                     // Get the name from the discussion.
                     $Row = $this->SQL->GetWhere('Discussion', array('DiscussionID' => $DiscussionID))->FirstRow();
                     if (is_object($Row)) {
                         $Title = $Row->Name;
                         $CategoryID = $Row->CategoryID;
                     }
                 }
                 $Offset = $this->GetOffset($CommentID);
                 // Index the discussion.
                 $Document = array('TableName' => 'Comment', 'PrimaryID' => $CommentID, 'PermissionJunctionID' => $CategoryID, 'Title' => $Title, 'Summary' => $FormPostValues['Body'], 'Url' => '/discussion/comment/' . $CommentID . '/#Comment_' . $CommentID, 'InsertUserID' => $Session->UserID);
                 $Search->Index($Document, $Offset == 1 ? $Document['Title'] . ' ' . $Document['Summary'] : NULL);
             }
             $this->UpdateUser($Session->UserID);
         }
     }
     return $CommentID;
 }
예제 #3
0
 public function RecordActivity($DiscussionID, $ActivityUserID, $CommentID)
 {
     // Get the author of the discussion
     $DiscussionModel = new Gdn_DiscussionModel();
     $Discussion = $DiscussionModel->GetID($DiscussionID);
     if ($Discussion->InsertUserID != $ActivityUserID) {
         AddActivity($ActivityUserID, 'DiscussionComment', '', $Discussion->InsertUserID, 'discussion/comment/' . $CommentID . '/#Comment_' . $CommentID);
     }
 }