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.
Options

BUG in Input Formatter - extra formatting cycles.

edited November 2011 in Vanilla 2.0 - 2.8
Create default.php file with this code and place it in new Plugin directory

<?php $PluginInfo['AnyFormatterPlugin'] = array( 'Name' => 'ANY FORMATTER PLUGIN', 'Description' => '', 'Version' => '', 'Author' => "", 'AuthorEmail' => '', 'AuthorUrl' => '' ); class FormatterPlugin extends Gdn_Plugin { public function Base_Render_Before($Sender) { } public function DiscussionController_BeforeCommentBody_Handler($Sender) { $Comment = $Sender->Discussion; $Comment->Body = ParseSyntax($Comment->Body); foreach($Sender->CommentData as $cdata) { $cdata->Body = ParseSyntax($cdata->Body);; } } // AJAX posting of comments public function PostController_BeforeCommentBody_Handler($Sender) { $this->DiscussionController_BeforeCommentBody_Handler($Sender); } // AJAX preview of new discussions public function PostController_BeforeDiscussionRender_Handler($Sender) { if ($Sender->View == 'preview') { $Sender->Comment->Body = ParseSyntax($Sender->Comment->Body); } } // AJAX preview of new comments public function PostController_BeforeCommentRender_Handler($Sender) { if ($Sender->View == 'preview') { $Sender->Comment->Body = ParseSyntax($Sender->Comment->Body); } } // AJAX discussion edit preview public function PostController_BeforeDiscussionPreview_Handler($Sender) { $Sender->Comment->Body = ParseSyntax($Sender->Comment->Body); } public function Setup() { // Nothing to do here! } } function ParseSyntax($String) { $String="0".$String."0"; return $String; }


if this Plugin is enabled you'll see in every post

rolling staring and ending "0" of each message in discussion.

0 The first mesage will have one 0 in the end and the start of the message. 0
00 The second - two "0". 00
000 The third - threee and so on.... 000

0000 It means that message processor makes the extra work to display messages. 0000

00000 How to correct this ? 00000
«1

Comments

  • Options
    Bug Here....

    public function DiscussionController_BeforeCommentBody_Handler($Sender) { $Comment = $Sender->Discussion; $Comment->Body = ParseSyntax($Comment->Body); foreach($Sender->CommentData as $cdata) { $cdata->Body = ParseSyntax($cdata->Body);; } }
  • Options
    x00x00 MVP
    edited November 2011
    There is a lot wrong with your code. You are looping in comment handlers that already in a loop meaning every comment you are parsing all the comments. Also why are you using an external function for your parser, why not make a method? This stops conflicts.

    You need to properly understand how to make a plugin and know when hook is in a loop, rather than just botching you way through.

    The irony is you are trying to get a syntax highlighter to work, you need to learn how to code.

    try BeforeCommentsRender if you want to do it that way.

    grep is your friend.

  • Options
    This is not my code.

    <<<<The irony is you are trying to get a syntax highlighter to work, you need to learn how to code.>>>>>>
    This is cleared code of plugin You adviced me :)

    PS. and my plain code works the finest way. I can give you a link if you forgot which is my code :)
  • Options
    try BeforeCommentsRender if you want to do it that way.
    Ok, so You say that DiscussionController_BeforeCommentBody_Handler($Sender) is a wastecode of plugin adviced by you .

    may I delete it at all if it is a wastecode ?
  • Options
    x00x00 MVP
    edited November 2011
    I didn't write that plugin. I advised you to use a similar plugin to help you get started, but here is no substitute for actually understanding what you are doing. Admittedly that plugin isn't the best example, however you should use your initiative.

    The long and the short of it is I don't have time to teach you how to program. It is still better to do a formatting addon as a plugin. It is theme independent, and you don't want to edit the core, becuase that will be replaced when you update. I also advised you not to put hunks of code in views. Another alternative is to create helper function or view module, but that is not really pluggable.

    BeforeCommentBody is self explanatory. It triggers before each comment body. BeforeCommentsRender triggers before comments are rendered, which is before the loop.

    You also may also need for your project to pick an earlier hook for any pre-processing, which is before it goes through the main formatter.

    grep is your friend.

  • Options
    Teach to program? LOL God with you man :)

    Don't try to teach a life anybody if you even have not tried to run the code . Ok?

    And selfexplanatory functions are not such you think of them.
    cause they simply don't work as they must. some of them don't work at all.

    Try to do a simple parser yourself. It's very easy :)

    Thanks a lot for your help nonetheless.

    ps
    if GeShi plugin works somehow then I can teach from it.
  • Options
    BeforeCommentBody is self explanatory. It triggers before each comment body. BeforeCommentsRender triggers before comments are rendered, which is before the loop.
    It's a wrong blind belief :)

    Try yourself :))))
  • Options
    Very simple goal at first. without tons of code.

    do a numerator of messages )))))))))))))

    static $NUMBER; $MessageText = $NUMBER.' - ' $MessageText; $NUMBER=$NUMBER+1;
  • Options
    I've coded plenty of parsers. There is also somethign called good practice, it save you pulling you hair out later. That all the help I'm giving you.

    grep is your friend.

  • Options
    It is still better to do a formatting addon as a plugin.
    totally agree :)
  • Options
    I've coded plenty of parsers. There is also somethign called good practice, it save you pulling you hair out later. That all the help I'm giving you.
    I beleive you - I'm too not the first day in army .
  • Options
    x00x00 MVP
    edited November 2011
    BeforeCommentsRender triggers once as expected
    BeforeCommentBody triggers every comment as expected.

    Now you have to figure out the code yourself. Find where those events are being fired work backwards.

    grep is your friend.

  • Options
    edited November 2011
    Yes, I figured out
    public function DiscussionController_BeforeCommentBody_Handler($Sender

    $Sender is a Parent object.

    $Sender->Discussion->Body - is a first comment of discussion
    $Sender->CommentData - an array of all messages in discussion

    Very usable. Yes.

    You've coded plenty of parsers.
    Suggest a way to get a link to current comment from $sender object.

  • Options
    x00x00 MVP
    edited November 2011
    ????

    BeforeCommentsRender is before the render loop allowing you to loop through the comments as you are

    BeforeCommentBody is during the loop. You have to use event arguments (which is the second parameter of the handler) Comment and Disccussion just before BeforeCommentDisplay. If you actually looked at the code where those event are, you will see this.

    Seriously I don't know what more help I can give.

    grep is your friend.

  • Options
    x00x00 MVP
    edited November 2011
    grep -R --include="*.php" "\->FireEvent(" .
    grep -R --include="*.php" "\->FireEvent('.*Comment" .


    grep is your friend.

  • Options
    :)

    No sense , that's a place where i was setting wrapper function .

    <?php $Sender->FireEvent('BeforeCommentBody'); $Object->FormatBody = Gdn_Format::To($Object->Body, $Object->Format); $Sender->FireEvent('AfterCommentFormat'); $Object = $Sender->EventArguments['Object']; echo $Object->FormatBody; $Sender->FireEvent('AfterCommentFormat'); $Object = $Sender->EventArguments['Object']; ?>


    The only I have in the handler routune is a link to $Sender .
  • Options
    edited November 2011
    Okey - this is stable code.

    class FormatterPlugin extends Gdn_Plugin { public function Base_Render_Before($Sender) {} public function DiscussionController_BeforeCommentBody_Handler($Sender) { //static counter of messages static $i; if ($i==null) { $Comment = $Sender->Discussion; // PARSING FIRST COMMENT $Comment->Body = '1. '.$Comment->Body; $i=2;} $number=2; //CommentData contains messages from №2 foreach($Sender->CommentData as $cdata) { if ($number==$i) {// PARSING OTHER COMMENTS $cdata->Body = $number.'. '.$cdata->Body;} $number=$number+1;} //Next message $i=$i+1; } public function Setup() {// Nothing to do here!} }
  • Options
    x00x00 MVP
    edited November 2011
    Since you have tried I will help you. A counter is not necessary your method is not very efficient. You chose the BeforeCommentBody hook so we will use that approach
    	public function DiscussionController_BeforeCommentBody_Handler($Sender, $Args) {
    if(in_array('Discussion',$Args)) {
    $Comment = $Args['Discussion'];
    }else{
    $Comment = $Args['Comment'];
    }
    $Comment->Body=ParseSyntax($Comment->Body);
    }
    I think you should do a php course though, before delving into more developing.


    grep is your friend.

  • Options
    thanx a lot.

    this is really helpful.

    but where from have You gotten Args syntax for your code?
  • Options
    TimTim Operations Vanilla Staff
    You don't even have to use the conditional. The Discussion or Comment will be in
    $Sender->EventArguments['Object'] regardless of what type it is.

    Vanilla Forums COO [GitHub, Twitter, About.me]

Sign In or Register to comment.