<?php
/**
 * Google Ads for DokuWiki
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Dirk Moeller, based on code by Bernd Zeimetz <bernd@bzed.de> and Terence J. Grant<tjgrant@tatewake.com>
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'admin.php');

//--- Exported code
include_once(DOKU_PLUGIN.'googleads/code.php');
//--- Exported code

/**
 * All DokuWiki plugins to extend the admin function
 * need to inherit from this class
 */
class admin_plugin_googleads extends DokuWiki_Admin_Plugin
{
var $state = 0;
var $googleads = '';

	/**
	 * Constructor
	 */
	function admin_plugin_googleads()
	{
		$this->setupLocale();
	}

	/**
	 * return some info
	 */
	function getInfo()
	{
		return array(
			'author' => 'Dirk Moeller',
			'email'  => 'dirk@systemengineers.de',
			'date'   => '2007-08-14',
			'name'   => 'Google Adsense Plugin',
			'desc'   => 'Plugin to embed your Google Adsense code in your site. Based on code from Bernd Zeimetz and Terence J.',
			'url'    => 'http://www.systemengineers.de/plugins/googleads',
		);
	}

	/**
	 * return sort order for position in admin menu
	 */
	function getMenuSort()
	{
		return 888;
	}
	

	/**
	 * handle user request
	 */
	function handle()
	{
		$this->state = 0;
	
		if (!isset($_REQUEST['cmd'])) return;   // first time - nothing to do

		if (!is_array($_REQUEST['cmd'])) return;

		$this->googleads = $_REQUEST['googleads'];

		if (is_array($this->googleads))
		{
			$this->state = 1;
		}
	}

	/**
	 * output appropriate html
	 */
	function html()
	{
		global $conf;
		global $gads_loaded, $gads_settings;

		if ($this->state != 0)	//If we are to save now...
		{
			$gads_settings['enabled'] = $this->googleads['enabled'] == 'on' ? 'checked' : '';
			$gads_settings['debug'] = $this->googleads['debug'] == 'on' ? 'checked' : '';
			$gads_settings['dontcountadmin'] = $this->googleads['dontcountadmin'] == 'on' ? 'checked' : '';
			$gads_settings['dontcountusers'] = $this->googleads['dontcountusers'] == 'on' ? 'checked' : '';

                        $gads_settings['google_ad_client'] = $this->googleads['google_ad_client'];
			$gads_settings['google_color_border'] = $this->googleads['google_color_border'];
			$gads_settings['google_color_bg'] = $this->googleads['google_color_bg'];
			$gads_settings['google_color_link'] = $this->googleads['google_color_link'];
			$gads_settings['google_color_url'] = $this->googleads['google_color_url'];
			$gads_settings['google_color_text'] = $this->googleads['google_color_text'];

			gads_save();
		}

		print $this->plugin_locale_xhtml('intro');

		ptln("<form action=\"".wl($ID)."\" method=\"post\">");
		ptln('  <input type="hidden" name="do"   value="admin" />');
		ptln('  <input type="hidden" name="page" value="'.$this->getPluginName().'" />');
		ptln('  <input type="hidden" name="cmd[googleads]" value="true" />');
		print '<center><table class="inline">';
		print '	<tr><th> '.$this->getLang('gads_item_type').' </th><th> '.$this->getLang('gads_item_option').' </th></tr>';
//		print '	<tr><td> '.$this->getLang('gads_googleads_code').' </td><td><TEXTAREA rows="15" cols="40" name="googleads[code]">' . stripslashes($gads_settings['code']) . '</TEXTAREA></td></tr>';

		print '	<tr><td> '.$this->getLang('gads_enabled').' </td><td><input type="checkbox" name="googleads[enabled]" '.$gads_settings['enabled'].'/></td></tr>';
		print '	<tr><td> '.$this->getLang('gads_debug').' </td><td><input type="checkbox" name="googleads[debug]" '.$gads_settings['debug'].'/></td></tr>';
		print '	<tr><td> '.$this->getLang('gads_dont_count_admin').' </td><td><input type="checkbox" name="googleads[dontcountadmin]" '.$gads_settings['dontcountadmin'].'/></td></tr>';
		print '	<tr><td> '.$this->getLang('gads_dont_count_users').' </td><td><input type="checkbox" name="googleads[dontcountusers]" '.$gads_settings['dontcountusers'].'/></td></tr>';


		print ' <tr><td> '.$this->getLang('google_ad_client').' </td><td><input type="text" name="googleads[google_ad_client]" value="'.$gads_settings['google_ad_client'].'"/></td></tr>';
		print ' <tr><td> '.$this->getLang('google_color_border').' </td><td><input type="text" name="googleads[google_color_border]" value="'.$gads_settings['google_color_border'].'"/></td></tr>';

		print ' <tr><td> '.$this->getLang('google_color_bg').' </td><td><input type="text" name="googleads[google_color_bg]" value="'.$gads_settings['google_color_bg'].'"/></td></tr>';

		print ' <tr><td> '.$this->getLang('google_color_link').' </td><td><input type="text" name="googleads[google_color_link]" value="'.$gads_settings['google_color_link'].'"/></td></tr>';

		print ' <tr><td> '.$this->getLang('google_color_url').' </td><td><input type="text" name="googleads[google_color_url]" value="'.$gads_settings['google_color_url'].'"/></td></tr>';

		print ' <tr><td> '.$this->getLang('google_color_text').' </td><td><input type="text" name="googleads[google_color_text]" value="'.$gads_settings['google_color_text'].'"/></td></tr>';


		print '</table>';
		print '<br />';
		print '<p><input type="submit" value="'.$this->getLang('gads_save').'"></p></center>';
		print '</form>';
	}
}

