<?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 MessageAction extends CommonAction{
	public function typeUcMessage()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'UcMessage';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		$this->display();
		return;
	}
	
	public function typeMessage()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'Message';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		$this->display();
		return;
	}
	
	public function typeOrder()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'Order';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		$this->display();
		return;
	}

	public function typeGoods()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'Goods';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		$this->display();
		return;
	}
	
	public function typeArticle()
	{
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		$map['pid'] = intval($_REQUEST['msg_id']);
		$map['rec_module'] = 'Article';
		$map['reply_type'] = intval($_REQUEST['reply_type']);
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		$this->display();
		return;
	}
	//查看与回复
	function edit() {
		$name=$this->getActionName();
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		$this->assign("param",array('msg_id'=>$id,'reply_type'=>1));  //输出查看回复列表的参数		
		$vo = $model->getById ( $id );
		$this->assign("param_back",array('msg_id'=>$vo['pid'],'reply_type'=>$vo['pid']>0?1:0));  //输出跳回回复列表页的参数
		$this->assign("rec_param",array("id"=>$vo['rec_id']));  //输出关联数据的参数
		$vo['user_info'] = D("User")->getById($vo['user_id']);
		$rec_module = $vo['rec_module'];  //当前留言类型
		//赋值管理员的回复
		$vo['adm_title'] = D("Message")->where("pid=".$vo['id']." and reply_type=2")->getField("title");
		$vo['adm_content'] = D("Message")->where("pid=".$vo['id']." and reply_type=2")->getField("content");
		$this->assign ( 'vo', $vo );	
		$this->assign('back_action','type'.$rec_module);	
		
		$this->display ();
	}
	
	
	function update() {
		//B('FilterString');
		$id = intval($_REQUEST['id']);
		$name=$this->getActionName();
		$model = D ( $name );
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		// 更新数据
		$list=$model->save ();
		if (false !== $list) {
			$msgData = D("Message")->getById($id);  //当前留言的信息
			//开始更新管理员的相应回复
			$adm_reply = D("Message")->where("pid=".$id." and reply_type=2")->find();
			if($adm_reply)
			{
				//已有回复的修改
				if($_REQUEST['adm_title']==""&&$_REQUEST['adm_content']==""||$_REQUEST['adm_title']==""&&$_REQUEST['adm_content']=="<br />")
				{
					//删除管理员回复
					D("Message")->where("pid=".$id." and reply_type=2")->delete();					
				}
				else 
				{
					$adm_reply['title'] = $_REQUEST['adm_title'];
					$adm_reply['content'] = $_REQUEST['adm_content'];
					$adm_reply['update_time'] = time();
					$adm_reply['score'] = 0;
					D("Message")->save($adm_reply);  //更新回复
				}
			}
			else 
			{
				//新增管理员回复
				if($_REQUEST['adm_title']!=""||$_REQUEST['adm_content']!="")
				{
					$adm_reply['title'] = $_REQUEST['adm_title'];
					$adm_reply['content'] = $_REQUEST['adm_content'];
					$adm_reply['create_time'] = time();
					$adm_reply['update_time'] = time();
					$adm_reply['pid'] = $id;
					$adm_reply['reply_type'] = 2;
					$adm_reply['status'] = 1;
					$adm_reply['user_id'] = 0;
					$adm_reply['user_name'] = $_SESSION['adm_name'];
					$adm_reply['rec_module'] = $msgData['rec_module'];
					if ($adm_reply['rec_module'] == 'Order'){
						$adm_reply['rec_id'] = $msgData['rec_id'];
					}else{
						$adm_reply['rec_id'] = $id;
					}
					
					$adm_reply['score'] = 0;
					D("Message")->add($adm_reply);
				}
			}
			D("Message")->where("id=".$id)->setField("update_time",time());  //更新时间
			//成功提示
			$this->saveLog(1);
			$this->success (L('EDIT_SUCCESS'));
		} else {
			//错误提示
			$this->saveLog(0);
			$this->error (L('EDIT_FAILED'));
		}
	}

	//禁用与恢复操作
	public function forbid() {
		$name=$this->getActionName();
		$model = D ($name);
		$pk = $model->getPk ();
		$id = $_REQUEST [$pk];
		$condition = array ($pk => array ('in', $id ) );
		$list=$model->forbid ( $condition );
		if ($list!==false) {
			$this->success ( L('FORBID_SUCCESS') );
		} else {
			$this->error  (  L('FORBID_FAILED') );
		}
	}
	function resume() {
		//恢复指定记录
		$name=$this->getActionName();
		$model = D ($name);
		$pk = $model->getPk ();
		$id = $_GET [$pk];
		$condition = array ($pk => array ('in', $id ) );
		if (false !== $model->resume ( $condition )) {
			$this->saveLog(1);
			$this->success ( L('RESUME_SUCCESS') );
		} else {
			$this->saveLog(0);
			$this->error ( L('RESUME_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 ()) {
					//echo $model->getlastsql();
					$model->where(array ("pid" => 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 ();
	}
}
?>