CommentModel::updateCommentCount PHP Method

updateCommentCount() public method

Events: BeforeUpdateCommentCount.
Since: 2.0.0
Since: 2.3 Added the $Options parameter.
public updateCommentCount ( $Discussion, array $Options = [] )
$Options array
    public function updateCommentCount($Discussion, $Options = array())
    {
        // Get the discussion.
        if (is_numeric($Discussion)) {
            $this->Options($Options);
            $Discussion = $this->SQL->getWhere('Discussion', array('DiscussionID' => $Discussion))->firstRow(DATASET_TYPE_ARRAY);
        }
        $DiscussionID = $Discussion['DiscussionID'];
        $this->fireEvent('BeforeUpdateCommentCountQuery');
        $this->Options($Options);
        $Data = $this->SQL->select('c.CommentID', 'min', 'FirstCommentID')->select('c.CommentID', 'max', 'LastCommentID')->select('c.DateInserted', 'max', 'DateLastComment')->select('c.CommentID', 'count', 'CountComments')->from('Comment c')->where('c.DiscussionID', $DiscussionID)->get()->firstRow(DATASET_TYPE_ARRAY);
        $this->EventArguments['Discussion'] =& $Discussion;
        $this->EventArguments['Counts'] =& $Data;
        $this->fireEvent('BeforeUpdateCommentCount');
        if ($Discussion) {
            if ($Data) {
                $this->SQL->update('Discussion');
                if (!$Discussion['Sink'] && $Data['DateLastComment']) {
                    $this->SQL->set('DateLastComment', $Data['DateLastComment']);
                } elseif (!$Data['DateLastComment']) {
                    $this->SQL->set('DateLastComment', $Discussion['DateInserted']);
                }
                $this->SQL->set('FirstCommentID', $Data['FirstCommentID'])->set('LastCommentID', $Data['LastCommentID'])->set('CountComments', $Data['CountComments'])->where('DiscussionID', $DiscussionID)->put();
                // Update the last comment's user ID.
                $this->SQL->update('Discussion d')->update('Comment c')->set('d.LastCommentUserID', 'c.InsertUserID', false)->where('d.DiscussionID', $DiscussionID)->where('c.CommentID', 'd.LastCommentID', false, false)->put();
            } else {
                // Update the discussion with null counts.
                $this->SQL->update('Discussion')->set('CountComments', 0)->set('FirstCommentID', null)->set('LastCommentID', null)->set('DateLastComment', 'DateInserted', false, false)->set('LastCommentUserID', null)->where('DiscussionID', $DiscussionID);
            }
        }
    }

Usage Example

コード例 #1
0
 /**
  * Handle flagging process in a discussion.
  */
 public function discussionController_flag_create($Sender)
 {
     // Signed in users only.
     if (!($UserID = Gdn::session()->UserID)) {
         return;
     }
     $UserName = Gdn::session()->User->Name;
     $Arguments = $Sender->RequestArgs;
     if (sizeof($Arguments) != 5) {
         return;
     }
     list($Context, $ElementID, $ElementAuthorID, $ElementAuthor, $EncodedURL) = $Arguments;
     $URL = htmlspecialchars(base64_decode(str_replace('-', '=', $EncodedURL)));
     $Sender->setData('Plugin.Flagging.Data', array('Context' => $Context, 'ElementID' => $ElementID, 'ElementAuthorID' => $ElementAuthorID, 'ElementAuthor' => $ElementAuthor, 'URL' => $URL, 'UserID' => $UserID, 'UserName' => $UserName));
     if ($Sender->Form->authenticatedPostBack()) {
         $SQL = Gdn::sql();
         $Comment = $Sender->Form->getValue('Plugin.Flagging.Reason');
         $Sender->setData('Plugin.Flagging.Reason', $Comment);
         $CreateDiscussion = c('Plugins.Flagging.UseDiscussions');
         if ($CreateDiscussion) {
             // Category
             $CategoryID = c('Plugins.Flagging.CategoryID');
             // New discussion name
             if ($Context == 'comment') {
                 $Result = $SQL->select('d.Name')->select('c.Body')->from('Comment c')->join('Discussion d', 'd.DiscussionID = c.DiscussionID', 'left')->where('c.CommentID', $ElementID)->get()->firstRow();
             } elseif ($Context == 'discussion') {
                 $DiscussionModel = new DiscussionModel();
                 $Result = $DiscussionModel->getID($ElementID);
             }
             $DiscussionName = val('Name', $Result);
             $PrefixedDiscussionName = t('FlagPrefix', 'FLAG: ') . $DiscussionName;
             // Prep data for the template
             $Sender->setData('Plugin.Flagging.Report', array('DiscussionName' => $DiscussionName, 'FlaggedContent' => val('Body', $Result)));
             // Assume no discussion exists
             $this->DiscussionID = null;
             // Get discussion ID if already flagged
             $FlagResult = Gdn::sql()->select('DiscussionID')->from('Flag fl')->where('ForeignType', $Context)->where('ForeignID', $ElementID)->get()->firstRow();
             if ($FlagResult) {
                 // New comment in existing discussion
                 $DiscussionID = $FlagResult->DiscussionID;
                 $ReportBody = $Sender->fetchView($this->getView('reportcomment.php'));
                 $SQL->insert('Comment', array('DiscussionID' => $DiscussionID, 'InsertUserID' => $UserID, 'Body' => $ReportBody, 'Format' => 'Html', 'DateInserted' => date('Y-m-d H:i:s')));
                 $CommentModel = new CommentModel();
                 $CommentModel->updateCommentCount($DiscussionID);
             } else {
                 // New discussion body
                 $ReportBody = $Sender->fetchView($this->getView('report.php'));
                 $DiscussionID = $SQL->insert('Discussion', array('InsertUserID' => $UserID, 'UpdateUserID' => $UserID, 'CategoryID' => $CategoryID, 'Name' => $PrefixedDiscussionName, 'Body' => $ReportBody, 'Format' => 'Html', 'CountComments' => 1, 'DateInserted' => date('Y-m-d H:i:s'), 'DateUpdated' => date('Y-m-d H:i:s'), 'DateLastComment' => date('Y-m-d H:i:s')));
                 // Update discussion count
                 $DiscussionModel = new DiscussionModel();
                 $DiscussionModel->updateDiscussionCount($CategoryID);
             }
         }
         try {
             // Insert the flag
             $SQL->insert('Flag', array('DiscussionID' => $DiscussionID, 'InsertUserID' => $UserID, 'InsertName' => $UserName, 'AuthorID' => $ElementAuthorID, 'AuthorName' => $ElementAuthor, 'ForeignURL' => $URL, 'ForeignID' => $ElementID, 'ForeignType' => $Context, 'Comment' => $Comment, 'DateInserted' => date('Y-m-d H:i:s')));
         } catch (Exception $e) {
         }
         // Notify users with permission who've chosen to be notified
         if (!$FlagResult) {
             // Only send if this is first time it's being flagged.
             $Sender->setData('Plugin.Flagging.DiscussionID', $DiscussionID);
             $Subject = isset($PrefixedDiscussionName) ? $PrefixedDiscussionName : t('FlagDiscussion', 'A discussion was flagged');
             $EmailBody = $Sender->fetchView($this->getView('reportemail.php'));
             $NotifyUsers = c('Plugins.Flagging.NotifyUsers', array());
             // Send emails
             $UserModel = new UserModel();
             foreach ($NotifyUsers as $UserID) {
                 $User = $UserModel->getID($UserID);
                 $Email = new Gdn_Email();
                 $Email->to($User->Email)->subject(sprintf(t('[%1$s] %2$s'), Gdn::config('Garden.Title'), $Subject))->message($EmailBody);
                 try {
                     $Email->send();
                 } catch (Exception $e) {
                     if (debug()) {
                         throw $e;
                     }
                 }
             }
         }
         $Sender->informMessage(t('FlagSent', "Your complaint has been registered."));
     }
     $Sender->render($this->getView('flag.php'));
 }
All Usage Examples Of CommentModel::updateCommentCount