Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Author Topic: [MOD] New comments improved - 9 possible modes of filtering  (Read 1019 times)

infomix

  • Newbie
  • *
  • Karma: 7
  • Posts: 19

This new function adds three new parameters which combined can generate up to nine different filters for the latest comments on your site  (eg: if you want to show comments from a specific category)

Quick explanation



MOD - Function new_comments improved V1.0

First, find the function new_comments (Is close to the line 1250) in the core file snews.php
Now add the code highlighted below:

function new_comments($number = 5, $stringlen = 30, $category = 0, $strict = false, $inverse = false) {
   global $_catID;
   $rule1 = ' = ';
   $rule2 = ' || ';
   if ($inverse == true) {$rule1 = ' != '; $rule2 = ' && ';}
   if ($category == -1) $category = !empty($_catID) ? $_catID : 0;
   $subcat = $strict == true ? '' : $rule2.'c.subcat'.$rule1.$category;
   $filter = $category == 0 ? '' : ' AND (c.id'.$rule1.$category.$subcat.')';

   $query = 'SELECT
         a.id AS aid,title,a.seftitle AS asef,
         category,co.id,articleid,co.name AS coname,comment,
         c.name,c.seftitle AS csef,c.subcat,
         x.name,x.seftitle AS xsef
      FROM '._PRE.'comments'.' AS co
      LEFT OUTER JOIN '._PRE.'articles'.' AS a
         ON articleid = a.id
      LEFT OUTER JOIN '._PRE.'categories'.' AS c
         ON category = c.id AND c.published =\'YES\'
      LEFT OUTER JOIN '._PRE.'categories'.' AS x
         ON c.subcat = x.id AND x.published =\'YES\'
      WHERE a.published = 1 AND (a.commentable = \'YES\' || a.commentable = \'FREEZ\' )
         AND approved = \'True\''.$filter.'
      ORDER BY co.id DESC LIMIT '.$number;


OR simply replace the entire function (copy and paste the code below)
Code: [Select]
// NEW COMMENTS IMPROVED V1.0 - link: http://snewscms.com/forum/index.php/topic,10460.0.html
function new_comments($number = 5, $stringlen = 30, $category = 0, $strict = false, $inverse = false) {
global $_catID;
$rule1 = ' = ';
$rule2 = ' || ';
if ($inverse == true) {$rule1 = ' != '; $rule2 = ' && ';}
if ($category == -1) $category = !empty($_catID) ? $_catID : 0;
$subcat = $strict == true ? '' : $rule2.'c.subcat'.$rule1.$category;
$filter = $category == 0 ? '' : ' AND (c.id'.$rule1.$category.$subcat.')';
$query = 'SELECT
a.id AS aid,title,a.seftitle AS asef,
category,co.id,articleid,co.name AS coname,comment,
c.name,c.seftitle AS csef,c.subcat,
x.name,x.seftitle AS xsef
FROM '._PRE.'comments'.' AS co
LEFT OUTER JOIN '._PRE.'articles'.' AS a
ON articleid = a.id
LEFT OUTER JOIN '._PRE.'categories'.' AS c
ON category = c.id AND c.published =\'YES\'
LEFT OUTER JOIN '._PRE.'categories'.' AS x
ON c.subcat = x.id AND x.published =\'YES\'
WHERE a.published = 1 AND (a.commentable = \'YES\' || a.commentable = \'FREEZ\' )
AND approved = \'True\''.$filter.'
ORDER BY co.id DESC LIMIT '.$number;
$result = mysql_query($query);
if (mysql_num_rows($result) === 0) {
echo '<li>'.l('no_comments').'</li>';
} else {
$comlim = s('comment_limit');
$comment_limit = $comlim < 1 ? 1 : $comlim;
$comments_order = s('comments_order');
while ($r = mysql_fetch_array($result)) {
$loopr = mysql_query("SELECT id FROM "._PRE.'comments'."
WHERE articleid = '$r[articleid]'
AND approved = 'True'
ORDER BY id $comments_order");
$num = 1;
while ($r_art = mysql_fetch_array($loopr)) {
if ($r_art['id'] == $r['id']) {
$ordinal = $num;
}
$num++;
}
$name = $r['coname'];
$comment = strip_tags($r['comment']);
$page = ceil($ordinal / $comment_limit);
$ncom = $name.' ('.$comment;
$ncom = strlen($ncom) > $stringlen ? substr($ncom, 0, $stringlen - 3).'...' : $ncom;
$ncom.= strlen($name) < $stringlen ? ')' : '';
$ncom = str_replace(' ...', '...', $ncom);
$paging = $page > 1 ? '/'.l('comment_pages').$page : ''; // Patch #1 - 1.7.0
unset($link); // Patch #10 - 1.7.0 - 4 strings
if (isset($r['xsef'])) { $link = $r['xsef'].'/'; }
if (isset($r['csef'])) { $link .= $r['csef'].'/'; }
$link .= $r['asef']; // end Patch #10 - 1.7.0
echo '<li><a href="'._SITE.$link.$paging.'/#'.l('comment').$ordinal.'"
title="'.l('comment_info').' '.$r['title'].'">'.$ncom.'</a>
</li>';
}
}
}

Examples of practical use

1) Default: Display comments of all categories
new_comments(10,30)


2) Display comments of a specific category (including the subcategories), eg: Category One (ID: 2 in MY example)
new_comments(10,30,2)


3) Display comments of the current category (where you/user is browsing, including the subcategories)
new_comments(10,30,-1)


4) Display comments of a specific category (but no include the subcategories), eg: Category One (ID: 2 in MY example)
new_comments(10,30,2,true)


5) Display comments of all categories, EXCEPT the category "Category Two" (ID: 4) and their subcategories
new_comments(10,30,4,false,true)


There are still four other possible combinations (which totals the ninth mentioned in the title), but the above examples are enough to understand the basic usage.


Quick guide: Finding the category ID
I said that to filter a category, you should use the ID of the category, but some users have no idea how to find, here's an easy way:

Go to the administration, categories, click "view" and leave the mouse over the category you want to know the ID, now just look at the link in the status bar

« Last Edit: July 03, 2012, 06:41:32 PM by infomix »
Logged

blankspace

  • Newbie
  • *
  • Karma: 3
  • Posts: 22
Re: [MOD] New comments improved - 9 possible modes of filtering
« Reply #1 on: July 03, 2012, 12:38:53 PM »

Hello mr infomix! I like this MOD, work very good. Only for me in my theme does not work just as I am wanting it to because latest comments appear on articles and pages too. I only was wanting them to appear on home and categories/subcategories. But it is small thing, I am already building new theme where this is in the plan, so is a good MOD. Thank you!

Ciao,
/Pax
Logged
All your internets are belong to us

infomix

  • Newbie
  • *
  • Karma: 7
  • Posts: 19
Re: [MOD] New comments improved - 9 possible modes of filtering
« Reply #2 on: July 03, 2012, 06:20:50 PM »

Hello mr infomix! I like this MOD, work very good. Only for me in my theme does not work just as I am wanting it to because latest comments appear on articles and pages too. I only was wanting them to appear on home and categories/subcategories. But it is small thing, I am already building new theme where this is in the plan, so is a good MOD. Thank you!

If I understand correctly, I believe that the code below is the solution for you, just add the code highlighted in your template:

<?php if(empty($_ID) && !in_array($categorySEF,explode(',',l('cat_listSEF')))) { ?>
    <h3>New Comments</h3>
    <ul><?php new_comments(10,30) ?></ul>
<?php } ?>
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: [MOD] New comments improved - 9 possible modes of filtering
« Reply #3 on: July 04, 2012, 01:01:29 AM »

Excellent mod infomix. Karma+  :)
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

blankspace

  • Newbie
  • *
  • Karma: 3
  • Posts: 22
Re: [MOD] New comments improved - 9 possible modes of filtering
« Reply #4 on: July 04, 2012, 04:12:24 PM »

Thank you infomix, new code works very good! You can see for example here: http://www.grand-central.eu/blankspace/changelogs/

I would give +1 Karma but I can not find this. Maybe because I am newbie, ha-ha!
Logged
All your internets are belong to us