<?php
/*
+--------------------------------------------------------------------------
|   CubeCart 4
|   ========================================
|	CubeCart is a registered trade mark of Devellion Limited
|   Copyright Devellion Limited 2006. All rights reserved.
|   Devellion Limited,
|   5 Bridge Street,
|   Bishops Stortford,
|   HERTFORDSHIRE.
|   CM23 2JU
|   UNITED KINGDOM
|   http://www.devellion.com
|	UK Private Limited Company No. 5323904
|   ========================================
|   Web: http://www.cubecart.com
|   Email: info (at) cubecart (dot) com
|	License Type: CubeCart is NOT Open Source Software and Limitations Apply 
|   Licence Info: http://www.cubecart.com/v4-software-license
+--------------------------------------------------------------------------
|	export.inc.php
|   ========================================
|	Export Catalogue	
+--------------------------------------------------------------------------
*/

if (!defined('CC_INI_SET')) die("Access Denied");
$lang = getLang("admin".CC_DS."admin_products.inc.php");

$whereClause = '';


if (isset($_POST['format']) && strtolower($_POST['format']) == 'googlebase') {
	
	$query = "SELECT * FROM ".$glob['dbprefix']."CubeCart_inventory INNER JOIN ".$glob['dbprefix']."CubeCart_category on ".$glob['dbprefix']."CubeCart_inventory.cat_id = ".$glob['dbprefix']."CubeCart_category.cat_id ".$whereClause." ORDER BY name ASC";
	$results = $db->select($query);

	if ($results == true) {
		$googleBaseContent = "id\tproduct_url\tname\tdescription\timage_url\tprice\tcurrency\r\n";
		
		for ($i=0; $i<count($results); $i++) {
			$salePrice = salePrice($results[$i]['price'], $results[$i]['sale_price']);
			$price = ($salePrice > 0) ? $salePrice : $price = $results[$i]['price'];
			
			$name = str_replace(array("&nbsp;","\t","\r","\n","\0","\x0B","
			"),"",strip_tags($results[$i]['name']));
			$name = str_replace("  ","",$name);
			$desc = str_replace(array("&nbsp;","\t","\r","\n","\0","\x0B","
			"),"",strip_tags($results[$i]['description']));
			$desc = str_replace("  ","",$desc);
			
			$googleBaseContent .= $results[$i]['productId']."\t";
			
			if ($config['sef'] == 0) {
				$googleBaseContent .= $glob['storeURL']."/index.php?_a=viewProd&productId=".$results[$i]['productId']."\t".$name."\t".$desc;
			} else {
				include_once("includes".CC_DS."sef_urls.inc.php");
				$googleBaseContent .= $glob['storeURL']. "/" .generateProductUrl($results[$i]['productId'])."\t".$name."\t".$desc;			
			}
			
			if ($results[$i]['image']) {
				$googleBaseContent .= "\t".$glob['storeURL']."/images/uploads/".$results[$i]['image'];
			} else {
				$googleBaseContent .= "\t".$glob['storeURL']."/skins/".$config['skinDir']."/styleImages/nophoto.gif";
			}
			
			$googleBaseContent .= "\t".$price."\t".$config['defaultCurrency']."\r\n";
		}
		
		$filename = "GoogleBaseFeed_".date("Ymd").".txt";
		header('Pragma: private');
		header('Cache-control: private, must-revalidate');
		header("Content-Disposition: attachment; filename=".$filename);
		header("Content-type: text/plain");
		header("Content-length: ".strlen($googleBaseContent));
		header("Content-Transfer-Encoding: binary");
		echo $googleBaseContent;
		exit;
	}
} else if (isset($_POST['format']) && strtolower($_POST['format']) == 'shopzilla') {
	
	$query = "SELECT * FROM ".$glob['dbprefix']."CubeCart_inventory INNER JOIN ".$glob['dbprefix']."CubeCart_category on ".$glob['dbprefix']."CubeCart_inventory.cat_id = ".$glob['dbprefix']."CubeCart_category.cat_id ".$whereClause." ORDER BY name ASC";
	$results = $db->select($query);

	if ($results == true) {
		$shopzillaContent = "Category\tManufacturer\tTitle\tProduct Description\tLink\tImage\tSKU\tStock\tCondition\tShipping Weight\tShipping Cost\tBid\tPromotional Description\tEAN/UPC\tPrice\n";
		
		for ($i=0; $i<count($results); $i++) {
			$row = $results[$i];
				
			$salePrice = salePrice($results[$i]['price'], $results[$i]['sale_price']);
			$price = ($salePrice > 0) ? $salePrice : $price = $results[$i]['price'];
						
			$name = str_replace(array("&nbsp;","\t","\r","\n","\0","\x0B","
			"),"",strip_tags($results[$i]['name']));
			$name = str_replace("  ","",$name);
			$name = substr($name, 0, 100);
			
			$desc = str_replace(array("&nbsp;","\t","\r","\n","\0","\x0B","
			"),"",strip_tags($results[$i]['description']));
			$desc = str_replace("  ","",$desc);
			$desc = substr($desc, 0, 1000);
			
			$cat_name = str_replace(array("&nbsp;","\t","\r","\n","\0","\x0B","
			"),"",strip_tags($results[$i]['cat_name']));
			$cat_name = str_replace("  ","",$cat_name);
			
			if (!empty($name)) {
				if ($config['sef'] == 0) {
					$shopzillaContent .= $cat_name."\t\t".$name.'\t"'.$desc.'"\t'.$glob['storeURL']."/index.php?_a=viewProd&productId=".$results[$i]['productId'];
				} else {
					include_once("includes".CC_DS."sef_urls.inc.php");
					$shopzillaContent .= $cat_name."\t\t".$name."\t\"".$desc."\"\t".$glob['storeURL'].'/'.generateProductUrl($results[$i]['productId']);
				}
				
				if ($results[$i]['image']) {
					$shopzillaContent .= "\t".$glob['storeURL']."/images/uploads/".$results[$i]['image'];
				} else {
					$shopzillaContent .= "\t".$glob['storeURL']."/skins/".$config['skinDir']."/styleImages/nophoto.gif";
				}
				
				$shopzillaContent .= "\t".$results[$i]['productCode']."\tIn Stock\tNew\t".$results[$i]['weight']."\t\t\t\t".$results[$i]['eanupcCode']."\t".$price."\n";
	//		$shopzillaContent .= "\t".$results[$i]['productCode']."\tIn Stock\tNew\t".$results[$i]['prodWeight']."\t\t\t\t".$results[$i]['eanupcCode']."\t".$price."\n";

      }
		}
		
		$filename = "ShopZilla_".date("Ymd").".txt";

		header('Pragma: private');
		header('Cache-control: private, must-revalidate');
		header("Content-Disposition: attachment; filename=".$filename);
		header("Content-type: text/plain");
		header("Content-length: ".strlen($shopzillaContent));

		//header("Content-Transfer-Encoding: binary");
		echo $shopzillaContent;
		exit;
	}
} elseif (isset($_POST['format']) && strtolower($_POST['format']) == 'shopping.com') {
	
	$query = "SELECT * FROM ".$glob['dbprefix']."CubeCart_inventory ".$whereClause." ORDER BY name ASC";
	$results = $db->select($query);

	if ($results == true) {
		$shoppingContent = "mpn,upc,manufacturer,product name,product description,price,stock,stock description,product url,image url,category\n";
		
		for ($i=0; $i<count($results); $i++) {
			$row = $results[$i];
				
			$salePrice = salePrice($results[$i]['price'], $results[$i]['sale_price']);
			$price = ($salePrice > 0) ? $salePrice : $price = $results[$i]['price'];
						
			$name = str_replace(array("&nbsp;","\t","\r","\n","\0","\x0B","
			"),"",strip_tags($results[$i]['name']));
			$name = str_replace("  ","",$name);
			$desc = str_replace(array("&nbsp;","\t","\r","\n","\0","\x0B","
			"),"",strip_tags($results[$i]['description']));
			$desc = str_replace("  ","",$desc);
			
			if (!empty($name) && $price > 0) {
				if ($config['sef'] == 0) {
					$url = $glob['storeURL']."/index.php?_a=viewProd&productId=".$results[$i]['productId'];
				} else {
					include_once("includes".CC_DS."sef_urls.inc.php");
					$url = $glob['storeURL'].'/'.generateProductUrl($results[$i]['productId']);
				}
				if ($results[$i]['image']) {
					$image = $glob['storeURL']."/images/uploads/".$results[$i]['image'];
				} else {
					$image = $glob['storeURL']."/skins/".$config['skinDir']."/styleImages/nophoto.gif";
				}				
				$shoppingContent .= sprintf("%s,%s,%s,\"%s\",\"%s\",%s,%s,%s,%s,%s,%s\n", $row['productCode'], $row['eanupcCode'], '', $name, addslashes(html_entity_decode_utf8($desc, ENT_QUOTES)), $price, $stock, '', $url, $image, '');
			}
		}
		
		$filename = "Shopping.com_".date("Ymd").".txt";
		header('Pragma: private');
		header('Cache-control: private, must-revalidate');
		header("Content-Disposition: attachment; filename=".$filename);
		header("Content-type: text/plain");
		header("Content-length: ".strlen($shoppingContent));
		//header("Content-Transfer-Encoding: binary");
		echo $shoppingContent;
		exit;
	} 
} else if (isset($_POST['format']) && strtolower($_POST['format']) == 'cubecart') {
		
	$query = "SELECT * FROM ".$glob['dbprefix']."CubeCart_inventory";
	$results = $db->select($query);

	if ($results == true) {
	
	$ccContent = "Product Name,Product Code,Product Description,Price,Sale Price,Image,Stock Level,Use Stock,Master Category ID\r\n";
	
		for ($i=0; $i<count($results); $i++) {
			
			$name = str_replace(array("&nbsp;","\t","\r","\n","\0","\x0B","
			"),"",strip_tags($results[$i]['name']));
			$name = str_replace("  ","",$name);
			$desc = str_replace(array("&nbsp;","\t","\r","\n","\0","\x0B","
			"),"",strip_tags($results[$i]['description']));
			$desc = str_replace("  ","",$desc);
			
			$ccContent 	.= 	"\"".$name.
							"\",\"".$results[$i]['productCode'].
							"\",\"".$desc.
							"\",\"".$results[$i]['price'].
							"\",\"".$results[$i]['sale_price'].
							"\",\"".$results[$i]['image'].
							"\",\"".$results[$i]['stock_level'].
							"\",\"".$results[$i]['useStockLevel'].
							"\",\"".$results[$i]['cat_id'].
							"\"\r\n";
			
		}
		
		$filename = "CubeCart_Products_".date("Ymd").".csv";
		header('Pragma: private');
		header('Cache-control: private, must-revalidate');
		header("Content-Disposition: attachment; filename=".$filename);
		header("Content-type: text/plain");
		header("Content-length: ".strlen($ccContent));
		header("Content-Transfer-Encoding: binary");
		echo $ccContent;
		exit;	
	}
	
} else if (isset($_POST['format']) && strtolower($_POST['format']) == 'csv') {
	
	$query		= "SELECT * FROM ".$glob['dbprefix']."CubeCart_inventory INNER JOIN ".$glob['dbprefix']."CubeCart_category on ".$glob['dbprefix']."CubeCart_inventory.cat_id = ".$glob['dbprefix']."CubeCart_category.cat_id ".$whereClause." ORDER BY name ASC";
	$results	= $db->select($query);
	
	if ($results) {
		
		$output[] = '';
	}
	
} else if (isset($_POST['format']) && strtolower($_POST['format']) == 'archive') {
	## To Do
}

###################################################

require($glob['adminFolder'].CC_DS."includes".CC_DS."header.inc.php");


/*
if (isset($_POST['export'])) {
	$msg = 'Creating catalogue backup...';
	
	$query		= sprintf("SELECT * FROM `%sCubeCart_inventory`", $glob['dbprefix']);
	$results	= $db->select($query);
	
	function formatstrings(&$value, $key, $separator) {
		$value = str_replace(array("\n","\r"), '', $value);
		$value = str_replace($separator, '\\'.$separator, $value);
	}
	
	$csvfile = '/tmp/exportfile';
	
	$separator = (isset($_POST['separator'])) ? $_POST['separator'] : ',';
	for ($i=0;$i<count($results);$i++) {
		if ($_POST['format'] == 'csv') {
			$row = $results[$i];
			if ($i==0) {
				foreach ($row as $key => $val) {
					$fields[] = $key;
				}
				$output[] = implode($separator, $fields);
			}
			// create CSV file with headers
			array_walk($row, 'formatstrings', $separator);
			$output[] = implode($separator, $row);
		}
		// write to file?
	}
	$fp = fopen($csvfile, 'w+');
	fwrite($fp, implode("\n", $output));
	fclose($fp);
	
	// Create the archive
	include "/home/martin/cubecart/classes/misc/zip.inc.php";
	$createZip = new createZip;  

	$createZip->addDirectory("catalog/");
	
	$zipArray[] = $csvfile;
	
	$createZip->addFiles($zipArray);

	$fileContents = file_get_contents("/tmp/exportfile");  
	$createZip->addFile($fileContents, "dir/exportfile");  

	$createZip->saveArchive('archive.zip');
//	$fileName = "archive.zip";


	$fd = fopen ($fileName, "wb");
	$out = fwrite ($fd, $createZip->getZippedfile());
	fclose ($fd);
	
	//@unlink($csvfile);
}
*/


?>
<p class="pageTitle"><?php echo $lang['admin']['products_export_cat']?></p>
<?php 
if (isset($msg)) echo msg($msg); 
?>

<form method="post">
<select name="format" class="textbox">
	<!-- <option value="csv">CSV (Comma Separated Value)</select> -->
	<option value="googlebase">Google Base</option>
	<!-- <option value="kelkoo">Kelkoo</option> -->
	<!-- <option value="pricerunner">Pricerunner</option> -->
	<option value="shopping.com">Shopping.com</option>
	<option value="shopzilla">ShopZilla</option>
	<option value="CubeCart">CubeCart CSV (Comma Separated Value)</option>
</select>
<input type="submit" class="submit" name="export" value="<?php echo $lang['admin']['products_export_cat']?>" />
</form>