<?php 

function getICEcatProductSpecs($ean, $drawdescription = 0, $drawpicture = 0)
{	
	// Username and password for usage with ICEcat	
	$username = "Digital Media";
	$password = "dm2011ingram";

	// Return 0 and exit function if no EAN available
	if($ean == null)
	{
		return 0;
	}

	// Get the product specifications in XML format
	$context = stream_context_create(array(
		'http' => array(
			'header'  => "Authorization: Basic " . base64_encode($username.":".$password)
		)
	));
	$data = file_get_contents('http://data.icecat.biz/xml_s3/xml_server3.cgi?ean_upc='.$ean.';lang=es;output=productxml', false, $context);
	$xml = new SimpleXMLElement($data);
	
	// Create arrays of item elements from the XML feed
	$productPicture = $xml->xpath("//Product");
	$productDescription = $xml->xpath("//ProductDescription");
	$categories = $xml->xpath("//CategoryFeatureGroup");
	$spec_items = $xml->xpath("//ProductFeature");
	$specs = NULL;
	//Draw product specifications table if any specs available for the product
	if($spec_items != null)
	{
		$categoryList = array();
		foreach($categories as $categoryitem) {
			$catId = intval($categoryitem->attributes());
			$titleXML = new SimpleXMLElement($categoryitem->asXML());
			if($titleXML->xpath("//Name")!=FALSE){
			$title = $titleXML->xpath("//Name");
			$catName = $title[0]->attributes();
			$categoryList[$catId] = utf8_decode($catName['Value']);
			}else{
				$categoryList[$catId] = "";
			}
		}
		
		$specs =  "";
		$i = 0;
		
		$drawnCategories = array();
		
		foreach($spec_items as $item) {
			$specValue = $item->attributes();
			$titleXML = new SimpleXMLElement($item->asXML());
			$title = $titleXML->xpath("//Name");
			$specName = $title[0]->attributes();
			$specCategoryId = intval($specValue['CategoryFeatureGroup_ID']);
			
			if($specName['Value'] != "Fuente de hoja de datos")
			{

				if(!in_array($specCategoryId, $drawnCategories))
				{
					$specs .= "	<p>".$categoryList[$specCategoryId]."</p>";
					$drawnCategories[$i] = $specCategoryId;
				}
				$specs .= "<p>".utf8_decode($specName['Value']).": ";	
											if($specValue['Presentation_Value'] == "Y")
											{
												$specs .= "si";
											}
											else if($specValue['Presentation_Value'] == "N")
											{
												$specs .= "no";
											}
											else
											{
												$specs .= utf8_decode($specValue['Presentation_Value']);
											}
							$specs .= "
								</p>";
			}
			$i++;
		}
		
		
		//Draw product description and link to manufacturer if available
		$drawdescription = 1;
		if( $drawdescription != 0)
		{
			foreach($productDescription as $item) {
				$productValues = $item->attributes();
				if($productValues['URL'] != null)
				{
					$specs .= "";
				}
				if($productValues['LongDesc'] != null)
				{
					$description = utf8_decode(str_replace('\n', '', $productValues['LongDesc']));
					$description = str_replace('<b>', '<strong>', $description);
					$description = str_replace('<B>', '<strong>', $description);
					$description = str_replace('</b>', '</strong>', $description);
					$specs .= "<p>".nl2br($description,false)."</p>";
				}else{
					$description = NULL;
				}
			}
		}

		return array("specs"=>$specs);
	}
	else
	{
		return 0;
	}

}
function getICEcatProductSpecsMan($manufacturer_part_number, $vendor)
{	
	// Username and password for usage with ICEcat	
	$username = "Digital Media";
	$password = "dm2011ingram";
	$drawdescription = 1;
	// Return 0 and exit function if no manufacturer_part_number available
	if($manufacturer_part_number == null)
	{
		return 0;
	}

	// Get the product specifications in XML format
	$context = stream_context_create(array(
		'http' => array(
			'header'  => "Authorization: Basic " . base64_encode($username.":".$password)
		)
	));

	//$data = file_get_contents('http://data.icecat.biz/xml_s3/xml_server3.cgi?vendor='.$vendor.';lang=es;output=productxml;prod_id='.$manufacturer_part_number, false, $context);
	$URL = 'http://'.$username.':'.$password.'@data.icecat.biz/xml_s3/xml_server3.cgi?vendor='.$vendor.';lang=es;output=productxml;prod_id='.$manufacturer_part_number;
	//$xml = new SimpleXMLElement($data);	
	$xml = simplexml_load_file (htmlentities(utf8_encode($URL)));
	if($xml!=false){
	// Create arrays of item elements from the XML feed
	$productPicture = $xml->xpath("//Product");
	$productDescription = $xml->xpath("//ProductDescription");
	$categories = $xml->xpath("//CategoryFeatureGroup");
	$spec_items = $xml->xpath("//ProductFeature");
	$specs = NULL;
	//Draw product specifications table if any specs available for the product
	if($spec_items != null)
	{
		$categoryList = array();
		foreach($categories as $categoryitem) {
			$catId = intval($categoryitem->attributes());
			$titleXML = new SimpleXMLElement($categoryitem->asXML());
			if($titleXML->xpath("//Name")!=FALSE){
				$title = $titleXML->xpath("//Name");
				$catName = $title[0]->attributes();
				$categoryList[$catId] = utf8_decode($catName['Value']);
			}else{
				$categoryList[$catId] = "";
			}
		}
		
		$specs =  "";
		$i = 0;
		
		$drawnCategories = array();
		
		foreach($spec_items as $item) {
			$specValue = $item->attributes();
			$titleXML = new SimpleXMLElement($item->asXML());
			$title = $titleXML->xpath("//Name");
			$specName = $title[0]->attributes();
			$specCategoryId = intval($specValue['CategoryFeatureGroup_ID']);
			
			if($specName['Value'] != "Fuente de hoja de datos")
			{
				
				
				if(!in_array($specCategoryId, $drawnCategories))
				{
					$specs .= "	<p>".$categoryList[$specCategoryId]."</p>";
					$drawnCategories[$i] = $specCategoryId;
				}
				$specs .= "<p>".utf8_decode($specName['Value']).": ";	
											if($specValue['Presentation_Value'] == "Y")
											{
												$specs .= "si";
											}
											else if($specValue['Presentation_Value'] == "N")
											{
												$specs .= "no";
											}
											else
											{
												$specs .= utf8_decode($specValue['Presentation_Value']);
											}
							$specs .= "</p>";
			}
			$i++;
		}
		
		
		//Draw product description and link to manufacturer if available
		if( $drawdescription != 0)
		{
			foreach($productDescription as $item) {
				$productValues = $item->attributes();
				if($productValues['URL'] != null)
				{
					$specs .= "";
				}
				if($productValues['LongDesc'] != null)
				{
					$description = utf8_decode(str_replace('\n', '', $productValues['LongDesc']));
					$description = str_replace('<b>', '<strong>', $description);
					$description = str_replace('<B>', '<strong>', $description);
					$description = str_replace('</b>', '</strong>', $description);
					$specs .= "<p>".nl2br($description,false)."</p>";
				}else{
					$description = NULL;
				}
			}
		}
		
		return array("specs"=>$specs);
	}
	else
	{
		return 0;
	}
	}else{
		return array("specs"=>NULL);
	}	
}
include("db.php");
//$rs = mysql_query("SELECT * FROM price_file_copy WHERE codigo_barras != '' AND codigo_barras != '0000000000000' AND codigo_barras != '9999999999999' AND ficha_tecnica__ IS NULL");		
$rs = mysql_query("SELECT * FROM price_file_copy WHERE ficha_tecnica__ IS NULL AND consultado = 'no'");		
//$rs = mysql_query("SELECT * FROM price_file WHERE upc_nbr != '' AND upc_nbr != '0000000000000' AND upc_nbr != '9999999999999' AND very_long_description = ''");		
//$rs = mysql_query("select p.sku, p.manufacturers_part_nbr,m.mf_name FROM price_file p INNER JOIN jos_vm_manufacturer m ON p.manufacturer = m.manufacturer_id where p.specs IS NULL limit 1500,2000");		
//$rs = mysql_query("select p.upc_nbr, p.sku FROM price_file p INNER JOIN jos_vm_manufacturer m ON p.manufacturer = m.manufacturer_id where p.specs IS NULL and p.upc_nbr != '' AND p.upc_nbr!=\"0000000000000\" AND p.upc_nbr !=\"9999999999999\"");		
//$rs = mysql_query("select p.sku, p.manufacturers_part_nbr,m.mf_name FROM price_file p INNER JOIN jos_vm_manufacturer m ON p.manufacturer = m.manufacturer_id where p.lowpic IS NULL");		
//$rs = mysql_query("SELECT * FROM price_file_copy WHERE id = 46126");		
$counter  = 0;

while ($row = mysql_fetch_assoc($rs))
{
	echo $row["sku"] . "<br>";
				//$specs_product = getICEcatProductSpecs($row["codigo_barras"],1,1);
				
				$specs_product = getICEcatProductSpecsMan($row["codigo_fabricante"],$row["marca"]);
				if ($specs_product["specs"]!=NULL || !empty($specs_product["specs"])) {
					$rs_a = mysql_query("UPDATE price_file_copy SET ficha_tecnica__ = '".$specs_product["specs"]."', consultado = 'si' WHERE id = ".$row["id"]); 						
					
					if(mysql_affected_rows()>0)
					{
						$counter++;
					}
				}else{
					$rs_a = mysql_query("UPDATE price_file_copy SET consultado = 'si' WHERE id = ".$row["id"]); 						
				}

}
echo $counter;
//print_r(getICEcatProductSpecs("0092636246440",1,1));
?>