<?php

    include_once( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );	
	include_once( PLOG_CLASS_PATH."plugins/smileys/class/smileys/smileys.class.php" );
	include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
    
    define( "DEFAULT_SMILEY_SET", "default" );
    
    define( "SMILEY_FILE_PATH", PLOG_CLASS_PATH."/plugins/smileys/icons/" );
	
	/**
	 * base class that registers everything
	 */
    class PluginSmileys extends PluginBase
    {
        var $_smileySet;
        var $_pluginEnabled;
        
        function PluginSmileys()
        {
            $this->PluginBase();
			
			// ---
			// set some information
			// ---
			$this->id = "smileys";
            $this->author = "The pLog Team";
            $this->desc = "The plugin offer smileys to comments and articles.";

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

            $this->init();  
        }

		function init()
		{
			// ---
			// register two actions for this plugin plus an option in the menu
			// ---
            $this->registerAdminAction( "smileys", "PluginSmileysConfigAction" );
			$this->registerAdminAction( "updateSmileysConfig", "PluginSmileysUpdateConfigAction" );
			
			// ---
			// register two actions for this plugin plus an option in the menu
			// ---
			$menu =& Menu::getMenu();
			if( !$menu->entryExists( "/menu/controlCenter/manageAppearancePlugins" ))						
				$this->addMenuEntry( "/menu/controlCenter", "manageAppearancePlugins", "", "", true, false );			
            $this->addMenuEntry( "/menu/controlCenter/manageAppearancePlugins", "Smileys", "?op=smileys", "" );

			// ---
			// finally, register the event-based plugin that will finally take care of 
			// 'translating' the smileys
			// ---
			$this->registerNotification( EVENT_POST_LOADED );
			$this->registerNotification( EVENT_POSTS_LOADED );			
		}

		function register()
		{
		    $blogSettings = $this->blogInfo->getSettings();
		    $this->_pluginEnabled = $blogSettings->getValue( "plugin_smileys_enabled" );
	        $this->_smileySet = $blogSettings->getValue( "plugin_smileys_iconset" );
	    }
	    
	    function isEnabled()
	    {
	        return $this->_pluginEnabled;
	    }		

		function _smileizePost( $post, $smileySet, $smileys )
		{
			// modify the text...
			$postIntroText = $post->getIntroText();
			$postIntroText = Smileys::smileys( $blogInfo, $postIntroText, $smileySet, $smileys );
			$postExtendedText = $post->getExtendedText();
			$postExtendedText = Smileys::smileys( $blogInfo, $postExtendedText, $smileySet, $smileys );
			$post->setText( $postIntroText.POST_EXTENDED_TEXT_MODIFIER.$postExtendedText );
			// ...modify the comments...
			$comments = $post->getComments();
			
			// check if there's anything to do with the comments
			if( !is_array($comments)) {
				return $post;
			}
			
			$output = Array();
			foreach( $comments as $comment ) {
				$tmpText = $comment->getText();
				$commentText = Smileys::smileys( $blogInfo, $tmpText, $smileySet, $smileys );
				$comment->setText( $commentText );
				array_push( $output, $comment );
			}

			$post->setComments( $output );
			
			return $post;
		}
		
		/**
		 * Receives and processes events
		 *
		 * @param eventType
		 * @param params
		 */
		function process( $eventType, $params )
		{
			// get the objects we need from the parameters array
			$blogInfo = $params[ "blogInfo" ];
			$blogSettings = $blogInfo->getSettings();
		    $pluginEnabled = $blogSettings->getValue( "plugin_smileys_enabled" );
			$from = $params["from"];
			
            if ( $pluginEnabled != 1 || $from != "")
                return true;			
			
			$smileySet = $blogSettings->getValue( "plugin_smileys_iconset" );
			if( $smileySet == "" )
				$smileySet = DEFAULT_SMILEY_SET;
				
			// load the set
			$smileys = Smileys::loadSmileySet( $smileySet );
			
			if( $eventType == EVENT_POST_LOADED ) {
				$post = $params[ "article" ];
				$post = $this->_smileizePost( $post, $smileySet, $smileys );
				// ...and finally put everything back together
				$params["article"] = $post;
			}
			elseif( $eventType == EVENT_POSTS_LOADED ) {
				$posts = $params[ "articles" ];
				$result = Array();
				foreach( $posts as $post ) {
					$post = $this->_smileizePost( $post, $smileySet, $smileys );
					array_push( $result, $post );
				}
				$params[ "articles" ] = $result;
			}
			
			//return $params;
			
			return true;
		}

        function generateSmileysBar()
        {
            if( $this->_pluginEnabled ) {
			    // load the set
			    $smileys = Smileys::loadSmileySet( $this->_smileySet );
			                
                $text = "";
                $rg =& RequestGenerator::getRequestGenerator( $blogInfo );
                $prevIcon = "";
                $currentIcon = "";
                foreach( $smileys as $smiley => $icon ) {
                    $currentIcon = $icon;
                    if ($currentIcon != $prevIcon) {
                        $iconTag = "<img style=\"vertical-align:middle;\" onClick=\"writeImgTag('".addslashes($smiley)."')\" alt=\"".$smiley."\" title=\"".$smiley."\" src=\"".
                                    $rg->getUrl( "/plugins/smileys/icons/".$this->_smileySet."/".$icon."\" />" );
                        $text .= $iconTag;
                        $prevIcon = $currentIcon;
                    }
                }
            } else {
                $locale = $this->blogInfo->getLocale();
                $text = $locale->tr("error_smileys_not_enabled");
            }
            
            return $text;
        }
			
    }
?>