<?php //Realizat pentru Webeshop 4.5 si anterior
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

header('Content-Type: text/html; charset=utf-8');
	require_once('config.php');
	$database = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD);
    //mysqli_query('SET NAMES utf8', $database);

//$this->load->model('catalog/model/tool/image');

//https://genarom.ro/image/cache/cache/17001-18000/17432/main/2950-bucatarie-junona-line-g-310-a2-decor-we-dso-0-2-0-1-2-1190x800.jpg
//https://genarom.ro/images/BUCATARIE%2FJUNONA+LINE%2FJUNONA+DSO-DSO%2Fjunona+g+310+a%2Fbucatarie-junona-line-g-310-a2-decor-dso-dso.jpg

//%2F

mysqli_set_charset($database, "utf8");
	if($database)
	{
		mysqli_select_db($database, DB_DATABASE) or die ("DB: " . DB_DATABASE . " NOT FOUND");
        
	}
	else
	{
		print 'db connection error';
		error_log('db connection error');
		exit;
	}

$query_preturi="SELECT p.product_id, pcat.category_id, pcatd.name, p.sku, p.quantity, p.model, p.image as main_image, p.quantity, p.price, psp.price AS special_price, psp.date_start, psp.date_end, p.stock_status_id, pd.name as product, pd.description, pal.keyword, pm.name as manufacturer 
FROM ".DB_PREFIX."product p 
LEFT JOIN ".DB_PREFIX."product_special psp ON psp.product_id = p.product_id AND customer_group_id  = '1'
LEFT JOIN ".DB_PREFIX."product_description pd ON pd.product_id = p.product_id AND pd.language_id = '2'
LEFT JOIN ".DB_PREFIX."url_alias pal ON pal.query = CONCAT('product_id=',p.product_id)
LEFT JOIN ".DB_PREFIX."manufacturer pm ON pm.manufacturer_id = p.manufacturer_id
LEFT JOIN ".DB_PREFIX."product_to_category pcat ON pcat.product_id = p.product_id
LEFT JOIN ".DB_PREFIX."category_description pcatd ON pcatd.category_id = pcat.category_id
WHERE (p.status = '1' AND pcat.category_id>0) AND p.sku>0 GROUP BY p.product_id DESC LIMIT 0,10000";

//echo $query_preturi;

$preturi=mysqli_query($database, $query_preturi); 
$row_preturi=mysqli_fetch_assoc($preturi);
//print_r($row_preturi);
$permis=array("\r\n","&trade;","&reg;","&nbsp;","&icirc;","&#259;","&#355;", "&deg;","&acirc;", "&amp;","|","\n","	","  ","#351;","&amp;","�","&");
$schimbat=array(" "," "," "," ","i","a","t","grade","a","si"," "," ",""," ","","","");
$permis_link=array(" ");
$schimbat_link=array("%20");
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>'?>
<products>
<?php
$ind=0;

function strip_tags_content($string) { 
    // ----- remove HTML TAGs ----- 
    $string = preg_replace ('/<[^>]*>/', ' ', $string); 
    // ----- remove control characters ----- 
    $string = str_replace("\r", '', $string);
    $string = str_replace("\n", ' ', $string);
    $string = str_replace("\t", ' ', $string);
    // ----- remove multiple spaces ----- 
    $string = trim(preg_replace('/ {2,}/', ' ', $string));
    return $string; 

}

do { 
    
//Extra custom data
    
$query_categories=sprintf("SELECT pcat.category_id, pcatd.name, pcatd.name
FROM ".DB_PREFIX."product_to_category pcat 
LEFT JOIN ".DB_PREFIX."category_description pcatd ON pcat.category_id = pcatd.category_id AND pcatd.language_id = '2'
WHERE pcat.product_id='%s' GROUP BY pcat.category_id ORDER BY pcat.category_id",
$row_preturi['product_id']                        
);
$run_categories=mysqli_query($database, $query_categories); 
$row_categories=mysqli_fetch_assoc($run_categories);

 $categories=NULL;   
do {

if($row_categories['name']) { 
    
    if($categories) { 
    $categories=$categories." ; ".$row_categories['name']; }
    else
    {
       $categories=$row_categories['name']; 
    }

}
    
} while($row_categories=mysqli_fetch_assoc($run_categories)); 
    
//Custom data    
    
    
$row_preturi['description']=html_entity_decode($row_preturi['description']);
$row_preturi['description']=str_replace("&nbsp;"," ", $row_preturi['description']);    
 $row_preturi['description']=strip_tags_content($row_preturi['description']);  
    
  $row_preturi['description']=str_replace(". ",".</br>
 ", $row_preturi['description']);
//$row_preturi['description'] = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $row_preturi['description']);    
/*$permis_descriere=array("<span>","</span>");    
$schimbat_descriere=array("|inceputp|","|sfarsitp|");
$reverse_descriere=array(" <p>","</p>");   
$row_preturi['description']=str_replace($permis_descriere, $schimbat_descriere, $row_preturi['description']); 
$row_preturi['description']=preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', strip_tags(trim($row_preturi['description'])));  
$row_preturi['description']=str_replace($schimbat_descriere, $reverse_descriere, $row_preturi['description']);   
$row_preturi['description']=str_replace("<p></p>","",$row_preturi['description']);  */
 
    
$subcat=NULL;	

    if($row_preturi['main_image']) {
        
        $poza='https://genarom.ro/image/cache/cache/1-1000/655/main/5811-comoda-kent-ekom5s-10-ka-0-2-0-1-2-1190x800.jpg';
        

$row_preturi['main_image'] = str_replace(" ", "%20", $row_preturi['main_image']);
        
       
    
        $poza=HTTPS_SERVER.'image/'.$row_preturi['main_image'];
        $link=HTTPS_SERVER.$row_preturi['keyword'];
    
    } else 
    { $poza=NULL;}
if($row_preturi['sku']) { $cod_produs=$row_preturi['sku']; } else { $cod_produs=$row_preturi['product_id'];}
 if($row_preturi['quantity']>0) { $delivery_time="In stoc"; } else { $delivery_time="";}  
    
//If discounted
$product_discounted=1;    
$current_date=date("Y-m-d");    
if($row_preturi['special_price']) { } else { $product_discounted=0; }   
if($current_date>=$row_preturi['date_start']) { } else { $product_discounted=0; }   
if($row_preturi['date_end']>=$current_date || ($row_preturi['date_end']="0000-00-00" || is_null($row_preturi['date_end']))) { } else { $product_discounted=0; }     
    
if($product_discounted) { $row_preturi['price']=$row_preturi['special_price'];}    
//Id discounted    
    
//if($row_preturi['subcategorie2']!="nocategory") { $subcat=$subcat." >> ".$row_preturi['subcategorie2'];}
  echo '
  	<product>
	    <manufacturer>'.str_replace($permis,$schimbat,$row_preturi['manufacturer']).'</manufacturer>
		<name><![CDATA['.preg_replace('/[\x00-\x1F\x7F]/', '',strip_tags($row_preturi['model'])).']]></name>
		<product_url>'.$link.'</product_url>
		<price>'.number_format($row_preturi['price'], 2, '.', '').'</price>
		<currency>Ron</currency>
		<image_url>'.$poza.'</image_url>
		<category><![CDATA['.str_replace($permis,$schimbat,$categories.$subcat).']]></category>
		<description><![CDATA['. 
$row_preturi['description'].']]></description>
		<productid><![CDATA['.str_replace($permis,$schimbat,$cod_produs).']]></productid>
		<identifier>'.$row_preturi['product_id'].'</identifier>
		<delivery_time>7 zile</delivery_time>
        <delivery_cost>200</delivery_cost>';
        
//Attributes

    $query_attributes=sprintf("SELECT pattv.attribute_id, pattd.name, pattv.text
FROM ".DB_PREFIX."product_attribute pattv 
LEFT JOIN ".DB_PREFIX."attribute_description pattd ON pattv.attribute_id = pattd.attribute_id AND pattd.language_id = '2'
LEFT JOIN ".DB_PREFIX."attribute patta ON patta.attribute_id = pattv.attribute_id
WHERE pattv.product_id='%s' GROUP BY pattv.attribute_id ORDER BY patta.sort_order",
$row_preturi['product_id']                        
);
$run_attributes=mysqli_query($database, $query_attributes); 
$row_attributes=mysqli_fetch_assoc($run_attributes);
 $rowcount=mysqli_num_rows($run_attributes);
    
 $attributes=NULL; 
  
    //echo "zzz ".$rowcount.$query_attributes;
    
if($rowcount)   {
 echo '<Attributes>';   
    
    
do {

if($row_attributes['name'] && row_attributes['text']) { 
?> 
<Attribute>
<Attribute_name><![CDATA[<?php echo $row_attributes['name']; ?>]]></Attribute_name>
<Attribute_value><![CDATA[<?php echo $row_attributes['text']; ?>]]></Attribute_value>
</Attribute>
<?php }
    
} while($row_attributes=mysqli_fetch_assoc($run_attributes)); 
    
//Attributes    
echo '</Attributes>'; 
}    
echo '</product>';
		$ind=$ind+1;
} while ($row_preturi=mysqli_fetch_assoc($preturi));
   


?>
</products>