<?php
/**
 * @version        1.9.5
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright	   Copyright (C) 2011 - 2024 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

use Joomla\CMS\Factory;
use Joomla\Filesystem\File;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\Filesystem\Path;

abstract class EDocmanHelperHtml
{
    /**
     * Function to render a common layout which is used in different views
     * @param string $layout	Relative path to the layout file
     * @param array $data	An array contains the data passed to layout for rendering
     */
    public static function loadCommonLayout($layout, $data = array())
    {
        $app = Factory::getApplication();
        $themeFile = str_replace('/tmpl', '', $layout);
        if (is_file(Path::clean(JPATH_THEMES . '/' . $app->getTemplate() . '/html/com_edocman/' . $themeFile)))
        {
            $path = JPATH_THEMES . '/' . $app->getTemplate() . '/html/com_edocman/' . $themeFile;
        }
        elseif (is_file(Path::clean(JPATH_ROOT . '/components/com_edocman/view/' . $layout)))
        {
            $path = JPATH_ROOT . '/components/com_edocman/view/' . $layout;
        }
        else
        {
            throw new RuntimeException(\Joomla\CMS\Language\Text::_('The given shared template path is not exist'));
        }
        // Start an output buffer.
        ob_start();
        extract($data);
        // Load the layout.
        include $path;
        // Get the layout contents.
        $output = ob_get_clean();

        return $output;
    }

	/**
	 * Function to add dropdown menu
	 *
	 * @param string $vName
	 */
	public static function renderSubmenu($vName = 'dashboard')
	{
		HTMLHelper::_('bootstrap.dropdown');
		$db    = Factory::getContainer()->get('db');
		$query = $db->getQuery(true);
		$query->select('*')
			->from('#__edocman_menus')
			->where('published = 1')
			->where('menu_parent_id = 0')
			->order('ordering');
		$db->setQuery($query);
		$menus = $db->loadObjectList();
		$html  = '';
		//$html  .= '<div class="clearfix"></div><ul id="mp-dropdown-menu" class="nav nav-tabs nav-hover">';

		if (version_compare(JVERSION, '4.0.0-dev', 'ge'))
		{
			$html .= '<ul id="mp-dropdown-menu" class="nav nav-tabs nav-hover edocman-joomla4">';
		}
		else
		{
			$html .= '<div class="clearfix"></div><ul id="mp-dropdown-menu" class="nav nav-tabs nav-hover">';
		}

		$currentLink = 'index.php' . Uri::getInstance()->toString(array('query'));
		for ($i = 0; $n = count($menus), $i < $n; $i++)
		{
			$menu = $menus[$i];
			$query->clear();
			$query->select('*')
				->from('#__edocman_menus')
				->where('published = 1')
				->where('menu_parent_id = ' . intval($menu->id))
				->order('ordering');
			$db->setQuery($query);
			$subMenus = $db->loadObjectList();

			if (!count($subMenus))
			{
				$class = '';
				if ($menu->menu_link == $currentLink)
				{
					$class = ' class="active"';
					$extraClass = 'active';
				}
				else
				{
					$class = '';
					$extraClass = '';
				}
				$html .= '<li' . $class . '><a class="nav-link dropdown-item ' . $extraClass . '" href="' . $menu->menu_link . '"><span class="icon-' . $menu->menu_class . '"></span> ' . \Joomla\CMS\Language\Text::_($menu->menu_name) .
					'</a></li>';
			}
			else
			{
				$class = ' class="dropdown"';
				for ($j = 0; $m = count($subMenus), $j < $m; $j++)
				{
					$subMenu = $subMenus[$j];
					if ($subMenu->menu_link == $currentLink)
					{
						$class = ' class="dropdown active"';
						break;
					}
				}
				if(EdocmanHelper::isJoomla4())
				{
					$dropdownToggle = 'data-bs-toggle="dropdown"';
				}
				else
				{
					$dropdownToggle = 'data-toggle="dropdown"';
				}
				$html .= '<li' . $class . '>';
				$html .= '<a id="drop_' . $menu->id . '" href="#" '.$dropdownToggle.' role="button" class="nav-link dropdown-toggle"><span class="icon-' . $menu->menu_class . '"></span> ' .
					\Joomla\CMS\Language\Text::_($menu->menu_name) . ' <b class="caret"></b></a>';
				$html .= '<ul aria-labelledby="drop_' . $menu->id . '" role="menu" class="dropdown-menu" id="menu_' . $menu->id . '">';
				for ($j = 0; $m = count($subMenus), $j < $m; $j++)
				{
					$subMenu = $subMenus[$j];
					$class   = '';

					$vars = array();
					parse_str($subMenu->menu_link, $vars);
					$view = isset($vars['view']) ? $vars['view'] : '';

					if ($subMenu->menu_link == $currentLink)
					{
						$class = ' class="active"';
						$extraClass = 'active';
					}
					else
					{
						$class = '';
						$extraClass = '';
					}
					$html .= '<li' . $class . '><a class="nav-link dropdown-item ' . $extraClass . '" href="' . $subMenu->menu_link .
						'" tabindex="-1"><span class="icon-' . $subMenu->menu_class . '"></span> ' . \Joomla\CMS\Language\Text::_($subMenu->menu_name) . '</a></li>';
				}
				$html .= '</ul>';
				$html .= '</li>';
			}
		}
		$html .= '</ul>';

		$layout = Factory::getApplication()->getInput()->getString('layout','');

		if($layout != "modal" && $layout != "modal1")
        {
            echo $html;
        }
	}

    /**
     * Get BootstrapHelper class for admin UI
     *
     * @return EventbookingHelperBootstrap
     */
    public static function getAdminBootstrapHelper()
    {
        if (version_compare(JVERSION, '4.0.0-dev', 'ge'))
        {
            return 4;
        }
        return 2;
    }

	public static function initModal()
	{
		static $modalLoaded;
		if (!$modalLoaded)
		{
			
			//EDocmanHelperJquery::colorbox('edocman-modal');
			//EDocmanHelperJquery::colorbox('modal');
			//EDocmanHelperJquery::colorbox('a.modalj4', '65%', '90%', false, false, true, true);
			//EDocmanHelperJquery::colorbox('a.sharing-modal', '65%', '90%', false, false, true, false);
			
			$modalLoaded = true;
		}
	}

	/**
	 * Get label of the field (including tooltip)
	 *
	 * @param           $name
	 * @param           $title
	 * @param   string  $tooltip
	 * @param   bool    $required
	 *
	 * @return string
	 */
	public static function getFieldLabel($name, $title, $tooltip = '', $required = false)
	{
		$label = '';
		$text  = $title;

		// Build the class for the label.
		$class = !empty($tooltip) ? 'hasTooltip hasTip' : '';

		// Add the opening label tag and main attributes attributes.
		$label .= '<label id="' . $name . '-lbl" for="' . $name . '" class="' . $class . '"';

		// If a description is specified, use it to build a tooltip.
		if (!empty($tooltip))
		{
			$label .= ' title="' . self::tooltipText(trim($text, ':'), $tooltip, 0) . '"';
		}

		$label .= '>' . $text . ($required ? '<span class="required">*</span>' : '') . '</label>';

		return $label;
	}

	/**
	 * Converts a double colon seperated string or 2 separate strings to a string ready for bootstrap tooltips
	 *
	 * @param   string  $title      The title of the tooltip (or combined '::' separated string).
	 * @param   string  $content    The content to tooltip.
	 * @param   int     $translate  If true will pass texts through JText.
	 * @param   int     $escape     If true will pass texts through htmlspecialchars.
	 *
	 * @return  string  The tooltip string
	 *
	 * @since   2.0.7
	 */
	public static function tooltipText($title = '', $content = '', $translate = 1, $escape = 1)
	{
		// Initialise return value.
		$result = '';

		// Don't process empty strings
		if ($content != '' || $title != '')
		{
			// Split title into title and content if the title contains '::' (old Mootools format).
			if ($content == '' && !(strpos($title, '::') === false))
			{
				list($title, $content) = explode('::', $title, 2);
			}

			// Pass texts through JText if required.
			if ($translate)
			{
				$title   = Text::_($title);
				$content = Text::_($content);
			}

			// Use only the content if no title is given.
			if ($title == '')
			{
				$result = $content;
			}
			// Use only the title, if title and text are the same.
            elseif ($title == $content)
			{
				$result = '<strong>' . $title . '</strong>';
			}
			// Use a formatted string combining the title and content.
            elseif ($content != '')
			{
				$result = '<strong>' . $title . '</strong><br />' . $content;
			}
			else
			{
				$result = $title;
			}

			// Escape everything, if required.
			if ($escape)
			{
				$result = htmlspecialchars($result);
			}
		}

		return $result;
	}

	public static function showFileIcon($file)
	{
		if($file->filesvg != "")
		{
			echo $file->filesvg;
		}
		else
		{
			echo '<i class="'.$file->fileicon.'"></i>';
		}
	}

	public static function showStateField($state)
	{
		$optionArr = [];
		$optionArr[] = HTMLHelper::_('select.option',1,Text::_('JPUBLISHED'));
		$optionArr[] = HTMLHelper::_('select.option',0,Text::_('JUNPUBLISHED'));
		$optionArr[] = HTMLHelper::_('select.option',2,Text::_('JARCHIVED'));
		echo HTMLHelper::_('select.genericlist',$optionArr,'jform[published]', 'class="form-select inputbox" aria-describedby="jform_published-desc"','value','text', $state);
	}
}
