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				$type = $mycargo[$potid]['output']['type'];
139				if ($type == 'formula') {
140					$result = number_format($result,$decimals,$sepdec,$sepmil);
141				} else {
142					$result = $result;
143				}
144				$error = $mycargo[$potid]['output']['error'];
145				$formula = $mycargo[$potid]['output']['formula'];
146				//print("<pre>".print_r($mycargo, true)."</pre>");
147				if ($formulaset == 'true') {
148					$renderer->doc .= $formula."&nbsp;=&nbsp;";
149					$renderer->doc .= "<span class='pot_result' id='$potid' style='$width text-align:right; '>";
150					$renderer->doc .= $result;
151					$renderer->doc .= "</span>";
152				}
153				else {
154					$renderer->doc .= "<span class='pot_result' id='$potid' style='$width  text-align:right; '>";
155					$renderer->doc .= $result;
156					$renderer->doc .= "</span>";
157				}
158				$renderer->doc .= "<span class='pot_error' >&nbsp;".$mycargo[$potid]['settings']['currency']."</span>";
159				if (isset($error)) {
160					$renderer->doc .= "<span class='pot_error style='$display'>&nbsp; $error <b><i>$formula</i></b></span>";
161				}
162				break;
163			case DOKU_LEXER_EXIT :
164				$renderer->doc .= "</span>";
165				break;
166			case DOKU_LEXER_SPECIAL :
167				break;
168		}
169		return true;
170	}
171}