<?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 UserAction extends CommonAction{
	public function add() {
		$default_lang_id = D("LangConf")->where("lang_name='".C('DEFAULT_LANG')."'")->field("id")->find();
		$default_lang_id = $default_lang_id['id'];  //默认语言的ID
		$select_dispname = "name_".$default_lang_id;
		$this->assign("select_dispname",$select_dispname);
		$this->assign("group_list",D("UserGroup")->findAll());
		$this->display ();
	}
	function insert() {
		//B('FilterString');
		$user_pwd = $_REQUEST['user_pwd'];
		$name=$this->getActionName();
		$model = D ($name);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		$model->user_pwd = md5($user_pwd);
		//保存当前数据对象
		$list=$model->add ();
		if ($list!==false) { //保存成功
			$user_info = D("User")->getById($list);
			$score = $user_info['score'];
			$money = $user_info['money'];
			user_money_log($list,$list,'User',$money,'#ADMIN_ADD_USER#');
			user_score_log($list,$list,'User',$score,'#ADMIN_ADD_USER#');
			$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'));
		}
	}

	public function edit() {
		$default_lang_id = D("LangConf")->where("lang_name='".C('DEFAULT_LANG')."'")->field("id")->find();
		$default_lang_id = $default_lang_id['id'];  //默认语言的ID
		$select_dispname = "name_".$default_lang_id;
		$this->assign("select_dispname",$select_dispname);
		$this->assign("group_list",D("UserGroup")->findAll());
		
		
		$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_user_pwd = $_REQUEST['new_user_pwd'];
		$id = intval($_REQUEST['id']);
		$name=$this->getActionName();
		$model = D ( $name );
		$data = $model->create();
		if (false === $data) {
			$this->error ( $model->getError () );
		}
		if($new_user_pwd!="")
		{
			$data['user_pwd'] = md5($new_user_pwd);
		}
		else 
		{
			$data['user_pwd'] = D("User")->where("id=".$id)->getField("user_pwd");
		}
		$o_data = D("User")->getById($id);  //原来的用户数据
		// 更新数据
		$list=$model->save ($data);
		if (false !== $list) {
			//成功提示
			$score = intval($_POST['score']) - intval($o_data['score']);
			$money = floatval($_POST['money']) - floatval($o_data['money']);
			
			if($money!=0)
			user_money_log($o_data['id'],$o_data['id'],'User',$money,'#ADMIN_EDIT_USER#',true);
			if($score!=0)
			user_score_log($o_data['id'],$o_data['id'],'User',$score,'#ADMIN_EDIT_USER#',true);
			
			$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 ) ) );
				if (false !== $model->where ( $condition )->delete ()) {
					//删除会员将永久删除相关的一切留言，回复，订单，订单商品等数据，请谨慎操作
					//删除相关会员留言
					$msglist = D("Message")->where(array ("user_id" => array ('in', explode ( ',', $id ) ) ))->findAll();
					D("Message")->where(array ("user_id" => array ('in', explode ( ',', $id ) ) ))->delete();
					foreach($msglist as $item)
					{
						D("Message")->where("pid=".$item['id'])->delete();
					}
					//删除订单及商品
					$orderlist = D("Order")->where( array ("user_id" => array ('in', explode ( ',', $id ) ) ))->findAll();
					D("Order")->where( array ("user_id" => array ('in', explode ( ',', $id ) ) ))->delete();
					foreach($orderlist as $orderitem)
					{
						D("OrderGoods")->where("order_id=".$orderitem['id'])->delete();
					}
					
					//删除会员的收货人列表
					D("UserConsignee")->where(array ("user_id" => array ('in', explode ( ',', $id ) ) ))->delete();
					
					//删除相关会员的资金与积分日志
					M("UserMoneyLog")->where(array ("user_id" => array ('in', explode ( ',', $id ) ) ))->delete();
					M("UserScoreLog")->where(array ("user_id" => array ('in', explode ( ',', $id ) ) ))->delete();
					$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 ();
	}
}
?>