<?php
if (!defined('IN_59MIAO'))
{
	exit('Access Defined');
}
//系统设置控制器
class ApiAction extends Action {
	protected $homenav='';
	public function __construct() {
		parent::__construct();		
	}	
	public function index(){
		$_Apisetting=$this->_model->findOne();		
		$this->_tpl->assign('Setting', $_Apisetting);
		$this->_tpl->display(SMARTY_ADMIN.'api/show.tpl');
	}	
	//修改系统设置
	public function update(){	
		if (isset($_POST['send'])){
			//如果更新成功则写入文件
			if($this->_model->update()){
				//写入配置文件			
				if(!$this->saveConfig()){
					$this->_redirect->error('写入配置文件失败，请检查配置文件目录(config)是否存在');
				}
				$this->_redirect->succ(Tool::getPrevPage(), '修改Api设置成功！');
			}
			else{
				$this->_redirect->error('修改Api设置失败！');	
			}
			
		}
	}	
	//调取59秒分类信息
	public function api59miao(){		
	
		return $this->_model->api59miao();
	}
	//调取淘宝分类信息
	public function taoapicategory(){
		return $this->_model->taoapicategory();
	}
	//获取首页调取的商品信息
	public function getHomedata(){

		return $this->_model->getHomedata();
	}
		
	//保存配置文件
	public function saveConfig(){
		
		$our_appkey=$_POST['our_appkey'];
		$our_appsecret=$_POST['our_appsecret'];
		$our_api_cachetime=$_POST['our_cachetime'];
		$our_api_cachepath=$_POST['our_cachepath'];
		
		$tao_pid=$_POST['tao_pid'];
		$tao_appkey=$_POST['tao_appkey'];				
		$tao_secret=$_POST['tao_secret'];
		$tao_api_cachetime=$_POST['tao_cachetime'];
		$tao_api_cachepath=$_POST['tao_cachepath'];		
		
		$_br = "\r\n";
		$_tab = "\t";
		
		$_profile = '<?php'.$_br;
		
		
		$_profile .= $_tab."//api配置文件".$_br;
	
		$_profile .= $_tab."if (!defined('IN_59MIAO'))
						{
							exit('Access Defined');
						}".$_br;
		$_profile .= $_tab."define('APPKEY','{$our_appkey}');".$_br;
		$_profile .= $_tab."define('APPSECRET','{$our_appsecret}');".$_br;
		$_profile .= $_tab."define('API_CACHETIME','{$our_api_cachetime}');".$_br;
		$_profile .= $_tab."define('API_CACHEPATH','{$our_api_cachepath}');".$_br;

		$_profile .= $_br;				
		
		$_profile .= $_tab."define('TAO_APPKEY','{$tao_appkey}');".$_br;
		$_profile .= $_tab."define('TAO_APPSECRET','{$tao_secret}');".$_br;
		$_profile .= $_tab."define('TAO_PID','{$tao_pid}');".$_br;
		$_profile .= $_tab."define('TAO_API_CACHETIME','{$tao_api_cachetime}');".$_br;
		$_profile .= $_tab."define('TAO_CACHEPATH','{$tao_api_cachepath}');".$_br;
		
		$_profile .= $_br;		

		$_profile .= $_tab."define('CHARSET','UTF-8');".$_br;
		$_profile .= $_tab."define('APIURL','http://gw.api.59miao.com/Router/Rest?');".$_br;
		$_profile .= $_tab."define('API_CLEARCACHE','1 23 * *');".$_br;
		
		$_profile .= '?>'.$_br;
		
		if (!file_put_contents('./configs/api_config.inc.php', $_profile)) {
			return false;
		}
		else{
			return true;
		}
	}
	
}
?>