<?php
/***************************************************************************
*                                                                          *
* Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
*                                                                          *
* This  is  commercial  software,  only  users  who have purchased a valid *
* license  and  accept  to the terms of the  License Agreement can install *
* and use this program.                                                    *
*                                                                          *
****************************************************************************
* PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
* "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
****************************************************************************/
//
//$Id: XmlfeedController.php, v 1.0 2011/06/14 20:52:11
//
class XmlfeedController extends Controller {	 
	// Google PLA Feeds
        public function indexAction() {
            
        }
	public function googleshoppingAction()
	{
		
		
		$languageIDs		= 	$this->ObjView->languageIDs;		
		$ObjProductList 	= 	new ProductListModel();
		$params                 =       array('languageIDs'	=>	$languageIDs,'userGroupID'=>0,'customOrderBy'=>'new_products','googlePLA'=>'Yes');
		$products		=	array();
		$listProducts           =       $ObjProductList->listActiveProducts($params);
                $siteName               =       $this->getSettingValue('site_settings', 'site_name');
                $siteUrl                =       $this->getSettingValue('site_settings', 'global_http_url');
		$xml                    =       '<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">';                
		$xml.='
					<channel>
						<title>'.$siteName.'</title>
						<link>'.$siteUrl.'</link>
						<description>'.$siteName. ' - Branded Shoes, Designer Clothing, Accessories for Men and Women. | Cash on Delivery, Free Shipping, 14 day returns | Online Shopping store in Pakistan</description>
			';
		$xmlProducts=array();
                $currencyCode   =   $this->ObjView->_currencies[$this->ObjView->_defaultCurrency]['currencyCode'];
		foreach($listProducts as $key=>$product){
                        $description 	= 	strip_tags(html_entity_decode($product['productDescription']));
                        $link           = 	$siteUrl.$product['productUrl'].'.html';
                        $image          =       $siteUrl.'public/uploads/catalog/product/large/'.$product['productImage'];
                        //get google product category
                        $getGoogleCategory    = $ObjProductList->listGoogleProductCategory($product['productID']);
                        $category             = str_replace('&','&amp;',stripslashes($getGoogleCategory['google_category_name']));
                        $xml.='<item>';
                        $xml.='<g:id>'.$product['optionSKU'].'</g:id>';
                        $xml.='<title>'.stripslashes($product['productTitle']).'</title>';			
                        $xml.='<description>'.stripslashes($description).'</description>';
                        $xml.='<g:google_product_category>'.$category.'</g:google_product_category>';
                        $xml.='<g:product_type>'.$category.'</g:product_type>';
                        $xml.='<link>'.$link.'</link>';
                        $xml.='<g:mobile_link>'.$siteUrl.'mobile/'.$product['productUrl'].'.html'.'</g:mobile_link>';                        
                        $xml.='<g:image_link>'.$image.'</g:image_link>';    
                        $xml.='<g:condition>new</g:condition>'; 
                        if($product['productOptionQuantitySum']>=1) {
                            $xml.='<g:availability>in stock</g:availability>';
                        } else {
                            $xml.='<g:availability>out of stock</g:availability>';
                        }
                        $product['offerPrice'] = number_format($product['offerPrice'],2);                        
                        $xml.='<g:price>'.str_replace(',','',$product['offerPrice']).' '.$currencyCode.'</g:price>';
                        //$xml.='<g:gtin>'.$product['productID'].'</g:gtin>';                        
                        $xml.='<g:mpn>'.$product['optionSKU'].'</g:mpn>';
                        $xml.='<g:custom_label_2>'.$product['optionSKU'].'</g:custom_label_2>';
                        if($product['brandName']!='') {
                            $xml.='<g:brand>'.$product['brandName'].'</g:brand>';
                        }
                        /*$xml.='	
						<g:shipping>
							<g:country>PK</g:country>
							<g:price>150 PKR</g:price>
						</g:shipping>
					</item>
				';
                         $xml.='<g:expiration_date>2004-08-19</g:expiration_date>';
                         */
			$xml.='<g:adwords_redirect>'.$link.'</g:adwords_redirect>';
			
                        $xml.='</item>';
			
			
		}		
		$xml.='		</channel>
				</rss>
		';
                $zipName  =	'products' . ".xml.gz"; // Zip name
                $xml  = gzencode($xml, 9);
		$fp   = fopen($zipName, 'wb');
		fwrite($fp, $xml);
		fclose($fp);
		//echo $xml;
		//header("Content-type: text/xml");
	}
}