<?php
   require("CommonAction.class.php");
  class subjectAction extends CommonAction
  {
     public function index()
	 {
	   $category = M('category');
	   $map['del'] = array('eq','0');
	   $map["parentid"] = array("eq",c('0.subjectid'));
	   $categoryData = $category->where($map)->field('id,name')->order('sort desc')->findall();
	   $this->assign('categoryData',$categoryData);
	   $this->assign('times',time());
	    $this->display('news/index');
	 }
     //添加新闻
	 public function addNews()
	 {
	    $news = D("admin.news");
		if ($news->create()) {
            if (false !== $news->add()) {
			    $this->assign("jumpUrl",__URL__."/lists");
                $this->success('新闻添加成功！');
            } else {
			    $this->assign("jumpUrl",__URL__."/lists");
                $this->error('新闻添加错误');
            }
        } else {
            // 字段验证错误
            $this->error($news->getError());
        }
		
	 }
	 //文章列表
	 public function lists()
	 {
	    $news = M("news");
		$category = M('category');
		import("@.ORG.Page");       //导入分页类
         //栏目栈
		$categorySt = array();	
		$map["del"] = array("eq","0");
		$mapd["parentid"] = array("eq",C('0.subjectid'));
		$categoryCl = $category->where($mapd)->field('id,name')->order('sort desc')->findall();
		foreach($categoryCl as $value)
		{
		   array_push($categorySt,$value["id"]);
		}
		$map["category"] = array("in",$categorySt);
		$count = $news->where($map)->count();    //计算总数
		$p = new Page ( $count, c("0.num") );
		$data = $news->limit($p->firstRow.','.$p->listRows)->where($map)->field("id,title,tj,times,hits,top,flash,category")->order("id desc,del desc")->findall();
		
		$p->setConfig('header','篇新闻');
		$page = $p->show ();            //分页的导航条的输出变量
		$this->assign( "page", $page);
		$this->assign("data",$data);
		$maps["del"] = array("eq","0");
		$this->assign("table","subject");
		$categoryData = $category->where($maps)->field('id,name')->order('sort desc')->findall();
	    $this->assign('categoryData',$categoryData);
	    $this->display('news/lists');
	 }

	//删除新闻
	public function del()
	{
	  $id = $_POST["id"];
	  $table = $_POST["table"];
	  $table = M($table);
	  $map["id"]=array("eq",$id);
	  $data = $table->where($map)->setField("del","-1");
	  if($data)
		{
		   $this->ajaxReturn(1,"删除成功",1);
		}else
		{
		   $this->ajaxReturn(0,"删除失败",0);
		}
	}
	//文章修改
	public function edit()
	{
	   $id=$_GET["id"];
	    if(!is_numeric($_GET["id"]))
	   {
	       $this->error("没有注入点谢谢");
		   exit;
	   }
	   $news = M("news");
	   $map["id"] = array("eq",$_GET["id"]);
	   $map["del"] = array("eq",0);
	   $data2 = $news->where($map)->select();
	   $category = M("category");
	   $maps["del"] = array("eq","0");
	   $data = $category->where($maps)->field("id,name")->select();
	   $this->assign("data",$data);
	   $this->assign("data2",$data2);
	   $this->display();
	}
	//文章修改
	public function updateNews()
	{
	   if(!is_numeric($_GET["id"]))
	   {
	       $this->error("没有注入点谢谢");
		   exit;
	   }
	   $map["id"] = array("eq",$_GET["id"]);
	   $news = M("news");
	   $data = $_POST;
	   $data["content"] = strip($_POST["content"]);
	   $data["top"] = judge($_POST["top"]);
	   $data["tj"] = judge($_POST["tj"]);
	   $data["flash"] = judge($_POST["flash"]);
	   $data["times"] = time();
	   $result = $news->where($map)->save($data);
	   if($result)
	   {
	     $this->assign("jumpUrl",__URL__."/lists");
	     $this->success("新闻更新成功");
	   }
	   else
	   {
	    $this->assign("jumpUrl",__URL__."/lists");
	     $this->success("新闻更新失败");
	   }
	 } 
	 //专题栏目的管理
	 public function manage()
	 {
	    import("@.ORG.Page");       //导入分页类
		$category = M("category");
		$map["del"] = array("eq","0");
		$map["parentid"] = array("eq",c('0.subjectid'));
		$count = $category->where($map)->count();    //计算总数
		$p = new Page ( $count, c("0.num") );
		$data = $category->limit($p->firstRow.','.$p->listRows)->where($map)->field("id,name,parentid,umen,sort")->order("sort asc")->findall();
		$p->setConfig('header','篇新闻');
		$page = $p->show ();            //分页的导航条的输出变量
		$this->assign("page", $page);
		$this->assign("data",$data);
	    $this->display("category/category");
	 }
	  public function categoryAdd()
	 {
	    $category = M("category");
	    $map["del"] = array("eq","0");
		$map["parentid"] = array("eq",c('0.subjectid'));
		$data = $category->where($map)->field("id,name")->order("id asc")->findall();
		//获取栏目的个数
	    $this->assign("data",$data);
		//栏目数
		$num = $this->getCategoryNum();
		$this->assign("num",$num);
	   $this->display('category/categoryAdd');
	 }
	 public function categoryedit()
	 {
		$id = $_GET["id"];
		$category = M("category");
	    $map["del"] = array("eq","0");
		$map["id"] = array("eq",$id);
		$categoryData = $category->where($map)->findall();
		$this->assign("categoryData",$categoryData);
		$maps["parentid"] = array("eq",c('0.subjectid'));
		$maps["del"] = array("eq",0);
		$data = $category->where($maps)->field("id,name")->order("id asc")->findall();
		$array = array("id"=>c("0.subjectid"),"name"=>"专题栏目");
		array_push($data,$array);
		
	    $this->assign("data",$data);
		//栏目数
	    $this->display('category/categoryedit');
	 }
	 
	 //栏目添加
	 public function addCategory()
	 {
	    $category = D("admin.category");
		 if ($category->create()) {
            if (false !== $category->add()) {
			    $this->assign("jumpUrl",__URL__."/category");
                $this->success('数据添加成功！');
            } else {
			    $this->assign("jumpUrl",__URL__."/category");
                $this->error('数据写入错误');
            }
        } else {
            // 字段验证错误
            $this->error($category->getError());
        }
	 }
	 //获取当前栏目的个数
	 private function getCategoryNum()
	 {
	     $category = M("Category");
		 $map["del"] = array("eq",0);
		 $num = $category->where($map)->count();
		 return $num;
	 }
	
	 //更新当前栏目
	 public function updateCategory()
	 {
	    //获取当前表单传值
		$category = M("category");
		$map["id"] = array("eq",$_POST["id"]);
		$result = $category->where($map)->save($_POST);
	    if($result)
		{
		    $this->assign("jumpUrl",__URL__."/category");
		   $this->success("栏目更新成功");
		}else
		{
		   $this->assign("jumpUrl",__URL__."/category");
		   $this->error("栏目更新失败");
		}
	 }
  
  }
?>
