<?php 
/**
* @version		$Id$
* @package		Elxis
* @subpackage	Helpers / Toolbar
* @copyright	Copyright (c) 2006-2025 Elxis CMS (https://www.elxis.org). All rights reserved.
* @license		Elxis Public License ( https://www.elxis.org/elxis-public-license.html )
* @author		Elxis Team ( https://www.elxis.org )
* @description 	Elxis CMS is free software. Read the license for copyright notices and details
*/

defined('_ELXIS_') or die ('Direct access to this location is not allowed');


class elxisToolbarHelper {

	private $buttons = array();


	/*********************/
	/* MAGIC CONSTRUCTOR */
	/*********************/
	public function __construct() {
	}


	/********************/
	/* RESET EVERYTHING */
	/********************/
	public function resetAll() {
		$this->buttons = array();
	}


	/**************/
	/* SET OPTION */
	/**************/
	public function setOption($option, $value) {//Deprecated. Left for backwards compatibility
	}


	/**********************************/
	/* GENERIC METHOD TO ADD A BUTTON */
	/**********************************/
	public function add($title, $image='', $icononly=false, $link='', $onclick='', $onmouseover='', $onmouseout='', $css='') {
		$addon_class = '';
		$icon = 'fas fa-dot-circle';
		if ($image != '') {
			if (strpos($image, 'http') === 0) { //custom icon => switch to fontawesome default
				$icon = 'fas fa-dot-circle';
			} else if (strpos($image, 'fa') === 0) {//fontawesome class
				$icon = $image;
			} else {//Elxis 4.x style image icon => switch to fontawesome + addon class
				switch ($image) {
					case 'save': $icon = 'fas fa-save'; $addon_class = ' elx5_sucbtn'; break;
					case 'saveedit': $icon = 'far fa-save'; $addon_class = ' elx5_sucbtn'; break;
					case 'cancel': $icon = 'fas fa-times'; break;
					case 'add': $icon = 'fas fa-plus'; break;
					case 'delete': $icon = 'fas fa-trash'; $addon_class = ' elx5_errorbtn'; break;
					case 'error': $icon = 'fas fa-exclamation-triangle'; $addon_class = ' elx5_errorbtn'; break;
					case 'edit': $icon = 'fas fa-pen'; break;
					case 'folder': $icon = 'fas fa-folder'; break;
					case 'help': $icon = 'fas fa-question'; break;
					case 'home': $icon = 'fas fa-home'; break;
					case 'info': $icon = 'fas fa-info'; break;
					case 'lock': $icon = 'fas fa-lock'; break;
					case 'media': $icon = 'fas fa-images'; break;
					case 'menu': $icon = 'fas fa-bars'; break;
					case 'settings': $icon = 'fas fa-cog'; break;
					case 'tick': $icon = 'fas fa-check'; break;
					case 'user': $icon = 'fas fa-user'; break;
					default: $icon = 'fas fa-dot-circle'; break;
				}
			}
		}

		if (($css == '') || ($css =='elx_toolbar') || ($css =='elx5_toptoolbar_item')) {//Elxis 4.x "elx_toolbar" => 5.x "elx5_toptoolbar_item"
			$css = 'elx5_toptoolbar_item'.$addon_class;
		}

		$btn = new stdClass;
		$btn->title = $title;
		$btn->icon = $icon;
		$btn->icononly = (bool)$icononly;
		$btn->link = $link;
		$btn->onclick = trim($onclick);
		$btn->onmouseover = trim($onmouseover);
		$btn->onmouseout = trim($onmouseout);
		$btn->css = $css;
		$this->buttons[] = $btn;
	}


	/*********************************/
	/* GENERATE TOOLBAR BUTTONS HTML */
	/*********************************/
	public function getHTML() {
		if (!$this->buttons) { return ''; }

		$rnd = mt_rand();
		$jsonload = array();

		$html = '<div class="elx5_toptoolbar">'."\n";
		foreach ($this->buttons as $k => $button) {
			$id = 'tb'.$rnd.'_'.$k;

			if ($button->link == '') {
				$html .= '<a href="#" id="'.$id.'" class="'.$button->css.'" title="'.$button->title.'"';
			} else {
				$html .= '<a href="'.$button->link.'" id="'.$id.'" class="'.$button->css.'" title="'.$button->title.'"';
			}
			if ($button->onclick != '') {
				$jsonload[] = 'document.getElementById(\''.$id.'\').addEventListener(\'click\', function(e){ e.preventDefault(); '.$button->onclick.' }, false);';
			}
			if ($button->onmouseover != '') {
				$jsonload[] = 'document.getElementById(\''.$id.'\').addEventListener(\'mouseover\', function(e){ e.preventDefault(); '.$button->onmouseover.' }, false);';

			}
			if ($button->onmouseout != '') {
				$jsonload[] = 'document.getElementById(\''.$id.'\').addEventListener(\'mouseout\', function(e){ e.preventDefault(); '.$button->onmouseout.' }, false);';

			}
			$html .= '><i class="'.$button->icon.'" aria-hidden="false"></i>';
			if (!$button->icononly) { $html .= '<span class="elx5_lmobhide"> '.$button->title.'</span>'; }
			$html .= '</a>';
		}
		$html .= '</div>';

		if ($jsonload) {
			$js = implode(' ', $jsonload);
			eFactory::getDocument()->addNativeDocReady($js);
		}

		$this->resetAll();
		return $html;
	}

}

?>