<?php
class ClientApiController extends AppController {
	var $name = 'client_api';
	var $uses = array('Shop','ShopType','Prefecture','City','Banner');
	var $autoLayout = false;
	var $autoRender = false;
	var $helpers = array('Html','UploadPack.Upload');

	/**
	 *
	 * 更新情報確認API
	 *
	 */
	function check_updated() {

		//前回更新日時取得(ない場合は、エラー終了)
		if($this->request->is('get')){
			$date = $this->request->query['date'];
			if(empty($date)){
				$data['rtn_code'] = 0;
				$json_value	= json_encode( $data );
				header( 'Content-Type: text/javascript; charset=utf-8' );
				echo $json_value;
				exit;
			}
		}
		$cond_shop = array('conditions'=>array('Shop.updated > '.$date ));
		$cond_shoptype = array('conditions'=>array('ShopType.updated > '.$date ));
		$cond_city = array('conditions'=>array('City.updated > '.$date ));

		$shop_count = $this->Shop->find('count',$cond_shop);
		$shoptype_count = $this->Shop->find('count',$cond_shoptype);
		$city_count = $this->Shop->find('count',$cond_city);

		$data['rtn_code'] = 1;
		if($shop_count > 0){
			$data['shop_updated'] = 1;
		}else{
			$data['shop_updated'] = 0;
		}
		if($shoptype_count > 0){
			$data['shoptype_updated'] = 1;
		}else{
			$data['shoptype_updated'] = 0;
		}
		if($city_count > 0){
			$data['city_updated'] = 1;
		}else{
			$data['city_updated'] = 0;
		}

		$json_value	= json_encode( $data );
		header( 'Content-Type: text/javascript; charset=utf-8' );
		echo $json_value;
	}

	/**
	 *
	 * 店舗データ取得用API
	 *
	 */
	function get_shop_data() {

		$this->Shop->recursive = 0;
		$this->Shop->unbindModel(array('belongsTo'=>array('ShopType','Prefecture','City')),false);
		$fields = array('Shop.id', 'Shop.shopcode', 'Shop.shop_type_id','Shop.name','Shop.kana', 'Shop.postcode', 'Shop.prefecture_id', 'Shop.city_id', 'Shop.address1', 'Shop.address2','(X(latlng)) as Shop__lat', '(Y(latlng)) as Shop__lng', 'Shop.tel', 'Shop.status');

		//日付取得(ない場合は、全件送信)
		if($this->request->is('get') || $this->request->is('post')){
			$date = $this->request->query['date'];
		}

		if(empty($date)){
			$shop_data = $this->Shop->find('all', array('fields'=>$fields));
		}else{
			$shop_data = $this->Shop->find('all', array('fields'=>$fields,'conditions'=>array('Shop.updated > '.$date)));
		}

		if(count($shop_data) > 0){
			$result['rtn_code'] = 1;
			$result['hit_num'] = count($shop_data);
			$shop_data = array_merge($result,$shop_data);
		}else {
			$shop_data['rtn_code'] = 0;
		}
		$json_value	= json_encode( $shop_data );
		header( 'Content-Type: text/javascript; charset=utf-8' );
		echo $json_value;
	}

	/**
	 *
	 * 業態データ取得用API
	 *
	 */
	function get_shoptype_data() {

		$this->ShopType->recursive = 0;
		$fields = array('ShopType.id', 'ShopType.name', 'ShopType.logo_file_name','ShopType.status');


		//日付取得(ない場合は、全件送信)
		if($this->request->is('get')){
			$date = $this->request->query['date'];
		}

		if(empty($date)){
			$data = $this->ShopType->find('all', array('fields'=>$fields));
		}else{
			$data = $this->ShopType->find('all', array('fileds'=>$fields,'conditions'=>array('ShopType.updated > '.$date)));
		}

		if(count($data) > 0){
			$this->set('datas',$data);
			$this->render();
		}else {
			$data['rtn_code'] = 0;
			$json_value	= json_encode( $data );
			header( 'Content-Type: text/javascript; charset=utf-8' );
			echo $json_value;
		}
	}

	/**
	 *
	 * 県コードデータ取得用API
	 *
	 */
	function get_prefecture_data() {

		$this->Prefecture->recursive = 0;
		$fields = array('Prefecture.id', 'Prefecture.name');

		$data = $this->Prefecture->find('all', array('fields'=>$fields));

		if(count($data) > 0){
			$result['rtn_code'] = 1;
			$result['hit_num'] = count($data);
			$data = array_merge($result,$data);
		}else {
			$data['rtn_code'] = 0;
		}
		$json_value	= json_encode( $data );
		header( 'Content-Type: text/javascript; charset=utf-8' );
		echo $json_value;
	}

	/**
	 *
	 * 都市コードデータ取得用API
	 *
	 */
	function get_city_data() {

		$this->City->recursive = 0;
		$fields = array('City.id','City.prefecture_id','City.name');


		//日付取得(ない場合は、全件送信)
		if($this->request->is('get')){
			$date = $this->request->query['date'];
		}

		if(empty($date)){
			$data = $this->City->find('all', array('fields'=>$fields));
		}else{
			$data = $this->City->find('all', array('fileds'=>$fields,'conditions'=>array('City.updated > '.$date)));
		}

		if(count($data) > 0){
			$result['rtn_code'] = 1;
			$result['hit_num'] = count($data);
			$data = array_merge($result,$data);
		}else {
			$data['rtn_code'] = 0;
		}
		$json_value	= json_encode( $data );
		header( 'Content-Type: text/javascript; charset=utf-8' );
		echo $json_value;
	}

	/**
	 *
	 * 広告データ取得用API
	 *
	 */
	function get_banner_data() {

		$this->Banner->recursive = 0;
		$fields = array('Banner.id','Banner.name','Banner.image_file_name','Banner.url');

		$data = $this->Banner->find('all', array('fileds'=>$fields,'conditions'=>array('Banner.status = 1')));

		if(count($data) > 0){
			$keyArray = array_rand($data,1);
			$result['rtn_code'] = 1;
			$data = $data[$keyArray];
			$data = array_merge($result,$data);
		}else{
			$data['rtn_code'] = 0;
		}

		$this->set('data',$data);
		$this->render();
	}

	/**
	 *
	 * イメージデータ取得用API
	 *
	 */
	function get_image() {

		if($this->request->is('get')){
			$image = $this->request->query['image'];
			if(empty($image)){
				$data['rtn_code'] = 0;
				$json_value	= json_encode( $data );
				header( 'Content-Type: text/javascript; charset=utf-8' );
				echo $json_value;
			}
		}

		$image = APP . WEBROOT_DIR . $image;

		if(!file_exists($image)){
			$data['rtn_code'] = 0;
			$json_value	= json_encode( $data );
			header( 'Content-Type: text/javascript; charset=utf-8' );
			echo $json_value;
			exit;
		}

		$extension = $this->get_file_extention($image);

		if($extension == "gif"){
			header('Content-type: image/gif');
		}else if($extension == "jpg"){
			header('Content-type: image/jpeg');
		}else if($extension == "png"){
			header('Content-type: image/png');
		}

		readfile($image);
	}


	function show_json_data(){

		//更新チェック
		//$url = "http://localhost/client_api/check_updated?date=20121205";
		//店舗
		$url = "http://localhost/client_api/get_shop_data?date=20121204";
		//業態
		//$url = "http://localhost/client_api/get_shoptype_data?date=20121204";
		//県コード
		//$url = "http://localhost/client_api/get_prefecture_data?date=";
		//都市コード
		//$url = "http://localhost/client_api/get_city_data?date=";
		//広告
		//$url = "http://localhost/client_api/get_banner_data";

		$json = file_get_contents($url,true);
		$obj = json_decode($json,true);

		header( 'Content-Type: text/javascript; charset=utf-8' );
		print($url . "\n");
		print_r($obj);
	}

	/**
	 *
	 * イメージデータ拡張子取得
	 *
	 */
	function get_file_extention($file){
		$pos = strrpos($file,".");
		if($pos)
		$ext = substr($file,$pos+1);

		return $ext;
	}

}
?>
