<?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/template/menu/menu.class.php" );
    
    /**
     * Mostra un avatar a cada comentari descarregant-lo de Gravatar.com. Versió 0.2
     */

    define( "AVATAR_FILE_PATH", PLOG_CLASS_PATH."/plugins/gravatar/avatars/" );

    class PluginGravatar extends PluginBase
    {
        var $pluginEnabled;
        var $rating;
        var $default;
        var $size;

        function PluginGravatar()
        {
            $this->PluginBase();

            $this->id = "gravatar";            
            $this->author = "Original by Dani Armengol, Modified by Mark Wu";
            $this->desc = "Returns the URL of an (gr)avatar for every comment. The image is loaded from Gravatar.com";

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

            $this->init();            
        }
        
		function init()
		{
            $this->registerAdminAction( "gravatar", "PluginGravatarConfigAction" );
			$this->registerAdminAction( "updateGravatarConfig", "PluginGravatarUpdateConfigAction" );
			
			$menu =& Menu::getMenu();
			if( !$menu->entryExists( "/menu/controlCenter/manageAppearancePlugins" ))						
				$this->addMenuEntry( "/menu/controlCenter", "manageAppearancePlugins", "", "", true, false );			
            $this->addMenuEntry( "/menu/controlCenter/manageAppearancePlugins", "Gravatar", "?op=gravatar", "" );
		}

		function register()
		{
		    $blogSettings = $this->blogInfo->getSettings();
		    $this->pluginEnabled = $blogSettings->getValue( "plugin_gravatar_enabled" );
		    $this->rating = $blogSettings->getValue( "plugin_gravatar_rating" );
	        $this->default = $blogSettings->getValue( "plugin_gravatar_default" );
	        $this->size = $blogSettings->getValue( "plugin_gravatar_size", 40 );	
	    }
	    
	    function isEnabled()
	    {
	        return $this->pluginEnabled;
	    }

	    function getSize()
	    {
	        return $this->size;
	    } 	       

        /**
         * Returns the gravatar object from www.gravatar.com
         */
         
        function gravatar($comment, $rating="", $default = "", $size="" )
        {
			if ( $rating == "") {
			    $rating = $this->rating;
			} else {
			    if ( $rating != "G" && $rating != "PG" && $rating != "R" && $rating != "X" )
			        $rating = "G";
			}

			if ( $default == "" ) {
			    $rg = $this->blogInfo->getBlogRequestGenerator();
				$default = $rg->getUrl( "/plugins/gravatar/avatars/".$this->default);
            }
			
			if ( $size == "") {
			    $size = $this->size;
			} else {
			    if ( $size < 30 || $size > 80)
			        $size = 40;
			
			}          
                
            if($comment->getUserEmail() != "")
            {
	            $gravatar_url = "http://www.gravatar.com/avatar.php?rating=".$rating."&amp;gravatar_id=".md5($comment->getUserEmail())."&amp;default=".urlencode($default)."&amp;size=".$size;
            } else {
	            $gravatar_url = $default;
            }

            return $gravatar_url;
        }
    }
?>
