<?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/net/rawrequestgenerator.class.php" );
    include_once( PLOG_CLASS_PATH."plugins/delicious/class/delicious/delicious.class.php" );
	include_once( PLOG_CLASS_PATH."class/template/menu/menu.class.php" );

    //Used as the root for building the bookmark URLs
    define ('DELICIOUS_ROOT', 'http://del.icio.us/'); 
    
    /**
     * Plugin that offers features to integrate the Del.icio.us Bookmark Service
     */
    class PluginDelicious extends PluginBase
    {
        var $pluginEnabled;
        var $userName;              //Your del.icio.us account logon 
        var $password;              //Your del.icio.u account password
        var $userRoot;             //Root for links in the output
        var $errors = array(
            	1=>'You must enter credentials',
            	2=>'You have been throttled. Please stop attempting to connect to del.icio.us',
            	3=>'The del.icio.us service did not return a proper HTTP response',
            	4=>'Unable to connect to del.icio.us',
            	5=>'Unable to send data to del.icio.us',
            	6=>'Unable to parse the XML returned from del.icio.us'
            );

        function PluginDelicious()
        {
            $this->PluginBase();
            
            $this->id = "delicious";
            $this->author = "Mark Wu";
            $this->desc = 'This plugin let you integrate your Del.icio.us with pLog.';

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

		function init()
		{
            $this->registerAdminAction( "delicious", "PluginDeliciousConfigAction" );
			$this->registerAdminAction( "updateDeliciousConfig", "PluginDeliciousUpdateConfigAction" );
			$this->registerBlogAction( "DeliciousShow", "PluginDeliciousShowAction" );
			
			$menu =& Menu::getMenu();
			if( !$menu->entryExists( "/menu/controlCenter/manageIntegrationPlugins" ))						
				$this->addMenuEntry( "/menu/controlCenter", "manageIntegrationPlugins", "", "", true, false );			
            $this->addMenuEntry( "/menu/controlCenter/manageIntegrationPlugins", "Delicious", "?op=delicious", "" );            
		}

		function register()
		{
		    $blogSettings = $this->blogInfo->getSettings();
			$this->pluginEnabled = $blogSettings->getValue( "plugin_delicious_enabled" );
			$this->userName = $blogSettings->getValue( "plugin_delicious_username" );
            $this->password = $blogSettings->getValue( "plugin_delicious_password" );
		}
	    
	    function isEnabled()
	    {
	        return $this->pluginEnabled;
	    }

        function pluginTemplatePage()
        {
 			$rg = new RawRequestGenerator($this->blogInfo);
            
        	$rg->addParameter( "op", "DeliciousShow" );
            $rg->addParameter( "blogId", $this->blogInfo->getId());

            $templatePage = $rg->getIndexUrl().$rg->getRequest();

            return $templatePage;
        }		
 
        /**
         * Get the photo albums from specific user
         */
        function getTagList()
        {
            $this->userRoot = $this->pluginTemplatePage();
 
            // instanciate delicious object
            $del = new delicious($this->userName,$this->password);
            
            // toggle debugging mode
            //$del->debug_flag = 1;
            
            // PLEASE USE SPARINGLY OR del.icio.us WILL THROTTLE YOU
            if(!$result = $del->get_tags())
            {
            	$message = $this->$errors[ $del->getError() ];
            }
            else
            {
            	// debugging helpers
            	//echo $del->htmlvar_dump($result);
            	//echo $del->request;
            	//echo $del->response;
            	
            	if( count($result) > 0)
            	{
            		// loop thru result set, inserting into db
            		$tags = array();
            		foreach($result as $properties)
            		{
            			$tag['id'] = str_replace(' ',',',$properties['tag']);
            			$tag['count'] = $properties['count'];
            			$tag['tagUrl'] = $this->userRoot . "&tag=" . $tag['id'];
            			$tag['deliciousUrl'] = DELICIOUS_ROOT . $this->userName . '/' . $tag['id'];
            			array_push( $tags, $tag);
            		}
            		return $tags;
            	}
            	else
            	{
            		$message = $result['code'];
            	}
            }
        }
    }
?>
