<?php
class Navbar_Controller extends Base_Controller{
	function __construct(){
		parent::__construct();
		$this->navbar	=	new Navbar_Model();
		$this->cat		=	new Model_Category_Model();
	}
	function index(){
		$this->view	=	new View("navbar/list");
		$this->view->lists=$this->navbar->get()->result_array();
		$this->display();
	}
	function add(){
		if($this->input->post()){
			$nav	=	$this->input->post("nav");
			$this->navbar->insert($nav);
			$this->index();
		}
		else{
			$this->view	=	new View("navbar/form");
			$catlist	=	$this->cat->allCats();
			$catlist2	=	$this->cat->allCats(false);
			$this->view->alllist	=	$this->navbar->catlists($catlist);
			$this->view->alllist2	=	$this->navbar->catlists($catlist2);
			$this->main->formAction	=	"navbar/add";
			$this->main->is_validator=	true;
			$this->display();
		}
	}
	function edit($navid=false){
		if($this->input->post()){
			$nav	=	$this->input->post("nav");
			$navid	=	$this->input->post("navid");
			$this->navbar->update($nav,$navid);
			$this->index();
		}
		else{
			$this->view	=	new View("navbar/form");
			$catlist	=	$this->cat->allCats();
			$catlist2	=	$this->cat->allCats(false);
			$this->view->alllist	=	$this->navbar->catlists($catlist);
			$this->view->alllist2	=	$this->navbar->catlists($catlist2);
			$this->main->formAction	=	"navbar/edit";
			$this->main->is_validator=	true;
			$this->view->nav		=	$this->navbar->get($navid)->current();
			$this->display();
		}		
	}
	function delete($navid=false){
		if($this->input->post()){
			$navids	=	$this->input->post('select_id');
		}
		else{
			$navids[]=	$navid;
		}
		$this->navbar->delete($navids);
		$this->index();
	}
	function update(){
		if($this->input->post()){
			$navid	=	$this->input->post("navid");
			$name	=	$this->input->post("name");
			$url	=	$this->input->post("url");
			$order	=	$this->input->post("order");
			for($i=0;$i<count($navid);$i++){
				$data	=	array("name"=>$name[$i],"url"=>$url[$i],"order"=>$order[$i]);
				$this->navbar->update($data,$navid[$i]);
			}
		}
		unset($_POST);
		$this->index();
	}
}