<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package fusionLib
 * @copyright Copyright (c) 2007 - 2013 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flFormElementChecklist Class
 *
 * Checklist form element.
 *
 * @package fusionLib
 */
class flFormElementChecklist extends flFormElement {

	/**
	 * Options
	 *
	 * @var array
	 */
	protected $options;

	/**
	 *	Construct the element.
	 *
	 *	@param flForm $form The form the element will be used with.
	 *	@param string $name The name of the element.
	 *	@param array $default The default value of the element.
	 *	@param array $options The options for the checklist
	 */
	function __construct($form, $name, $default, $options) {
		parent::__construct($form, $name, $default);
		$this->options = $options;
	}

	/**
	 * Process the request data, normally called by the flForm::isSubmitted().
	 * Validation of the data is also performed and the results stored.
	 *
	 * @param flRequest $request The request object.
	 */
	function processRequest($request) {
		if(!$request->hasParam($this->name))
			$this->value = array();

		parent::processRequest($request);

		if($this->isValid) {
			if(!is_array($this->value))
				$badInput = true;
			else {
				$badInput = false;
				foreach($this->value as $v) {
					if(!array_key_exists($v, $this->options)) {
						$badInput = true;
						break;
					}
				}
			}

			if($badInput) {
				$this->value = array();
				$this->isValid = false;
				$this->error = "Please select a valid item.";
			}
		}
	}

	/**
	 * Draw the element.
	 *
	 * @return string The HTML.
	 */
	function render() {
		$html = "<ol id=\"{$this->name}\" class=\"checklist\"";
		if($this->label) $html .= ' title="' . $this->label . '"';
		$html .= ">\n";
		foreach($this->options as $v => $t) {
			$html .= "<li><label><input name=\"$this->name[]\" type=\"checkbox\" value=\"$v\"";
			if(in_array($v, $this->value)) $html .= ' checked="checked"';
			$html .= $this->attribHTML() . " /> $t</label></li>\n";
		}
		return $html . "</ol>\n";
	}

	/**
	 * Get the JavaScript for the element if any.
	 *
	 * @return string The string holding the JavaScript for the element.
	 */
	function getJavaScript() {
		static $isFirst = true;

		if($isFirst) {
			$isFirst = false;
			return parent::getJavaScript() . "\$fl('ol.checklist input').each(function() { if(\$fl(this).value()) \$fl(this).parent().parent().addClass('selected'); })
.bind('change', function() { \$fl(this).parent().parent().toggleClass('selected'); });\n";
		}
		else
			return parent::getJavaScript();
	}

	/**
	 * Get the name of the element for use in JS validation rules.
	 *
	 * @return string The element name for use in a client rule.
	 */
	protected function _getClientRuleName() {
		return $this->name . '[]';
	}

	/**
	 * Get the elements option list.
	 *
	 * @return array The array of options passed to the constructor.
	 */
	function getOptions() {
		return $this->options;
	}
}
