<?php
/*
 *		Powered by YiCms 
 *
 *		mailto:yixiao@yicms.cn
 *
 */
class NavAction 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('1_6_1');
		$data = M('Nav');
		$list = $data->where('type=0')->field("id,name,pid,path,url,title,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('1_6_2');
		$data = M('Nav');
		$list = $data->where('type=0')->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('至少要填写导航名称! = =');	
		if(empty($_POST['url'])) $this->error('连接的地址要填写! = =');	
		$this->checkUser('1_6_2');
		$data = D('Nav');
		if($data->create()){
			if($data->add()){
				$this->assign('jumpUrl','__URL__/index/');
				$this->success('添加导航成功!');
			}else{
				$this->error('添加导航失败!请联系网络管理员');	
			}
		}else{
			$this->error($data->getError());
		}
	}
	public function edit(){
		$this->checkUser('1_6_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']) || empty($_POST['url'])) $this->error('其他你都能带走,但你得留下标题和网址! ^_^');	
		$this->checkUser('1_6_2');
		unset($_POST['__hash__']);
		$data = M('Nav');
		if(!$data->save($_POST)) $this->error('修改导航内容失败!请联系网络管理员');
		$this->assign('jumpUrl','__URL__/index/');
		$this->success('修改导航内容成功! ^_^');
	}
	public function del(){
		$this->checkUser('1_6_2');
		$id = (int)$_GET['id'];
		$data = M('Nav');
		if(!$data->where("id = $id")->delete()) $this->error('删除导航失败!请联系网络管理员!');
		$this->success('删除导航成功!');
	}
	public function stop(){
		$this->checkUser('1_6_2');
		$list['id'] = (int)$_GET['id'];
		$list['state'] = (int)$_GET['state'];
		$data = M('Nav');
		if(!$data->save($list)) $this->error('操作失败!请重试');
		$this->success('操作成功!');
	}
}
?>	