<?

class webshop_import_xml {

function webshop_import_xml($id='') {
	if($id) {
		global $dbi;
		$sql="select url_domain from webshop_import_xml where id='$id'";
		$res=sql_query($sql,$dbi);
		extract(sql_fetch_assoc($res,$dbi));
		if(substr($url_domain,-1)!='/') $url_domain.='/';
		$this->domain_url=$url_domain;
	}
}

function getImportXmls() {
	global $dbi;
	$sql="select * from webshop_import_xml";
	$res=sql_query($sql,$dbi);
	while($a=sql_fetch_assoc($res,$dbi)) {
		$data[]=$a;
	}
	return $data;
}

function getDomainUrl($xml_id) {

}

function saveNewImportXml($post) {
	global $dbi;
	$sql="insert into webshop_import_xml (name,url_domain,url_xml) values ('$post[name]','$post[url_domain]','$post[url_xml]')";
	sql_query($sql,$dbi);
}

function getXmlContent($id,$checkAdded=false) {
	
	global $dbi;
	
	$sql="select url_xml from webshop_import_xml where id='$id'";
	$res=sql_query($sql,$dbi);
	extract(sql_fetch_assoc($res,$dbi));

	$xml=new xml($url_xml,2);
		
	$list=$xml->load();
	
	if($checkAdded) {
		$added=$this->getImportProductIds($id);
		if($added)
			foreach($list as &$p)
				if(in_array($p[id],$added)) $p[added]=1;
	}
	
	return $list;
}

function getImportProductIds($id){

	global $dbi;

		$sql="select remote_product_id,product_id from webshop_import_xml_added where xml_id='$id'";
		$res=sql_query($sql,$dbi);
		while(list($rpid,$pid)=sql_fetch_array($res,$dbi)) {
			$d[]=array('remote'=>$rpid,'local'=>$pid);
		}
		return $d;
}

function ImportSelectedItems($xml_id,$remote_product_ids,$category_id) {

	global $dbi,$auth;
	
	$ws=new webshop;
	$gal=new gallery;

	// van-e már ez a termék abban a csoportban
	
	$allproducts=$this->getXmlContent($xml_id);
	foreach($allproducts as $p) {
		if(in_array($p[id],$remote_product_ids)) {
		
			// xml_id,remote_product_id
			$sql="select count(*) as db from webshop_import_xml_added where xml_id=$xml_id and remote_product_id=$p[id] ";
			$res=sql_query($sql,$dbi);
			list($db)=sql_fetch_array($res,$dbi);
			if($db==0){
				
				// termék
				$sql="insert into webshop_product (name) values('$p[product]')";
				sql_query($sql,$dbi);

				$sql="select max(id) from webshop_product";
				$res=sql_query($sql,$dbi);
				list($product_id)=sql_fetch_row($res,$dbi);
			
				// kategória
				$sql="insert into webshop_product_conn (product_id,category_id) values ('$product_id','$category_id')";
				sql_query($sql,$dbi);
			
				// ár
				$price=$p[netto_value];
				$sql="insert into webshop_product_price (product_id,type_id,price,user_id,datum) values('$product_id',3,'$price','$auth->uid',now())";
				sql_query($sql,$dbi);

				// kép
				$pic=$this->domain_url.$p[picture];

				$sql="select max(image_products_id) from image_products";
				$res=sql_query($sql,$dbi);
				list($s)=sql_fetch_row($res,$dbi);
				$s++;
				while(strlen($s)<10) $s='0'.$s;
				$ext=strrchr($p[picture],'.');
				$newfname="p$s";

				if($file=fopen($pic,"rb")) {
					$newf=fopen(PRODUCT_IMAGES.$newfname.$ext,"wb");
					while(!feof($file)) {
						fwrite($newf,fread($file,1024*8),1024*8);
					}
					fclose($file);
					fclose($newf);
				}

				$params=array(src_file=>$pic, dst_file_name=>PRODUCT_IMAGES.$newfname.'_t', x=>$gal->pic_thumbnail_width, y=>$gal->pic_thumbnail_height, dst_ext=>'jpg');
				$gal->uploadPicture($params);
			
				$sql="insert into image_products (product_id,file,main,pos) values($product_id,'$newfname$ext',1,1)";
				sql_query($sql,$dbi);
			
				// xml import helyzete, termék távoli kapcsolata
				$sql="insert into webshop_import_xml_added (product_id,xml_id,remote_product_id) values ('$product_id','$xml_id','$p[id]')";
				sql_query($sql,$dbi);
			
			} // ha nincs meg a termek a csoportban
			
			
		}
	}
}

/*
 * 
 * 		ar automata frissiteshez
 * 
 * 
 */

function getImportedXMLids(){

	global $dbi;
	
	$sql="select distinct xml_id from webshop_import_xml_added order by xml_id";
	$res=sql_query($sql,$dbi);
	while($r=sql_fetch_assoc($res,$dbi)){
		$data[]=$r[xml_id];	
	}
	return $data;
}

}

?>
