<?php
/**
* @version 1.0g
* @package MetaMod
* @copyright Copyright (C) 2007-2008 Stephen Brandon. All rights reserved.
* @license
*/

// ensure this file is being included by a parent file
defined( '_VALID_MOS' ) or die( 'Restricted access' );

class MetaModParameters extends mosParameters {
	
	/**
	* @param string The name of the form element
	* @param string The value of the element
	* @param object The xml element for the parameter
	* @param string The control name
	* @return string The html for the element
	*/
	function _form_modulelist( $name, $value, &$node, $control_name ) {
		global $database;

		$query = "SELECT id, title, module, position"
		. "\n FROM #__modules"
		. "\n WHERE published = 1"
		. "\n AND client_id != 1"
		. "\n ORDER BY title"
		;
		$database->setQuery( $query );
		$options = $database->loadObjectList();
		
		$r = '<style>
			table.modulelist td {padding:0.1em 0.5em; margin:0.1em; height:auto;}
			table.modulelist td.modid {text-align:right;}
			table.modulelist th.modid {text-align:right;}
			</style>
		<table class="modulelist" cellpadding="0" cellspacing="0" border="0">
		<tr><th>name</th><th class="modid">id</th><th>type</th><th>position</th></tr>';
		foreach ($options as $o) {
			$r .= "<tr>\n";
			$r .= "<td>".$o->title.'</td><td class="modid"><b>'.$o->id.'</b></td><td>'.$o->module.'</td><td>'.$o->position.'</td>';
			$r .= "</tr>\n";
		}
		$r .= "</table>";
		return $r;
	}

	function _form_help( $name, $value, &$node, $control_name ) {
		return 'Create some PHP code to determine which module should be used. Once the script has decided which module or modules to display, return the module id, a comma-separated list of module ids as a string, or an array of module ids. For more help and recipes, see the <a href="http://www.brandonitconsulting.co.uk/mod_metamod/" target="_blank">MetaMod home page</a>. e.g.
<pre>if (time() >= strtotime("1 Oct 2007") && time() < strtotime("30 Oct 2007")) return 74;
if (time() < strtotime("1 Nov 2008")) return 75;
if ($fromCountryId == "US") return 55;
if ($fromCountryId == "GB") return "55,56,57";
if ($fromCountryId == "NL") return array(58,59,73);
if ($fromCountryName == "New Zealand") return 21;</pre>
<p>You have access to the following PHP variables:
	<ul><li><b>$fromCountryId</b> - the upper-case 2-letter ISO country code (e.g. GB, US, FR, DE)</li>
	<li><b>$fromCountryName</b> - the official ISO country name</li>
	<li><b>$geoip</b> - if you have enabled GeoLiteCity or GeoIPCity, a record containing the following items:</li>
	<ul>
	<li><b>$geoip-&gt;country_name</b> - full country name, as above</li>
	<li><b>$geoip-&gt;country_code</b> - 2-letter ISO country code, as above</li>
	<li><b>$geoip-&gt;country_code3</b> - 3-letter ISO country code (e.g. GBR, USA, FRA, DEU)</li>
	<li><b>$geoip-&gt;region</b> - 2-letter code. For US/Canada, <a href="http://www.maxmind.com/app/iso3166_2" target="_blank">ISO-3166-2</a> code for the state/province name, e.g. "GA" (Georgia, USA). Outside of the US and Canada, <a href="http://www.maxmind.com/app/fips10_4" target="_blank">FIPS 10-4</a> code., e.g. "M9" (Staffordshire, UK)</li>
	<li><b>$geoip-&gt;city</b> - full city name</li>
	<li><b>$geoip-&gt;postal_code</b> - For US, Zipcodes, for Canada, postal codes. Available for about 56% of US GeoIP Records. <a href="http://www.maxmind.com/app/faq#postalcode" target="_blank">More info</a>.</li>
	<li><b>$geoip-&gt;latitude</b></li>
	<li><b>$geoip-&gt;longitude</b></li>
	<li><b>$geoip-&gt;dma_code</b> - 3-digit <a href="http://www.maxmind.com/app/metro_code" target="_blank">DMA/Metro code</a> (US only)</li>
	<li><b>$geoip-&gt;area_code</b> - 3-digit telephone prefix (US Only)</li>
	</ul>
	<li><b>$Itemid</b> - the Itemid of the main component on the page</li>
	<li><b>$option</b> - the option of the main component on the page (e.g. com_content)</li>
	<li><b>$task</b> - the task of the main component on the page (e.g. "view" or "category")</li>
	<li><b>$id</b> - the id of the item in the main component on the page (e.g. "24:content-layouts")</li>
	<li><b>$database</b> - in case you want to query the database for anything (for experts!)</li>
	<li><b>$language</b> - a lower-case language code. By default this returns the default language of the web visitor&rsquo;s browser, but can alternatively return the language code of the Joomla front-end, or intelligently find the best match between a user&rsquo;s browser languages and a list of languages that you provide. Typical language strings returned include: en, en-gb, en-us, fr, de and <a href="http://www.brandonitconsulting.co.uk/mod_metamod/language-codes/">many others</a>.</li>
	<li><b>$language_code</b> - the 2-letter language code without region (lower case) e.g. "en"</li>
	<li><b>$language_region</b> - if it exists, the 2-letter region code (lower case). e.g. if $language == "en-us", $language_code == "en" and $language_region == "us". Having them in separate variables like this makes it easier to put into MetaMod rules.</li>
	<li><b>$my</b> - information about the user, if they are logged in. e.g.</li>
	<ul>
	<li><b>$my-&gt;id</b></li>
	<li><b>$my-&gt;name</b></li>
	<li><b>$my-&gt;username</b></li>
	<li><b>$my-&gt;email</b></li>
	<li><b>$my-&gt;usertype</b> e.g. "" or "Public Frontend"=not logged in (test for both), otherwise "Registered", "Author", "Editor", "Publisher", "Manager", "Administrator" or "Super Administrator"</li>
	<li><b>$my-&gt;gid</b> (group id: 0=Public, 1=Registered, 2=Special)</li>
	<li><b>$user-&gt;registerDate</b> e.g. &quot;2007-05-17
          01:25:52&quot;</li>
	<li><b>$user-&gt;lastvisitDate</b> e.g. &quot;2007-11-02
              18:51:29&quot;</li>
	</ul>
	
	</ul>
	<b>Note:</b> <b>$fromCountryName</b> and <b>$fromCountryId</b> will only be available if you have one of the "Enable GeoIP" options selected above, and if you have one of the GeoLite Country, GeoIP Country, GeoLiteCity or GeoIPCity databases installed (see <a href="http://www.maxmind.com/app/geolitecountry" target="_blank">Maxmind</a>, <a href="http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz">direct GeoLite Country download</a>, or <a href="http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz">direct GeoLite City download</a>)';
	}

	function _form_geoipcheck( $name, $value, &$node, $control_name ) {
		global $mosConfig_absolute_path;
		$files = $this->geoIPFolders();
		foreach ($files as $file) {
			$country = file_exists($mosConfig_absolute_path.$file.'/GeoIP.dat');
			$litecity = file_exists($mosConfig_absolute_path.$file.'/GeoLiteCity.dat');
			$city = file_exists($mosConfig_absolute_path.$file.'/GeoIPCity.dat');
			$messages = array();
			
			if ($country) {
				$age = intval((time() - filemtime($mosConfig_absolute_path.$file.'/GeoIP.dat'))/(24*60*60));
				if ($age > 30) $age = "<span style='color:red;'>File is $age days old. Please update your database from <a href='http://www.maxmind.com/app/ip-location' target='_blank'>MaxMind</a>.</span>";
				else $age = "";
				$messages[] = $file."/GeoIP.dat found. All GeoIP Country features enabled. $age";
			}
			if ($litecity) {
				$age = intval((time() - filemtime($mosConfig_absolute_path.$file.'/GeoLiteCity.dat'))/(24*60*60));
				if ($age > 30) $age = "<span style='color:red;'>File is $age days old. Please update your database from <a href='http://www.maxmind.com/app/ip-location' target='_blank'>MaxMind</a>.</span>";
				else $age = "";
				$messages[] = $file."/GeoLiteCity.dat found. All GeoIP City/region features enabled. $age";
			}
			if ($city) {
				$age = intval((time() - filemtime($mosConfig_absolute_path.$file.'/GeoIPCity.dat'))/(24*60*60));
				if ($age > 30) $age = "<span style='color:red;'>File is $age days old. Please update your database from <a href='http://www.maxmind.com/app/ip-location' target='_blank'>MaxMind</a>.</span>";
				else $age = "";
				$messages[] = $file."/GeoIPCity.dat found. All GeoIP City/region features enabled. $age";
			}
			
			if ($country || $litecity || $city) {
				return "<b>".implode("<br/>",$messages).'</b>
				<br />Keep your GeoIP databases up to date from
				<a href="http://www.maxmind.com/app/ip-location" target="_blank">MaxMind</a>';
			}
			
		}
		return '<b>I couldn\'t find any GeoIP database at <i>jooma_root</i>/geoip/GeoIP.dat or GeoFreeCity.dat or GeoIPCity.dat.</b><br />
			If you want to use the GeoIP Country features, please obtain the GeoLite Country or GeoIP Country database
			at <a href="http://www.maxmind.com/app/geolitecountry" target="_blank">MaxMind</a>
			(<a href="http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz">direct download</a>),
			uncompress it, and install it as <i>jooma_root</i>/geoip/GeoIP.dat. For full City and 
			location features, please obtain the GeoLite City or GeoIP City database
			at <a href="http://www.maxmind.com/app/geolitecity" target="_blank">MaxMind</a>,
			uncompress it, and install it as <i>jooma_root</i>/geoip/GeoLiteCity.dat';
	}

	function geoIPFolders() {
		return array(
			"/geoip",
			"/GeoIP",
			"/geoIP",
			"/GEOIP",
			"/GEO IP",
			"/",
			"/geo_ip",
			"/geo_IP",
			"/Geo_IP"
			);
	}
	
}
