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 WikiParser_PluginOutput
9{
10	private $format;
11	private $data;
12
13	private function __construct($format, $data)
14	{
15		$this->format = $format;
16		$this->data = $data;
17	}
18
19	public static function wiki($text)
20	{
21		return new self('wiki', $text);
22	}
23
24	public static function html($html)
25	{
26		return new self('html', $html);
27	}
28
29	public static function internalError($message)
30	{
31		return self::error(tra('Internal error'), $message);
32	}
33
34	public static function userError($message)
35	{
36		return self::error(tra('User error'), $message);
37	}
38
39	public static function argumentError($missingArguments)
40	{
41		$content = tra('Plugin argument(s) missing:');
42
43		$content .= '<ul>';
44
45		foreach ($missingArguments as $arg) {
46			$content .= "<li>$arg</li>";
47		}
48
49		$content .= '</ul>';
50
51		return self::userError($content);
52	}
53
54	public static function error($label, $message)
55	{
56		$smarty = TikiLib::lib('smarty');
57		$smarty->loadPlugin('smarty_block_remarksbox');
58		$repeat = false;
59
60		return new self(
61			'html',
62			smarty_block_remarksbox(
63				[
64							'type' => 'error',
65							'title' => $label,
66						],
67				$message,
68				$smarty,
69				$repeat
70			)
71		);
72	}
73
74	public static function disabled($name, $preferences)
75	{
76		$content = tr('Plugin <strong>%0</strong> cannot be executed.', $name);
77
78		if (Perms::get()->admin) {
79			$smarty = TikiLib::lib('smarty');
80			$smarty->loadPlugin('smarty_function_preference');
81			$smarty->loadPlugin('smarty_modifier_escape');
82			$smarty->loadPlugin('smarty_function_ticket');
83			$content .= '<form method="post" action="tiki-admin.php">';
84			foreach ($preferences as $pref) {
85				$content .= smarty_function_preference(['name' => $pref], $smarty->getEmptyInternalTemplate());
86			}
87			$content .= smarty_function_ticket([], $smarty->getEmptyInternalTemplate());
88			$content .= '<input type="submit" class="btn btn-primary btn-sm" value="'
89				. smarty_modifier_escape(tra('Set')) . '">';
90			$content .= '</form>';
91		}
92		return self::error(tra('Plugin disabled'), $content);
93	}
94
95	function toWiki()
96	{
97		switch ($this->format) {
98			case 'wiki':
99				return $this->data;
100			case 'html':
101				return "~np~{$this->data}~/np~";
102		}
103	}
104
105	function toHtml($parseOptions = [])
106	{
107		switch ($this->format) {
108			case 'wiki':
109				return $this->parse($this->data, $parseOptions);
110			case 'html':
111				return $this->data;
112		}
113	}
114
115	private function parse($data, $parseOptions = [])
116	{
117		global $tikilib;
118
119		return TikiLib::lib('parser')->parse_data($data, $parseOptions);
120	}
121}
122