1<?php
2/**
3 * Prolog plug-in : Rule-based System for Groupware.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Paweł Kupka <pawel.kupka@gmail.com>
7 */
8require_once('prolog_tag.php');
9
10class AttributeOptions extends PrologTag {
11
12	var $name = 'options';
13	var $allowedOptions = array('debug', 'files', 'source');
14	var $authorizedOption = null;
15
16	/**
17	 * Constructor
18	 */
19	function AttributeOptions()
20	{
21			$this->setAttributePattern($this->name);
22	}
23
24	/**
25	 * Reads the option from the options attribute
26	 * @param string $optionsValue the value from attribute options
27	 * @return array $options array with options
28	 */
29	function getOptions($optionsValue)
30	{
31		$options = array();
32		$exValues = explode(',', $optionsValue);
33		foreach($exValues as $option)
34		{
35			$option = trim($option);
36			if(!empty($option))
37				array_push($options, $option);
38		}
39		return $options;
40	}
41
42	/**
43	 * Checks the option permission to be operate
44	 * @param string $option
45	 * @return bool
46	 */
47	function isAuthorizedOption($option)
48	{
49		if($this->authorizedOption == $option)
50			return true;
51		else
52			return false;
53	}
54
55	/**
56	 * Gives permission to the option to be operate
57	 * @param string $option
58	 */
59	function authorizeOption($option)
60	{
61		if($this->isAllowedOption($option))
62			$this->authorizedOption = $option;
63		else
64			$this->authorizedOption = null;
65	}
66
67	/**
68	 * Checks if a particular option is allowed
69	 * @param string $option
70	 * @return bool
71	 */
72	function isAllowedOption($option)
73	{
74		if(in_array($option, $this->allowedOptions))
75			return true;
76		else
77			return false;
78	}
79
80	/**
81	 * Handles the "files" option
82	 * @param array $files files paths list
83	 * @return string $filesList formatted files list to display
84	 */
85	function handleOptionFiles($files = array())
86	{
87		if(!$this->isAuthorizedOption('files'))
88			return;
89
90		$filesList = '<pre><u>Prolog plug-in</u> - option <i><b>files</b></i><br /><b>Included files:</b><br />';
91		if(count($files))
92			foreach($files as $file)
93				$filesList .= $file.'<br />';
94		$filesList .= wikiFN(getID()).'<br />';
95		$filesList .= '</pre>';
96		return $filesList;
97	}
98
99	/**
100	 * Handles the "source" option
101	 * @param string $sourceCode prolog source code
102	 * @return string $source formatted prolog source code to display
103	 */
104	function handleOptionSource($sourceCode = null)
105	{
106		if(!$this->isAuthorizedOption('source'))
107			return;
108
109		$source = '<pre><u>Prolog plug-in</u> - option <i><b>source</b></i><br /><b>Source code:</b><br />';
110		$source .= $sourceCode;
111		$source .= '</pre>';
112		return $source;
113	}
114
115	/**
116	 * Handles the "debug" option
117	 * @param string $errors output from errors pipe from prolog interpreter
118	 * @return string $debug formatted prolog errors pipe content to display
119	 */
120	function handleOptionDebug($errors = null)
121	{
122		if(!$this->isAuthorizedOption('debug'))
123			return;
124
125		$debug = '<pre><u>Prolog plug-in</u> - option <i><b>debug</b></i><br /><b>Compilation results:</b><br />';
126		$debug .= $errors;
127		$debug .= '</pre>';
128		return $debug;
129	}
130}
131?>