<?php
// +----------------------------------------------------------------------
// | Sunnyi 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.sunnyi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 商易网络(admin@sunnyi.cn)
// +----------------------------------------------------------------------

// 系统配置

class SysConfAction extends CommonAction{
	
	public function index(){
		$list = D($this->getActionName())->where("status=1 and group_id>0")->findAll();
		$conf_list = array(); //用于输出分组格式化后的数组
		foreach($list as $k=>$v)
		{
			if($v['name']=='DEFAULT_LANG')
			{
			   $lang_list =D("LangConf")->findAll();
			   $v['val_arr'] = array();
			   foreach($lang_list as $lang_item)
			   {
			   		$v['val_arr'][] = $lang_item['lang_name'];
			   }
			}
			elseif($v['name']=='DEFAULT_USER_GROUP')
			{
			   $group_list =D("UserGroup")->findAll();
			   $v['val_arr'] = array();
			   foreach($group_list as $group_item)
			   {
			   		$v['val_arr'][] = $group_item['id'];
			   }
			}
			else			
			{
				$v['val_arr'] = explode(",",$v['val_arr']);
			}
			if($v['name']!='URL_MODEL')
			$conf_list[L("SYSCONF_GROUP_".$v['group_id'])][$k] = $v;
		}	
		$this->assign("conf_list",$conf_list);
		$this->display();
	}
	
	public function update()
	{
		$upload_list = $this->uploadFile(0,"public");
		if($upload_list)
		{
			foreach($upload_list as $upload_item)
			{
				if($upload_item['key']=="SHOP_LOGO")
				{
					$shop_logo = $upload_item['recpath'].$upload_item['savename'];
				}
				if($upload_item['key']=="WATER_IMAGE")
				{
					$water_image = $upload_item['recpath'].$upload_item['savename'];
				}
				if($upload_item['key']=="NO_PIC")
				{
					$no_pic = $upload_item['recpath'].$upload_item['savename'];
				}
			}
		}
		$list = D($this->getActionName())->where("status=1")->findAll();
		foreach($list as $k=>$v)
		{
			$v['val'] = isset($_REQUEST[$v['name']])?$_REQUEST[$v['name']]:$v['val'];
			if($v['name']=="SHOP_LOGO")
			{
				if($shop_logo)
				{
					@unlink($this->getRealPath().$v['val']);
					$v['val'] = $shop_logo;
				}
			}

			if($v['name']=="WATER_IMAGE")
			{
				if($water_image)
				{
					@unlink($this->getRealPath().$v['val']);
					$v['val'] = $water_image;
				}
			}
			if($v['name']=="NO_PIC")
			{
				if($no_pic)
				{
					@unlink($this->getRealPath().$v['val']);
					$v['val'] = $no_pic;
				}
			}
			if($v['name']=='URL_ROUTE')
			{
				$url_model_cfg_id = D("SysConf")->where("name='URL_MODEL'")->getField("id");
				if($v['val']==0)
				{
					$sql = "update ".C("DB_PREFIX")."sys_conf set val = 0 where id=".$url_model_cfg_id;
				}
				else 
				{
					$sql = "update ".C("DB_PREFIX")."sys_conf set val = 2 where id=".$url_model_cfg_id;
				}
			}
			D($this->getActionName())->save($v);
		}
		D("SysConf")->query($sql);

		$a = Dir::delDir($this->getRealPath()."/admin/Runtime/");        
        $f = Dir::delDir($this->getRealPath()."/app/Runtime/");	
        @mkdir($this->getRealPath()."/admin/Runtime/",0777);
        @mkdir($this->getRealPath()."/app/Runtime/",0777);

		$this->success(L('EDIT_SUCCESS'));
	}
}
?>