<?php
/**@license setplus: a lightweight HTML editor
 * @author  Levente Hunyadi
 * @version 0.2.0
 * @remarks Copyright (C) 2010-2011 Levente Hunyadi
 * @remarks Licensed under GNU/GPLv3, see http://www.gnu.org/licenses/gpl-3.0.html
 * @see     http://hunyadi.info.hu/projects/setplus
 **/

/*
* setplus: a lightweight HTML editor
* Copyright 2010-2011 Levente Hunyadi
*
* setplus 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 3 of the License, or
* (at your option) any later version.
*
* setplus 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 setplus.  If not, see <http://www.gnu.org/licenses/>.
*/

// no direct access
defined( 'JPATH_BASE' ) or die( 'Restricted access' );

jimport('joomla.html.html');
jimport('joomla.form.formfield');

/**
* Check box list form field class.
*/
class JFormFieldToolbar extends JFormField
{
	/**
	* The form field type.
	*
	* @var string
	*/
	protected $type = 'Toolbar';

	/**
	* Flag to tell the field to always be in multiple values mode.
	*
	* @var boolean
	*/
	protected $forceMultiple = true;

	/**
	* Method to get the field input markup.
	*
	* @return string The field input markup.
	*/
	protected function getInput()
	{
		$doc = JFactory::getDocument();
		$doc->addStyleDeclaration('#'.$this->id.' > ul { overflow:auto; min-width:250px; max-height:240px; }');
		$doc->addStyleDeclaration('#'.$this->id.' > ul > li > * { display:inline; float:none; clear:none; vertical-align:middle; }');

		// Initialize variables.
		$html = array();

		// Initialize some field attributes.
		$class = $this->element['class'] ? ' class="checkboxes '.(string) $this->element['class'].'"' : ' class="checkboxes"';

		// Start the checkbox field output.
		$html[] = '<fieldset id="'.$this->id.'"'.$class.'>';

		// Get the field options.
		$options = $this->getOptions();

		// Build the checkbox field output.
		$html[] = '<ul>';
		foreach ($options as $i => $option) {

			// Initialize some option attributes.
			$checked	= (in_array((string)$option->value,(array)$this->value) ? ' checked="checked"' : '');
			$class		= !empty($option->class) ? ' class="'.$option->class.'"' : '';
			$disabled	= !empty($option->disable) ? ' disabled="disabled"' : '';

			// Initialize some JavaScript option attributes.
			$onclick	= !empty($option->onclick) ? ' onclick="'.$option->onclick.'"' : '';

			// Initialize icon for item.
			$icon = JURI::root(true).'/media/editors/setplus/css/toolbar/' . (!empty($option->icon) ? $option->icon : htmlspecialchars($option->value, ENT_COMPAT, 'UTF-8').'.png' );

			$html[] = '<li>';
			$html[] = '<img src="'.$icon.'" />';
			$html[] = '<input type="checkbox" id="'.$this->id.$i.'" name="'.$this->name.'"' .
					' value="'.htmlspecialchars($option->value, ENT_COMPAT, 'UTF-8').'"'
					.$checked.$class.$onclick.$disabled.'/>';

			$html[] = '<label for="'.$this->id.$i.'"'.$class.'>'.JText::_($option->text).'</label>';
			$html[] = '</li>';
		}
		$html[] = '</ul>';

		// End the checkbox field output.
		$html[] = '</fieldset>';

		return implode($html);
	}

	/**
	* Method to get the field options.
	*
	* @return array The field option objects.
	*/
	protected function getOptions()
	{
		// Initialize variables.
		$options = array();

		foreach ($this->element->children() as $option) {

			// Only add <option /> elements.
			if ($option->getName() != 'option') {
				continue;
			}

			// Create a new option object based on the <option /> element.
			$tmp = JHtml::_('select.option', (string) $option['value'], trim((string) $option), array(
				'option.attr' => 'icon',
				'attr'        => $option['icon'],
				'disable'     => (string) $option['disabled']=='true'
			));

			// Set some option attributes.
			$tmp->class = (string) $option['class'];

			// Set some JavaScript option attributes.
			$tmp->onclick = (string) $option['onclick'];

			// Add the option object to the result set.
			$options[] = $tmp;
		}

		reset($options);

		return $options;
	}
}
