<?php
date_default_timezone_set('Europe/Bucharest');
$date = date('Y-m-d');




function getUniqueCode($length = "")
{
    $code = md5(uniqid(rand(), true));
    if ($length != "") return substr($code, 0, $length);
    else return $code;
}

require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

ini_set('display_errors', 1);
ini_set('max_execution_time', 3600);
ini_set('memory_limit', '-1');




	$xml = file_get_contents("http://test.onbuy.ro/hbtq.xml", "r");
	$xml_parser = simplexml_load_string($xml);	
	$products = $xml_parser->product;

	
	$data = array();
	foreach($products as $key => $value):
		//$data = array();
		$sku = $value->sku;
		$manufacturer = $value->manufacturer;
		$name = (string)$value->name;
		$price = $value->price;
		$special_price = $value->special_price;
		if($special_price < 10){
			$special_price = "";
		}
		/*if($special_price ==  $price){
			$special_price = "";
		}*/
		$image_url = $value->images->url;	
		$description = (string)$value->description;
		$short_description = (string)$value->short_description;	
		$category = $value->category;
		if($category == ""){
			$category = "Fara categorie";
		}

		if($description == ""){
			$description = $name;
		}
		
		$is_in_stock = $value->availability;
		$qty = $value->qty;		

		$image = $value->images->url;
		$first_image = $value->images->url;	
		$array_image = explode("/", $first_image);
		$img_name = $array_image[count($array_image) - 1];

		
		$images = array();
		$s = rand(100, 99999999);
		$i = 1;
		
		foreach($image as $key3=>$value3):
			$images[] = (string)$value3;
		endforeach;

		$data[] =
		array(
			'sku' => "$sku",//$sku,
			'_type' => 'simple',
			'_attribute_set' => 'Default',
			'_product_websites' => 'hbtq',
			'name' => "$name",
			'manufacturer' => "$manufacturer",
			'price' => "$price",
			'special_price' => "$special_price",
			'description' => "$description",
			'short_description' => "$short_description",	
			//'vendor_id' => "2",
			//'ultimul_update' => "$date",
			//'categorie_import' => "$category",					
			'weight' => 1,
			'status' => 1,
			'visibility' => 4,
			'tax_class_id' => 2,
			'qty' => "$qty",
			'is_in_stock' => "$is_in_stock",
			'_media_image' => $images,
			'_media_target_filename' => $img_name,
			'image' => $img_name,
			'small_image' => $img_name,
			'thumbnail' => $img_name,
			// this is the product you import with all your fancy attributes as shown under
			// https://avstudnitz.github.io/AvS_FastSimpleImport/products.html
		);
		/*$data[] =   array(
			// now you use a new row for each custom option you want to add to the above product
			
			'_custom_option_title'       => 'Marime',
			'_custom_option_type'        => Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN,
			'_custom_option_is_required' => true,
			'_custom_option_row_title'   => $size_def,
			'_custom_option_row_sort'  => $size_ord,
			'_custom_option_row_customoptions_qty' => $size_qty
	
		);*/
		
		/*try {			
			$import = Mage::getModel('fastsimpleimport/import');
			$import->processProductImport($data);
		} catch (Exception $e) {
		}*/
	endforeach;
		
	/*echo "<pre>";	
	print_r($data);	
	echo "</pre>";*/	
		
	$time = microtime(true);

	try {		
		$import = Mage::getModel('fastsimpleimport/import');
		$import->processProductImport($data);
	} catch (Exception $e) {
		print_r($import->getErrorMessages());
		echo "<pre>";
		print_r($e);
		echo "<pre>";
	}

	echo 'Elapsed time: ' . round(microtime(true) - $time, 2) . 's' . "\n";