<?php
    /*
    Copyright 2006, 2008 Paul Westbrook (paul@westbrooks.org)
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    */



	lt_include( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
	lt_include( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );


	class PluginAddThis extends PluginBase
	{
		var $pluginEnabled;
		var $addThisUsername;
		
		function PluginAddThis( $source = "" )
		{
			$this->PluginBase($source);

			$this->id      = "addthis";
			$this->author  = "Paul Westbrook";
			$this->desc    = "The AddThis plugin creates an AddThis button for each post.  This allows your users to bookmark or share your articles";
			$this->version = "20080717";

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

			if( $source == "admin" )
				$this->initAdmin();
		}
		
		function initAdmin()
		{
		        $this->registerAdminAction( "addthisConfig", "PluginAddThisConfigAction" );
			$this->registerAdminAction( "updateAddThisConfig", "PluginAddThisUpdateConfigAction" );
			
			$menu =& Menu::getMenu();
			if( !$menu->entryExists( "/menu/controlCenter/manageAppearancePlugins" ))						
				$this->addMenuEntry( "/menu/controlCenter", "manageAppearancePlugins", "", "", true, false );			
			
			$this->addMenuEntry( "/menu/controlCenter/manageAppearancePlugins", "addthis", "?op=addthisConfig", "" );
		}

		function register()
		{
		    $blogSettings = $this->blogInfo->getSettings();
		    $this->pluginEnabled = $blogSettings->getValue( "plugin_addthis_enabled" );
		    $this->addThisUsername = $blogSettings->getValue( "plugin_addthis_username" );
		}

	    function isEnabled()
	    {
	        return $this->pluginEnabled;
	    }
	    

	    function show($postId)
	    {
		$str = '';
	    
		if ($this->isEnabled())
            	{
            
		    // articles object
                    $articles = new Articles;
    
    
		    $rg = $this->blogInfo->getBlogRequestGenerator();
		    $rg->setXHTML( false );
        	    $post = $articles->getBlogArticle($postId);

		    if ($post) 
		    {
			$blogSettings = $this->blogInfo->getSettings();
		    	$locale =& $this->blogInfo->getLocale();

                    	$blogname = $this->blogInfo->getBlog();
                    	$permalink = $rg->postPermalink($post);
                    	$title = $post->getTopic();


                    	$str .= '<!-- ADDTHIS BUTTON BEGIN -->';
		    	$str .= '<script type="text/javascript">';
		    	$str .= 'addthis_pub             = "' . $this->addThisUsername . '";';
		    	$str .= 'addthis_brand           = "' . $blogname . '";';
		    	$str .= '</script>';
		    	$str .= '<a href="http://www.addthis.com/bookmark.php" onmouseover="return addthis_open(this, \'\', \'' . $permalink . '\', \'' . $title . '\')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s9.addthis.com/button1-share.gif" width="125" height="16" border="0" alt="" /></a>';
		    	$str .= '<script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script>';
		    	$str .= '<!-- ADDTHIS BUTTON END -->';
		    }

		} 
	    	return $str;
	   }
		
	}
?>
