<?php
/**
 * Prolog plug-in : Rule-based System for Groupware.
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Paweł Kupka <pawel.kupka@gmail.com>
 */
require_once('prolog_tag.php');

class AttributeOptions extends PrologTag {

	var $name = 'options';
	var $allowedOptions = array('debug', 'files', 'source');
	var $authorizedOption = null;

	/**
	 * Constructor
	 */
	function AttributeOptions() 
	{
			$this->setAttributePattern($this->name);
	}
	
	/**
	 * Reads the option from the options attribute
	 * @param string $optionsValue the value from attribute options
	 * @return array $options array with options
	 */
	function getOptions($optionsValue) 
	{
		$options = array();
		$exValues = explode(',', $optionsValue);
		foreach($exValues as $option) 
		{
			$option = trim($option);
			if(!empty($option))
				array_push($options, $option);
		}
		return $options;
	}
	
	/**
	 * Checks the option permission to be operate
	 * @param string $option
	 * @return bool
	 */
	function isAuthorizedOption($option) 
	{
		if($this->authorizedOption == $option)
			return true;
		else
			return false;
	}
	
	/**
	 * Gives permission to the option to be operate
	 * @param string $option
	 */
	function authorizeOption($option) 
	{
		if($this->isAllowedOption($option))
			$this->authorizedOption = $option;
		else
			$this->authorizedOption = null;
	}
	
	/**
	 * Checks if a particular option is allowed
	 * @param string $option
	 * @return bool
	 */
	function isAllowedOption($option) 
	{
		if(in_array($option, $this->allowedOptions))
			return true;
		else
			return false;
	}
	
	/**
	 * Handles the "files" option
	 * @param array $files files paths list
	 * @return string $filesList formatted files list to display
	 */
	function handleOptionFiles($files = array()) 
	{
		if(!$this->isAuthorizedOption('files'))
			return;

		$filesList = '<pre><u>Prolog plug-in</u> - option <i><b>files</b></i><br /><b>Included files:</b><br />';
		if(count($files))
			foreach($files as $file)
				$filesList .= $file.'<br />';
		$filesList .= wikiFN(getID()).'<br />';
		$filesList .= '</pre>';
		return $filesList;
	}
	
	/**
	 * Handles the "source" option
	 * @param string $sourceCode prolog source code
	 * @return string $source formatted prolog source code to display
	 */
	function handleOptionSource($sourceCode = null) 
	{
		if(!$this->isAuthorizedOption('source'))
			return;

		$source = '<pre><u>Prolog plug-in</u> - option <i><b>source</b></i><br /><b>Source code:</b><br />';
		$source .= $sourceCode;
		$source .= '</pre>';
		return $source;
	}
	
	/**
	 * Handles the "debug" option
	 * @param string $errors output from errors pipe from prolog interpreter
	 * @return string $debug formatted prolog errors pipe content to display
	 */
	function handleOptionDebug($errors = null) 
	{
		if(!$this->isAuthorizedOption('debug'))
			return;
					
		$debug = '<pre><u>Prolog plug-in</u> - option <i><b>debug</b></i><br /><b>Compilation results:</b><br />';
		$debug .= $errors;
		$debug .= '</pre>';
		return $debug;
	}
}
?>