<?php
require_once 'Services/AmazonECS4.php';
class amazon_search {

var $access_key='';
var $associate_id='';
var $amazon; // instance of amazon
var $search_kind_array=array();
var $sort_option_array=array();
var $response_options = array('Sort'=>'salesrank',
						'ResponseGroup' =>'Small,Images,ItemAttributes');
var $search_kind = 'Books';

function amazon_search($access_key,$associate_id,$cache_dir = '/tmp') {
	$this->access_key = $access_key;
	$this->associate_id = $associate_id;
	$this->init_options();
	$this->amazon = new Services_AmazonECS4($this->access_key,$this->associate_id);
	$this->amazon->setLocale('JP');
	$this->amazon->setVersion('2007-01-17');
	$dir_path = explode('/',dirname(__FILE__));
	$dummy = array_pop($dir_path);
	$home_dir = implode('/',$dir_path);
	
	$result = $this->amazon->setCache('file',array('cache_dir' => $cache_dir));
	if (PEAR::isError($result)) {
		echo htmlspecialchars($result->message, ENT_QUOTES);
		echo "Quicty error : Amazon.class.php : キャッシュの設定に失敗しました";
		exit();
	}
	$this->amazon->setCacheExpire(24*60*60);
}

function keyword_search($keywords) {
	$code = mb_internal_encoding();
	$myKeywords = $keywords;
	mb_convert_variables('UTF-8',$code,$myKeywords);
	$this->response_options['Keywords'] = $myKeywords;
	$result = $this->amazon->ItemSearch($this->search_kind,$this->response_options);
	if (PEAR::isError($result)) {
		$tmp['error'] = true;
		$error_message = mb_convert_encoding($result->message,$code,'UTF-8');
		$tmp['message'] = htmlspecialchars($error_message, ENT_QUOTES);
		$result = $tmp;
	} else {
		mb_convert_variables($code,'UTF-8',$result);
	}
	return $result;
}

function asin_search($asin) {
	$code = mb_internal_encoding();
	$pattern = array('ISBN','isbn','Isbn','ASIN','asin','Asin','-');
	$myAsin = str_replace($pattern,'',$asin);
	if(strlen($myAsin)==13) {
		$this->response_options['IdType'] = 'ISBN';
		$this->response_options['SearchIndex'] = 'Books';
	}
	$result = $this->amazon->ItemLookup($myAsin,$this->response_options);
	if (PEAR::isError($result)) {
		$tmp['error'] = true;
		$error_message = mb_convert_encoding($result->message,$code,'UTF-8');
		$tmp['message'] = htmlspecialchars($error_message, ENT_QUOTES);
		$result = $tmp;
	} else {
		mb_convert_variables($code,'UTF-8',$result);
	}
	return $result;
}


//-------------------------------------------------------------

function set_response_option($response_group='',$sort_option='') {
	if($response_group) {
		$myResponse_group = $response_group;
	} else {
		$myResponse_group = 'Small,Images,ItemAttributes,SalseRank';
	}
	if($sort_option and array_key_exists($sort_option,$this->sort_option_array)) {
		$mySort_option = $sort_option;
	} else {
		$mySort_option = 'salesrank';
	}
	$this->response_options = array(
		'Sort' => $mySort_option,
		'ResponseGroup' => $myResponse_group,
	);
	return $this->response_options;
}


function set_search_kind($search_kind) {
	if($search_kind and 
		array_key_exists($search_kind,$this->search_kind_array)) {
		$this->search_kind = $search_kind;
	} else {
		$this->search_kind = 'Books';
	}
	return $this->search_kind;
}


function init_options() {
	$this->search_kind_array = array(
		'Books'=>'書籍（和書）',
		'Classical'=>'クラシック音楽',
		'DVD'=>'DVD',
		'Electronics'=>'電化製品',
		'ForeignBooks'=>'書籍（洋書）',
		'Hobbies'=>'ホビー',
		'Kitchen'=>'ホーム＆キッチン',
		'Music'=>'音楽',
		'MusicTracks'=>'音楽（曲名）',
		'Software'=>'ソフトウェア',
		'SportingGoods'=>'スポーツ用品',
		'Toys'=>'おもちゃ＆ホビー',
		'VHS'=>'ビデオ',
		'Video'=>'DVD＆ビデオ',
		'VideoGames'=>'ゲーム',
		'Blended'=>'すべて'
	);
	$this->sort_option_array = array(
		'salserank'=>'売り上げ順',
		'pricerank'=>'価格順（安い順）',
		'-pricerank'=>'価格順（高い順）',
		'titlerank'=>'名前順',
		'-titlerank'=>'名前逆順',
		'orig-rel-date'=>'オリジナル発売日順（古い順）',
		'-orig-rel-date'=>'オリジナル発売日順（新しい順）',
		'release-date'=>'発売日順（古い順）',
		'-release-date'=>'発売日順（新しい順）'
	);
}


} // end of class amazon_search

?>