<?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
 */

/**
 * flFormElementCheckbox Class
 *
 * Checkbox form element.
 *
 * @package fusionLib
 */
class flFormElementCheckbox extends flFormElement {

	/**
	 * 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) {
		parent::processRequest($request);
		$this->value = $request->hasParam($this->name);
	}

	/**
	 * Draw the element.
	 */
	function render() {

		$label = $this->label === null ? '&nbsp;' : $this->label;

		$html = "<label id=\"{$this->name}-label\" aria-atomic=\"true\" aria-live=\"polite\">";
		$html .= "<input name=\"$this->name\" id=\"$this->name\" type=\"checkbox\" value=\"1\"";
		if($this->value) $html .= ' checked="checked"';
		$html .= $this->attribHTML() . " /> $label";

		if($this->required) $html .= ' <em>' . ($this->form !== null ? $this->form->getRequiredText() : '(Required)') . '</em>';

		if($this->hasValidators() && $this->isSubmitted()) {
			$cls = $this->isValid() ? 'ok' : 'failed';

			// Optionally hide generic valid messages
			if($cls == 'ok' && $this->isValidText === null && $this->form !== null && $this->form->getShowOnlyErrors()) {
				$cls = 'hide';
				$msg = '&nbsp;';
			}
			else
				$msg = $this->getError();
		}
		else {
			$cls = '';
			$msg = '&nbsp;';
		}

		return $html . " <span class=\"validation $cls\" id=\"{$this->name}-validation\"><strong>$msg</strong></span></label>";
	}

	/**
	 * Generate the HTML for the elements label.
	 *
	 * @return string The label HTML.
	 */
	function getLabelHTML() {
		return '';
	}
}
