Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How to trigger a function to work only when a comment is made?

edited September 2006 in Vanilla 1.0 Help
Thank you mary for pointing me to the PreSaveComment and PostSaveComment delegates in the DiscussionForm control in order to trigger a new function/extension I'm working on to only work when a new comment or discussion item is added. In my testing, I have had success except for one thing. The function is executed but the display of the information is not updated in the side panel unless I select the Discussions tab or Categories or something else. Can you help me determine what I need to call to cause the page to refresh? Do I need to add a reference to CallDelegate('PostSaveComment")?
Thank you.


  • Does it work if you turn javascript off in your browser?
  • No, unfortunately I still have to select something to trigger the update on screen. I've tried changing this call in different ways:
    $Context->AddToDelegate("DiscussionForm", "PostSaveData", "xxx");
    by replacing PostSaveData with different delegates. Maybe I just have chosen the correct delegate.
  • Well that's weird, with Javascript off it should be reloading the page when you click "Add your comments"
  • I know my function is triggered and working because I am writing data to a file and the file is updated correctly, but the side panel display is one step behind.
  • Is your extension adding the data to the side panel before it updates it?

    It would help if you told us a little more about the extension (ie. what are you trying to do?)...
  • edited August 2006
    My extension is to display a total word count of comments and discussion items in the side panel. It writes out the data to a file when a new comment or discussion is added. It definitely does this. Then it displays this in the side panel. Pretty simple. I'm doing this to learn a little more about how Vanilla works because the number of words in your forum is probably not useful to most. However, it is a proof of concept because I can see this being used in other ways. Here is the code: <?php /* Extension Name: Prolific Extension Url: Description: Calculates the forum-wide word count and writes it to a file each time you publish a new comment. Then display the word count in the side panel. Ported the Prolific WP plugin by Justin Blanton at Version: 1.0 Author: Jim Wurster Author Url: */ function prolific(&$CommentForm) { global $countdisplay,$count; $file = "extensions/Prolific/wordcount.txt"; $query = "SELECT * FROM LUM_Comment WHERE Body > ' ' AND CommentID > 1 AND Deleted = '0'"; $result = mysql_query($query); $num_results = mysql_num_rows($result); $string = ''; if ($result) { while ($row = mysql_fetch_array($result)) { $postwords = strip_tags($row[7]); $string = $string.$postwords; } $string = strtolower($string); // Remove punctuation. $wordlist = preg_split('/\s*[\s+\.|\?|,|(|)|\-+|\'|\*|\"|=|;|%|\!|\[|\]|×|\$|\/|:|{|}]\s*/i', $string); foreach ($wordlist as $words) { $postWords = strip_tags($words); $postWords = explode(' ', $postWords); $count = count($postWords) + $count; } $count = number_format($count); } else { $count = 0; } $fh = fopen($file, 'w') or die("could not open file"); fwrite($fh, $count) or die('could not write to file'); fclose($fh); return $count; } // add hook $Context->AddToDelegate("DiscussionForm", "PostSaveData", "prolific"); if (in_array($Context->SelfUrl, array("index.php", "account.php", "categories.php", "comments.php", "post.php", "search.php", "settings.php"))) { class wordcount extends PostBackControl { function display_wordcount () { global $countdisplay; $file = "extensions/Prolific/wordcount.txt"; $fh = fopen($file, 'r') or die('could not open file!'); $data = fread($fh, filesize($file)) or die('could not read file!'); $countdisplay = '<h2>Total Words</h2>'; $countdisplay .= '<br />' .$data .' words'; fclose($fh); return $countdisplay; } } $wordcount = $Context->ObjectFactory->NewContextObject($Context, 'wordcount'); $wordcount->display_wordcount(); $Panel->AddString($countdisplay, 200); $Head->AddStyleSheet('extensions/Prolific/style.css'); } ?>
  • NickENickE New
    edited August 2006
    That's not exactly the most efficient method of going about this. For starters, you are querying every single comment from the db every single time a comment is made. Would it not be wiser to query them all the first time the extension is enabled (say, check if the file exists), then simply add the word count of the just added comment to the total (you wouldn't even have to query the db)? I notice you're also retrieving all the columns from the database yet only actually using the body one. Furthermore, you're using global variables that don't exist, and using a class when you don't need to.

    On a lighter note, it's not updating on that page because you add the panel string before your hook is called. I'd recomment using a delegate (for the panel string) that is sure to be called after 'PostSaveData', but will be called whether or not the comment is being saved.
  • SirNot: Thank you for your insight. I'll work on your suggestions. Is there a rule-of-thumb when to use a class as opposed to just a function?
  • I wouldn't say there's a 'rule-of-thumb', but a class generally makes handling mutiple functions and variables pertaining to the same thing easier. In your case it's redundant to have a class as it's just one function which you only call once and do not modify in any way.
  • Whoa, wack dude, I was surfing around the wordpress site, trying to pass the time, and I stumbled accross your blog. Weird.
  • Weird good or bad? My design is currently undergoing changes, once I found a style that validated okay (from a friend). Now the fun begins to fit all my pieces in it nicely.
  • I wasn't saying your blog was weird, I was just pointing out the situation was sort of weird.
  • Yes, life is full of things that one never expects.
  • *runs through thread nude
  • SirNot: I finally found the correct delegate to get the information displayed in the side panel as soon as you saved the comment. I also trimmed down the code based upon your suggestions. Thank you. I do have one question. You mentioned that I wouldn't have to do a query for just the new comment. If you could help, what object would contain this comment data at this point?
  • $CommentForm->Comment->Body should have the text.
This discussion has been closed.