Recent Topics

[PLUGIN] List of articles per author

Started by on Jan 04, 2009 – Contents updated: Jan 04, 2009

Jan 04, 2009 18:57    

I made a plugin and tested it on b2evo 2.4.5. You can see an example here http://blogs.lessthandot.com/index.php/All/?disp=authdir

Use and improve this as you like.

Here is the first file the biggest too.

I called it _authors.plugin.php and it goes in the plugins folder.

<?php
/**
 * This file implements the Authors plugin. This plugin is based on the Archives plugin.
 *
 * Displays a list of posts per author.
 *
 * @package plugins
 *
 * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
 * @author chrissie1: Christiaan Baes.
 *
 * @version $Id: _authors.plugin.php,v 0.1 2009/01/03 $
 */
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
 
 
load_funcs('_core/ui/results/_results.class.php');
 
 
 
/**
 * Authors Plugin
 *
 * This plugin displays
 */
class authors_plugin extends Plugin
{
    /**
     * Variables below MUST be overriden by plugin implementations,
     * either in the subclass declaration or in the subclass constructor.
     */
 
    var $name = 'Authors Widget';
    var $code = 'evo_Auth';
    var $priority = 50;
    var $version = '0.1';
    var $author = 'chrissie1';
    var $group = 'widget';
 
 
    /**
     * Init
     */
    function PluginInit( & $params )
    {
        $this->short_desc = T_('This skin tag displays a list of posts per author.');
        $this->long_desc = T_('Post are grouped by author.');
 
        $this->dbtable = 'T_items__item';
        $this->dbprefix = 'post_';
        $this->dbIDname = 'post_ID';
    }
 
 
    /**
     * Event handler: SkinTag
     *
     * @param array Associative array of parameters. Valid keys are:
     *                - 'block_start' : (Default: '<div class="bSideItem">')
     *                - 'block_end' : (Default: '</div>')
     *                - 'title' : (Default: T_('Authors'))
     *                - 'mode' : 'list|author' 
     *                - 'author' : 
     *                - 'limit' : # of archive entries to display or '' (Default: 12)
     *                - 'more_link' : more link text (Default: 'More...')
     *                - 'list_start' : (Default '<ul>')
     *                - 'list_end' : (Default '</ul>')
     *                - 'line_start' : (Default '<li>')
     *                - 'line_end' : (Default '</li>')
     *                - 'day_date_format' : (Default: conf.)
     * @return boolean did we display?
     */
    function SkinTag( $params )
    {
        /**
         * @var Blog
         */
        global $Blog;
 
        if( empty($Blog) )
        {
            return false;
        }
 
        /**
         * Default params:
         */
        // This is what will enclose the block in the skin:
        if(!isset($params['block_start'])) $params['block_start'] = '<div class="bSideItem">';
        if(!isset($params['block_end'])) $params['block_end'] = "</div>\n";
 
        // Title:
        if(!isset($params['block_title_start'])) $params['block_title_start'] = '<h3>';
        if(!isset($params['block_title_end'])) $params['block_title_end'] = '</h3>';
        // Authors mode:
        if(!isset($params['mode']))
            $params['mode'] = 'list';
        if($params['mode']=='author')
        {
            if(!isset($params['title'])) $params['title'] = T_('Author');
        }
        else
        {
            if(!isset($params['title'])) $params['title'] = T_('Authors');
        }
        
        // Number of archive entries to display:
        if(!isset($params['limit'])) $params['limit'] = 12;
        
        // Number of archive entries to display:
        if(!isset($params['author'])) $params['author'] = '';
 
        // More link text:
        if(!isset($params['more_link'])) $params['more_link'] = T_('More...');
 
        // This is what will enclose the list:
        if(!isset($params['list_start'])) $params['list_start'] = '<ul>';
        if(!isset($params['list_end'])) $params['list_end'] = "</ul>\n";
 
        // This is what will separate the archive links:
        if(!isset($params['line_start'])) $params['line_start'] = '<li>';
        if(!isset($params['line_end'])) $params['line_end'] = "</li>\n";
 
        $AuthorsList = & new AuthorsList( $params['mode'], $params['limit'], $params['author'], $this->dbtable, $this->dbprefix, $this->dbIDname );
 
        echo $params['block_start'];
 
        if( !empty($params['title']) )
        {   // We want to display a title for the widget block:
            echo $params['block_title_start'];
            echo $params['title'];
            echo $params['block_title_end'];
        }
 
        if ($AuthorsList->total_rows>0)
        {
            echo $params['list_start'];
            if($params['mode']=='author')
            {
                $AuthorsList->get_item( $arc_author_ID, $arc_author, $arc_count, $post_ID, $post_title, $views, $comments);
                echo '<h2>'.$arc_author.'</H2>';
                echo '<br />';
                echo $params['line_start'];
                if($post_title=='')
                {
                    $post_title='No title';
                }
                echo '<a href="'.url_add_param( $Blog->get('url'), 'p='.$post_ID ).'">'.$post_title.'</a> (Views: '.$views.', Comments: '.$comments.')';
                echo $params['line_end'];
            }
            while( $AuthorsList->get_item( $arc_author_ID, $arc_author, $arc_count, $post_ID, $post_title, $views, $comments) )
            {
                echo $params['line_start'];
                switch( $params['mode'] )
                {
                    case 'list':
                        echo '<a href="'.url_add_param( $Blog->get('url'), 'disp=authdir&amp;author='.$arc_author_ID ).'">'.$arc_author.'</a>';
                        echo ' <span class="dimmed">('.$arc_count.')</span>';
                        break;
 
                    case 'author':
                        if($post_title=='')
                        {
                            $post_title='No title';
                        }
                        echo '<a href="'.url_add_param( $Blog->get('url'), 'p='.$post_ID ).'">'.$post_title.'</a> (Views: '.$views.', Comments: '.$comments.')';
                        break;
                }
 
                echo $params['line_end'];
            }
 
            // Display more link:
            if( !empty($params['more_link']) )
            {
                echo $params['line_start'];
                echo '<a href="'.url_add_param( $Blog->get('url'), 'disp=authdir' );
                echo '">'.format_to_output($params['more_link']).'</a>';
                echo $params['line_end'];
            }
        
        
            echo $params['list_end'];
        }
        else
        {
            echo 'Author not found.';
        }
        
        if($params['mode']=='author')
        {
            echo '<br />';
            echo '<br />';
            echo '<a href="'.url_add_param( $Blog->get('url'), 'disp=authdir' );
            echo '">Back to authorslist</a>';
        }
 
        echo $params['block_end'];
 
        return true;
    }
}
 
 
/**
 * Archive List Class
 *
 * @package evocore
 */
class AuthorsList extends Results
{
    var $archive_mode;
    var $author;
 
    /**
     * Constructor
     *
     * Note: Weekly archives use MySQL's week numbering and MySQL default if applicable.
     * In MySQL < 4.0.14, WEEK() always uses mode 0: Week starts on Sunday;
     * Value range is 0 to 53; week 1 is the first week that starts in this year.
     *
     * @link http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
     *
     * @todo categories combined with 'ALL' are not supported (will output too many archives,
     * some of which will resolve to no results). We need subqueries to support this efficiently.
     *
     * @param string
     * @param integer
     * @param boolean
     */
    function AuthorsList(
        $archive_mode = 'list',
        $limit = 100,
        $author = '',
        $dbtable = 'T_items__item',
        $dbprefix = 'post_',
        $dbIDname = 'ID' )
    {
        global $DB;
        global $blog, $cat, $catsel;
        global $show_statuses;
        global $timestamp_min, $timestamp_max;
        global $s, $sentence, $exact;
 
        $this->dbtable = $dbtable;
        $this->dbprefix = $dbprefix;
        $this->dbIDname = $dbIDname;
        $this->archive_mode = $archive_mode;
        $this->author = $author;
 
        /*
         * WE ARE GOING TO CONSTRUCT THE WHERE CLAUSE...
         */
        $this->ItemQuery = & new ItemQuery( $this->dbtable, $this->dbprefix, $this->dbIDname ); // TEMPORARY OBJ
 
        $this->ItemQuery->where_chapter( $blog, '', array() );
 
        // * Restrict to the statuses we want to show:
        $this->ItemQuery->where_visibility( $show_statuses );
 
        // - - - + * * timestamp restrictions:
        $this->ItemQuery->where_datestart( '', '', '', '', $timestamp_min, $timestamp_max );
 
 
        $this->from = $this->ItemQuery->get_from();
        $this->where = $this->ItemQuery->get_where();
        $this->group_by = $this->ItemQuery->get_group_by();
 
        switch( $this->archive_mode )
        {
            case 'list':
                $sql = 'SELECT                      post_creator_user_ID,
                                                    user_login,
                                                    COUNT(DISTINCT postcat_post_ID) AS count '
                                                    .$this->from . ', evo_users '
                                                    .$this->where.'
                                                    AND post_creator_user_ID = user_ID
                                                    GROUP BY post_creator_user_ID
                                                    ORDER BY COUNT(DISTINCT postcat_post_ID) DESC';
                break;
 
            case 'author':
                $sql = "SELECT                      DISTINCT '.$this->dbIDname.' 
                                                    ,post_ID
                                                    ,user_login
                                                    ,post_title
                                                    ,post_views 
                                                    ,(select count(*) from evo_comments where comment_post_ID = post_id and comment_status = 'published') as comments "
                                                    .$this->from. ", evo_users "
                                                    .$this->where."
                                                    AND post_creator_user_ID = ".$author."
                                                    AND post_creator_user_ID = user_ID
                                                    GROUP BY post_ID
                                                    ORDER BY post_datestart DESC";
                break;
 
        }
 
        parent::Results( $sql, 'AuthorsList_', '', $limit );
 
        $this->restart();
    }
 
 
    /**
     * Count the number of rows of the SQL result
     *
     * These queries are complex enough for us not to have to rewrite them:
     * dh> ???
     */
    function count_total_rows()
    {
        global $DB;
 
        switch( $this->archive_mode )
        {
            case 'list':
                $sql_count = 'SELECT COUNT( DISTINCT  post_creator_user_ID) '
                                                    .$this->from
                                                    .$this->where;
                break;
 
            case 'author':
                $sql_count = 'SELECT COUNT( DISTINCT post_ID)'
                                                    .$this->from
                                                    .$this->where.'
                                                    AND post_creator_user_ID = '.$this->author;
                break;
 
        }
 
        $this->total_rows = $DB->get_var( $sql_count ); //count total rows
    }
 
 
    /**
     * Rewind resultset
     */
    function restart()
    {
        // Make sure query has executed at least once:
        $this->query();
 
        $this->current_idx = 0;
    }
 
    /**
     * Getting next item in archive list
     *
     * WARNING: these are *NOT* Item objects!
     */
    function get_item( & $arc_author_ID, & $arc_author, & $arc_count, & $post_ID, & $post_title, & $views, & $comments )
    {
        if( $this->current_idx >= $this->result_num_rows )
        {   // No more entry
            return false;
        }
 
        $arc_row = $this->rows[ $this->current_idx++ ];
 
        switch( $this->archive_mode )
        {
            case 'list':
                $arc_author_ID  = $arc_row->post_creator_user_ID;
                $arc_author  = $arc_row->user_login;
                $arc_count = $arc_row->count;
                return true;
 
            case 'author':
                $post_ID = $arc_row->post_ID;
                $arc_author  = $arc_row->user_login;
                $post_title = $arc_row->{$this->dbprefix.'title'};
                $views = $arc_row->post_views;
                $comments = $arc_row->comments;
                return true;
        }
    }
}
 
 
 
 
 
?>

Then I created this file. _authdir.disp.php and that goes in the skins folder.

<?php
/**
 * This is the template that displays the archive directory for a blog
 *
 * This file is not meant to be called directly.
 * It is meant to be called by an include in the main.page.php template.
 * To display the archive directory, you should call a stub AND pass the right parameters
 * For example: /blogs/index.php?disp=authdir
 *
 * b2evolution - {@link http://b2evolution.net/}
 * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
 * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/}
 *
 * @package evoskins
 */
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
 
$author = param('author');
if(!is_numeric($author))
{$author='';}
if (!$author=='')
{
    // Call the Authors plugin WITH NO LIMIT & NO MORE LINK:
    $Plugins->call_by_code( 'evo_Auth', array( 'mode'=>'author',
                                                'author'=>$author,
                                              'block_start'=>'',
                                              'block_end'=>'',
                                              'limit'=>'',
                                              'more_link'=>'' ) );
}
else
{
    // Call the Authors plugin WITH NO LIMIT & NO MORE LINK:
    $Plugins->call_by_code( 'evo_Auth', array( 'title'=>'',
                                              'block_start'=>'',
                                              'block_end'=>'',
                                              'limit'=>'',
                                              'more_link'=>'' ) );
}
?>

I also had to adapt the _skin.func.php file a little in the inc/skins folder.

I added this line of code

'disp_authdir'         => '_authdir.disp.php',

after this line of code.

'disp_arcdir'         => '_arcdir.disp.php',

Jan 05, 2009 10:49

That's not the same as what I have. I have a widget with a list of authors with the number of posts and a page with a list of article titles. not the complete articles. But yeah I didn't know about that. And that feature isn't very easily accessible. Or is it?

Jan 05, 2009 12:03

It means that you should be able to convert your widget to use $MainList instead of $ArchiveList ... no need for a new class that way ... and still get the same results ?

¥

Jan 05, 2009 12:08

Ok, I will look into Mainslist to see if it can replace authorslist.

May 04, 2009 23:47

Hello,

could you explain how to change this one into plugin, that could show the list of authors with number of their Comments. And clicking on the author then it could provide with a list of comments with a first line of comment as a title for example. I'm struggling with that one... :(

I'm using custom skin on b2e 2.4.6

thank you for help!

Ben

Aug 04, 2009 15:31

What would I need to change in the scripts here so that it shows the full name of the author instead of just the nickname? I would really appreciate this :)


Form is loading...

Run your own website! – This forum is powered by b2evolution CMS, a complete engine for your website.