<?php
/**
 * Shopzilla Feed Parser 
 * Vinod Kumar 3/31/10
 * Version 1.0
 *
 */

/**
 * Search Shopzilla products
 *
 * @param array $params
 * @return array $Data
 */

class Shopzilla {

	private $apiKey = 'e35ea94a50d2f721b9036f815ffcb42e';
	private $publisherId = 35211;
	private $apiURL = "http://catalog.bizrate.com/services/catalog/v1/us/product?";
	private $allowedAttributes = array(
	'apiKey',
	'publisherId',
	'placementId',
	'categoryId',
	'keyword',
	'productId',
	'productIdType',
	'offersOnly',
	'merchantId',
	'brandId',
	'biddedOnly',
	'minPrice',
	'maxPrice',
	'minMarkdown',
	'zipCode',
	'freeShipping',
	'start',
	'results',
	'backfillResults',
	'startOffers',
	'resultsOffers',
	'sort',
	'attFilter',
	'attWeights',
	'attributeId',
	'resultsAttribute',
	'resultsAttributeValues',
	'showAttributes',
	'showProductAttributes',
	'minRelevancyScore',
	'maxAge',
	'showRawUrl',
	'imageOnly',
	'format',
	);

	public function __construct($apiKey=null, $publisherId=null) {

		if($apiKey) $this->apiKey = $apiKey;
		if($publisherId) $this->publisherId = $publisherId;
	}

	protected function _filterParams($params) {

		if(empty($params['results']))
		$params['results'] = 10;
		if(empty($params['start']))
		$params['start'] = 0;

		//Override these settings
		$params['format'] = 'json';
		$params['publisherId'] = $this->publisherId;
		$params['apiKey'] = $this->apiKey;

		$url_params = '';
		foreach ($params as $key => $value) {

			if(in_array($key, $this->allowedAttributes)) {
				$url_params .= $key.'='.$value.'&';
			}
		}

		return $url_params;
	}

	/**
	 * Search products with available filters
	 *
	 * @param array	$params
	 * @return array $Data
	 */
	public function ProductSearch($params)
	{

		$FeedUrl = $this->apiURL.$this->_filterParams($params);
		if($_GET['debug'] == 'msa123')
		echo '<hr>'.$FeedUrl;

		###### CURL & PARSE XML #####
		$CurlData = array(	"url" => $FeedUrl, "timeout" => '20');

		$result = json_decode(curl_get_contents($CurlData));
		//	prexit($result);

		$Data = array();

		$Data['TotalMatches'] = $result->products->totalResults;

		if ($Data['TotalMatches'] > 0) {

			$Data['MinPrice']['Label'] = $result->products->priceSet->minPrice->value;
			$Data['MinPrice']['Value'] = $result->products->priceSet->minPrice->integral;

			$Data['MaxPrice']['Label'] = $result->products->priceSet->maxPrice->value;
			$Data['MaxPrice']['Value'] = $result->products->priceSet->maxPrice->integral;

			foreach ($result->products->product as $i=>$product) {

				$Data['Products'][$i]['Title'] =  $product->title;
				$Data['Products'][$i]['Description'] = $product->description;
				$Data['Products'][$i]['Manufacturer'] = $product->manufacturer;
				$Data['Products'][$i]['Url'] = $product->url->value;
				$Data['Products'][$i]['ID'] = $product->id;
				$Data['Products'][$i]['Category_ID'] = $product->categoryId;

				foreach ($product->images->image as $k=>$image) {
					$Data['Products'][$i]['Images'][$k] = $image->value;
				}

				$Data['Products'][$i]['Stores'] = $product->priceSet->stores;
				$Data['Products'][$i]['MinPrice'] = $product->priceSet->minPrice->value;
				$Data['Products'][$i]['MaxPrice'] = $product->priceSet->maxPrice->value;
			}
		}
		return $Data;
	}

	/**
	 * Get Product Detail
	 *
	 * @param int $product_id
	 * @return array
	 */
	public function ProductDetail($product_id)
	{

		$FeedUrl = $this->apiURL.$this->_filterParams(array("productId"=>$product_id));
		if($_GET['debug'] == 'msa123')
		echo '<hr>'.$FeedUrl;

		###### CURL & PARSE XML #####
		$CurlData = array(	"url" => $FeedUrl, "timeout" => '20');

		$result = json_decode(curl_get_contents($CurlData));
//		prexit($result);

		$Product = array();

		if ($result->products->totalResults > 0) {

			$obj = $result->products->product[0];

			$Product['Title'] =  $obj->title;
			$Product['Description'] = $obj->description;
			$Product['Manufacturer'] = $obj->manufacturer;
			$Product['Url'] = $obj->url->value;
			$Product['ID'] = $obj->id;
			$Product['Category_ID'] = $obj->categoryId;

			foreach ($obj->images->image as $k=>$image) {
				$Product['Images'][$k] = $image->value;
			}

			$Product['Stores'] = $obj->priceSet->stores;
			$Product['MinPrice'] = $obj->priceSet->minPrice->value;
			$Product['MaxPrice'] = $obj->priceSet->maxPrice->value;
		}
		return $Product;
	}
}
?>