<?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 AdminAction extends CommonAction{
	public function add()
	{
		$role_list = D("Role")->where("status=1")->findAll();
		$this->assign("role_list",$role_list);
		$this->display();
	}
	
	function insert() {
		//B('FilterString');
		$adm_pwd = $_REQUEST['adm_pwd'];
		$name=$this->getActionName();
		$model = D ($name);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		$model->adm_pwd = md5($adm_pwd);
		//保存当前数据对象
		$list=$model->add ();
		if ($list!==false) { //保存成功
			$this->saveLog(1,$list);
//			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success (L('ADD_SUCCESS'));
		} else {
			//失败提示
			$this->saveLog(0,$list);
			$this->error (L('ADD_FAILED'));
		}
	}
	
	function edit() {
		$role_list = D("Role")->where("status=1")->findAll();
		$this->assign("role_list",$role_list);
		$name=$this->getActionName();
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		$vo = $model->getById ( $id );

		$this->assign ( 'vo', $vo );
		$this->display ();
	}
	
	function update() {
		//B('FilterString');
		$new_adm_pwd = $_REQUEST['adm_pwd_new'];
		$id = intval($_REQUEST['id']);
		$name=$this->getActionName();
		$model = D ( $name );
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		if($new_adm_pwd!="")
		{
			$model->adm_pwd = md5($new_adm_pwd);
		}
		else 
		{
			$model->adm_pwd = D("Admin")->where("id=".$id)->getField("adm_pwd");
		}
		// 更新数据
		$list=$model->save ();
		if (false !== $list) {
			//成功提示
			$this->saveLog(1);
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success (L('EDIT_SUCCESS'));
		} else {
			//错误提示
			$this->saveLog(0);
			$this->error (L('EDIT_FAILED'));
		}
	}
	
	public function foreverdelete() {
		//删除指定记录
		$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 ) ) );
				$adm_list =D($name)->where($condition)->findAll();
				foreach($adm_list as $adm_item)
				{
					if($adm_item['adm_name']==sunnyiC("SYS_ADMIN"))
					{
						$this->saveLog(0);
						$this->error ( L('CANNT_DELETE_DEFAULT_ADM') );
					}
				}
				if (false !== $model->where ( $condition )->delete ()) {
					//echo $model->getlastsql();
					$this->saveLog(1);
					$this->success (L('DEL_SUCCESS'));
				} else {
					$this->saveLog(0);
					$this->error (L('DEL_FAILED'));
				}
			} else {
				$this->saveLog(0);
				$this->error ( L('INVALID_OP') );
			}
		}
		$this->forward ();
	}
	
	public function forbid() {
		$name=$this->getActionName();
		$model = D ($name);
		$pk = $model->getPk ();
		$id = $_REQUEST [$pk];
		$condition = array ($pk => array ('in', $id ) );
				$adm_list =D($name)->where($condition)->findAll();
				foreach($adm_list as $adm_item)
				{
					if($adm_item['adm_name']==sunnyiC("SYS_ADMIN"))
					{
						$this->saveLog(0);
						$this->error ( L('CANNT_FORBID_DEFAULT_ADM') );
					}
				}
		$list=$model->forbid ( $condition );
		if ($list!==false) {
			$this->saveLog(1);
			$this->assign ( "jumpUrl", $this->getReturnUrl () );
			$this->success ( L('FORBID_SUCCESS') );
		} else {
			$this->saveLog(0);
			$this->error  (  L('FORBID_FAILED') );
		}
	}
}
?>