<?php 
/**
* @version		$Id: mod_adminlang.php 2536 2023-12-11 19:01:08Z IOS $
* @package		Elxis
* @subpackage	Module Administration language
* @copyright	Copyright (c) 2006-2024 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');


if (!class_exists('modadminLang', false)) {
	class modadminLang {

		private $lock = false;
		private $elxis_uri = '';
		private $ssl = false;
		

		/*********************/
		/* MAGIC CONSTRUCTOR */
		/*********************/
		public function __construct() {
			$eURI = eFactory::getURI();

			if (!defined('ELXIS_ADMIN')) { return; }

			$this->elxis_uri = $eURI->getComponent();
			if (($this->elxis_uri == 'cpanel') || ($this->elxis_uri.':/' == 'cpanel:/')) { $this->elxis_uri = ''; }

			$segs = $eURI->getSegments();
			$n = count($segs);

			if ($n > 0) {
				$last_segment = $segs[$n - 1];
				if (in_array($last_segment, array('add.html', 'edit.html', 'new.html', 'config.html', 'configuration.html', 'settings.html'))) { $this->lock = true; }
				$this->elxis_uri .= ($this->elxis_uri == '') ? implode('/', $segs) : ':'.implode('/', $segs);
				if (!preg_match('#\.#', $last_segment)) { $this->elxis_uri .= '/'; }
			} else {
				$this->elxis_uri .= ($this->elxis_uri != '') ? '/' : '';
			}

			$this->ssl = $eURI->detectSSL();
		}


		/********************/
		/* RUN FOREST, RUN! */
		/********************/
		public function run() {
			if (!defined('ELXIS_ADMIN')) {
				echo '<div class="elx5_warning">This module is available only in Elxis administratrion area!</div>'."\n";
				return;
			}

			if (ELXIS_INNER == 1) { return; }

			$elxis = eFactory::getElxis();
			$eLang = eFactory::getLang();

			$ilangs = $eLang->getAllLangs(false);
			if (!$ilangs) { return; }

			$baselink = $elxis->makeAURL('xx:'.$this->elxis_uri, '', $this->ssl);
			$curlang = $eLang->currentLang();

			echo '<div class="elx5_cptoptool">'."\n";
			echo '<div class="modalang_lngdropdown">'."\n";
			if (!$this->lock) {
				echo '<a href="javascript:void(null);" class="modalang_curlng">'.strtoupper($curlang).' <span class="fas fa-caret-down"></span></a>'."\n";
			} else {
				echo '<a href="javascript:void(null);" class="modalang_curlng">'.strtoupper($curlang).'</a>'."\n";
			}
			if (!$this->lock) {
				if ($ilangs) {
					$deflang = $elxis->getConfig('LANG');
					echo '<div class="modalang_content">'."\n";
					foreach ($ilangs as $lng) {
						if ($lng == $curlang) { continue; }
						if ($deflang == $lng) {
							$url = str_replace('/xx/', '/', $baselink);
						} else {
							$url = str_replace('/xx/', '/'.$lng.'/', $baselink);
						}
						echo '<a href="'.$url.'">'.strtoupper($lng).'</a>'."\n";
					}
					echo "</div>\n";
				}
			}
			echo "</div>\n";//modalang_lngdropdown
			echo "</div>\n";//elx5_cptoptool
		}

	}
}


$modadmlang = new modadminLang();
$modadmlang->run();
unset($modadmlang);

?>