<?php
session_start();
if($_SESSION['adminkey'] !="security"){
	echo"You must be logged in as an administrator in order to export your menu.";
	exit;
}

//open db connection
$host = 'localhost'; 
$username = 'restaura_taste';
include('../custom/db1.php');
$password = 'Daughters2'; 
include ('../includes/mysql_connect.php');
$server = $_SERVER['SERVER_NAME'];
$image_path = $server . "/images/menu/";

$params = array();

$query="select linkpos_partner_name, linkpos_partner_pwd, linkpos_company, linkpos_store_name, linkpos_store_pwd from storePrefs";
$rt=mysql_query($query);
while($nt=mysql_fetch_array($rt)){
	$params['companyName'] = $nt[linkpos_company];
	$params['storeName'] = $nt[linkpos_store_name];
	$params['storePassword'] = $nt[linkpos_store_pwd];
	$params['partnerName'] = $nt[linkpos_partner_name];
	$params['partnerPassword'] = $nt[linkpos_partner_pwd];
	$params['menuKey'] = "1";
	
	if((empty($nt[linkpos_company]))||(empty($nt[linkpos_store_name]))||(empty($nt[linkpos_store_pwd]))||(empty($nt[linkpos_partner_name]))||(empty($nt[linkpos_partner_pwd]))){
		echo $nt[linkpos_company].",". $nt[linkpos_store_name] . ",". $nt[linkpos_store_pwd].",". $nt[linkpos_partner_name].",". $nt[linkpos_partner_pwd];			
		echo"You need to setup your LinkPOS credentials before exporting your menu.";
		exit;
	}

	
}
//	initialize the 5 main arrays
$categories = array();
$items = array();
$options = array();
$fixings = array();
$sizes = array();

//loop through categories
$query = "Select * from menuCategory where Active='1' order by orderNum";
//echo "<br>category qry = ".$query;
$rt=mysql_query($query); 
$cat_x = 0;  
$cat_str="";
while($nt=mysql_fetch_array($rt)){
	${'$category' . $cat_x} = new stdClass();
	${'$category' . $cat_x}->categoryKey = $nt[RecordKey];
	${'$category' . $cat_x}->name = $nt[CategoryName];
	//${'$category' . $cat_x}->description = $nt[description];
	//send empty description for now
	${'$category' . $cat_x}->description = "";
	if($nt[category_type]==1){
		$cat_type='1';
	} else {
		$cat_type='0';
	}
	${'$category' . $cat_x}->pizzaCategory = "$cat_type";
	${'$category' . $cat_x}->order = $nt[orderNum];
	if(($nt[CategoryName]=="Options")||($nt[CategoryName]=="Fixings")||($nt[CategoryName]=="Pizza Toppings")||($nt[CategoryName]=="Pizza Sauce")||($nt[CategoryName]=="Pizza Crust")){
		$hidden='1';
	} else {
		$hidden='0';
	}
	${'$category' . $cat_x}->hiddenCategory = "$hidden";
	//add to categories array
	$categories[$cat_x] = ${'$category' . $cat_x};	
	$cat_x++;
	if($cat_str==""){
		$cat_str=$nt[RecordKey];
	} else {
		$cat_str.=", ".$nt[RecordKey];
	}
}


	//loop through all active menu items
	$query = "Select * from menuItem where active='1' and categoryKey in ($cat_str) order by orderNum";
	//echo "<br>item qry = ".$query;
	$rt=mysql_query($query); 
	$item_x=0;
	$item_list = "";
	while($nt=mysql_fetch_array($rt)){
		//build array string of all active items
		if(empty($item_list)){
			$item_list = $nt[itemKey];
		} else {
			$item_list .= ", " . $nt[itemKey];
		}
		${'$item' . $item_x} = new stdClass();
		${'$item' . $item_x}->categoryKey = $nt[categoryKey];
		${'$item' . $item_x}->itemKey = $nt[itemKey];
		${'$item' . $item_x}->name = $nt[itemName];
		if($nt[itemPrice]>0){
			${'$item' . $item_x}->price = $nt[itemPrice];
		} else {
			${'$item' . $item_x}->price = "0";
		}
		
		$item_desc = preg_replace( "/\r|\n/", "", $nt[description]);
		$item_desc = str_replace("", "", $item_desc);
		//$item_desc = str_replace("™", "", $item_desc);
		//$item_desc = str_replace("–", "", $item_desc);
		//$item_desc = str_replace("'", "&apos;", $item_desc);
		
		//$item_desc = str_replace("’", "&apos;", $item_desc);
		
		//$item_desc = str_replace("<", "&lt;", $item_desc);
		//$item_desc = str_replace(">", "&gt;", $item_desc);
		//$item_desc = str_replace('"', '&quot;', $item_desc);
		//$item_desc = str_replace("ñ", "n", $item_desc);
		//$item_desc = str_replace("¢", " cents", $item_desc);
		//$item_desc = mb_convert_encoding($item_desc, "UTF-8");		
		//$item_desc = substr($item_desc, 0, 500);
		
		//${'$item' . $item_x}->description =$item_desc;
		//send empty item descriptions for now
		${'$item' . $item_x}->description ="";
		if($nt[pizzaTopping]=='1'){
			$topping='1';
		}else{
			$topping='0';
		}
		${'$item' . $item_x}->pizzaTopping = "$topping";
		${'$item' . $item_x}->multiSized = $nt[multisized];
		${'$item' . $item_x}->taxable = $nt[taxable];
		${'$item' . $item_x}->freeToppings = $nt[freeTopping];
		${'$item' . $item_x}->useFixings = $nt[usefixings];
		${'$item' . $item_x}->pizzaCrust = "0";
		${'$item' . $item_x}->pizzaSauce = "0";
		if($nt[imagePath]>""){
			$imageURL = "http://" .$image_path . $nt[imagePath];
		} else {
			$imageURL = "";
		}
		${'$item' . $item_x}->imageURL = $imageURL;
		${'$item' . $item_x}->order = $nt[orderNum];
		//add to items array
		$items[$item_x] = ${'$item' . $item_x};	
		$item_x++;
	}


	//loop through all options for all active menu items
	$query = "Select * from options where itemkey in (".$item_list.") order by optionkey";
	//echo "<br>option qry = ".$query;
	$rt=mysql_query($query); 
	$option_x=0;
	while($nt=mysql_fetch_array($rt)){
		${'$option' . $option_x} = new stdClass();
		${'$option' . $option_x}->choice = $nt[choice];
		${'$option' . $option_x}->parentKey = $nt[parentkey];
		${'$option' . $option_x}->itemKey = $nt[itemkey];
		${'$option' . $option_x}->price = $nt[price];
		//add to options array
		$options[$option_x] = ${'$option' . $option_x};	
		$option_x++;
	}
	
	//loop through all fixings for all active menu items
	$query = "Select * from fixings where itemkey in (".$item_list.") order by fixingkey";
	//echo "<br>fixing qry = ".$query;
	$rt=mysql_query($query); 
	$fixing_x=0;
	while($nt=mysql_fetch_array($rt)){
		${'$fixing' . $fixing_x} = new stdClass();
		${'$fixing' . $fixing_x}->itemKey = $nt[itemkey];
		${'$fixing' . $fixing_x}->parentKey = $nt[parentkey];
		${'$fixing' . $fixing_x}->price = $nt[price];
		//add to fixings array
		$fixings[$fixing_x] = ${'$fixing' . $fixing_x};	
		$fixing_x++;
	}

//loop through all sizes for all active menu items
	$query = "Select * from itemSize where itemKey in (".$item_list.") order by itemKey";
	//echo "<br>size qry = ".$query;
	$rt=mysql_query($query); 
	$size_x=0;
	while($nt=mysql_fetch_array($rt)){
		${'$size' . $size_x} = new stdClass();
		${'$size' . $size_x}->itemKey = $nt[itemKey];
		${'$size' . $size_x}->sizeName = $nt[size];
		${'$size' . $size_x}->price = $nt[price];
		//add to fixings array
		$sizes[$size_x] = ${'$size' . $size_x};	
		$size_x++;
	}
	


//add all arrays to params array
$params['menuCategories'] = $categories;
$params['menuItems'] = $items;
$params['menuOptions'] = $options;
$params['menuFixings'] = $fixings;
$params['menuItemSizes'] = $sizes;

//$params= htmlspecialchars($params, ENT_COMPAT,'ISO-8859-1', true);
//$params= htmlentities($params, ENT_COMPAT,'utf-8', true);

print_r ($params);

//old urls
//$wsdl = "http://olypointofsale.com/POSMenu.asmx?WSDL";
//$wsdl = "http://www.olypointofsale.com/postest/posmenuetta.asmx?WSDL";

$prod_url ="http://olympuspos.net/pos/posmenuetta.asmx?WSDL";
$test_url ="http://olypointofsale.com/postest/posmenuetta.asmx?WSDL";

$wsdl = $test_url;

$client = new soapclient($wsdl, array('trace' => 1));

$result = $client->SaveMenu($params);

echo "<br><br>Returned from SOAP: ";
print_r($result);


//echo "REQUEST:\n" . $client->__getLastRequest() . "\n";

//echo "<br>status: " . $result->success . "<br/>Error: " . $result->errorMessage;
//print_r($response);

?>