<?php
class CategoryAction extends CommonAction {

	public function index(){
			$category=D('category');
			
			$list=$category->field("id,name,title,pid,path,concat(path,'-',id) as bpath")->order('bpath')->select();
			
			foreach($list as $key=>$value){
				$list[$key]['count']=count(explode('-',$value['bpath']));
			}
			

			$this->assign('list',$list);
			
			$this->display();
		}
		
	
    public function insert() {
        $Cate = D('Category');
        if ($category = $Cate->create()) {
            $list = $Cate->add();
            if ($list) {
                $category["id"] = $list;
               
                $this->success("类别增加成功！");
            }else {
                $this->error("类别增加失败!");
            }
        }else {
            $this->error($Cate->getError());
        }
    }

    public function delete() {
        $Cate = M('Category');
        $id = $_REQUEST[$Cate->getPk()];
        if (isset($id)) {
            $condition[$Cate->getPk()] = $id;
            if ($Cate->where($condition)->delete()) {
                $this->ajaxReturn($id, $id."分类删除成功！", 1);
            }else {
                $this->error($Cate->getError());
            }
        }
        
    }
        // 编辑数据 
    public function edit() { 
        if(!empty($_GET['id'])) { 
            $Category    =    M("Category"); 
            $vo    =    $Category->getById($_GET['id']); 
            if($vo) { 
                $this->assign('vo',$vo); 
                $this->display(); 
            }else{ 
                exit('编辑项不存在！'); 
            } 
        }else{ 
            exit('编辑项不存在！'); 
        } 
    } 

     // 更新数据 
    public function update() { 
        $Category    =    D("Category"); 
        if($vo = $Category->create()) { 
            $list=$Category->save(); 
            if($list!==false){ 
                $this->success('数据更新成功！'); 
            }else{ 
                $this->error("没有更新任何数据!"); 
            } 
        }else{ 
            $this->error($Category->getError()); 
        } 
    } 
    

}
?>