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

/**
 * flWidgetToolbar Class
 *
 * Class to simplify generation of a toolbar.
 *
 * @package fusionLib
 */
class flWidgetToolbar {

	/**
	 * The CSS class to apply to the table.
	 *
	 * @var string
	 */
	protected $css = 'toolbarwidget';

	/**
	 * Storage for tools.
	 *
	 * @var array
	 */
	protected $tools = array();


	/**
	 * Set the CSS class to use.
	 *
	 * @param string $css The CSS class, default is 'toolbarwidget'.
	 * @return flWidgetToolbar This object.
	 */
	function setClass($css) {
		$this->css = $css;
		return $this;
	}

	/**
	 * Add a tool to the toolbar.
	 *
	 * @param string The tool caption.
	 * @param string The icon file to show.
	 * @param string The link to trigger on click.
	 * @param string Optional ID to assign to the link.
	 * @return flWidgetToolbar This object.
	 */
	function addTool($caption, $icon, $link, $id = null) {
		$this->tools[] = "<li><a href=\"$link\" title=\"$caption\"" . ($id ? "id=\"$id\"" : '') . "><img src=\"$icon\" alt=\"$caption\" /></a></li>";
		return $this;
	}

	/**
	 * Render the toolbar.
	 *
	 * @return string The string holding the HTML representation of the toobar.
	 */
	function render() {
		return count($this->tools) ? "<ul class=\"" . $this->css . "\">\n" . join("\n", $this->tools) . "\n</ul>\n" : '';
	}
}
