Location: PHPKode > projects > Aukyla Platform > base/ConfigEditor.php
<?php
/*
     ConfigEditor.php, functions for editing configuration options
     Copyright (C) 2003-2005 Arend van Beelen, Auton Rijnsburg

     This program is free software; you can redistribute it and/or modify it
     under the terms of the GNU General Public License as published by the Free
     Software Foundation; either version 2 of the License, or (at your option)
     any later version.

     This program is distributed in the hope that it will be useful, but WITHOUT
     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
     more details.

     You should have received a copy of the GNU General Public License along
     with this program; if not, write to the Free Software Foundation, Inc.,
     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

     For any questions, comments or whatever, you may mail me at: hide@address.com
*/

require_once('Config.php');
require_once('Decoration.php');
require_once('Forms.php');
require_once('GUI.php');
require_once('Locale.php');
require_once('Login.php');
require_once('Messages.php');
require_once('Navigation.php');
require_once('Widgets.php');

/**
 * @brief Configuration editor.
 *
 * This class provides an easy way to create configuration pages the user can
 * use to change his configuration. Widgets are automatically created for the
 * configuration options you specify and the results are automatically written
 * back to the correspond config files if the user clicks on an Apply button.
 */
class ConfigEditor extends Form
{
	/**
	 * Constructor.
	 *
	 * @param parent        The parent widget to add this editor to.
	 * @param viewContainer ID of the view container this editor is part of.
	 * @param cancelView    The view to return to if editing is cancelled.
	 */
	public function __construct(Container $parent, $viewContainer = 'view', $cancelView = 'default')
	{
		parent::__construct($parent, Config::globals('baseURL')."?view=ConfigEditor&amp;action=ConfigEditor&amp;{$viewContainer}_cancel={$cancelView}");

		new PageIcon($this, 'base/icons/settings');
		$this->tabWidget = new TabWidget($this, 'ConfigEditor');
		$this->tabWidget->setParameters('view=ConfigEditor');
	}

	public function addWidget(Widget $widget)
	{
		if(is_a($widget, 'ConfigTab'))
		{
			$this->tabWidget->addTab($widget->config(), $widget->label());
			$this->tabWidget->setTabContainer($widget->config(), $widget);
		}
		else
		{
			parent::addWidget($widget);
		}
	}

	/**
	 * @internal
	 *
	 * This function is called automatically to check whether the user has
	 * submitted a configuration page and will automatically save all
	 * changes.
	 */
	public static function checkAction()
	{
		// save settings
		if(Config::request('action') == 'ConfigEditor')
		{
			if(Config::request('button') == i18n('Apply changes'))
			{
				$config = new Config(Config::request('ConfigEditor__config'), Login::username());
				foreach(Config::requests() as $var => $val)
				{
					if(String::startsWith($var, 'ConfigEditor_') == false ||
					   $var == 'ConfigEditor__config' ||
					   Config::request("{$var}__checkbox") == 'true')
					{
						continue;
					}

					$var = String::substringAfter($var, 'ConfigEditor_');

					// special case for checkboxes
					if(String::endsWith($var, '__checkbox'))
					{
						$var = String::substringBefore($var, '__checkbox');
						$val = (Config::request("ConfigEditor_$var") == 'true' ? 'true' : 'false');
					}

					$config->setVariable($var, $val);
				}
				$config->saveConfiguration();
				Locale::init('base', $config->variable('language', 'en'));
				Messages::confirm(i18n('Preferences saved.'));
			}
		}
	}

	public function show()
	{
		$box = new Box($this);
		$box->setOrientation('horizontal');
		$applyButton = new Button($box, 'button');
		$applyButton->setValue(i18n('Apply changes'));
		$applyButton->setConfirm(true);
		$cancelButton = new Button($box, 'button');
		$cancelButton->setValue(i18n('Cancel'));

		parent::show();
	}

	protected $id;

	private $tabWidget;
}

// immediately check actions from a previous page
ConfigEditor::checkAction();

/**
 * @brief A configuration tab.
 *
 * Provides a single tab with configuration options to the user.
 */
class ConfigTab extends Container
{
	/**
	 * Constructor.
	 *
	 * @param parent The configuration editor to add this tab to.
	 * @param label  The label put on the tab.
	 * @param config The module name whose configuration file should be used
	 *               to store the settings in.
	 */
	public function __construct(ConfigEditor $parent, $label, $config)
	{
		$this->label = $label;
		$this->config = $config;

		parent::__construct($parent);

		$hiddenInput = new HiddenInput($this, 'ConfigEditor__config');
		$hiddenInput->setValue($config);
	}

	/**
	 * Returns the label of the page.
	 */
	public function label()
	{
		return $this->label;
	}

	/**
	 * Returns the module name whose configuration file is connected to this page
	 * of the page.
	 */
	public function config()
	{
		return $this->config;
	}

	private $label;
	private $config;
}

/**
 * @brief A plain text configuration option.
 */
class TextConfigOption extends TextInput
{
	/**
	 * Constructor.
	 *
	 * @param parent      The configuration page to show this option in.
	 * @param variable    The variable name of the option.
	 * @param value       The current value of the option.
	 * @param description A short description of the option.
	 * @param tooltip     Optional tooltip text with an explanation of the
	 *                    option.
	 */
	public function __construct(ConfigTab $parent, $variable, $value,
	                            $description, $tooltip = '')
	{
		parent::__construct($parent, "ConfigEditor_$variable");

		$this->setValue($value);
		$this->setText($description);

		if($tooltip != '')
		{
			new Image($this, 'resources/base/icons/buttons/help.png');
			new Tooltip($this, $tooltip);
		}
	}
}

/**
 * @brief A password configuration option.
 */
class PasswordConfigOption extends PasswordInput
{
	/**
	 * Constructor.
	 *
	 * @param parent      The configuration page to show this option in.
	 * @param variable    The variable name of the option.
	 * @param description A short description of the option.
	 * @param tooltip     Optional tooltip text with an explanation of the
	 *                    option.
	 */
	public function __construct(ConfigTab $parent, $variable,
	                            $description, $tooltip = '')
	{
		parent::__construct($parent, "ConfigEditor_$variable");

		$this->setText($description);

		if($tooltip != '')
		{
			new Image($this, 'resources/base/icons/buttons/help.png');
			new Tooltip($this, $tooltip);
		}
	}
}

/**
 * @brief A true/false configuration option, represented by a checbox.
 */
class CheckboxConfigOption extends CheckboxInput
{
	/**
	 * Constructor.
	 *
	 * @param parent      The configuration page to show this option in.
	 * @param variable    The variable name of the option.
	 * @param value       The current value of the option. Should be either
	 *                    'true' or 'false'.
	 * @param description A short description of the option.
	 * @param tooltip     Optional tooltip text with an explanation of the
	 *                    option.
	 */
	public function __construct(ConfigTab $parent, $variable, $value,
	                            $description, $tooltip = '')
	{
		parent::__construct($parent, "ConfigEditor_$variable");

		$this->setChecked($value == 'true');
		$this->setText($description);

		$hiddenInput = new HiddenInput($this, $this->id().'__checkbox');
		$hiddenInput->setValue('true');

		if($tooltip != '')
		{
			new Image($this, 'resources/base/icons/buttons/help.png');
			new Tooltip($this, $tooltip);
		}
	}
}

/**
 * @brief A selection list configuration option.
 *
 * A configuration option where the user can choose an option out of a
 * selection, where all options are listed in a selection list.
 */
class SelectConfigOption extends SelectInput
{
	/**
	 * Constructor.
	 *
	 * @param parent      The configuration page to show this option in.
	 * @param variable    The variable name of the option.
	 * @param value       The current value of the option.
	 * @param options     The available options the user can choose among.
	 *                    They should consist of key => text pairs.
	 * @param description A short description of the option.
	 * @param tooltip     Optional tooltip text with an explanation of the
	 *                    option.
	 */
	public function __construct(ConfigTab $parent, $variable, $value, $options,
	                            $description, $tooltip = '')
	{
		parent::__construct($parent, "ConfigEditor_$variable");

		$this->setOptions($options);
		$this->setValue($value);
		$this->setText($description);

		if($tooltip != '')
		{
			new Image($this, 'resources/base/icons/buttons/help.png');
			new Tooltip($this, $tooltip);
		}
	}
}

/**
 * @brief A radio button configuration option.
 *
 * A configuration option where the user can choose an option out of a
 * selection, where each option is represent with a radio button.
 */
class ConfigEditorRadioOption extends Container
{
	/**
	 * Constructor.
	 *
	 * @param parent      The configuration page to show this option in.
	 * @param variable    The variable name of the option.
	 * @param value       The current value of the option.
	 * @param options     The available options the user can choose among.
	 *                    They should consist of key => text pairs.
	 * @param description A short description of the option.
	 * @param tooltip     Optional tooltip text with an explanation of the
	 *                    option.
	 */
	public function __construct(ConfigTab $parent, $variable, $value, $options,
	                            $description, $tooltip = '')
	{
		parent::__construct($parent);

		new Paragraph($this, $description);
		$radioGroup = new RadioGroup($this, "ConfigEditor_{$variable}");
		foreach($options as $key => $text)
		{
			$radioButton = new RadioButton($radioGroup, $key);
			$radioButton->setText($text);
			$radioButton->setChecked($key == $this->value);
		}

		if($tooltip != '')
		{
			new Image($this, 'resources/base/icons/buttons/help.png');
			new Tooltip($this, $tooltip);
		}
	}
}

?>
Return current item: Aukyla Platform