<?php

defined('_JEXEC') or die('Restricted access');

	class HelperAdmin{
		
		private static $arrScripts = array();
		private static $arrStyles = array();
		private static $serial = 1;
		
		
		
		/**
		 * print scripts, empty array
		 */
		public static function printScripts(){
			
			foreach(self::$arrScripts as $script){
				?>
				<script src="<?php echo $script?>" type="text/javascript"></script>
				<?php 
			}
			
			if(class_exists("UniteProviderFunctionsUG"))
				UniteProviderFunctionsUG::printScripts();
			
			self::$arrScripts = array();
		}
		
		/**
		 * print styles
		 */
		public static function printStyles(){
			
			if(class_exists("UniteProviderFunctionsUG"))
				UniteProviderFunctionsUG::printStyles();
			
		}
		
		
		/**
		 *
		 * register script helper function
		 * @param $scriptFilename
		 */
		public static function addScript($scriptName, $folder="template/js", $handle=null){
			if($handle == null){
				self::$serial++;
				$handle = "script-".self::$serial;
			}
			
			$urlScript = ConfigCMS::$urlBase.$folder."/".$scriptName.".js";
			
			self::$arrScripts[$handle] = $urlScript;
		}
		
		
		/**
		 *
		 * register common script helper function
		 * the handle for the common script is coming without plugin name
		 */
		public static function addScriptCommon($scriptName, $handle=null, $folder="template/js"){
			if($handle == null)
				$handle = $scriptName;
					
			self::addScript($scriptName, $folder, $handle);
		}

		
		/**
		 *
		 * register script helper function
		 * @param $scriptFilename
		 */
		public static function addScriptAbsoluteUrl($urlScript, $handle = null){
			
			if(empty($handle)){
				self::$serial++;
				$handle = "script-".self::$serial;
			}
			
			self::$arrScripts[] = $urlScript;
			
		}
		
		
		/**
		 *
		 * register style helper function
		 * @param $styleFilename
		 */
		public static function addStyle($styleName,$handle=null,$folder="css"){
			if($handle == null)
				$handle = GlobalsUG::PLUGIN_NAME."-".$styleName;
			
			UniteProviderFunctionsUG::addStyle($handle, GlobalsUG::$urlPlugin .$folder."/".$styleName.".css");
			
		}
		
		
		/**
		 *
		 * register common script helper function
		 * the handle for the common script is coming without plugin name
		 */
		public static function addStyleCommon($styleName,$handle=null,$folder="css"){
			if($handle == null)
				$handle = $styleName;
			self::addStyle($styleName,$handle,$folder);
		
		}
		
		
		/**
		 *
		 * register style absolute url helper function
		 */
		public static function addStyleAbsoluteUrl($styleUrl, $handle){
			
			UniteProviderFunctionsUG::addStyle($handle, $styleUrl);
			
		}
				
		
		/**
		 * helper admin functions
		 */
		public static function isSSL(){
			if (empty($_SERVER['HTTPS'])) {
				return false;
			}
			return true;
		}
		
		/**
		 * throw error
		 */
		public static function throwError($message,$code=null){
			if(!empty($code))
				throw new Exception($message);
			else
				throw new Exception($message);
		}
		

		/**
		 * get variable from post or from get. get wins.
		 */
		public static function getPostGetVar($name,$initVar = ""){
			$var = $initVar;
			if(isset($_POST[$name])) $var = $_POST[$name];
			else if(isset($_GET[$name])) $var = $_GET[$name];
			return($var);
		}
		
		
		/**
		 * get post var
		 */
		public static function getPostVar($name,$initVar = ""){
			$var = $initVar;
			if(isset($_POST[$name])) $var = $_POST[$name];
			return($var);
		}
		
		
		/**
		 * get get variable
		 */
		public static function getGetVar($name,$initVar = ""){
			$var = $initVar;
			if(isset($_GET[$name])) $var = $_GET[$name];
			return($var);
		}
	
		
		/**
		 * get value from array. if not - return alternative
		 */
		public static function getVal($arr, $key, $altVal=""){
						
			if(isset($arr[$key]))
			  return($arr[$key]);
			
			return($altVal);
		}
				
		
		/**
		 *
		 * validate that some file exists, if not - throw error
		 */
		public static function validateFilepath($filepath,$errorPrefix=null){
			if(file_exists($filepath) == true)
				return(false);
			if($errorPrefix == null)
				$errorPrefix = "File";
			$message = $errorPrefix." $filepath not exists!";
			self::throwError($message);
		}
		
		/**
		 * validate not empty
		 */
		public static function validateNotEmpty($var, $varName = "variable"){
			
			if(empty($var))
				self::throwError("The variable <b>{$varName}</b> shold not be empty: ");
			
		}
		
		/**
		 * get path view
		 */
		public static function getPathView($viewName, $path=null, $validate = true){
			
			if($path == null)
				$path = ConfigCMS::$pathViews;
			
			$viewPath = $path.$viewName.".php";
			
			if($validate == true)
				HelperAdmin::validateFilepath($viewPath,"View");
			
			return($viewPath);
		}
		
		
		/**
		 * get view url
		 */
		public static function getUrlView($view = "galleries", $params = ""){
			
			$url = ConfigCMS::$urlBase."index.php";
			
			if(!empty($view))
				$url .= "?view=".$view;
			
			if(!empty($params))
				$url .= "&".$params;
			
			return($url);
		}
		
	}

	
	//---------------------------------------------------------------------------------------------------------------------	
	
	if(!function_exists("dmp")){
		function dmp($str){
			echo "<div align='left'>";
			echo "<pre>";
			print_r($str);
			echo "</pre>";
			echo "</div>";
		}
	}
		
?>