<?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/articles.class.php" );
    
    /**
	* Plugin that offers features to get all articles from specific article cateogries 
	* as announcements and sticky articles.
	*/
    class PluginStickyPosts extends PluginBase
    {
        var $pluginEnabled;
        var $stickyCategoryId;			//store stickyCategoryId
        var $announceCategoryId;		//store AnnounceCategoryId
		var $maxStickys;
		var $maxAnnounces;
        
        function PluginStickyPosts()
        {
			$this->PluginBase();
			
            $this->id = "stickyposts";
            $this->author = "lss";
            $this->desc = "This plugin offers features to get all articles from specific article cateogries as announcements and sticky articles.";
  
            $this->locales = Array( "en_UK" , "zh_TW" , "zh_CN", "es_ES" );
            
            $this->init();
        }

		function init()
		{
			$this->registerAdminAction( "stickyposts", "PluginStickyPostsConfigAction" );
			$this->registerAdminAction( "updatestickyposts", "PluginStickyPostsUpdateConfigAction" );
			
			$menu =& Menu::getMenu();
			if( !$menu->entryExists( "/menu/controlCenter/manageRecentPlugins" ))						
				$this->addMenuEntry( "/menu/controlCenter", "manageRecentPlugins", "", "", true, false );			
            $this->addMenuEntry( "/menu/controlCenter/manageRecentPlugins", "StickyPosts", "?op=stickyposts");
		}

		function register()
		{
		    $blogSettings = $this->blogInfo->getSettings();
		    $this->pluginEnabled = $blogSettings->getValue( "plugin_stickyposts_enabled" );
	        $this->stickyCategoryId = $blogSettings->getValue( "plugin_stickyposts_stickycategoryid" );
	        $this->announceCategoryId = $blogSettings->getValue( "plugin_stickyposts_announcecategoryid" );
	        $this->maxStickys = $blogSettings->getValue( "plugin_stickyposts_maxstickys" );
	        $this->maxAnnounces = $blogSettings->getValue( "plugin_stickyposts_maxannounces" );
	    }
	    
	    function isEnabled()
	    {
	        return $this->pluginEnabled;
	    }

        /**
		* Returns the stickyposts articles object of current blog
		*/
        function getArticles( $catId )
        {
			if ( 'sticky' == $catId ){
				$catId = $this->stickyCategoryId;
				$maxpost = $this->maxStickys;
			} else if ( 'announce' == $catId ){
				$catId = $this->announceCategoryId;
				$maxpost = $this->maxAnnounces;
			} else if ( is_numeric( $catId ) ){
				$maxpost = -1;
			}else return null;
						
			$blogId = $this->blogInfo->getId();
			
			$articles = new Articles();
			$ret_articles = $articles->getBlogArticles( $blogId, -1, $maxpost, $catId, POST_STATUS_PUBLISHED );
			
			return $ret_articles;
        }
        
        function getCategory( $catId )
        {
			if ( 'sticky' == $catId ){
				$catId = $this->stickyCategoryId;
			} else if ( 'announce' == $catId ){
				$catId = $this->announceCategoryId;
			} else if ( !is_numeric( $catId ) ) return null;
						
			$blogId = $this->blogInfo->getId();
			
			$categories = new ArticleCategories();
			$ret_category = $categories->getCategory( $catId, $blogId );
			
			return $ret_category;
        }
    }
?>
