<?php
final class Partner {
  static public $HOST="77.246.157.182";
  static public $BASE=" ";
  static public $PWD=" ";

  //-----------------------------------------------------------------------------------------------------------
  static public function SetHeaders($type,&$document,&$data,$config,$category_info,$product_info="") {

	 if ($type=="category"){

			$document->title = $category_info['name']." | ". $config->get('config_name');
			$document->keywords = $category_info['meta_keywords'];
			$document->description = $category_info['meta_description'];
			$data['heading_title'] = trim($category_info['h1'])!=""?$category_info['h1']:$category_info['name'];
			$data['description'] = html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8');

			//breadcrumbs
			//$document->breadcrumbs[count($document->breadcrumbs)-1]['text']=$data['heading_title'];
	  }
	  else if ($type=="product"){
			if (isset($category_info))	$document->title = $category_info['name']." - ".$product_info['name']." | ". $config->get('config_name');
			  else $document->title = $product_info['name']." | ". $config->get('config_name');
			$document->keywords = $product_info['meta_keywords'];
			$document->description = $product_info['meta_description'];

			$data['heading_title'] = $product_info['name'];
			$data['description'] = html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8');
			
	  }
	  else if ($type=="w_categories"){
			$str = $category_info['name'];
			if ($category_info['title'] != "")$str=$category_info['title'];
			 else if ($category_info['h1'] != "")$str=$category_info['h1'];
			
			$data['heading_title'] = $str;
			$data['description'] = html_entity_decode($category_info['description'], ENT_QUOTES, 'UTF-8');
	  }
  }

  //-----------------------------------------------------------------------------------------------------------
  static public function GetCategoriesList() {
	    $catalog = array();
	    $link = mysql_connect(Partner::$HOST,Partner::$BASE,Partner::$PWD); 
		mysql_select_db("export") or die(mysql_error());  
		mysql_query("set names UTF8");
		$res = mysql_query("select * from categories where level=0") or die(mysql_error()); 
		while ($row=mysql_fetch_array($res)) { 
		   //echo "<br>".$row["name"];
		   $catalog[]=array(0 => $row["name"]." (все)", 1 => $row["categoryID"]);
 	 	   $res2 = mysql_query("select * from categories where level=1 and parent=".$row["categoryID"]) or die(mysql_error()); 
	       while ($row2=mysql_fetch_array($res2)) { 
		     $catalog[]=array(0 => $row["name"]."->".$row2["name"], 1 => $row2["categoryID"]);
			 }
		}
        $catalog[]=array(0 => "- Весь каталог -", 1 => "1");
		
	  	mysql_close($link);  
		return $catalog;
  }

  //-----------------------------------------------------------------------------------------------------------
  static public function synOrderDataToCommon(&$obj,$order_id) {

      $link = mysql_connect(Partner::$HOST,Partner::$BASE,Partner::$PWD); 
	  mysql_select_db("export") or die(mysql_error());  
 	  mysql_query("set names UTF8");
	  
	  $partner=$obj->config->get('config_partner');
	  $prtype=$obj->config->get('config_prtype');
	  
	  $order_id_ext=$obj->config->get('config_shpref').$order_id;
	  
	  mysql_query("delete from orders where partner=$partner and order_id=$order_id_ext");
	  mysql_query("delete from order_cart where partner=$partner and order_id=$order_id_ext");
	  
	  $query = $obj->db->query("select * from `".DB_PREFIX."order` where order_id='".(int)$order_id."'" );
	  $fields=array_keys($query->row);
	  
	  $fldstr="partner,prtype,order_id";
	  $valstr="'$partner','$prtype',$order_id_ext";
	  for($i=0;$i<count($fields);$i++)
	    if ($fields[$i]!="order_id"){
		  $fldstr.=",".$fields[$i];
	      $valstr.=",'".$query->row[$fields[$i]]."'";
	  	  }
	  $qw="insert into orders ($fldstr) values($valstr) ";	
	  mysql_query($qw);


	  $query = $obj->db->query("select * from `".DB_PREFIX."order_product` where order_id='".(int)$order_id."'" );
	  
	  foreach ($query->rows as $row) {
		  $fields=array_keys($row);
		  
		  $fldstr="partner,order_id,variant";
		  $valstr="'$partner',$order_id_ext";
		  
	      $query2 = $obj->db->query("select * from `".DB_PREFIX."order_option` where order_id='".(int)$order_id."' and order_product_id='".$row["order_product_id"]."'");
		 // echo "<br>"."select * from `".DB_PREFIX."order_option` where order_id='".(int)$order_id."' and order_product_id='".$row["order_product_id"]."' and name='Размер/Вес'";	
		  if ($query2->num_rows){
			$val=""; 
			foreach ($query2->rows as $row2) 
			     $val.=(($val=="")?"":",").$row2["name"];
			$valstr.=",'(".$val.")'";	 
		    } else $valstr.=",''";
			
		  for($i=0;$i<count($fields);$i++)
	        if ($fields[$i]!="order_id"){
		   	  $fldstr.=",".$fields[$i];
			  $valstr.=",'".$row[$fields[$i]]."'";
		 	  }
		  $qw="insert into order_cart ($fldstr) values($valstr) ";	
		  //echo "<br>".$qw;
		  mysql_query($qw);
		  }
		  
	  $query = $obj->db->query("select value from `".DB_PREFIX."order_total` where order_id='".(int)$order_id."' and sort_order=3" );
	  if ($query->num_rows){
		  $qw="update orders set shipping_cost='".$query->row["value"]."' where order_id='".(int)$order_id_ext."' and partner=$partner";	
		  mysql_query($qw);
		  }

	  //echo $qw;
  
  	  mysql_close($link);  
  }

  //-----------------------------------------------------------------------------------------------------------
  static public function synOrderDataFromCommon(&$obj,$order_id) {
  
  }
	
  static public function synProductsData(&$obj,&$products) {
  }

}
?>
