<?php
/**
 * @version		   1.7.5
 * @package        Joomla
 * @subpackage     EDocman
 * @author         Tuan Pham Ngoc
 * @copyright	   Copyright (C) 2011 - 2024 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\HTML\HTMLHelper;

abstract class EDocmanHelperJquery
{		
	/**
	 * Method to load the plupload into the document head
	 *
	 * If debugging mode is on an uncompressed version of plupload is included for easier debugging.
	 *
	 * @return  void
	 */
	public static function upload()
	{
		static $loaded = false;
		if ($loaded)
		{
			return;
		}
		EDocmanHelper::loadJQuery();
		$document = Factory::getApplication()->getDocument();
		EdocmanHelper::addScript(Uri::root().'components/com_edocman/assets/js/noconflict.js');
		EdocmanHelper::addStyleSheet(Uri::root().'components/com_edocman/assets/js/plupload/jquery.plupload.queue/css/jquery.plupload.queue.css');
		EdocmanHelper::addScript(Uri::root().'components/com_edocman/assets/js/plupload/plupload.full.min.js');
		EdocmanHelper::addScript(Uri::root().'components/com_edocman/assets/js/plupload/jquery.plupload.queue/jquery.plupload.queue.min.js');

		$activeLanguageTag = Factory::getLanguage()->getTag();
		$allowedLanguageTags = array('ar-AA', 'bs-BA', 'ca-ES', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB',
			'en-US', 'es-ES', 'et-EE', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hr-HR', 'hu-HU', 'it-IT', 'ja-JP', 'ko-KR',
			'lv-LV', 'nl-NL', 'pl-PL', 'pt-BR', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'sr-YU', 'sv-SE', 'th-TH', 'tr-TR',
			'uk-UA', 'zh-CN', 'zh-TW'
		);
		$showedLanguage = in_array($activeLanguageTag, $allowedLanguageTags) ? $activeLanguageTag : 'en-GB';

		$document->addScript(Uri::root(true).'/components/com_edocman/assets/js/plupload/i18n/' . $showedLanguage . '.js');

		$loaded = true;
	}
	

	/**
	 * Method to load the colorbox into the document head
	 *
	 * If debugging mode is on an uncompressed version of colorbox is included for easier debugging.
	 *
	 * @param string $class
	 * @param string $width
	 * @param string $height
	 * @param string $iframe
	 * @param string $inline
	 *
	 * @return  void
	 */
	public static function colorbox($class = 'sr-iframe', $width = '80%', $height = '80%', $iframe = "true", $inline = "false", $scrolling = "true", $onClosed = false)
	{
		static $loaded = false;
		$siteUrl = EdocmanHelper::getSiteUrl();
		if (!$loaded)
		{
			$document = Factory::getApplication()->getDocument();
			EdocmanHelper::addStyleSheet(Uri::root().'components/com_edocman/assets/js/colorbox/colorbox.css');
			EdocmanHelper::addScript(Uri::root().'components/com_edocman/assets/js/colorbox/jquery.colorbox.min.js');

			$activeLanguageTag   = Factory::getLanguage()->getTag();
			$allowedLanguageTags = array('ar-AA', 'bg-BG', 'ca-ES', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'es-ES', 'et-EE',
				'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hr-HR', 'hu-HU', 'it-IT', 'ja-JP', 'ko-KR', 'lv-LV', 'nb-NO', 'nl-NL',
				'pl-PL', 'pt-BR', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA', 'zh-CN', 'zh-TW',
			);

			// English is bundled into the source therefore we don't have to load it.
			if (in_array($activeLanguageTag, $allowedLanguageTags))
			{
				HTMLHelper::_('script', $siteUrl . 'components/com_edocman/assets/js/colorbox/i18n/jquery.colorbox-' . $activeLanguageTag . '.js', false, false);
			}

			$loaded = true;
		}

		if ($class == 'a.modalj4' || $class == 'a.sharing-modal')
		{
			$options = array(
					'maxWidth'  => $width,
					'maxHeight' => $height,
				);
			$script  = 'Edocman.jQuery(document).ready(function(){Edocman.jQuery("' . $class . '").colorbox(' . self::getJSObject($options, $onClosed) . ');});';
		}
		elseif($class == 'a.edocman-modal')
		{
			$options = array(
					'maxWidth'  => '80%',
					'maxHeight' => '80%',
				);
			$script  = 'Edocman.jQuery(document).ready(function(){Edocman.jQuery("' . $class . '").colorbox(' . self::getJSObject($options, $onClosed) . ');});';
		}
		else
		{
			$options = array(
				'iframe'     => $iframe,
				'fastIframe' => false,
				'inline'     => $inline,
				'width'      => $width,
				'height'     => $height,
				'scrolling'  => $scrolling,
			);
			$script  = 'Edocman.jQuery(document).ready(function(){Edocman.jQuery(".' . $class . '").colorbox(' . self::getJSObject($options,false) . ');});';
		}

		//Factory::getDocument()->getWebAssetManager()->addInlineScript($script);
	}

	/**
	 * Convert an array to js object
	 *
	 * @param array $array
	 *
	 * @return string
	 */
	public static function getJSObject(array $array = array(), $onClosed = false)
	{
		$object = '{';

		// Iterate over array to build objects
		foreach ((array) $array as $k => $v)
		{
			if (is_null($v))
			{
				continue;
			}

			if ($v === 'true')
			{
				$v = true;
			}

			if ($v === 'false')
			{
				$v = false;
			}

			if (is_bool($v))
			{
				$object .= ' ' . $k . ': ';
				$object .= ($v) ? 'true' : 'false';
				$object .= ',';
			}
			elseif (!is_array($v) && !is_object($v))
			{
				$object .= ' ' . $k . ': ';
				$object .= (is_numeric($v) || strpos($v, '\\') === 0) ? (is_numeric($v)) ? $v : substr($v, 1) : "'" . $v . "'";
				$object .= ',';
			}
			else
			{
				$object .= ' ' . $k . ': ' . self::getJSObject($v) . ',';
			}
		}

		if($onClosed)
		{
			$object .= 'onClosed:function() { location.reload(true); },';
		}

		if (substr($object, -1) == ',')
		{
			$object = substr($object, 0, -1);
		}

		$object .= '}';

		return $object;
	}
	
}
