Num of entries in each category

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
mikrogen
Posts: 3
Joined: Sun Sep 12, 2004 11:30 pm

Num of entries in each category

Post by mikrogen »

I would like to see a number of entries for each category on the sidebar. Is any easy way how to do it?

Thanks for reply.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Num of entries in each category

Post by garvinhicking »

We are currently rewriting serendipity to utilize smarty templating; there such a thing will be easier.

Until then, you can use this class to replace the one currently available in serendipity_sidebar_items.php (or insert it as a new external plugin):

Code: Select all

<?php
class serendipity_categories_plugin extends serendipity_plugin {
    function introspect(&$propbag) {
        global $serendipity;

        $propbag->add('name',        CATEGORIES);
        $propbag->add('description', CATEGORY_PLUGIN_DESC);
        $propbag->add('configuration', array('authorid', 'image'));

        $row_authors = serendipity_db_query("SELECT username, authorid FROM {$serendipity['dbPrefix']}authors");
        $authors     = array('all' => ALL_AUTHORS);
        foreach($row_authors AS $rownr => $row) {
            $authors[$row['authorid']] = $row['username'];
        }

        $this->author_select_values = $authors;
    }

    function introspect_config_item($name, &$propbag)
    {
        switch($name) {
            case 'authorid':
                $propbag->add('type',         'select');
                $propbag->add('name',         CATEGORIES_TO_FETCH);
                $propbag->add('description',  CATEGORIES_TO_FETCH_DESC);
                $propbag->add('select_values', $this->author_select_values);
                $propbag->add('default',     'all');
                break;

            case 'image':
                $propbag->add('type',         'string');
                $propbag->add('name',         XML_IMAGE_TO_DISPLAY);
                $propbag->add('description',  XML_IMAGE_TO_DISPLAY_DESC);
                $propbag->add('default',     serendipity_getTemplateFile('img/xml.gif'));
                break;

            default:
                return false;
        }
        return true;
    }

    function generateCategoryList($cats, $select = array(0), $type = 0, $id = 0, $level = 0, $xmlImg = '', $countArray = '') {
        global $serendipity;
    
        if (!is_array($cats) || !count($cats))
            return;
    
        $ret = '';
        foreach ($cats as $cat) {
            if ($cat['parentid'] == $id) {
                $category_id = serendipity_makeFilename($cat['category_name']);
                if (!empty($xmlImg)) {
                    $ret .= sprintf(
                      '<a href="%s" title="%s"><img alt="xml" %s src="%s" /></a> %s' .
                      '<a href="%s" title="%s">%s</a>' . (isset($countArray[$cat['categoryid']]) ? ' (' . $countArray[$cat['categoryid']] . ')' : '') . '<br />',
                      $serendipity['serendipityHTTPPath'] . 'rss.php?category=' . $cat['categoryid'] . '_' . $category_id,
                      htmlspecialchars($cat['category_description']),
                      ($serendipity['XHTML11'] ? 'style="display: inline; border: 0px"' : 'border="0"'),
                      $xmlImg,
                      str_repeat(' ', $level * 3),
                      $serendipity['serendipityHTTPPath'] . ($serendipity['rewrite'] == 'none' ? $serendipity['indexFile'] . '?/' : '') . PATH_CATEGORIES . '/' . $cat['categoryid'] . '-' . $category_id,
                      htmlspecialchars($cat['category_description']),
                      htmlspecialchars($cat['category_name']));
                } else {
                    $ret .= sprintf(
                      '%s<a href="%s" title="%s">%s</a>' . (isset($countArray[$cat['categoryid']]) ? ' (' . $countArray[$cat['categoryid']] . ')' : '') . '<br />',
                      str_repeat(' ', $level * 3),
                      $serendipity['serendipityHTTPPath'] . ($serendipity['rewrite'] == 'none' ? $serendipity['indexFile'] . '?/' : '') . PATH_CATEGORIES . '/' . $cat['categoryid'] . '-' . $category_id,
                      htmlspecialchars($cat['category_description']),
                      htmlspecialchars($cat['category_name']));
                }
                $ret .= $this->generateCategoryList($cats, $select, $type, $cat['categoryid'], $level + 1, $xmlImg, $countArray);
            }
        }

        return $ret;
    }

    function generate_content(&$title) {
        global $serendipity;

        $which_category = $this->get_config('authorid');
        $categories = serendipity_fetchCategories(empty($which_category) ? 'all' : $which_category);
        $title      = CATEGORIES;
        $html       = '';
        $image = $this->get_config('image', serendipity_getTemplateFile('img/xml.gif'));
        $image = (($image == "'none'" || $image == 'none') ? '' : $image);
        if (is_array($categories) && count($categories)) {
            $querystring = "SELECT
                                   c.categoryid,
                                   count(e.id) AS entrycount
                              FROM {$serendipity['dbPrefix']}category AS c
                   LEFT OUTER JOIN {$serendipity['dbPrefix']}entrycat AS ec ON c.categoryid = ec.categoryid
                   LEFT OUTER JOIN {$serendipity['dbPrefix']}entries  AS e  ON ec.entryid   = e.id
                          GROUP BY c.categoryid
                          ORDER BY category_name";
        
            $countArray = array();
            $rows = serendipity_db_query($querystring);
            if (is_array($rows)) {
                foreach($rows AS $idx => $row) {
                    $countArray[$row['categoryid']] = $row['entrycount'];
                }
            }

            $html .= $this->generateCategoryList($categories, array(0), 3, 0, 0, $image, $countArray);
        }

        $html .= sprintf(
            '<br /><a href="%s" title="%s">%s</a>',

            $serendipity['serendipityHTTPPath'] . $serendipity['indexFile'],
            ALL_CATEGORIES,
            ALL_CATEGORIES
        );

        echo $html;
    }
}
?>
# 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/
mikrogen
Posts: 3
Joined: Sun Sep 12, 2004 11:30 pm

Post by mikrogen »

It works without a problem. :)

Thank you.
Post Reply