Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Adding comment ID to comment meta class

hbfhbf MVP
edited December 2011 in Vanilla 2.0 - 2.8

i'd like a plugin that adds the comment id number to the comment meta class. if anyone has already done this i'd like to avoid re-inventing the wheel, otherwise any help getting started writing my own would be appreciated.

Best Answer

  • ToddTodd Vanilla Staff
    Answer ✓

    I think you want to start with $Count = $Sender->CurrentOffset and then just increment it as you go through the comments.

Answers

  • hbfhbf MVP
    edited December 2011

    ok so i got a little ways but now am totally stuck. I used this code (hack of the quotes plugin) to display the comment ID on the comment. problem is, that number has nothing to do with the number of comments in the thread, it's the database entry id. so the 2nd comment on a thread started today on my forum could be comment # 28000. when im looking for 2. so can i modify the database queries (simply) to get the right number?

       public function DiscussionController_CommentOptions_Handler($Sender) {
          $this->AddPostNum($Sender);
       }
       
       public function PostController_CommentOptions_Handler($Sender) {
          $this->AddPostNum($Sender);
       }
       
       protected function AddPostNum($Sender) {
          if (!Gdn::Session()->UserID) return;
          
          $Object = !isset($Sender->EventArguments['Comment']) ? $Sender->Data['Discussion'] : $Sender->EventArguments['Comment'];
          $ObjectID = !isset($Sender->EventArguments['Comment']) ? 'Discussion_'.$Sender->Data['Discussion']->DiscussionID : 'Comment_'.$Sender->EventArguments['Comment']->CommentID;
          $postID = str_replace('Comment_','',$ObjectID);
          echo <<<POSTNUM
          <span class="PostNum">$postID
    POSTNUM;
    }
    
  • so, i think i need the item number for the discussion instead of comment ID, but im sure thats a calculated value.

    can anyone tell me how to extract that for each comment?

  • ToddTodd Vanilla Staff
    Answer ✓

    I think you want to start with $Count = $Sender->CurrentOffset and then just increment it as you go through the comments.

  • Todd said:
    I think you want to start with $Count = $Sender->CurrentOffset and then just increment it as you go through the comments.

    i quick test did not yield what i was hoping for

       protected function AddPostNum($Sender) {
          if (!Gdn::Session()->UserID) return;
          
          $Object = $Sender->Data['Discussion'];
          $Total = $Object->CountComments;
          $Count = $Sender->CurrentOffset;
          $postID = 'Post '.$Count.' of '.$Total;
          echo <<<POSTNUM
          <span class="PostNum">$postID
    POSTNUM;
       }

    displays Post of 26453
    so the current offset isnt on the sender object for either of the two hooks im using

      public function DiscussionController_CommentOptions_Handler($Sender) {
          $this->AddPostNum($Sender);
       }
       
       public function PostController_CommentOptions_Handler($Sender) {
          $this->AddPostNum($Sender);
       }

    thanks in advance for your help!

  • $Count = $Sender->CurrentOffset;

    should be $Count = $Sender->Offset;

  • while the controller offset wasn't the final answer, it led me to find the simplest cleanest solution.

    $Offset = !isset($Sender->EventArguments['Comment']) ? 1 : $Sender->CommentModel->GetOffset($Sender->EventArguments['Comment']) + 2;

    there it is. one line of simple perfection.

    If i can figure out how to do it, i will post my new, ultra snazzy plugin for all to benefit from. (and bask in its glorious simplicity)

  • My first official plug-in:

    http://vanillaforums.org/addon/postnum-plugin-.1

    Feel free to code review and let me know if I did something stupid.

Sign In or Register to comment.