1<?php
2
3
4/**
5* calculate the input
6* replace the string in: { ?/@/#/! string }  with the value of one or more potids value
7*/
8
9$potformula = preg_replace("/^\s*?$/", "\" \"", trim($match)); //if only spaces are set, replace space with " " to avoid calc errors
10//$potformula = preg_replace('/<|>/', '', $potformula); //replace tags like <div>  to _div_ </div>
11$potformula = htmlspecialchars($potformula);
12$potformula = preg_replace("/,/", ".", $potformula); //replace , with . in case user writes a comma instead of a dot
13
14$pattern = '/\s*\{{1}\s*([\?@!#&)]?)\s*(.*?)\s*\}{1}\s*/';
15$complete = preg_replace_callback($pattern, function ($stew) {
16	$potid = $this->cargo['pot'];
17	$all = $stew[0];
18	$spice = $stew[1];
19	$searchpotid = $stew[2];
20	$completeformula = '';
21
22	switch ($spice) {
23		case '@': // get the result of exactly one potid
24			$thepotid = $this->cargo[$searchpotid]['output']['result'];
25			if (isset($thepotid)) {
26				$completeformula .= "( ".$this->cargo[$searchpotid]['output']['formula']." )";
27			} else {
28				$completeformula .= " 'not set: {@$searchpotid}' ";
29				$this->cargo[$potid]['output']['error'] = " error:";
30			}
31			break;
32		case '#': //sum values with matching string in input "{# string}"
33		$pattern = "/.*($searchpotid).*/x";
34
35			$forkey = '';
36			foreach ($this->cargo as $key => $value) {
37				preg_match($pattern, $key, $match); //.*(gut).*
38				if ($key == $match[0]) {
39					if (isset($value['output']['result'])) {
40						$forkey .= " + ".$value['output']['result'];
41					}
42				}
43			}
44			$completeformula .= "( " . ltrim($forkey, '+ ') . " )";
45
46			break;
47		case '&': // sum results matching the pot id of the asking potid
48			preg_match('/^(.\*?)(\d\*?)$/', $this->cargo['pot'], $potidarr); //fetch potid of this potid
49			$searchval = $potidarr[1]; // only first part, no mumber
50			$sumarr = array();
51			foreach ($this->cargo as $key => $value) {
52				preg_match("/.*($searchval).*/", $key, $match);
53
54				if ($key == $match[0]) {
55					if (isset($value['output']['result'])) {
56						$forkey .= " + ".$value['output']['result'];
57					}
58				}
59			}
60			$completeformula .= "( " . ltrim($forkey, '+ '). " )";
61			break;
62		default:
63			$completeformula .= $all;
64			break;
65	}
66
67	return $completeformula;
68
69}, $potformula);
70
71$mdec = $this->cargo[$potid]['settings']['decimals'];
72$calculated = $pothelper->_calculate($complete, $mdec);
73
74
75$this->cargo[$potid]['output']['result'] = $calculated['result']; // set the var to use it global
76$this->cargo[$potid]['output']['formula'] = $calculated['formula'];
77$this->cargo[$potid]['output']['type'] = $calculated['type'];
78if ($error != '') {
79	$this->cargo[$potid]['output']['error'] = $calculated['error'];
80}