1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Search_Formatter_Plugin_WikiTemplate implements Search_Formatter_Plugin_Interface
9{
10	private $template;
11	private $format;
12
13	function __construct($template)
14	{
15		$this->template = WikiParser_PluginMatcher::match($template);
16		$this->format = self::FORMAT_WIKI;
17	}
18
19	function setRaw($isRaw)
20	{
21		$this->format = $isRaw ? self::FORMAT_HTML : self::FORMAT_WIKI;
22	}
23
24	function getFormat()
25	{
26		return $this->format;
27	}
28
29	function getFields()
30	{
31		$parser = new WikiParser_PluginArgumentParser;
32
33		$fields = [];
34		foreach ($this->template as $match) {
35			$name = $match->getName();
36
37			if ($name === 'display') {
38				$arguments = $parser->parse($match->getArguments());
39
40				if (isset($arguments['name']) && ! isset($fields[$arguments['name']])) {
41					$fields[$arguments['name']] = isset($arguments['default']) ? $arguments['default'] : null;
42				}
43			}
44		}
45
46		return $fields;
47	}
48
49	function prepareEntry($valueFormatter)
50	{
51		$matches = clone $this->template;
52
53		foreach ($matches as $match) {
54			$name = $match->getName();
55
56			if ($name === 'display') {
57				$match->replaceWith((string) $this->processDisplay($valueFormatter, $match->getBody(), $match->getArguments()));
58			} else if ($name === 'calc') {
59				$match->replaceWith((string) $this->processCalc($valueFormatter, $match));
60			}
61		}
62
63		return $matches->getText();
64	}
65
66	function renderEntries(Search_ResultSet $entries)
67	{
68		$out = '';
69		foreach ($entries as $entry) {
70			$out .= $entry;
71		}
72		return $out;
73	}
74
75	private function processDisplay($valueFormatter, $body, $arguments)
76	{
77		$parser = new WikiParser_PluginArgumentParser;
78		$arguments = $parser->parse($arguments);
79
80		$name = $arguments['name'];
81
82		if (isset($arguments['format'])) {
83			$format = $arguments['format'];
84		} else {
85			$format = 'plain';
86		}
87
88		unset($arguments['format']);
89		unset($arguments['name']);
90		return $valueFormatter->$format($name, $arguments);
91	}
92
93	/**
94	 * Process {calc} plugins
95	 *
96	 * @param \Search_Formatter_ValueFormatter $valueFormatter
97	 * @param \WikiParser_PluginMatcher_Match $match
98	 *
99	 * @return mixed
100	 */
101	private function processCalc($valueFormatter, $match) {
102		$runner = new Math_Formula_Runner(
103			[
104				'Math_Formula_Function_' => '',
105				'Tiki_Formula_Function_' => '',
106			]
107		);
108		try {
109			$runner->setFormula($match->getBody());
110			$runner->setVariables($valueFormatter->getPlainValues());
111			$value = $runner->evaluate();
112		} catch (Math_Formula_Exception $e) {
113			$value = tr('Error evaluating formula %0: %1', $match->getBody(), $e->getMessage());
114		}
115		return $value;
116	}
117}
118