<?php
class XinwenAction extends AdmAction{
	// 新闻首页
	public function index(){
		// 获取游戏
		$yxList = F("yxList");
		$this->assign("yxList",$yxList);
		// 查询新闻
		$Data = M("Xw");
		$xwList = $Data->field("xw_id,yx_name,xw_type,xw_title,xw_date")->join("yeyou_yx ON yx_id = xw_yx")->order("xw_date DESC")->limit(50)->select();
		$this->assign("xwList",$xwList);
		
		// 更新缓存
		$Data = M("Xw");
		$xwList = $Data->field("xw_id,xw_title,xw_date")->where("xw_type = 1")->order("xw_date DESC")->limit(50)->select();
		F("xwList",$xwList);
		$Data = M("Xw");
		$hdList = $Data->field("xw_id,xw_title,xw_date")->where("xw_type = 2")->order("xw_date DESC")->limit(50)->select();
		F("hdList",$hdList);
		$Data = M("Xw");
		$zxList = $Data->field("xw_id,xw_type,xw_title,xw_date")->order("xw_date DESC")->limit(7)->select();
		F("zxList",$zxList);
		// 显示页面
		$this->display();
	}
	// 修改新闻
	public function updxw(){
		// 获取编号
		$id = INT("id");
		if(!$id){
			redirect("/Adm/Xinwen/index.html");
			exit;
		}
		// 查询新闻
		$Data = M("Xw");
		$xwObj = $Data->where("xw_id = $id")->find();
		if(!$xwObj){
			redirect("/Adm/Xinwen/index.html");
			exit;
		}
		$this->assign("xwObj",$xwObj);
		// 修改新闻
		if($_POST){
			// 获取参数
			$id = INT("id");
			$yx = INT("yx");
			$type = INT("type");
			$title = GET("title");
			$content = $_POST["content"];
			// 判断参数
			if(!$title || !$content){
				$this->error("每一项都必须填写或选择!");
				exit;
			}
			// 修改新闻
			$Data = M("Xw");
			$xwData["xw_yx"] = $yx;
			$xwData["xw_type"] = $type;
			$xwData["xw_title"] = $title;
			$xwData["xw_content"] = $content;
			$rel = $Data->where("xw_id = $id")->save($xwData);
			if($rel === false){
				$this->error("修改失败!");
				exit;
			}
			
			// 添加成功
			$this->success("操作成功!");
			exit;
		}
		
		// 获取游戏
		$yxList = F("yxList");
		$this->assign("yxList",$yxList);
		// 显示页面
		$this->display();
	}
	// 添加新闻
	public function addxw(){
		// 获取参数
		$yx = INT("yx");
		$type = INT("type");
		$title = GET("title");
		$content = $_POST["content"];
		// 判断参数
		if(!$title || !$content){
			$this->error("每一项都必须填写或选择!");
			exit;
		}
		// 添加新闻
		$Data = M("Xw");
		$xwData["xw_yx"] = $yx;
		$xwData["xw_type"] = $type;
		$xwData["xw_title"] = $title;
		$xwData["xw_content"] = $content;
		$xwData["xw_date"] = time();
		$rel = $Data->add($xwData);
		if($rel === false){
			$this->error("新闻添加失败!");
			exit;
		}
		// 添加成功
		$this->success("操作成功!");
		exit;
	}
	// 删除新闻
	public function delxw(){
		// 获取编号
		$id = INT("id");
		if(!$id){
			redirect("/Adm/Xinwen/index.html");
			exit;
		}
		// 查询新闻
		$Data = M("Xw");
		$xwObj = $Data->where("xw_id = $id")->find();
		if(!$xwObj){
			redirect("/Adm/Xinwen/index.html");
			exit;
		}
		// 删除新闻
		$rel = $Data->where("xw_id = $id")->delete();
		if($rel === false){
			$this->error("删除失败!");
			exit;
		}
		// 操作成功
		$this->success("操作成功!","/Adm/Xinwen/index.html");
		exit;
	}
}
?>