<?php

    include_once( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
    include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );
    include_once( PLOG_CLASS_PATH."class/dao/trackbacks.class.php" );
    include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );
    
    /**
     * Plugin that offers features to returns the top trackback posts object of current blog
     */
    class PluginTopTrackbackPosts extends PluginBase
    {
        var $prefix;
        var $pluginEnabled;
        var $maxPosts;        
    
        function PluginTopTrackbackPosts()
        {
            $this->PluginBase();
  
            $this->id = "toptrackbackposts";
            $this->author = "Mark Wu";
            $this->desc = "This plugin offers the rank list of articles by number of trackbacks.";
  
            $this->prefix = Db::getPrefix();
            
            $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( "toptrackbackposts", "PluginTopTrackbackPostsConfigAction" );
			$this->registerAdminAction( "updateTopTrackbackPostsConfig", "PluginTopTrackbackPostsUpdateConfigAction" );
			
			$menu =& Menu::getMenu();
			if( !$menu->entryExists( "/menu/controlCenter/manageTopPlugins" ))						
				$this->addMenuEntry( "/menu/controlCenter", "manageTopPlugins", "", "", true, false );			
            $this->addMenuEntry( "/menu/controlCenter/manageTopPlugins", "TopTrackbackPosts", "?op=toptrackbackposts", "" );            
		}

		function register()
		{
		    $blogSettings = $this->blogInfo->getSettings();
		    $this->pluginEnabled = $blogSettings->getValue( "plugin_toptrackbackposts_enabled" );
	        $this->maxPosts = $blogSettings->getValue( "plugin_toptrackbackposts_maxposts" );
	    }
	    
	    function isEnabled()
	    {
	        return $this->pluginEnabled;
	    }

        /**
         * Returns the top trackback posts object of current blog
         */
        function getTopTrackbackPosts($maxPosts = 0, $based = 'BLOG')
        {
            $trackbacks = new Trackbacks();
            $blogId = $this->blogInfo->getId();
 
            $query = "SELECT id, num_nonspam_trackbacks ".
                "FROM ".$this->prefix."articles WHERE ";
            if ($based == 'BLOG')
                $query .= "blog_id='".$blogId."' ";
            $query .= "AND status=1 AND num_nonspam_trackbacks != 0 ".
                "ORDER BY num_nonspam_trackbacks DESC";

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

            $db =& Db::getDb();
            $result = $db->Execute( $query ); 
            print $query;
            if( !$result ) 
                return false; 
            
            $toptrackbackposts = Array();
            while( $row = $result->FetchRow()) { 
                $article = $this->getArticle($row["id"]);
                array_push( $toptrackbackposts, $article); 
            } 

            return $toptrackbackposts; 
        }
        
        function getArticle( $artId )
        {
            $articles = new Articles();
            $blogId = $this->blogInfo->getId();
            
            $query = "SELECT * FROM ".$this->prefix."articles WHERE id = ".$artId;

               $query .= " AND blog_id = ".$blogId;

            $query .= ";";

            // we send the query and then fetch the first array with the result
            $db =& Db::getDb();
            $result = $db->Execute( $query ); 

            if( $result == false )
                return false;

            if ( $result->RecordCount() == 0)
                return false;

            $row = $result->FetchRow( $result );

            $article = $articles->mapRow( $row );

            return $article;
        }
    }

?>