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

/**
 * flWidgetTabs Class
 *
 * Create and manage breadcrumb trails.
 *
 * @package fusionLib
 */
class flWidgetTabs {

	/**
	 * The name of the widget.
	 *
	 * @var string
	 */
	protected $name;

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

	/**
	 * The tab information.
	 *
	 * @var array
	 */
	protected $tabs = array();

	/**
	 * Create the tabs widget.
	 *
	 * @param string $name The unique name of the widget.
	 */
	function __construct($name) {
		$this->name = $name;
	}

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

	/**
	 * Add a tab to the widget.
	 *
	 * @param string $name The name of the tab.
	 * @param string $html The HTML for the tab
	 * @return flWidgetTabs This object.
	 */
	function addTab($name, $html) {
		$this->tabs[] = array($name, $html);
		return $this;
	}

	/**
	 * Render the tab view.
	 *
	 * @param flBufferCode The code buffer object if any.
	 * @return string The HTML to render the tree.
	 */
	function render($codeBuffer = null) {
		if(!count($this->tabs))
			return '';

		if($codeBuffer !== null) {
			$codeBuffer->requireCore()
				->bufferHeadJS($codeBuffer->getSysJSURI() . 'fusionLib.widgets.js');
		}

		$s = '<div id="' . $this->name . '" class="' . $this->css . '" role="tabpanel"><ul class="tabs" role="tablist">';
		$p = '<ul class="tabpanels">';
		foreach($this->tabs as $i => $t) {
			$s .= '<li role="tab" data-tabpanel="' . $this->name . "-$i\">$t[0]</li>";
			$p .= '<li id="' . $this->name . "-$i\" role=\"tabpanel\">$t[1]</li>";
		}
		$s .= '</ul>' . $p . '</ul></div>';

		return $s;
	}
}
