1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     i-net software <tools@inetsoftware.de>
5 * @author  Gerry Weissbach <gweissbach@inetsoftware.de>
6 */
7
8// must be run within Dokuwiki
9if(!defined('DOKU_INC')) die();
10
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'syntax.php');
13
14/**
15 * All DokuWiki plugins to extend the parser/rendering mechanism
16 * need to inherit from this class
17 */
18class syntax_plugin_translator_label extends DokuWiki_Syntax_Plugin {
19
20	var $functions = null;
21	var $category = null;
22	var $version = null;
23	var $XML = null;
24
25	/**
26	 * return some info
27	 */
28	function getInfo(){
29		return array_merge(confToHash(dirname(__FILE__).'/../info.txt'), array(
30				'name' => 'Property Exchanger (Syntax Component)',
31		));
32	}
33
34	function getType(){ return 'substition'; }
35	function getPType(){ return 'block'; }
36	function getSort(){ return 110; }
37
38
39
40	/**
41	 * Connect pattern to lexer
42	 */
43	function connectTo($mode){
44		$this->Lexer->addSpecialPattern('\$\$label\(.*?\)\$\$',$mode,'plugin_translator_label');
45	}
46	/**
47	 * Handle the match
48	 */
49	function handle($match, $state, $pos, &$handler){
50
51		return substr($match, 7, -3);
52	}
53
54	/**
55	 *  Render output
56	 */
57	function render($mode, &$renderer, $data) {
58		global $ID;
59
60		if ($mode == 'xhtml') {
61
62			$renderer->nocache();
63
64			$this->version = $this->getConf('version');
65			$this->category = $this->getConf('category');
66			$renderer->doc .= $this->__getLabelFor($data);
67
68			return true;
69		}
70		return true;
71	}
72
73	function __labelCallBackReplace(&$DATA) {
74		$DATA = preg_replace_callback('/\$\$label\((.*?)\)\$\$/i', array($this, '__getLabelFor'), $DATA);
75	}
76
77	function __getLabelFor($value) {
78		global $conf;
79
80		// If this is a callback, the value is the first column
81		if ( is_array($value) ) {
82			$value = $value[1];
83		}
84
85		$return = $value;
86
87		// Instantiate the helper
88		if ( empty($this->functions) && !$this->functions =& plugin_load('helper', 'translator') ) {
89			return $this->lang['helpermissing'];
90		}
91
92		// Connect to the Database and load Languages
93		$this->functions->init_database();
94		$EXECUTE = array($value);
95		$SQL = "SELECT Value FROM tblMasterKey INNER JOIN tblMaster ON tblMaster.KeyID=tblMasterKey.KeyID WHERE ResourceKey=?";
96
97		if ( !empty( $this->category) ) {
98			// Check If Categroy is Numeric, otherwise get the ID from the Database
99			if ( !is_numeric($this->category) ) {
100				$this->category = $this->functions->_getCategoryFromName($this->category);
101
102				// Everything OK with the ID? than do a query with it
103				if ( $this->category === false || !is_array($this->category) ) {
104					return $return;
105				}
106
107				list($this->category, $fileName) = $this->category;
108			}
109
110			$SQL .= " AND CategoryID=?";
111			$EXECUTE[] = $this->category;
112		}
113
114		if ( !empty( $this->version) ) {
115			$SQL .= " AND Version=?";
116			$EXECUTE[] = $this->version;
117		} else {
118			$SQL .= " Order By Version";
119		}
120
121		if ( $conf['lang'] != $this->getConf('default_language') ) {
122			$SQL = "SELECT tblTranslation.Value FROM tblMaterKey LEFT JOIN
123					( tblTranslation INNER JOIN (SELECT KeyID, MAX(Date) AS Date FROM tblTranslation WHERE Lang=? GROUP BY KeyID) as tblOldestTrans
124					tblOldestTrans.KeyID=tblTranslation.KeyID AND tblOldestTrans.Date=tblTranslation.Date)
125					ON tblMasterKey.KeyID=tblTranslation.KeyID WHERE RessourceKey=?";
126			array_unshift($EXECUTE, $conf['lang']);
127		}
128
129		$this->functions->database->prepare("$SQL;");
130		$this->functions->database->execute($EXECUTE);
131
132		if ( $this->functions->database->num_rows() > 0 ) {
133			$data = array(); $this->functions->database->bind_assoc($data); $this->functions->database->fetch();
134			$return = $data['Value'];
135			$this->__labelCallBackReplace($return);
136		}
137
138		return stripslashes($return);
139	}
140}
141