1<?php
2/**
3* pot Plugin: Re-usable user pot
4*
5* @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6* @author
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
14class syntax_plugin_pot extends DokuWiki_Syntax_Plugin {
15
16	/**
17	* What kind of syntax are we?
18	*/
19	function getType() {
20		return 'formatting';
21	}
22
23	/**
24	* Where to sort in?
25	*/
26	function getSort() {
27		return 499;
28	}
29	/*
30     * Paragraph Type
31     */
32	function getPType() {
33		return 'normal';
34	}
35
36	function getAllowedTypes() {
37		return array(
38			'disabled'
39		);
40	}
41
42	/**
43	* Connect pattern to lexer
44	*/
45	public function connectTo($mode) {
46		$this->Lexer->addEntryPattern('<[P|p][O|o][T|t]\b *[^>]*>(?=.*?<\/[P|p][O|o][T|t]>)', $mode, 'plugin_pot');
47	}
48
49	public function postConnect() {
50		$this->Lexer->addExitPattern('<\/[P|p][O|o][T|t]>', 'plugin_pot');
51	}
52
53	private $cargo = array(); //set class property to set var https://forum.dokuwiki.org/d/14219-persistent-tag-attributes-solved
54
55	/**
56	* Handle the match
57	*/
58	public function handle($match, $state, $pos, Doku_Handler $handler) {
59		global $ID;
60		// pot helper
61		$pothelper = plugin_load('helper', 'pot');
62
63		switch ($state) {
64			case DOKU_LEXER_ENTER :
65				$try = $match;
66				// lets try, and do some changes inside the unmatched input
67				$try = preg_replace('/(\<POT\b\s*)(.*?)\s*?(\>)/', '$1 $2 | type=text $3', $try);// if we have "<POT ... >" we ad a | type=text
68				$try = preg_replace('/~\s*([a-zA-Z0-9]+)\s*|\|/', '|defaultsettings=$1|', $try); // replace a '@value' to '|defaultsettings=value|'
69				$try = preg_replace('/!/', '|float=right|', $try);// replace a '!' to '|float=right|'
70				$try = preg_replace('/#\s*([a-zA-Z0-9]+)(\s*|\|)/', '|potid=$1_'.uniqid().'|', $try); // replace a '#value' to '|potid=value_stamp|'
71				$try = preg_replace('/@\s*([a-zA-Z0-9]+)(\s*|\|)/', '|potid=$1|', $try); // replace a '@value' to '|potid=value|'
72
73				preg_match('/\<[P|p][O|o][T|t]\b\s*(.*?)\s*?\>/', $try, $potset); // get the info
74				$potsettings = $potset[1];
75				//print("<pre enter:>".print_r($potsettings, true)."</pre>");
76				include 'source/format.php'; // the long staff is written in an other file
77
78				break;
79			case DOKU_LEXER_MATCHED :
80				break;
81			case DOKU_LEXER_UNMATCHED :
82				$potid = $this->cargo['pot'];
83				if (trim($this->cargo[$potid]['settings']['type']) == 'text') {
84					include 'source/text.php';
85				} else {
86					include 'source/calculate.php';
87				}
88				break;
89			case DOKU_LEXER_EXIT :
90				break;
91			case DOKU_LEXER_SPECIAL :
92				break;
93		}
94		$mycargo = $this->cargo; //fetch global var
95
96		return array($state, $pot, $mycargo);
97	}
98
99	/**
100	* Create output
101	*/
102	public function render($mode, Doku_Renderer $renderer, $data) {
103		if ($mode !== 'xhtml') {
104			return false;
105		}
106
107		global $INFO;
108		$renderer->info['cache'] = false;
109		list($state, $pot, $mycargo) = $data;
110
111
112		switch ($state) {
113			case DOKU_LEXER_ENTER :
114				$potid = $mycargo['pot'];
115				$float = $mycargo[$potid]['settings']['float'];
116				$display = $mycargo[$potid]['settings']['display'];
117
118				$result = $mycargo[$potid]['output']['result'];
119				if ($result <0) {
120					$color = $mycargo[$potid]['settings']['color-'];
121				} else {
122					$color = $mycargo[$potid]['settings']['color+'];
123				}
124				$renderer->doc .= "<span class='pot' id='pot_$potid'  style='$display $float $color'>";
125				break;
126			case DOKU_LEXER_MATCHED :
127				break;
128			case DOKU_LEXER_UNMATCHED :
129				$potid = $mycargo['pot'];
130				$width = $mycargo[$potid]['settings']['width'];
131				$decimals = $mycargo[$potid]['settings']['decimals'];
132				$sepdec = $mycargo[$potid]['settings']['sepdec'];
133				$sepmil = $mycargo[$potid]['settings']['sepmil'];
134				$display = '';
135				$formulaset = $mycargo[$potid]['settings']['formula'];
136				$formula = $mycargo[$potid]['output']['formula'];
137				$result = $mycargo[$potid]['output']['result'];
138				$wherecurrency = strpos($mycargo[$potid]['settings']['currency'],'_');
139				$currency = preg_replace('/_/','',$mycargo[$potid]['settings']['currency']);
140				$type = $mycargo[$potid]['output']['type'];
141				if ($type == 'formula') {
142					$result = number_format($result,$decimals,$sepdec,$sepmil);
143				} else {
144					$result = $result;
145				}
146				$error = $mycargo[$potid]['output']['error'];
147				$formula = $mycargo[$potid]['output']['formula'];
148				//print("<pre>".print_r($mycargo, true)."</pre>");
149
150				if ($wherecurrency == 0) {
151					$renderer->doc .= "<span class='pot_error' >".$currency."&nbsp;</span>";
152				}
153
154				if ($formulaset == 'true') {
155					$renderer->doc .= $formula."&nbsp;=&nbsp;";
156					$renderer->doc .= "<span class='pot_result' id='$potid' style='$width text-align:right; '>";
157					$renderer->doc .= $result;
158					$renderer->doc .= "</span>";
159				}
160				else {
161					$renderer->doc .= "<span class='pot_result' id='$potid' style='$width  text-align:right; '>";
162					$renderer->doc .= $result;
163					$renderer->doc .= "</span>";
164				}
165				if ($wherecurrency != 0) {
166					$renderer->doc .= "<span class='pot_error' >&nbsp;".$currency."</span>";
167				}
168				if (isset($error)) {
169					$renderer->doc .= "<span class='pot_error style='$display'>&nbsp; $error <b><i>$formula</i></b></span>";
170				}
171				break;
172			case DOKU_LEXER_EXIT :
173				$renderer->doc .= "</span>";
174				break;
175			case DOKU_LEXER_SPECIAL :
176				break;
177		}
178		return true;
179	}
180}