<?php
namespace admin\Controller;
use Think\Controller;
class NavController extends CommonController {
	public function _initialize(){
		 F('nav',NULL);
	}
	public function lists(){
	
		$data = D("Nav");
		$list = $data->order('sort DESC, id ASC')->select();
		$this->assign('list',$list);
		$this->display();
	}
	
	public function add(){
		if(IS_POST)
		{
				$data = D("Nav");
				if (!$data->create()){
					$this->error($data->getError());
				}
				else
				{
					if ($data->type=="1")//外部链接
					{
						if (empty($data->url)){
						$this->error('请填写链接地址');
						exit();
						}
					}
					$data->add();
					$this->success('保存完成',RU('nav/lists'));
				}
		}
		else
		{
		 	$this->display();
		}
	}
	public function edit(){
		if(IS_POST)
		{
				$data = D("Nav");
				if (!$data->create()){
					$this->error($data->getError());
				}
				else
				{
					if ($data->type=="1")//外部链接
					{
						if (empty($data->url)){
						$this->error('请填写链接地址');
						exit();
						}
					}
					 
					$data->save();
					$this->success('保存完成',RU('nav/lists'));
				}
		}
		else
		{
			$id=I('get.id','','intval');
			if ($id)
			{
				$data = M("Nav");
				$map['id']=$id;
				$show=$data->where($map)->limit(1)->find();
				$this->assign('show',$show);
				$this->display();
				//dump($show);
			}
			else
			{
			$this->error('非法请求');
			}
		}
	}
	
	public function delete(){
	
		$ids = I('request.id');
		$ids = is_array($ids)?implode(",",$ids):$ids;
		if ($ids && preg_match("/^(\d{1,10},)*(\d{1,10})$/",$ids))
		{
			$data = M("Nav");
			$s=$data->delete($ids);
			$this->success("成功删除{$s}条数据");
		}
		else
		{
		$this->error('请选择要删除的信息！');
		}
	
 	}
//保存所有分类
	public function allSave(){
	 	$id = I('post.id');
        $title = I('post.title');
		$pagealias = I('post.pagealias');
		$sort = I('post.sort');
		$display = I('post.display');
		$target = I('post.target');
        
		 
		$num = 0;
		$data = M("Nav");
		if (is_array($id) && count($id)>0)
        {
            foreach($id as $k=>$v)
            {
                unset($map);
                $map['id'] = intval($v);
                $map['title']=trim($title[$k]);
                $map['pagealias']=trim($pagealias[$k]);
				$map['sort']=intval($sort[$k]);
				$map['display']=intval($display[$k]);
				$map['target']=intval($target[$k]);
				$data->save($map);
                $num++;
         
            }
        }
		if($num>0){
            $this->success('成功保存'.$num.'个分类！');
        }else{
            $this->error('未保存任何分类！');
        }
		 
	
 	}
 
}
?>