<?php
/**
 * @link         http://www.openzc.cn/
 * @author       hexipeng | e-mail:2017412656@qq.com
 * @copyright    Copyright (c) 2020, Openzc, Inc.
 * @license      http://www.openzc.cn/license.txt
 */
class variable{
	private $init_static;
	function page_var_handle(){
		global $product_class;
		if(array_key_exists("main_page",$_GET)){
			$main_page=$_GET['main_page'];
			switch($main_page){
				case "create_account":
					$GLOBALS['selected_state']=zen_prepare_country_zones_pull_down($GLOBALS['selected_country']);
					$GLOBALS['country_list']=$this->get_country_list($GLOBALS['selected_country']);
				break;
			}
			if($_SESSION['customer_id']){
				$account['order_count']=zen_count_customer_orders();
			}
		}
		if(!isAjax()){
			setcookie("position",base64_encode($GLOBALS['tplpage_route'][$GLOBALS['tpl_page_body']][0]));
		}
		
	}
	function checkDirectory($GET){
		return true;
		if($GET['main_page']=="advanced_search_result"){
			return false;
		}else{
			return true;
		}
	}
	function shippingAddress($data){
		$error=false;
		$required=explode(",","zone_id,country_id,city,street_address,firstname,lastname,postcode,sendto");
		foreach($required as $k => $v){
			if(!isset($data[$v]) || !$data[$v]){
				$error=true;
			}
		}
		if($error==true){return false;}
		$address=openzcQuery("SELECT * FROM " . TABLE_ADDRESS_BOOK . " WHERE customers_id = ".$_SESSION['customer_id']." AND address_book_id = ".$data['sendto']);
		$address=openzc_table_to_list($address)[0];
		foreach($address as $k => $v){
			$key=str_replace("entry_","",$k);
			if($address[$k]!=$data[$key] && isset($data[$key])){
				@$values.=$k."='".$data[$key]."',";
			}
		}
		if($values){$values.="#";$values=str_replace(",#","",$values);}
		if($values){
			$sql="update ".TABLE_ADDRESS_BOOK." set ".$values." where address_book_id=".$data['sendto'];
			openzcQuery($sql);
		}
		return true;
	}
	function getCheckoutConfirmation(){
		global $payment_modules,$order;
		
		$billing=zen_address_format($order->billing['format_id'], $order->billing, 1, ' ', '<br />');
		define("TEXT_BILLING_ADDRESS",$billing);
		
		define("TEXT_PAYMENT_METHOD",$GLOBALS[$_SESSION['payment']]->title);
		$delivery=zen_address_format($order->delivery['format_id'], $order->delivery, 1, ' ', '<br />');
		define("TEXT_DELIVERY_ADDRESS",$delivery);
		define("TEXT_SHIPPING_METHOD",$order->info['shipping_method']);
		define("TEXT_ORDER_COMMENTS",$order->info['comments']);
	}
	function get_state_list($selected_country="",$selected_state=''){
		
		if($_GET['main_page']=="address_book_process" && isset($_GET['edit']) && is_numeric($_GET['edit']) && !$selected_country){
			$entry = openzcQuery("SELECT * FROM " . TABLE_ADDRESS_BOOK . " WHERE customers_id = ".$_SESSION['customer_id']." AND address_book_id = ".$_GET['edit']);
			$selected_country=$entry->fields['entry_country_id'];
		}
		if(!$selected_country){$selected_country=STORE_COUNTRY;}
		
		$state=zen_prepare_country_zones_pull_down($selected_country);
		
		foreach($state as $k => $v){
			$state[$k]['status']="";
			if($v['id']==$selected_state){$state[$k]['status']="active";}
		}
		return $state;
	}
	function get_country_list($selected_country) {
		
		if($_GET['main_page']=="address_book_process" && isset($_GET['edit']) && is_numeric($_GET['edit'])){
			$entry = openzcQuery("SELECT * FROM " . TABLE_ADDRESS_BOOK . " WHERE customers_id = ".$_SESSION['customer_id']." AND address_book_id = ".$_GET['edit']);
			$selected_country=$entry->fields['entry_country_id'];
		}
		if(!$selected_country){
			$selected_country=STORE_COUNTRY;
			if($_GET['main_page']=="checkout_shipping"){}
		}
		$countriesAtTopOfList = array();
		$countries_array = array(array('id' => '', 'text' => PULL_DOWN_DEFAULT));
		$countries = zen_get_countries();

		// Set some default entries at top of list:
		if (STORE_COUNTRY != SHOW_CREATE_ACCOUNT_DEFAULT_COUNTRY) $countriesAtTopOfList[] = SHOW_CREATE_ACCOUNT_DEFAULT_COUNTRY;
		$countriesAtTopOfList[] = $selected_country;
		
		foreach ($countriesAtTopOfList as $key=>$val) {
			if($val==$selected_country){
				$countries_array[] = array('id' => $val, 'text' => zen_get_country_name($val),'status'=>"active");
			}else{
				$countries_array[] = array('id' => $val, 'text' => zen_get_country_name($val),'status'=>"");
			}
		}
		// now add anything not in the defaults list:
		for ($i=0, $n=sizeof($countries); $i<$n; $i++) {
			$alreadyInList = FALSE;
			foreach($countriesAtTopOfList as $key=>$val) {
				if ($countries[$i]['countries_id'] == $val){
					$alreadyInList = TRUE;
					continue;
				}
			}
			if (!$alreadyInList) $countries_array[] = array('id' => $countries[$i]['countries_id'], 'text' => $countries[$i]['countries_name'],'status'=>"");
		}
		
		return $countries_array;
	}
	
	function init_template_file($file=""){

		$page=["tpl_index_categories","tpl_index_product_list","tpl_featured_products_default","tpl_specials_default","tpl_advanced_search_default"];
		$error="true";
		foreach($page as $k => $v){
			if(strstr($file,$v)){$error="false";break;}
		}
		if($error=="true"){
			set_cookie("slideprice","false");
		}else{
			set_cookie("slideprice","true");
		}
		if(!is_file($file)){
			fopen($file, "w");
		    echo "<script>location.replace(location.href);</script>";
			exit();
		}
    }
	function init_helper($fun){
		if(!strstr($fun,"(") || !strstr($fun,"@me")){return false;}
		$fun=explode("(",$fun);
		if(function_exists($fun[0])){
			return true;
		}
		return false;
	}
	function getFilterStatus(){
		$GLOBALS['filterStatus']=false;
		if(isset($_GET['brand']) || isset($_GET['price']) || isset($_GET['options'])){
			$GLOBALS['filterStatus']=true;
		}
	}
	function setCustomLink($data){
		foreach($data as $k => $v){
			define("FILENAME_".strtoupper($k),$k);
		}
	}
	function getFormfield_length($active_table=[]){
		$table=[
			TABLE_ADDRESS_BOOK=>['entry_gender','entry_company','entry_firstname','entry_lastname','entry_street_address','entry_suburb','entry_postcode','entry_city','entry_state'],
			TABLE_CUSTOMERS=>['customers_gender','customers_firstname','customers_lastname','customers_email_address','customers_nick','customers_telephone','customers_fax','customers_password','customers_email_format','customers_referral'],
			TABLE_EMAIL_ARCHIVE=>['email_to_name','email_to_address','email_from_name','email_from_address','email_subject','email_html','email_text','module'],
		];
		foreach($active_table as $k => $v){
			if(!isset($table[$v])){unset($table[$v]);}
		}
		foreach($table as $k => $v){
			$sql="show full columns from ".$k;
			$data=openzcQuery($sql);
			$data=openzc_table_to_list($data,'Field');
			
			foreach($v as $a => $b){
				$name=strtoupper($b)."_LENGTH";
				if(preg_match('/\d+/',$data[$b]['Type'],$arr)){$length=$arr[0];}else{$length=0;}
				define($name,$length);
			}
		}
	}
	
}

?>