<?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) 2012 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flFormElementImage Class
 *
 * Form image button element, value is true if clicked else false.
 *
 * @package fusionLib
 */
class flFormElementImage extends flFormElementSubmit {

	/**
	 * The image src.
	 *
	 * @var string
	 */
	protected $src;

	/**
	 *	Construct the element.
	 *
	 * @param flForm $form The form the element will be used with or null if not using flForm
	 * @param string $name The name of the element.
	 * @param string $src The source image to show.
	 * @param string $default The default value of the element.
	 */
	function __construct($form, $name, $src, $default = null) {
		parent::__construct($form, $name, $default);
		$this->src = $src;
	}

	/**
	 * Draw the element.
	 */
	function render() {
		$html = "<input type=\"image\" id=\"$this->name\" name=\"$this->name\" src=\"$this->src\" value=\"$this->name\" ";
		if($this->caption !== null) $html .= " title=\"$this->caption\"";
		return $html . $this->attribHTML() . " />\n";
	}

	/**
	 * 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) {
		// Unify all browsers
		if($request->hasParam($this->name . '_x') && $request->hasParam($this->name . '_y') && !$request->hasParam($this->name))
			$request->setParam($this->name, $this->name);

		parent::processRequest($request);
	}
}