<?php
	ini_set('memory_limit', '512M');
	require_once '../app/Mage.php';

	umask(0);
	Mage::app()->setCurrentStore(Mage_Core_Model_App::DISTRO_STORE_ID);
	Mage::setIsDeveloperMode(true);

	require_once 'methods.php';
	$baseUrl = Mage::getBaseUrl();
	$realBaseUrl = str_replace("catfeed/xmlproduct.php/", "", $baseUrl);
	//$baseCurrencyCode = Mage::app()->getStore()->getDefaultCurrencyCode();
###############################
	$categoryId = Mage::app()->getRequest()->getParam('catid');
	if(empty($categoryId)){
		$categoryId = Mage::app()->getRequest()->getPost('catid');
	}

	if($categoryId){
		$category = Mage::getModel('catalog/category')->load($categoryId);
	} else {
		$message = Mage::helper('core')->__("There is no category with this id");
		die($message);
	}
	if(!$category->getId()){
		$message = Mage::helper('core')->__("There is no category with this id");
		die($message);
	}
	$storeId = Mage::app()->getStore()->getId();

	$productCollection = $category->getProductCollection();
    $productCollection	->addMinimalPrice()
						->addFinalPrice()
						->addTaxPercents()
			            ->addAttributeToSelect(array('product_url','name','price','description','manufacturer','image','model'))
			            ->joinField('qty',
			                'cataloginventory/stock_item',
			                'qty',
			                'product_id=entity_id',
			                '{{table}}.stock_id=1',
			                'left');

   	Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($productCollection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($productCollection);
    $productCollection->addUrlRewrite($category->getId());

	header("content-type:text/xml");

	$xml.="<PRODUCTS>\n";
	foreach($productCollection as $product){
		$xml.="<PRODUCT>\n";
			//$xml.="<CATEGORY_NAME><![CDATA[{$category->getName()}]]></CATEGORY_NAME>\n";
			$flateRate = Mage::getStoreConfig('carriers/flatrate/price');

			$getProductUrl = str_replace("xmlproduct.php/", "index.php/", $product->getProductUrl());
			$getImageUrl = str_replace("xmlproduct.php/", "index.php/", $product->getImageUrl());

			$xml.="<PRODUCT_URL><![CDATA[{$getProductUrl}]]></PRODUCT_URL>\n";
			$xml.="<PRODUCT_NAME><![CDATA[{$product->getName()}]]></PRODUCT_NAME>\n";
			$xml.="<MODEL><![CDATA[{$product->getModel()}]]></MODEL>\n";
			$xml.="<DETAILS><![CDATA[{$product->getDescription()}]]></DETAILS>\n";
			$xml.="<PRICE><![CDATA[{$product->getMinimalPrice()}]]></PRICE>\n";
			//$xml.="<STOCK>{$product->getQty()}</STOCK>\n";
			$xml.="<CATALOG_NUMBER>{$product->getSku()}</CATALOG_NUMBER>\n";
			$xml.="<CURRENCY>ILS</CURRENCY>\n";
			$xml.="<SHIPMENT_COST>{$flateRate}</SHIPMENT_COST>\n";
			$xml.="<DELIVERY_TIME>14 DAYS</DELIVERY_TIME>\n";
			$xml.="<MANUFACTURER><![CDATA[{$product->getAttributeText('manufacturer')}]]></MANUFACTURER>\n";
			$xml.="<WARRANTY>1 YEAR</WARRANTY>\n";
			$xml.="<IMAGE><![CDATA[{$getImageUrl}]]></IMAGE>\n";
		$xml.="</PRODUCT>\n";
	}
	$xml.="</PRODUCTS>\n";

	echo $xml;
?>