Another way for most recent comment

This discussion is related to the DiscussionExcerpt2 addon.
edited April 2012 in Feedback

Hello dear friends. Here is another way to get the plugin to display most recent comment, with minimal code changes:

    // get last comment body
    $LastCommentBody = Gdn::SQL()->Select('Body')->From('Comment')->Where('CommentID', $Sender->EventArguments['Discussion']->LastCommentID)->Get()->ResultArray();         

    if (strlen($LastCommentBody[0]["Body"]) > 0)
    {
        // only assign if there's something to be assigned (otherwise, it is fine with original $FirstComment)
        $FirstComment = $LastCommentBody[0]["Body"];
    }

The code is inserted in default.php in DiscussionsController_AfterDiscussionTitle_Handler(&$Sender)

Original function:

    public function DiscussionsController_AfterDiscussionTitle_Handler(&$Sender) {
   $Number_of_words = Gdn::Config('DiscussionExcerpt2.Number_of_words', 15);
   $Show_announcements = Gdn::Config('DiscussionExcerpt2.Show_announcements', false);
   $Show_images = Gdn::Config('DiscussionExcerpt2.Show_images', false);

   $Discussion = $Sender->EventArguments['Discussion'];
   if($Show_images) {
       $FirstComment = strip_tags($Discussion->Body, '<img>');
   } else {
       $FirstComment = strip_tags($Discussion->Body);
   }
       $Announce = $Discussion->Announce;

   $words = explode(' ', $FirstComment);
       if ( ($Announce != 1 ) || ( $Announce && $Show_announcements )) {
         if (count($words) > $Number_of_words) {
         echo '<div class="DiscussionExcerpt2">' . implode(' ', array_slice($words, 0, $Number_of_words)) . ' ...</div>';
     } else {
             echo $FirstComment;
     }
   }
   return;
}

Modified function:

    public function DiscussionsController_AfterDiscussionTitle_Handler(&$Sender) {
   $Number_of_words = Gdn::Config('DiscussionExcerpt2.Number_of_words', 15);
   $Show_announcements = Gdn::Config('DiscussionExcerpt2.Show_announcements', false);
   $Show_images = Gdn::Config('DiscussionExcerpt2.Show_images', false);

   $Discussion = $Sender->EventArguments['Discussion'];
   if($Show_images) {
       $FirstComment = strip_tags($Discussion->Body, '<img>');
   } else {
       $FirstComment = strip_tags($Discussion->Body);
   }
       $Announce = $Discussion->Announce;

    // get last comment body
    $LastCommentBody = Gdn::SQL()->Select('Body')->From('Comment')->Where('CommentID', $Sender->EventArguments['Discussion']->LastCommentID)->Get()->ResultArray();         

    if (strlen($LastCommentBody[0]["Body"]) > 0)
    {
        // only assign if there's something to be assigned (otherwise, it is fine with original $FirstComment)
        $FirstComment = $LastCommentBody[0]["Body"];
    }

   $words = explode(' ', $FirstComment);
       if ( ($Announce != 1 ) || ( $Announce && $Show_announcements )) {
         if (count($words) > $Number_of_words) {
         echo '<div class="DiscussionExcerpt2">' . implode(' ', array_slice($words, 0, $Number_of_words)) . ' ...</div>';
     } else {
             echo $FirstComment;
     }
   }
   return;
}

Opinions/comments welcome.

Victor

Tagged:
Sign In or Register to comment.