<?php
/**
 * @package com_spmedical
 * @author JoomShaper http://www.joomshaper.com
 * @copyright Copyright (c) 2010 - 2025 JoomShaper
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
 */

// No Direct Access
defined ('_JEXEC') or die('Restricted Access');

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\Component\ComponentHelper;

class SpmedicalHelper
{

	public static function getItemid($view = 'specialists')
	{
		$db = Factory::getDbo();

		$query = $db->getQuery(true);
		$query->select($db->quoteName(array('id')));
		$query->from($db->quoteName('#__menu'));
		$query->where($db->quoteName('link') . ' LIKE '. $db->quote('%option=com_spmedical&view='. $view .'%'));
		$query->where($db->quoteName('published') . ' = '. $db->quote('1'));
		$query->where($db->quoteName('client_id') . ' = 0');		
		if (Multilanguage::isEnabled()) {
			$query->where('language in (' . $db->quote(Factory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
		}		
		$db->setQuery($query);
		$result = $db->loadResult();

		if($result)
		{
			return '&Itemid=' . $result;
		}

		return;
	}

	// Item Meta
	public static function itemMeta($meta = array())
	{
		$config 	= Factory::getConfig();
		$app 		= Factory::getApplication();
		$doc 		= Factory::getDocument();
		$menus   	= $app->getMenu();
		$menu 		= $menus->getActive();
		$title 		= '';

		//Title
		if (isset($meta['title']) && $meta['title'])
		{
			$title = $meta['title'];
		}
		else
		{
			if ($menu)
			{
				if ($menu->getParams()->get('page_title', ''))
				{
					$title = $menu->getParams()->get('page_title');
				}
				else
				{
					$title = $menu->title;
				}
			}
		}
		
		//Include Site title
		$sitetitle = $title;
		if ($config->get('sitename_pagetitles')==2)
		{
			$sitetitle = $title . ' | ' . $config->get('sitename');
		}
		elseif ($config->get('sitename_pagetitles')===1)
		{
			$sitetitle = $config->get('sitename') . ' | ' . $title;
		}

		$doc->setTitle($sitetitle);
		$doc->addCustomTag('<meta property="og:title" content="' . $title . '" />');

		//Keywords
		if (isset($meta['keywords']) && $meta['keywords'])
		{
			$keywords = $meta['keywords'];
			$doc->setMetadata('keywords', $keywords);
		}
		else
		{
			if ($menu)
			{
				if ($menu->getParams()->get('menu-meta_keywords'))
				{
					$keywords = $menu->getParams()->get('menu-meta_keywords');
					$doc->setMetadata('keywords', $keywords);
				}
			}
		}

		//Metadescription
		if (isset($meta['metadesc']) && $meta['metadesc'])
		{
			$metadesc = $meta['metadesc'];
			$doc->setDescription($metadesc);
			$doc->addCustomTag('<meta property="og:description" content="'. $metadesc .'" />');
		}
		else
		{
			if ($menu)
			{
				if ($menu->getParams()->get('menu-meta_description'))
				{
					$metadesc = $menu->getParams()->get('menu-meta_description');
					$doc->setDescription($menu->getParams()->get('menu-meta_description'));
					$doc->addCustomTag('<meta property="og:description" content="'. $metadesc .'" />');
				}
			}
		}

		//Robots
		if ($menu)
		{
			if ($menu->getParams()->get('robots'))
			{
				$doc->setMetadata('robots', $menu->getParams()->get('robots'));
			}
		}

		//Open Graph
		foreach ( $doc->_links as $k => $array )
		{
			if ( $array['relation'] == 'canonical' )
			{
				unset($doc->_links[$k]);
			}
		} // Remove Joomla canonical

		$doc->addCustomTag('<meta property="og:type" content="website" />');
		$doc->addCustomTag('<link rel="canonical" href="' . Uri::current() . '" />');
		$doc->addCustomTag('<meta property="og:url" content="' . Uri::current() . '" />');

		if (isset($meta['image']) && $meta['image'])
		{
			$doc->addCustomTag('<meta property="og:image" content="'. $meta['image'] .'" />');
			$doc->addCustomTag('<meta property="og:image:width" content="600" />');
			$doc->addCustomTag('<meta property="og:image:height" content="315" />');
		}
	}

	// Get departments
	public static function getDepartments($short = false)
	{

		$db = Factory::getDbo();
		$query = $db->getQuery(true);
		// Order it by the ordering field.
		if ($short)
		{
			$query->select($db->quoteName(array('id', 'title', 'alias')));
		}
		else
		{
			$query->select($db->quoteName(array('id', 'title', 'alias', 'description' , 'image', 'treatments', 'investigations', 'others_services')));
		}
		$query->from($db->quoteName('#__spmedical_departments'));
		$query->where($db->quoteName('published')." = 1");
		$query->where('language in (' . $db->quote(Factory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
		$query->order('ordering DESC');
		$db->setQuery($query);

		$results = $db->loadObjectList();
		foreach ($results as $result)
		{
			$result->url = Route::_('index.php?option=com_spmedical&view=department&id='.$result->id.':'.$result->alias . SpmedicalHelper::getItemid('departments'));
		}

		return $results;
	}

	// Get Specialists
	public static function getSpecialists($short = false)
	{
		$db = Factory::getDbo();
		$query = $db->getQuery(true);
		// Order it by the ordering field.
		if ($short)
		{
			$query->select($db->quoteName(array('id', 'title', 'alias')));
		}
		else
		{
			$query->select('*');
		}
		$query->from($db->quoteName('#__spmedical_specialists'));
		$query->where($db->quoteName('published')." = 1");
		$query->where('language in (' . $db->quote(Factory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
		$query->order('ordering DESC');
		$db->setQuery($query);
		
		$results = $db->loadObjectList();
		foreach ($results as $result)
		{
			$result->url = Route::_('index.php?option=com_spmedical&view=department&id='.$result->id.':'.$result->alias . SpmedicalHelper::getItemid('departments'));
		}

		return $results;
	}

	// Get Specialist from all specialists
	public static function getSpecialities()
	{
		$db = Factory::getDbo();
		$query = $db->getQuery(true);
		// Order it by the ordering field.
		$query->select($db->quoteName(array('specialitist_on')));
		$query->from($db->quoteName('#__spmedical_specialists'));
		$query->where($db->quoteName('published')." = 1");
		$query->order('ordering DESC');
		$db->setQuery($query);
		$results = $db->loadObjectList();

		$marge_specialities = implode(', ', array_map(function ($item) {
			return $item->specialitist_on;
		}, $results));

		$explode_specialities = explode(', ' , $marge_specialities);

		$specialities = array();
		foreach($explode_specialities as $speciality)
		{
			$hash = str_replace(array(' ', '.'), array('-', ''), strtolower($speciality) );
			$specialities[$hash] = $speciality;
		}

		return $specialities;
	}

	//check json validation
	public static function isJson($string)
	{
		json_decode($string);
		return (json_last_error() == JSON_ERROR_NONE);
	}

	// Generate Currency
	public static function generateCurrency($amt = 0)
	{

		//Joomla Component Helper & Get LMS Params
		$params = ComponentHelper::getParams('com_spmedical');
		//Get Currency
		$currency = explode(':', $params->get('currency', 'USD:$'));
		//$currency =  $currency[1];
		switch ($currency[0]) {
			case 'USD':
				$lancode = 'en_GB';
				break;

			case 'GBP':
				$lancode = 'en_GB';
				break;

			case 'RUB':
				$lancode = 'ru_RU';
				break;

			case 'BRL':
				$lancode = 'pt_BR';
				break;

			case 'CAD':
				$lancode = 'en_CA';
				break;

			case 'CZK':
				$lancode = 'cs_CZ';
				break;

			case 'DKK':
				$lancode = 'en_DK';
				break;

			case 'EUR':
				$lancode = 'fr_FR';
				break;

			case 'HKD':
				$lancode = 'zh_HK';
				break;

			case 'HUF':
				$lancode = 'hu_HU';
				break;

			case 'ILS':
				$lancode = 'zh_HK';
				break;

			case 'JPY':
				$lancode = 'ja_JP';
				break;

			case 'MXN':
				$lancode = 'es_MX';
				break;

			case 'NOK':
				$lancode = 'nb_NO';
				break;

			case 'NZD':
				$lancode = 'en_GB';
				break;

			case 'PHP':
				$lancode = 'en_PH';
				break;

			case 'PLN':
				$lancode = 'pl_PL';
				break;

			case 'SGD':
				$lancode = 'zh_SG';
				break;

			case 'SEK':
				$lancode = 'sv_SE';
				break;

			case 'CHF':
				$lancode = 'de_LI';
				break;

			case 'TWD':
				$lancode = 'zh_TW';
				break;

			case 'THB':
				$lancode = 'th_TH';
				break;

			case 'TRY':
				$lancode = 'tr_TR';
				break;

			default:
				$lancode = 'en_GB';
				break;
		}

		$allowedCurrency = ['EUR', 'RUB', 'CZK', 'HUF', 'PLN'];

		if (\in_array($currency[0], $allowedCurrency))
		{
			$fmt = numfmt_create($lancode, NumberFormatter::CURRENCY);
			return numfmt_format_currency($fmt, $amt, $currency[0]);
		}
		
		$fmt = numfmt_create($lancode, NumberFormatter::CURRENCY);
		return numfmt_format_currency($fmt, $amt, $currency[0]);
	}

}