<?php

    include_once( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
    include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );
    
    /**
     * Plugin that offers features to returns the top comment visitors object of current blog
     */
    class PluginTopCommentVisitors extends PluginBase
    {
        var $pluginEnabled;
        var $maxVisitors;        

        function PluginTopCommentVisitors()
        {
            $this->PluginBase();
  
            $this->id = "topcommentvisitors";
            $this->author = "Mark Wu";
            $this->desc = "This plugin offers the rank list of visitors by number of comments.";
  
            $this->locales = Array( "en_UK" , "zh_TW" , "zh_CN" );
            
            $this->init();            
        }

		function init()
		{
			include_once( PLOG_CLASS_PATH."class/template/menu/menu.class.php" );
            $this->registerAdminAction( "topcommentvisitors", "PluginTopCommentVisitorsConfigAction" );
			$this->registerAdminAction( "updateTopCommentVisitorsConfig", "PluginTopCommentVisitorsUpdateConfigAction" );
			
			$menu =& Menu::getMenu();
			if( !$menu->entryExists( "/menu/controlCenter/manageTopPlugins" ))						
				$this->addMenuEntry( "/menu/controlCenter", "manageTopPlugins", "", "", true, false );			
            $this->addMenuEntry( "/menu/controlCenter/manageTopPlugins", "TopCommentVisitors", "?op=topcommentvisitors", "" );            
		}

		function register()
		{
		    $blogSettings = $this->blogInfo->getSettings();
		    $this->pluginEnabled = $blogSettings->getValue( "plugin_topcommentvisitors_enabled" );
	        $this->maxVisitors = $blogSettings->getValue( "plugin_topcommentvisitors_maxvisitors" );
	    }
	    
	    function isEnabled()
	    {
	        return $this->pluginEnabled;
	    }
 
        /**
         * Returns the top comment visitors of current blog
         */
        function getTopCommentVisitors($maxVisitors = 0, $based = 'BLOG')
        {
            $blogId = $this->blogInfo->getId();
            $prefix = Db::getPrefix();
            
            $query = "SELECT ".$prefix."articles_comments.user_name, ".
                "max(".$prefix."articles_comments.user_url) AS user_url, ".
                "count(".$prefix."articles_comments.user_name) AS user_count ".
                "FROM ".$prefix."articles_comments,".$prefix."articles WHERE ";
            if ($based == 'BLOG')
                $query .= $prefix."articles.blog_id=".$blogId." AND ";
            $query .= $prefix."articles_comments.article_id=".$prefix."articles.id ".
                "AND ".$prefix."articles.status=1 ".
                "GROUP BY ".$prefix."articles_comments.user_name ORDER BY user_count DESC";

            if( $maxVisitors > 0 ) 
            {
                $query .= " LIMIT " . $maxVisitors; 
            } else {
                $query .= " LIMIT " . $this->maxVisitors;
            }

            $db =& Db::getDb();
            $result = $db->Execute( $query ); 

            if( !$result ) 
                return false; 
            
            $topcommentvisitors = Array();
            while( $row = $result->FetchRow()) { 
               array_push( $topcommentvisitors, $row); 
            } 

            return $topcommentvisitors; 
        }
    }
?>