<?php
/*
 *		Powered by YiCms 
 *
 *		mailto:yixiao@yicms.cn
 *
 */
class NewssortAction extends Action{
    public function checkUser($level){
		if(empty($_SESSION['aid'])) $this->redirect('/Admin/Index/login');
		if(!empty($level) && !in_array($level,$_SESSION['levels'])) $this->error('您没有此权限!.');
	}
	public function index(){
		$this->checkUser('3_1_1');
		$data = M('Nav');
		$list = $data->where('type=1')->field("id,name,pid,path,title,keywords,description,taxis,state,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 add(){
		$this->checkUser('3_1_2');
		$data = M('Nav');
		$list = $data->where('type=1')->field("id,name,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 check(){
		if(empty($_POST['name'])) $this->error('至少要填写分类名称! = =');	
		$this->checkUser('3_1_2');
		$_POST['dir'] = rtrim($_POST['dir'],'/');
		if($_POST['dir']=='html') $this->error('文件保存目录不能直接为html目录!');
		$data = D('Nav');
		if($data->create()){
			if($data->add()){
				$this->assign('jumpUrl','__URL__/');
				$this->success('添加分类成功!');
			}else{
				$this->error('添加分类失败!请重试');
			}
		}else{
			$this->error($data->getError());
		}
	}
	public function edit(){
		$this->checkUser('3_1_2');
		$id = (int)$_GET['id'];
		$data = M('Nav');
		$nav = $data->where("id = $id")->find();
		$this->assign('nav',$nav);
		$this->display();
	}
	public function amend(){
		if(empty($_POST['name'])) $this->error('啥都可以带走，请留下分类名称。 = =');	
		$this->checkUser('3_1_2');
		unset($_POST['__hash__']);
		$data = M('Nav');
		if(!$data->save($_POST)) $this->error('修改分类内容失败! = =');
		$this->assign('jumpUrl','__URL__/');
		$this->success('修改分类内容成功! ^_^');
	}
	public function del(){
		$this->checkUser('3_1_2');
		$id = (int)$_GET['id'];
		$nav = M('Nav');
		$list = $nav->where("concat('-',path,'-') like '%-{$id}-%' OR id={$id}")->field('id')->select();
		$data = M('News');
		foreach($list as $key=>$value){
			$id = $value['id'];
			$data->where("pid = $id")->delete();
		}
		$list = $nav->where("concat('-',path,'-') like '%-{$id}-%' OR id={$id}")->delete();
		$this->success('删除分类成功!');
	}
	public function stop(){
		$this->checkUser('3_1_2');
		$list['id'] = (int)$_GET['id'];
		$list['state'] = (int)$_GET['state'];
		$data = M('Nav');
		if(!$data->save($list)) $this->error('操作失败!请重试');
		$this->success('操作成功!');
	}
}
?>