<?php
include('includes/application_top.php');
//  Start TIMER
//  -----------
$stimer = explode( ' ', microtime() );
$stimer = $stimer[1] + $stimer[0];
//  -----------


function soobic_get_p_categoryname($oldpid) {
	$psql='select categories_id from products_to_categories where products_id ='.$oldpid.' limit 1';
    $p_query = tep_db_query($psql);
    $p_result = tep_db_fetch_array($p_query);
	$cid=$p_result['categories_id'];

	$nsql='select categories_name from categories_description where categories_id ='.$cid.' and language_id='.(int)$languages_id .' limit 1';
    $n_query = tep_db_query($nsql);
    $n_result = tep_db_fetch_array($n_query);
	$cname=$p_result['categories_name'];
    return $cname;
}

define(FROOGLE_FTP_FILENAME,'froogle_en_gbp.txt');	
$OutFile = DIR_FS_CATALOG."\\feeds\\".FROOGLE_FTP_FILENAME; 
$destination_file = FROOGLE_FTP_FILENAME;  //"CHANGEME-filename-to-upload-to-froogle.txt" ;
$source_file = $OutFile;
$imageURL = 'http://www.ebest24.com/images/products_images/unfurl/';
$productURL = 'http://www.ebest24.com/product_info.php?currency=GBP&language=en&products_id=';
$productURL_static = 'http://www.ebest24.com/products/';
$already_sent = array();

$home=DB_SERVER;
$user=DB_SERVER_USERNAME;
$pass=DB_SERVER_PASSWORD;
$base=DB_DATABASE;

$ftp_server = FROOGLE_FTP_SERVER;

$ftp_user_name = FROOGLE_FTP_USER;

$ftp_user_pass = FROOGLE_FTP_PASS;

$ftp_directory = ""; // leave blank for froogle

$taxRate = 0; //default = 0 (e.g. for 17.5% tax use "$taxRate = 17.5;")
$taxCalc = ($taxRate/100) + 1;  //Do not edit
$convertCur = false; //default = false
$curType = "USD"; // Converts Currency to any defined currency (eg. USD, EUR, GBP)
if($convertCur)
{
$productURL = "CHANGEME-http://www.yourwebsite.com/product_info.php?currency=" . $curType . "&products_id=";  //where CURTYPE is your currency type (eg. USD, EUR, GBP)
}

//START Advance Optional Values

//(0=False 1=True) (optional_sec must be enabled to use any options)
$optional_sec = 0;
$instock = 0;
$shipping = 0;
	$lowestShipping = "4.95";  //this is not binary.
$brand = 0;
$upc = 0;   //Not supported by default osC
$manufacturer_id = 0;  //Not supported by default osC
$product_type = 0;
$currency = 0;
	$default_currency = "USD";  //this is not binary.
$feed_language = 0;
	$default_feed_language = "en";  //this is not binary.
$ship_to = 0;
	$default_ship_to = "ALL"; //this is not binary, not supported by default osC for individual products.
$ship_from = 0;
	$default_ship_from = "USD"; //this is not binary, not supported by default osC for individual products.

//END of Advance Optional Values

if (!($link=mysql_connect($home,$user,$pass)))
{
echo "Error when connecting itself to the data base";
exit();
}
if (!mysql_select_db( $base , $link ))
{
echo "Error the data base does not exist";
exit();
}


//get currencies value
	$gbp_value = 0;
	$sql_c = "SELECT	currencies.value AS curGBP FROM currencies 	WHERE currencies.code = 'GBP'";
	$result_c=mysql_query( $sql_c )or die( $FunctionName . ": SQL error " . mysql_error() . "| sql_c = " . htmlentities($sql_c) );
	$row_c = mysql_fetch_array( $result_c );
	$gbp_value=$row_c['curGBP'];
//get currencies value end

if ( file_exists( $OutFile ) )
unlink( $OutFile );

$output = "code \t link \t title \t description \t image_link \t id \t expiration_date \t labels \t category \t price \t currency";

$output .= "\n";

$sql = "
SELECT 
products.code,
concat( '" . $productURL . "' ,products.products_id) AS product_url,
products.products_id AS id,
products_description.products_name AS name,
products_description.products_description AS description,
products_description.keywords,
products.products_quantity AS quantity,
products.products_status AS prodStatus,
FORMAT(products.products_price,2) AS price,
CONCAT( '" . $imageURL . "' ,products.products_imagebig) AS image_url
FROM 
products,
products_description
WHERE 
products.products_id=products_description.products_id
AND products_description.language_id=4
and products.products_status=1
ORDER BY
products.products_id ASC
";
echo "sql====".$sql;
echo "<br>";

$result=mysql_query($sql);



$_strip_search = array(
"![\t ]+$|^[\t ]+!m", // remove leading/trailing space chars
'%[\r\n]+%m'); // remove CRs and newlines
$_strip_replace = array(
'',
' ');
$_cleaner_array = array(">" => "> ", "&reg;" => "", "®" => "", "&trade;" => "", "™" => "", "\t" => "", "	" => "");

$loop_counter = 0;
while( $row = mysql_fetch_array( $result ) )
{
//make static file url link
		$desc=preg_replace($_strip_search, $_strip_replace, strip_tags( strtr($row['description'], $_cleaner_array) ) );
		if (strlen($desc)>600)
			$desc=substr($desc,0,600);
		$output .= $row['code']. "\t" .
		$productURL.$row['id']. "\t" .
		preg_replace($_strip_search, $_strip_replace, strip_tags( strtr($row['name'], $_cleaner_array) ) ) . "\t" .		
		$desc . "\t" .
		$row['image_url'] . "\t" .
		$row['id'] . "\t" .
		"2008-1-1" . "\t" .
		preg_replace($_strip_search, $_strip_replace, strip_tags( strtr($row['keywords'], $_cleaner_array) ) ) . "\t" .
		soobic_get_p_categoryname($row['id']) . "\t" .
		round($row['price']*$gbp_value,2) . "\t" .
		"GBP";
		$output .= " \n";
	
		$loop_counter++;
}

echo "file===".$OutFile;
echo "<br>";
echo "p_count=".$loop_counter;
echo "<br>";

$fp = fopen( $OutFile , "a" );
$fout = fwrite( $fp , $output );
fclose( $fp );
echo "File completed: " . $destination_file . "<br>\n";
chmod($OutFile, 0777);
?>