<?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 make the first character of article as lower case
     */
    class PluginDropCase extends PluginBase
    {
        var $pluginEnabled;
    
        function PluginDropCase()
        {
			$this->PluginBase();

			$this->id = "dropcase";
            $this->author = "Mark Wu";
            $this->desc = "Drop case letter (a huge letter) for the first letter of every post.";

			$this->locales = Array( "en_UK" , "zh_TW" , "zh_CN" );

			$this->init();            
        }

		function init()
		{
            $this->registerAdminAction( "dropcase", "PluginDropCaseConfigAction" );
			$this->registerAdminAction( "updateDropCaseConfig", "PluginDropCaseUpdateConfigAction" );
			
			$menu =& Menu::getMenu();
			if( !$menu->entryExists( "/menu/controlCenter/manageAppearancePlugins" ))						
				$this->addMenuEntry( "/menu/controlCenter", "manageAppearancePlugins", "", "", true, false );			
            $this->addMenuEntry( "/menu/controlCenter/manageAppearancePlugins", "DropCase", "?op=dropcase", "" );            

			$this->registerNotification( EVENT_POST_LOADED );
			$this->registerNotification( EVENT_POSTS_LOADED );
		}
		
		function register()
		{
		    $blogSettings = $this->blogInfo->getSettings();
		    $this->pluginEnabled = $blogSettings->getValue( "plugin_dropcase_enabled" );
	    }
	    
	    function isEnabled()
	    {
	        return $this->pluginEnabled;
	    }		

		function process($eventType, $params)
		{
            if ( $eventType != EVENT_POST_LOADED && $eventType != EVENT_POSTS_LOADED)
                return true;
                
		    $blogSettings = $this->blogInfo->getSettings();
		    $pluginEnabled = $blogSettings->getValue( "plugin_dropcase_enabled" );
            if ( $pluginEnabled != 1 )
                return true;

			$from = $params["from"];
			if ( $from != "" )
				return true;

			if( $eventType == EVENT_POST_LOADED ) {
				$post = $params[ "article" ];
				$post = $this->dropcase_process($post);
				$params["article"] = $post;
			}
			elseif( $eventType == EVENT_POSTS_LOADED ) {
				$posts = $params[ "articles" ];
				$result = Array();
				foreach( $posts as $post ) {
					$post = $this->dropcase_process($post);
					array_push( $result, $post );
				}
				$params[ "articles" ] = $result;
			}

			return true;
		}
 
        /**
         * Make the first character of article as lower case
         */

        function dropcase( $text )
        {
            $newText = explode("</p>", $text); 

            // Piece the output back together. 
            // $bufText = "<div class=\"drop-case\">".$newText[0]."</div>"; 

			if (ereg("^<p>", $newText[0])) { 
				$bufText = str_replace( "<p>", "<p id=\"drop-case\">", $newText[0] ); 
			} 
			else { 
				$bufText = ereg_replace("^", "<p id=\"drop-case\">", $newText[0] ); 
			} 
            $bufText .= "</p>";
            
            for($i = 1; $i < count($newText); $i ++){ 
                $bufText .= $newText[$i];
                $bufText .= "</p>";
            }

            // Return the updated markup 
            return $bufText;
        }

		function dropcase_process( $post )
		{
			// modify the text...
			$postIntroText = $post->getIntroText();
			$postIntroText = $this->dropcase( $postIntroText );
			$postExtendedText = $post->getExtendedText();
			$post->setText( $postIntroText.POST_EXTENDED_TEXT_MODIFIER.$postExtendedText );
			return $post;
		}
    }
?>