Displaying the number of comments pending?

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
User avatar
RobA
Regular
Posts: 317
Joined: Wed Apr 27, 2005 5:11 pm
Contact:

Displaying the number of comments pending?

Post by RobA » Tue Jul 03, 2007 4:41 pm

Is there a smarty hook to display the number of comments pending on a particular entry?

I'd like to say something like "XXX comments pending moderation." just above the add new comment section so people will know.

-Rob A>
* My s9y blog:
* Rob A's (Im)Personal Blog
* http://www.silent9.com/blog

User avatar
garvinhicking
Core Developer
Posts: 30014
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Displaying the number of comments pending?

Post by garvinhicking » Tue Jul 03, 2007 4:46 pm

Hi!

Nice idea. Sadly, there is no smarty function to achieve this. But you can use the PHP function serendipity_fetchComments() and pass an additional SQL where string to it to filter for "pending" comments only, then count the number of returned rows. You could write a custom smarty functions for that and put it in your template's config.inc.php file.

Do you know how to achieve that? I think you're already quite far with stuff like that, so I saved myself to create it for you... :)

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

User avatar
RobA
Regular
Posts: 317
Joined: Wed Apr 27, 2005 5:11 pm
Contact:

Post by RobA » Tue Jul 03, 2007 7:42 pm

Thanks -

I'm 99% there, just having an issue with the fetchComments().

If I use:

Code: Select all

$comments = serendipity_fetchComments($params['entry'], null, '', true, 'NORMAL', ' AND co.status = \'pending\'');
return count($comments);


It always returns 0, even on entries with pending comments.

If I strip out the where parameter:

Code: Select all

$comments = serendipity_fetchComments($params['entry'], null, '', true, 'NORMAL');
return count($comments);


It returns the count of all the comments (including pending).

I tried the first query directly against the database and it works....th oughts?

-Rob A>
* My s9y blog:
* Rob A's (Im)Personal Blog
* http://www.silent9.com/blog

User avatar
garvinhicking
Core Developer
Posts: 30014
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking » Tue Jul 03, 2007 8:39 pm

Hi!

Try this:

Code: Select all

$comments = serendipity_fetchComments($params['entry'], null, '', false, 'NORMAL', ' AND co.status = \'pending\'');



the ShowAll parameter should not be set to true, I think.

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

User avatar
RobA
Regular
Posts: 317
Joined: Wed Apr 27, 2005 5:11 pm
Contact:

Post by RobA » Tue Jul 03, 2007 8:50 pm

It has to be true. Here is the code snippet from functions_comments.inc.php:

Code: Select all

    if (!$showAll) {
        $and .= ' AND co.status = \'approved\'';
    }


So it has to be true to prevent the default behaviour of only returning approved comments.

EDIT

Found the culprit... I needed to throw in an order parameter. serendipity_fetchComments doesn't seem to handle null or empty strings for that parameter gracefully.

This works:

Code: Select all

    $comments = serendipity_fetchComments($params['entry'], null, 'co.id', true, 'NORMAL', ' AND co.status = \'pending\'');
    return count($comments);


I'll post the full code and the smarty code in a follow-up shortly.

-Rob A>
Last edited by RobA on Tue Jul 03, 2007 8:59 pm, edited 1 time in total.
* My s9y blog:
* Rob A's (Im)Personal Blog
* http://www.silent9.com/blog

User avatar
garvinhicking
Core Developer
Posts: 30014
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking » Tue Jul 03, 2007 8:58 pm

Exactly this is the reason why it should NOT be using that. The status can only EITHER be 'approved' OR 'pending'! It cannot be both. If your SQL-statement filters for "pending", the function should not first filter for 'approved'. :)

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

User avatar
RobA
Regular
Posts: 317
Joined: Wed Apr 27, 2005 5:11 pm
Contact:

Post by RobA » Tue Jul 03, 2007 9:04 pm

garvinhicking wrote:Exactly this is the reason why it should NOT be using that. The status can only EITHER be 'approved' OR 'pending'! It cannot be both. If your SQL-statement filters for "pending", the function should not first filter for 'approved'. :)

Best regards,
Garvin


Exactly! so it has to be set to true to show all the comments, not just the approved! (We might just be saying the same thing but backwards....)

-Rob A>
* My s9y blog:
* Rob A's (Im)Personal Blog
* http://www.silent9.com/blog

User avatar
RobA
Regular
Posts: 317
Joined: Wed Apr 27, 2005 5:11 pm
Contact:

Post by RobA » Tue Jul 03, 2007 9:33 pm

OK -

Here it is:

In config.inc.php of your template folder add the following:

Code: Select all

$serendipity['smarty']->register_function('serendipity_smartyfetchPendingCount', 'serendipity_smartyfetchPendingCount');
function serendipity_smartyfetchPendingCount($params, &$smarty) {
    global $serendipity;
    if (!isset($params['entry'])) {$smarty->trigger_error(__FUNCTION__ .": missing 'entry' parameter");
        return;
    }
    $comments = serendipity_fetchComments($params['entry'], null, 'co.id', true, 'NORMAL', ' AND co.status = \'pending\'');
    return count($comments);
}


Note you will need to also have a:

Code: Select all

<?php 


at the start and a:

Code: Select all

?>


at the end, depending what else is in there...

Edit your entries.tpl and add the following smarty code where you want this to appear:

Code: Select all

                {capture name="returned_value"}
                {serendipity_smartyfetchPendingCount entry=$entry.id}
                {/capture}
                {assign var="pendCount" value=$smarty.capture.returned_value}
                {if $pendCount>0}
                    <div class="serendipity_center serendipity_msg_notice">{$pendCount} Comment{if $pendCount>1}s{/if} Pending Approval</div>
                {/if}


You can see an example here:
http://ffaat.pointclark.net/blog/archives/108-ATM-Windows-Error.html

(I added the serendipity_msg_notice to get it in green. just remove that to make it the same as the default text colour.

-Rob A>
* My s9y blog:
* Rob A's (Im)Personal Blog
* http://www.silent9.com/blog

User avatar
RobA
Regular
Posts: 317
Joined: Wed Apr 27, 2005 5:11 pm
Contact:

Post by RobA » Mon Jul 09, 2007 5:43 am

P.S. Feel free to add this to the wiki as an example of creating and using a custom smarty function...

-Rob A>
* My s9y blog:
* Rob A's (Im)Personal Blog
* http://www.silent9.com/blog

Post Reply