Esempio n. 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;
 }