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_ValueFormatter_Wikiplugin extends Search_Formatter_ValueFormatter_Abstract
9{
10	private $arguments;
11
12	function __construct($arguments)
13	{
14		$this->arguments = $arguments;
15	}
16
17	function render($name, $value, array $entry)
18	{
19		if (substr($name, 0, 11) !== 'wikiplugin_') {
20			return $value;
21		} else {
22			$name = substr($name, 11);
23		}
24
25		if (isset($this->arguments['content'])) {
26			$content = $this->arguments['content'];
27
28			// replace args in the plugin body with values from the results
29			// look for %field_name% in the content and replace with $entry['field_name']
30			if ($rc = preg_match_all('/%(\w+)%/', $content, $matches)) {
31				for ($i = 0; $i < $rc; $i++) {
32					if (isset($entry[$matches[1][$i]])) {
33						$content = str_replace($matches[0][$i], $entry[$matches[1][$i]], $content);
34					}
35				}
36			}
37
38			unset($this->arguments['content']);
39		} else {
40			$content = '';
41		}
42		$defaults = [];
43		if (isset($this->arguments['default'])) {
44			parse_str($this->arguments['default'], $defaults);
45		}
46
47		$params = [];
48		foreach ($this->arguments as $key => $val) {
49			if (isset($entry[$val])) {
50				$params[$key] = $entry[$val];
51			} elseif (isset($defaults[$key])) {
52				$params[$key] = $defaults[$key];
53			} elseif ($key !== 'default') {
54				$params[$key] = $val;
55			}
56		}
57
58		$parserlib = TikiLib::lib('parser');
59		$out = $parserlib->plugin_execute(
60			$name,
61			$content,
62			$params,
63			0,
64			false,
65			[
66				'context_format' => 'html',
67				'ck_editor' => false,
68				'is_html' => 'y'
69			]
70		);
71
72		return '~np~' . $out . '~/np~';
73	}
74}
75