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

/**
 * flWidgetFloatingActionMenu Class
 *
 * Class to build floating action menus or if only 1 item given floating action buttons.
 *
 * Icons are from the Google material icons.
 *
 * @see https://design.google.com/icons/
 * @package fusionLib
 */
class flWidgetFloatingActionMenu {

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

	/**
	 * Flags if the menu should default to open.
	 *
	 * @var bool
	 */
	protected $defaultOpen = false;

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

	/**
	 * Set the CSS class to use.
	 *
	 * @param string $css The additional CSS classes to apply.
	 * @return flWidgetFloatingActionMenu This object.
	 */
	function setCSS($css) {
		$this->css = $css;
		return $this;
	}

	/**
	 * Set the default open state of the menu.
	 *
	 * @param bool $defaultOpen true if the menu should default to open.
	 * @return flWidgetFloatingActionMenu This object.
	 */
	function setDefaultToOpen($defaultOpen = true) {
		$this->defaultOpen = $defaultOpen;
		return $this;
	}

	/**
	 * Add an action to the menu.
	 *
	 * @param string $icon The icon to show.
	 * @param string $title The icon title.
	 * @param string $link Optional link to trigger on click.
	 * @param string $id Optional ID to assign to the link.
	 * @param bool $leaveOpen true to leave the menu open after click.
	 * @param bool $hide The option.
	 * @return flWidgetFloatingActionMenu This object.
	 */
	function addAction($icon, $title, $link = null, $id = null, $leaveOpen = false, $hide = false) {
		$this->actions[] = array(
			'icon' => $icon,
			'link' => $link,
			'id' => $id,
			'leaveOpen' => $leaveOpen,
			'title' => $title,
			'hide' => $hide
		);

		return $this;
	}

	/**
	 * Render the floating action menu.
	 *
	 * @return string The string holding the HTML representation of the action menu.
	 */
	function render() {
		$html = '';
		$actions = count($this->actions);

		if($actions > 1) {
			$html = '<div class="fam ' . $this->css . '">
<a class="button floating accent"><i class="material-icons">more_vert</i></a>
<ul' . ($this->defaultOpen ? ' class="exposed"' : '') . '>';

			foreach ($this->actions as $a) {
				$html .= '<li><a class="button floating accent mini';
				if ($a['leaveOpen'])
					$html .= ' leave-open';
				$html .= '"';
				if ($a['id'])
					$html .= ' id="' . $a['id'] . '"';
				if ($a['link'])
					$html .= ' href="' . $a['link'] . '"';
				$html .= ' title="' . $a['title'] . '"';
				if ($a['hide'])
					$html .= ' style="display: none;"';
				$html .= '><i class="material-icons">' . $a['icon'] . '</i></a></li>';
			}

			$html .= '</ul></div>';
		}
		else if($actions) {
			$a = $this->actions[0];

			$html = '<div class="fab ' . $this->css . '">';
			$html .= '<a class="button floating accent"';
			if ($a['id'])
				$html .= ' id="' . $a['id'] . '"';
			if ($a['link'])
				$html .= ' href="' . $a['link'] . '"';
			$html .= ' title="' . $a['title'] . '"';
			$html .= '><i class="material-icons">' . $a['icon'] . '</i></a></li>';
			$html .= '</div>';
		}
		return $html;
	}
}
