<?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 LangConfAction extends CommonAction{
	public function add()
	{
		$arr  =   Dir::getList($this->getRealPath()."/app/Tpl/");
		foreach($arr as $item)
		{
			if($item!='..'&&$item!='.')
			{
				$themes[] = $item;
			}
		}
		$this->assign("themes",$themes);
		
		$currency_list = D("Currency")->findAll();
		$default_lang_id = D("LangConf")->where("lang_name='".C('DEFAULT_LANG')."'")->getField("id");
		foreach($currency_list as $k=>$v)
		{
			$currency_list[$k]['name'] = $v['name_'.$default_lang_id];
		}
		$this->assign('currency_list',$currency_list);
		parent::add();
	}
	public function edit()
	{
		$arr  =   Dir::getList($this->getRealPath()."/app/Tpl/");
		foreach($arr as $item)
		{
			if($item!='..'&&$item!='.')
			{
				$themes[] = $item;
			}
		}
		$this->assign("themes",$themes);
		
		$currency_list = D("Currency")->findAll();
		$default_lang_id = D("LangConf")->where("lang_name='".C('DEFAULT_LANG')."'")->getField("id");
		foreach($currency_list as $k=>$v)
		{
			$currency_list[$k]['name'] = $v['name_'.$default_lang_id];
		}
		$this->assign('currency_list',$currency_list);
		parent::edit();
	}
	//增
	public function insert()
	{
		$lang_conf_table = C("LANG_CONF"); //需要增加字段的表集合
		
		//自动创建数据对象
		$name=$this->getActionName();
		$model = D ($name);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}		
		$id = $model->add();
		
		if($id)
		{
			//开始处理相应的多语言表节构
			foreach($lang_conf_table as $table_name => $table)
			{
				$sql = "ALTER TABLE ".C("DB_PREFIX").$table_name." ADD COLUMN ";
				foreach($table as $column_name=>$column_type)
				{
					$sql_tmp=$sql.$column_name."_".$id." ".$column_type." CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ";
					D()->execute($sql_tmp);
				}			
			}
			$this->success (L('ADD_SUCCESS'));
		}
		else 
		{
			//失败提示
			$this->error (L('ADD_FAILED'));
		}		
	}
	
	//永久删除
	public function foreverdelete()
	{
		$lang_item = D("LangConf")->getById(intval($_REQUEST['id']));
		if($lang_item['lang_name'] == sunnyiC("DEFAULT_LANG"))
		{
			$this->error ( L('DEFAULT_LANG_CANNOT_DELETE') );
			exit;
		}
		$lang_conf_table = C("LANG_CONF"); //需要增加字段的表集合		
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk];
			if (isset ( $id )) {
				$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
				if (false !== $model->where ( $condition )->delete ()) {
					//成功时删除相关表结构
					
					foreach($lang_conf_table as $table_name=>$table)
					{
						$sql = "ALTER TABLE ".C("DB_PREFIX").$table_name." drop COLUMN ";
						foreach($table as $column_name=>$column_type)
						{
							$sql_tmp = $sql.$column_name."_".$id;
							D()->execute($sql_tmp);
						}
					}
					$this->success (L('DEL_SUCCESS'));
				} else {
					$this->error (L('DEL_FAILED'));
				}
			} else {
				$this->error ( L('INVALID_OP') );
			}
		}
		$this->forward ();
		
		//$this->ajaxReturn(array(),"删除失败",0);
	}
	
	public function update()
	{
		$lang_item = D("LangConf")->getById(intval($_REQUEST['id']));
		if($lang_item['lang_name'] == sunnyiC("DEFAULT_LANG"))
		{
			if($_REQUEST['lang_name']!=$lang_item['lang_name'])
			{
			$this->error ( L('DEFAULT_LANGNAME_CANNOT_MODIFY') );
			exit;
			}
		}
		parent::update();
	}
}
?>