<?php
/**
 * @package Unlimited Elements
 * @author UniteCMS.net
 * @copyright (C) 2017 Unite CMS, All Rights Reserved. 
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 * */
defined('UNLIMITED_ELEMENTS_INC') or die('Restricted access');


/**
 * 
 * creator helper functions class
 *
 */
	class HelperHtmlUC extends UniteHelperBaseUC{
		
		private static $isGlobalJSPut = false;
		private static $isPutBrowsersOnce = false;
		
		
		protected function z_GETTERS(){}

		
		/**
		 *
		 * get link html
		 */
		public static function getHtmlLink($link,$text,$id="",$class="", $isNewWindow = false){
		
			if(!empty($class))
				$class = " class='$class'";
		
			if(!empty($id))
				$id = " id='$id'";
		
			$htmlAdd = "";
			if($isNewWindow == true)
				$htmlAdd = ' target="_blank"';
		
			$html = "<a href=\"$link\"".$id.$class.$htmlAdd.">$text</a>";
			return($html);
		}

		
		/**
		 *
		 * get select from array
		 */
		public static function getHTMLSelect($arr,$default="",$htmlParams="",$assoc = false, $addData = null, $addDataText = null){
		
			$html = "<select $htmlParams>";
			//add first item
			if($addData == "not_chosen"){
				$selected = "";
				$default = trim($default);
				if(empty($default))
					$selected = " selected ";
					
				$itemText = $addDataText;
				if(empty($itemText))
					$itemText = "[".esc_html__("not chosen", "unlimited-elements-for-elementor")."]";
					
				$html .= "<option $selected value=''>{$itemText}</option>";
			}
			
			foreach($arr as $key=>$item){
				$selected = "";
		
				if($assoc == false){
					if($item == $default) 
						$selected = " selected ";
				}
				else{
					if(trim($key) == trim($default))
						$selected = " selected ";
				}
				
				$addHtml = "";
				if(strpos($key, "html_select_sap") !== false)
					$addHtml = " disabled";
				
				if($assoc == true)
					$html .= "<option $selected value='$key' $addHtml>$item</option>";
				else
					$html .= "<option $selected value='$item' $addHtml>$item</option>";
			}
			$html.= "</select>";
			return($html);
		}
		
		
		/**
		 * get row of addons table
		 */
		public static function getTableAddonsRow($addonID, $title){
			
			$editLink = HelperUC::getViewUrl_EditAddon($addonID);
			
			$htmlTitle = htmlspecialchars($title);
			
			$html = "<tr>\n";
			$html.= "<td><a href='{$editLink}'>{$title}</a></td>\n";
			$html.= "	<td>\n";
			$html.= " 	  <a href='{$editLink}' class='unite-button-secondary float_left mleft_15'>". esc_html__("Edit","unlimited-elements-for-elementor") . "</a>\n";
			$html.= "		<a href='javascript:void(0)' data-addonid='{$addonID}' class='uc-button-delete unite-button-secondary float_left mleft_15'>".esc_html__("Delete","unlimited-elements-for-elementor")."</a>";
			$html.= "		<span class='loader_text uc-loader-delete mleft_10' style='display:none'>" . esc_html__("Deleting", "unlimited-elements-for-elementor") . "</span>";
			$html.= "		<a href='javascript:void(0)' data-addonid='{$addonID}' class='uc-button-duplicate unite-button-secondary float_left mleft_15'>" . esc_html__("Duplicate","unlimited-elements-for-elementor")."</a>\n";
			$html.= "		<span class='loader_text uc-loader-duplicate mleft_10' style='display:none'>" . esc_html__("Duplicating", "unlimited-elements-for-elementor") . "</span>";
			$html.= "		<a href='javascript:void(0)' data-addonid='{$addonID}' data-title='{$htmlTitle}' class='uc-button-savelibrary unite-button-secondary float_left mleft_15'>" . esc_html__("Save To Library","unlimited-elements-for-elementor")."</a>\n";
			$html.= "		<span class='loader_text uc-loader-save mleft_10' style='display:none'>" . esc_html__("Saving to library", "unlimited-elements-for-elementor") . "</span>";
			$html.= "	</td>\n";
			$html.= "	</tr>\n";
			
			return($html);
		}

		
		/**
		 * get global js output for plugin pages
		 */
		public static function getGlobalJsOutput(){
			
			//insure that this function run only once
			if(self::$isGlobalJSPut == true)
				return("");
			
						
			self::$isGlobalJSPut = true;
			
			$jsArrayText = UniteFunctionsUC::phpArrayToJsArrayText(GlobalsUC::$arrClientSideText,"				");
			
			//prepare assets path
			$pathAssets = HelperUC::pathToRelative(GlobalsUC::$pathAssets, false);
			$pathAssets = urlencode($pathAssets);
			
			//check catalog
			$objWebAPI = new UniteCreatorWebAPI();
			$isNeedCheckCatalog = $objWebAPI->isTimeToCheckCatalog();
			
			$arrGeneralSettings = array();
			$arrGeneralSettings["color_picker_type"] = HelperUC::getGeneralSetting("color_picker_type");
			
			$arrGeneralSettings = UniteProviderFunctionsUC::applyFilters(UniteCreatorFilters::FILTER_CLIENTSIDE_GENERAL_SETTINGS, $arrGeneralSettings);
			
			//dmp($arrGeneralSettings);exit();
			
			$strGeneralSettings = UniteFunctionsUC::jsonEncodeForClientSide($arrGeneralSettings);
			
			$js = "";
			$js .= self::TAB2.'var g_pluginNameUC = "'.GlobalsUC::PLUGIN_NAME.'";'.self::BR;
			$js .= self::TAB2.'var g_pathAssetsUC = decodeURIComponent("'.$pathAssets.'");'.self::BR;
			$js .= self::TAB2.'var g_urlAjaxActionsUC = "'.GlobalsUC::$url_ajax.'";'.self::BR;
			$js .= self::TAB2.'var g_urlViewBaseUC = "'.GlobalsUC::$url_component_admin.'";'.self::BR;
			$js .= self::TAB2.'var g_urlViewBaseNowindowUC = "'.GlobalsUC::$url_component_admin_nowindow.'";'.self::BR;
			$js .= self::TAB2.'var g_urlBaseUC = "'.GlobalsUC::$url_base.'";'.self::BR;
			$js .= self::TAB2.'var g_urlAssetsUC = "'.GlobalsUC::$url_assets.'";'.self::BR;
			$js .= self::TAB2.'var g_settingsObjUC = {};'.self::BR;
			$js .= self::TAB2.'var g_ucAdmin;'.self::BR;
			
			
			if(GlobalsUC::$is_admin_debug_mode == true)
				$js .= self::TAB2.'var g_ucDebugMode=true;'.self::BR;
			
			$js .= self::TAB2."var g_ucGeneralSettings = {$strGeneralSettings};".self::BR;
			
			if(GlobalsUC::$enableWebCatalog == true){
				if($isNeedCheckCatalog)
					$js .= self::TAB2.'var g_ucCheckCatalog = true;'.self::BR;
										
				$js .= self::TAB2.'var g_ucCatalogAddonType="'.GlobalsUC::$defaultAddonType.'";'.self::BR;
			}
						
			//output icons
			$jsonFaIcons = UniteFontManagerUC::fa_getJsonIcons();
			$js .= self::TAB2.'var g_ucFaIcons = '.$jsonFaIcons.';'.self::BR;
			
			//output shapes
			$objShapes = new UniteShapeManagerUC();
			$jsonShapes = $objShapes->getJsonShapes();
			$js .= self::TAB2.'var g_ucArrSvgShapes = '.$jsonShapes.';'.self::BR;
			
			
			//get nonce
			if(method_exists("UniteProviderFunctionsUC", "getNonce"))
				$js .= self::TAB2 . "var g_ucNonce='".UniteProviderFunctionsUC::getNonce()."';";
			
			$js .= self::TAB2.'var g_uctext = {'.self::BR;
			$js .= self::TAB3.$jsArrayText.self::BR;
			$js .= self::TAB2.'};'.self::BR;
					
			return($js);
		}
		
		
		/**
		 * get flobal debug divs
		 */
		public static function getGlobalDebugDivs(){
			$html = "";
			
			$html .= self::TAB2.'<div id="div_debug" class="unite-div-debug"></div>'.self::BR;
			$html .= self::TAB2.'<div id="debug_line" style="display:none"></div>'.self::BR;
			$html .= self::TAB2.'<div id="debug_side" style="display:none"></div>'.self::BR;
			$html .= self::TAB2.'<div class="unite_error_message" id="error_message" style="display:none;"></div>'.self::BR;
			$html .= self::TAB2.'<div class="unite_success_message" id="success_message" style="display:none;"></div>'.self::BR;
			
			return($html);
		}
		
		
		
		
		/**
		 * get version text
		 */
		public static function getVersionText(){
			$filepath = GlobalsUC::$pathPlugin."release_log.txt";
			$content = file_get_contents($filepath);
			
			return($content);
			
		}
		
		/**
		 * get error message html
		 */
		public static function getErrorMessageHtml($message, $trace = ""){
		
			$html = '<div class="unite-error-message">';
			$html .= '<div style="unite-error-message-inner">';
			$html .= $message;
		
			if(!empty($trace)){
				$html .= '<div class="unite-error-message-trace">';
				$html .= "<pre>{$trace}</pre>";
				$html .= "</div>";
			}
		
			$html .= '</div></div>';
		
			return($html);
		}
		
		/**
		 * get settings html
		 */
		public static function getHtmlSettings($filename, $formID, $arrValues = array()){
			
			ob_start();
			
			$html = self::putHtmlSettings($filename, $formID, $arrValues);
			$html = ob_get_contents();
			
			ob_clean();
			ob_end_clean();
		
			return($html);			
		}
		
		
		/**
		 * get custom scripts from array of scripts
		 */
		public static function getHtmlCustomScripts($arrScripts){
			
			if(empty($arrScripts))
				return("");
			
			if(is_array($arrScripts) == false){
				UniteFunctionsUC::throwError("arrScripts should be array");
			}
			
			if(count($arrScripts) == 1 && empty($arrScripts[0]))
				return("");
			
			$html = "<script type='text/javascript'>".self::BR;
			
			foreach($arrScripts as $script){
				$html .= $script.self::BR;
			}
			
			$html .= "</script>".self::BR;
			
			return($html);
		}
		
		/**
		 * get custom html styles
		 */
		public static function getHtmlCustomStyles($arrStyles, $wrapInTag = true){
			
			if(empty($arrStyles))
				return("");
			
			$css = "";
			
			if(is_array($arrStyles) == false)
				$css = $arrStyles;
			else{	//if array
				if(count($arrStyles) == 1 && empty($arrStyles[0]))
					return("");				
								
				foreach($arrStyles as $style)
					$css .= $style.self::BR;
				
			}
			
			if($wrapInTag == false)
				return($css);
			
			$html = "<style type='text/css'>".self::BR;
			$html .= $css;
			$html .= "</style>".self::BR;
			
			return($html);
		}
		
		/**
		 * get css include
		 */
		public static function getHtmlCssInclude($url){
			
			$html = "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$url}\">";
			
			return($html);
		}
		
		
		/**
		 * get css include
		 */
		public static function getHtmlJsInclude($url){
						
			$html = "<script type=\"text/javascript\" src=\"{$url}\"></script>";
			
			return($html);
		}
		
		
		/**
		 * 
		 * get html of some includes
		 */
		private static function getHtmlIncludes($type, $arrIncludes, $tab = null){
						
			if(empty($arrIncludes))
				return("");
			
			$html = "";
			foreach($arrIncludes as $urlInclude){
				if($tab !== null)
					$html .= $tab;
					
				if($type == "css")
					$html .= self::getHtmlCssInclude($urlInclude).self::BR;
				else 
					$html .= self::getHtmlJsInclude($urlInclude).self::BR;
				
			}
			
			return($html);
			
		}
		
		/**
		 * get html includes from array includes
		 */
		public static function getHtmlCssIncludes($arrCssIncludes, $tab = null){
			
			return self::getHtmlIncludes("css", $arrCssIncludes, $tab);
		}
		
		/**
		 * get html includes from array includes
		 */
		public static function getHtmlJsIncludes($arrJsIncludes, $tab = null){
			
			return self::getHtmlIncludes("js", $arrJsIncludes, $tab);
		}
		
		
		protected function z_________PUTTERS_______(){}
		
		
		/**
		 * put all browser dialogs of all addon types
		 */
		public static function putAddonTypesBrowserDialogs($filterType = null, $objLayoutType = null){
			
			if(self::$isPutBrowsersOnce == true)
				return(false);
			
			self::$isPutBrowsersOnce = true;

			$arrMultipleTypesRegular = null;
			if(!empty($objLayoutType) && !empty($objLayoutType->arrLayoutBrowserAddonTypes))
				$arrMultipleTypesRegular = $objLayoutType->arrLayoutBrowserAddonTypes;

			
			//put other addon type browser
			
			if(!empty($filterType)){
				
				if(is_array($arrTypes))
					$arrTypes = $filterType;
				else
					$arrTypes = array($filterType);
			}else
				$arrTypes = array(
					GlobalsUC::ADDON_TYPE_REGULAR_ADDON, 
					GlobalsUC::ADDON_TYPE_SHAPE_DEVIDER, 
					GlobalsUC::ADDON_TYPE_LAYOUT_SECTION,
					GlobalsUC::ADDON_TYPE_SHAPES,
					GlobalsUC::ADDON_TYPE_BGADDON,
				);
			
			
			foreach($arrTypes as $addontype){
				
				if($addontype == GlobalsUC::ADDON_TYPE_REGULAR_ADDON && !empty($arrMultipleTypesRegular))
						$addontype = $arrMultipleTypesRegular;
				
				$objBrowser = new UniteCreatorBrowser();
				$objBrowser->initAddonType($addontype);
				$objBrowser->putBrowser();
			}
			
			
		}
		
		
		
		/**
		 * put global framework
		 */
		public static function putGlobalsHtmlOutput(){
			
			if(self::$isGlobalJSPut == true)
				return(false);
						
			?>
			<script type="text/javascript">
				
				<?php echo self::getGlobalJsOutput();?>
				
			</script>
			
			<?php 
				
				echo self::getGlobalDebugDivs();
				
				if(method_exists("UniteProviderFunctionsUC", "putMasterHTML"))
					UniteProviderFunctionsUC::putMasterHTML() 
			?>			
			
			<?php 			
		}

		
		/**
		 * put control fields notice to dialogs that use it
		 */
		public static function putDialogControlFieldsNotice(){
			?>
				<div class="unite-inputs-sap"></div> 
			
				<div class="unite-inputs-label unite-italic">
					* <?php esc_html_e("only dropdown and radio boolean field types are used for conditional inputs", "unlimited-elements-for-elementor")?>.
				</div>
			
			<?php 
		}
		
		
		/**
		 * put dialog actions
		 */
		public static function putDialogActions($prefix, $buttonTitle, $loaderTitle, $successTitle, $buttonClass="primary"){
			
			$prefix = esc_attr($prefix);
			
			?>
				<div id="<?php echo esc_attr($prefix)?>_actions_wrapper" class="unite-dialog-actions">
					
					<a id="<?php echo esc_attr($prefix)?>_action" href="javascript:void(0)" class="unite-button-<?php echo esc_attr($buttonClass)?>"><?php echo esc_html($buttonTitle)?></a>
					<div id="<?php echo esc_attr($prefix)?>_loader" class="loader_text" style="display:none"><?php echo esc_html($loaderTitle)?></div>
					<div id="<?php echo esc_attr($prefix)?>_error" class="unite-dialog-error"  style="display:none"></div>
					<div id="<?php echo esc_attr($prefix)?>_success" class="unite-dialog-success" style="display:none"><?php echo esc_html($successTitle)?></div>
					
				</div>
			<?php 
		}
		
		/**
		 * put plugin version html
		 */
		public static function putPluginVersionHtml(){
			
			$objPlugins = new UniteCreatorPlugins();
			
			$arrPlugins = $objPlugins->getArrPlugins();
			
			if(empty($arrPlugins))
				return(false);
						
			foreach($arrPlugins as $plugin){
				
				$name = UniteFunctionsUC::getVal($plugin, "name");
				$title = UniteFunctionsUC::getVal($plugin, "title");
				$version = UniteFunctionsUC::getVal($plugin, "version");
				$silentMode = UniteFunctionsUC::getVal($plugin, "silent_mode");
				$silentMode = UniteFunctionsUC::strToBool($silentMode);
				
				if($silentMode == true)
					continue;
				
				switch($name){
					case "create_addons":
							$title = "Create addons plugin {$version}";
					break;
					default:
						$title = "$title {$version}";
					break;
				}
				
				echo ", ";
				
				echo UniteProviderFunctionsUC::escCombinedHtml($title);
			}
			
		}

		/**
		 * output exception
		 */
		public static function outputError(Error $e){
						
			$message = $e->getMessage();
			$trace = $e->getTraceAsString();
			$line = $e->getLine();
			$file = $e->getFile();
			
			dmp("PHP Error Occured!!!");
			
			dmp("<b>$message </b>");
			dmp("in file: <b>$file</b> (line: <b>$line</b>)");
			dmp("trace: ");
			dmp($trace);
				
		}
		
		
		/**
		 * output exception
		 */
		public static function outputException($e, $prefix="", $forceTrace = false){
			
			if(empty($prefix))
				$prefix = GlobalsUC::$currentPluginTitle." Error: ";
			
			$message = $prefix.$e->getMessage();
			$trace = $e->getTraceAsString();
			
			echo "<div style='color:darkred;'>";
			
			dmp($message);
			
			if(GlobalsUC::SHOW_TRACE == true || $forceTrace === true)
				dmp($trace);
			else
				if($e instanceof Error)
					dmp($trace);
					
			echo "</div>";
		}
		
		
		
		
		/**
		 * output exception in a box
		 */
		public static function outputExceptionBox($e, $prefix=""){
			
			$message = $e->getMessage();
			
			if(!empty($prefix))
				$message = $prefix.":  ".$message;
			
			$trace = "";
			
			$showTrace = GlobalsUC::SHOW_TRACE_FRONT;
			if(UniteProviderFunctionsUC::isAdmin() == true)
				$showTrace = GlobalsUC::SHOW_TRACE;
				
			if($showTrace)
				$trace = $e->getTraceAsString();
			
			$html = self::getErrorMessageHtml($message, $trace);
			
			echo UniteProviderFunctionsUC::escCombinedHtml($html);
		}
		
		/**
		 * get hidden input field
		 */
		public static function getHiddenInputField($name, $value){
			$value = htmlspecialchars($value);
			
			$name = esc_attr($name);
			$value = esc_attr($value);
			
			$html = '<input type="hidden" name="'.$name.'" value="'.$value.'">';
			
			return($html);
		}
		
		
		/**
		 * put settings html from filepath
		 */
		public static function putHtmlSettings($filename, $formID, $arrValues = array(), $pathSettings = null){
			
			if($pathSettings === null)
				$pathSettings = GlobalsUC::$pathSettings;
			
			$filepathSettings = $pathSettings."{$filename}.xml";
			
			UniteFunctionsUC::validateFilepath($filepathSettings, "settings file - {$filename}.xml");
			
			$settings = new UniteSettingsAdvancedUC();
			$settings->loadXMLFile($filepathSettings);
			
			if(!empty($arrValues))
				$settings->setStoredValues($arrValues);
			
			$output = new UniteSettingsOutputWideUC();
			$output->init($settings);
			$output->draw($formID);
			
		}
		
		
		/**
		 * draw settings and get html output
		 */
		public static function drawSettingsGetHtml($settings, $formName){
			
			$output = new UniteSettingsOutputWideUC();
			$output->init($settings);
			
			ob_start();
			$output->draw($formName);
			
			$htmlSettings = ob_get_contents();
			
			ob_end_clean();
					
			return($htmlSettings);
		}
		
		
		
		/**
		 * draw settings
		 */
		public static function drawSettings($settings, $formName = null){
			
			$output = new UniteSettingsOutputWideUC();
			
			$drawForm = false;
			if(!empty($formName))
				$drawForm = true;
			
			$output->init($settings);
			$output->draw($formName, $drawForm);
			
		}
		
		
		/**
		 * output memory log html
		 */
		public static function outputMemoryUsageLog(){
			$arrLog = HelperUC::getLastMemoryUsage();
			
			if(empty($arrLog)){
				echo "no memory log found";
				return(false);
			}
			
			$timestamp = $arrLog[0]["time"];
			$date = UniteFunctionsUC::timestamp2DateTime($timestamp);
			
			$urlPage = UniteFunctionsUC::getVal($arrLog[0], "current_page");
			
			?>
			<div class="unite-title1">Last log from: <b><?php echo esc_html($date)?></b></div>
			<div>Page: <b><?php echo esc_html($urlPage)?></b></div>
			<br>
			
			<table class="unite-table">
				<tr>
					<th>
						Operation
					</th>
					<th>
						Usage
					</th>
					<th>
						Diff
					</th>
				</tr>
			
			<?php 
			
			foreach($arrLog as $item):
				$operation  = $item["oper"];
				$usage = $item["usage"];
				$diff = $item["diff"];
				
				$usage = number_format($usage);
				$diff = number_format($diff);
				
				?>
				<tr>
					<td>
						<?php echo esc_html($operation)?>
					</td>
					<td>
						<?php echo esc_html($usage)?>
					</td>
					<td>
						<?php echo esc_html($diff)?>
					</td>
				</tr>
				<?php 
							
			endforeach;
			?>
			</table>
			<?php 
		}

		
		/**
		 * put admin notices html
		 */
		public static function putHtmlAdminNotices(){
			
			$arrNotices = HelperUC::getAdminNotices();
			if(empty($arrNotices))
				return(false);
			
			$html = "";
			foreach($arrNotices as $notice){
				$notice = esc_attr($notice);
				$html .= "\n<div class='unite-admin-notice'>{$notice}</div>\n";
			}
			
			echo UniteProviderFunctionsUC::escCombinedHtml($html);
			
			//clear admin notices
			
		}
		
		/**
		 * put admin notices from footer
		 */
		public static function putFooterAdminNotices(){
			
			$arrNotices = HelperUC::getAdminNotices();
			if(empty($arrNotices))
				return(false);
			
			?>
			<script>
				jQuery(document).ready(function(){
					var objHeader = jQuery(".unite_header_wrapper");
					if(objHeader){
						<?php foreach($arrNotices as $notice):
							$notice = htmlspecialchars($notice);
							echo "objHeader.append(\"<div class=\'unite-admin-notice\'>{$notice}</div>\");";
						endforeach;
						?>
					}
				});
			</script>
			<?php 
						
		}
		
		/**
		 * put admin notices
		 */
		public static function putInternalAdminNotices(){
			
			$masterNotice = null;
			
			if(strpos(GlobalsUC::URL_API, "http://localhost") !== false)
				$masterNotice = "Dear developer, Please remove local API url in globals";
			
			if(empty($masterNotice))
				return(false);
						
			?>
			
			<div class="unite-admin-notice">
				<?php echo esc_html($masterNotice)?>
			</div>
			<?php 
		}
		
	
		
		/**
		 * wrap in media query
		 */
		public static function wrapCssMobile($css, $isTablet = false){
			
			if(empty($css))
				return($css);
			
			if(is_string($isTablet))
				$isTablet = ($isTablet == "tablet");
				
			if($isTablet == true){
				$output = "@media (max-width:768px){{$css}}";
			
			}else{
				$output = "@media (max-width:480px){{$css}}";
			}
			
			return($output);
		}
		
		
		
	} //end class
	
	
	