<?php
/*
　* @$Id
　* @
　* @Ver: 2.0
　* @Create: 2009-11-18 8:10
　* @Modify: 
　* @Author: CPWEB http://www.cpweb.net.cn
　* @Copyright (C) 2008 www.cpweb.net.cn. All Rights Reserved.
　* @License CPWEB is free software and use is subject to license terms
　*/
class SettingsAction extends GlobalAction{
	public function index()
	{
		$map["type_values"]=$_GET['sett'];
		$settings=D('SettingsView')->where($map)->findall();
		foreach ($settings AS $key ){
			$setting[$key['title']]=$key['set_values'];
		}
		$this->assign('setting',$setting);
		$this->display($_GET['sett']);
	}
	public function update(){
		$Settings=D("Settings");
		$data=$_POST;
		//填充为空的项目		
		if($_GET['sett']=='xt_set'){
			if($data["setting"]["sitename"]=='')$data["setting"]["sitename"]='cpweb' ;
			if($data["setting"]["siteurl"]=='')$data["setting"]["siteurl"]='http://www.zilaiye.org' ;
		}
		if($_GET['sett']=='attach_set'){
			if($data["setting"]["attach"]=='')$data["setting"]["attach"]='true' ;
			if($data["setting"]["attachsize"]=='')$data["setting"]["attachsize"]=1048576 ;
			if($data["setting"]["attachext"]=='')$data["setting"]["attachext"]='jpg,gif,png' ;
			if($data["setting"]["thumbmaxwidth"]=='')$data["setting"]["thumbmaxwidth"]=200 ;
			if($data["setting"]["thumbmaxheight"]=='')$data["setting"]["thumbmaxheight"]=300 ;
			if($data["setting"]["thumbsuffix"]=='')$data["setting"]["thumbsuffix"]='_thumb' ;
		}
		if(get_magic_quotes_gpc()){
			$data["setting"]['js']=stripslashes($data["setting"]['js']);
		}
		//LOGO设置
		if($_FILES['fileToUpload']['name']){
			$Curd=A('Curd');
			$cpweb_upload=$Curd->cpweb_upload();
			$data["setting"]['logo']=$cpweb_upload['cpweb_url'];
		}
		//更新配置数据
		$map_1["type_values"]=$_GET['sett'];
		$type=M('Type')->where($map_1)->find();
		$map["type_id"]=$type['id'];
		$settings=$Settings->where($map)->findall();
		$sett=array();
		foreach ($settings AS $key ){
			$sett[$key['title']]=$key;
		}
        foreach($data["setting"] AS $key => $value) {
            if($key!=='think_html_token'){
				if(array_key_exists($key,$sett)){
					//修改设置
					$datasave['id']=$sett[$key]['id'];
					$datasave["type_id"]=$type['id'];
					$datasave['title']=$key;
					$datasave["set_values"]=$value;
					$Settings->save($datasave);
				}else{
					//添加设置
					$dataadd["type_id"]=$type['id'];
					$dataadd['title']=$key;
					$dataadd["set_values"]=$value;
					$Settings->add($dataadd);
				}
			}
        }
		$this->assign('jumpUrl',__URL__.'/index/sett/'.$_GET['sett'].'/');
       	$this->success(L('CPWEB_SUCCESS_EDIT'));
	}
}
?>