<?php
    require("CommonAction.class.php");
  class categoryAction extends CommonAction
  {
   	 //栏目设定
	 public function category()
	 {
	    $category = M("category");
		$map["del"] = array("eq","0");
		$map["parentid"] = array("eq",0);
		$data = $category->where($map)->field("id,name,parentid,umen,sort")->order("sort asc")->findall();
		foreach($data as $key=>$value)
		{
		   $where["parentid"] = array("eq",$value["id"]);
		   $menu_data = $category->where($where)->field("id,name,parentid,umen,sort")->order("sort asc")->findall();
		   array_push($value,$menu_data);
		   $data[$key] = $value;
		}
		//获取栏目的个数
		//dump($data);
	    $this->assign("data",$data);
	    $this->display();
	 }
	 
	 public function categoryAdd()
	 {
	    $category = M("category");
	    $map["del"] = array("eq","0");
		$map["parentid"] = array("eq",0);
		$data = $category->where($map)->field("id,name")->order("id asc")->findall();
		//获取栏目的个数
	    $this->assign("data",$data);
		//栏目数
		$num = $this->getCategoryNum();
		$this->assign("num",$num);
	   $this->display();
	 }
	 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",0);
		$maps["del"] = array("eq",0);
		$data = $category->where($maps)->field("id,name")->order("id asc")->findall();
		$array = array("id"=>"0","name"=>"根目录");
		array_push($data,$array);
		
	    $this->assign("data",$data);
		//栏目数
	    $this->display();
	 }
	 
	 //栏目添加
	 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("栏目更新失败");
		}
	 }
  
  }



?>