<?php
/**
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     i-net software <tools@inetsoftware.de>
 * @author  Gerry Weissbach <gweissbach@inetsoftware.de>
 */

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');

/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_translator_label extends DokuWiki_Syntax_Plugin {

	var $functions = null;
	var $category = null;
	var $version = null;
	var $XML = null;

	/**
	 * return some info
	 */
	function getInfo(){
		return array_merge(confToHash(dirname(__FILE__).'/../info.txt'), array(
				'name' => 'Property Exchanger (Syntax Component)',
		));
	}

	function getType(){ return 'substition'; }
	function getPType(){ return 'block'; }
	function getSort(){ return 110; }



	/**
	 * Connect pattern to lexer
	 */
	function connectTo($mode){
		$this->Lexer->addSpecialPattern('\$\$label\(.*?\)\$\$',$mode,'plugin_translator_label');
	}
	/**
	 * Handle the match
	 */
	function handle($match, $state, $pos, &$handler){

		return substr($match, 7, -3);
	}

	/**
	 *  Render output
	 */
	function render($mode, &$renderer, $data) {
		global $ID;

		if ($mode == 'xhtml') {

			$renderer->nocache();

			$this->version = $this->getConf('version');
			$this->category = $this->getConf('category');
			$renderer->doc .= $this->__getLabelFor($data);

			return true;
		}
		return true;
	}

	function __labelCallBackReplace(&$DATA) {
		$DATA = preg_replace_callback('/\$\$label\((.*?)\)\$\$/i', array($this, '__getLabelFor'), $DATA);
	}

	function __getLabelFor($value) {
		global $conf;

		// If this is a callback, the value is the first column
		if ( is_array($value) ) {
			$value = $value[1];
		}

		$return = $value;

		// Instantiate the helper
		if ( empty($this->functions) && !$this->functions =& plugin_load('helper', 'translator') ) {
			return $this->lang['helpermissing'];
		}

		// Connect to the Database and load Languages
		$this->functions->init_database();
		$EXECUTE = array($value);
		$SQL = "SELECT Value FROM tblMasterKey INNER JOIN tblMaster ON tblMaster.KeyID=tblMasterKey.KeyID WHERE ResourceKey=?";

		if ( !empty( $this->category) ) {
			// Check If Categroy is Numeric, otherwise get the ID from the Database
			if ( !is_numeric($this->category) ) {
				$this->category = $this->functions->_getCategoryFromName($this->category);

				// Everything OK with the ID? than do a query with it
				if ( $this->category === false || !is_array($this->category) ) {
					return $return;
				}

				list($this->category, $fileName) = $this->category;
			}

			$SQL .= " AND CategoryID=?";
			$EXECUTE[] = $this->category;
		}

		if ( !empty( $this->version) ) {
			$SQL .= " AND Version=?";
			$EXECUTE[] = $this->version;
		} else {
			$SQL .= " Order By Version";
		}

		if ( $conf['lang'] != $this->getConf('default_language') ) {
			$SQL = "SELECT tblTranslation.Value FROM tblMaterKey LEFT JOIN
					( tblTranslation INNER JOIN (SELECT KeyID, MAX(Date) AS Date FROM tblTranslation WHERE Lang=? GROUP BY KeyID) as tblOldestTrans
					tblOldestTrans.KeyID=tblTranslation.KeyID AND tblOldestTrans.Date=tblTranslation.Date)
					ON tblMasterKey.KeyID=tblTranslation.KeyID WHERE RessourceKey=?"; 
			array_unshift($EXECUTE, $conf['lang']);
		}

		$this->functions->database->prepare("$SQL;");
		$this->functions->database->execute($EXECUTE);

		if ( $this->functions->database->num_rows() > 0 ) {
			$data = array(); $this->functions->database->bind_assoc($data); $this->functions->database->fetch();
			$return = $data['Value'];
			$this->__labelCallBackReplace($return);
		}

		return stripslashes($return);
	}
}
