<?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 return a recent article comments from the current blog
     */
    class PluginRecentComments extends PluginBase
    {
        var $pluginEnabled;
        var $maxComments;
        
        function PluginRecentComments()
        {
            $this->PluginBase();
  
            $this->id = "recentcomments";
            $this->author = "Mark Wu";
            $this->desc = "This plugin offers the most recently article comments.";
            $this->locales = Array( "en_UK" , "zh_TW" , "zh_CN", "es_ES" );
            $this->init();
        }

		function init()
		{
            $this->registerBlogAction( "recentcommentsrss", "PluginRecentCommentsRssAction" );

            $this->registerAdminAction( "recentcomments", "PluginRecentCommentsConfigAction" );
			$this->registerAdminAction( "updateRecentCommentsConfig", "PluginRecentCommentsUpdateConfigAction" );
			include_once( PLOG_CLASS_PATH."class/template/menu/menu.class.php" );
            
			$menu =& Menu::getMenu();
			if( !$menu->entryExists( "/menu/controlCenter/manageRecentPlugins" ))						
				$this->addMenuEntry( "/menu/controlCenter", "manageRecentPlugins", "", "", true, false );			
            $this->addMenuEntry( "/menu/controlCenter/manageRecentPlugins", "RecentComments", "?op=recentcomments", "" );            
		}

		function register()
		{
		    $blogSettings = $this->blogInfo->getSettings();
		    $this->pluginEnabled = $blogSettings->getValue( "plugin_recentcomments_enabled" );
	        $this->maxComments = $blogSettings->getValue( "plugin_recentcomments_maxcomments", 10);
	    }
	    
	    function isEnabled()
	    {
	        return $this->pluginEnabled;
	    }

        /**
         * Returns the recent comments object of current blog
         */
        function getRecentComments()
        {
            include_once( PLOG_CLASS_PATH."class/dao/commentscommon.class.php" );

            $blogId = $this->blogInfo->getId();
            $commentsCommon = new CommentsCommon();
            $prefix = Db::getPrefix();

            $query = "SELECT c.*
                      FROM {$prefix}articles_comments c, {$prefix}articles a
                      WHERE c.blog_id = '".Db::qstr( $blogId )."'";
            $query .= " AND c.article_id = a.id";
            $query .= " AND a.status = ".POST_STATUS_PUBLISHED;
            $query .= " AND c.status = ".COMMENT_STATUS_NONSPAM;
            $query .= " ORDER BY c.date DESC";
            $query .= " LIMIT ".$this->maxComments;

            $db =& Db::getDb();
            $result = $db->Execute( $query ); 
            
            if( !$result ){
                return Array();
            }
                
            if( $result->RowCount() == 0 ){
                $result->Close();           
                return Array();
            }
                
            $comments = Array();
            while( $row = $result->FetchRow()) {
                $comments[] = $commentsCommon->mapRow( $row );
            }
            $result->Close();           

            return( $comments );
        }

            /**
             * Returns an article given an id
             * This is needed here because we need to get various
             * articles, not just the most recent, etc. that are generally
             * available to the templates.
             */
        function getArticle( $artId )
        {
			include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );				
            $articles = new Articles();
            return $articles->getArticle($artId);
        }

        function getRssFeedUrl(){
            include_once( PLOG_CLASS_PATH."class/net/rawrequestgenerator.class.php" );

            $rg = new RawRequestGenerator($this->blogInfo);
            $rg->addParameter( "op", "recentcommentsrss" );
            $rg->addParameter( "blogId", $this->blogInfo->getId());

            $feedUrl = $rg->getIndexUrl().$rg->getRequest();
            return $feedUrl;
        }
    }
?>