<?php defined('SYSPATH') or die('No direct script access.');

class Menu_Menu_Model extends Model {

	function __construct(){

		$this->db = new Database();

	}

	public function menulist($true=false){
		if($true){
			$this->db->where('parent_id', 0);
		}
		$this->db->orderby("menu.menu_order","DESC");
		return $this->db->get('menu');

	}
	function grouplist($id=false){
		$this->db->where('group_id',$id);

		return $this->db->get('menu_group');

	}
	function childrenlist($id){
		//$this->db->select("id");
		$this->db->where(array('parent_id'=>$id));
		return $this->db->get('menu');
	}

	public function getone($id){


		$this->db->where('id', $id);
		return $this->db->get('menu');

	}

	function getroles($id){
		$this->db->where('menu_id', $id);
		$arr=$this->db->get('menu_group')->result_array(false);
		foreach($arr as $arr ){
			$array[]=$arr["group_id"];
		}
		return @$array;
	}

	function editgroup($datas,$roles){
		$this->db->delete('menu_group',array("group_id"=>$roles));
		if(is_array($datas)){
			foreach($datas as $data){
				$this->db->insert('menu_group', array("menu_id"=>$data,"group_id"=>$roles));
			}
			return true;
		}
	}

	function add($data,$roles){
		$inster=$this->db->insert('menu', $data);
		$id=$inster->insert_id();
		if(is_array($roles)){
			foreach($roles as $role){

				$this->db->insert('menu_group', array("menu_id"=>$id,"group_id"=>$role));
			}
		}
		return  $inster;

	}

	function delete($id_list){

		@$arr=$this->db->delete('menu',$id_list);
		$this->db->delete('menu_group',array("menu_id"=>$id_list['id']));
		$id=$this->db->getwhere('menu',array("parent_id"=>$id_list['id']))->result_array(false);
		if(is_array($id)){
			foreach($id as $id){

				$this->db->delete('menu',array('id'=>$id['id']));
				$this->db->delete('menu_group',array("menu_id"=>$id['id']));
				$this->delete(array('id'=>$id['id']));
			}}
			return  $arr;
	}

	function edit($data,$roles){
		$this->db->delete('menu_group',array("menu_id"=>$data['id']));
		if(is_array($roles)){

			foreach($roles as $role){

				$this->db->insert('menu_group', array("menu_id"=>$data['id'],"group_id"=>$role));
			}
		}
		$this->db->where('id', $data['id']);
		return  $this->db->update('menu', $data);

	}

	function getMenu($menu_id){
		if($menu_id!==false){
			return	$this->db->select("menu.id,menu.menu_name,menu.menu_url,menu.parent_id,menu_group.group_id")
			->from('menu')
			->join('menu_group','menu_group.menu_id','menu.id','left')
			->where("menu.parent_id","$menu_id")
			->orderby("menu.menu_order","DESC")
			->get()
			->result_array();
		}
		else{
			return	$this->db->select("menu.id,menu.menu_name,menu.menu_url,menu.parent_id,menu_group.group_id")
			->from('menu')
			->join('menu_group','menu_group.menu_id','menu.id','left')
			->where("menu.parent_id",0)
			->orderby("menu.menu_order","DESC")
			->get()
			->result_array();
		}
	}
	function cache(){
		$this->group=	new Auth_Group_Model();
		$userGroup	=	$this->group->grouplist("admin")->result_array();
		$menu	=	$this->getMenu(false);
		foreach ($userGroup as $tmpGroup){
			$cache	=	"<?php \r\n";
			foreach($menu as $tmp){
				if($tmp->group_id!=$tmpGroup->group_id) continue;
				$cache.="\$menu_1[]=array('menu_id'=>".$tmp->id.",'menu_name'=>'".$tmp->menu_name."','menu_url'=>'".$tmp->menu_url."','menu_parent'=>".$tmp->parent_id.");\r\n";
				$menuII	=	$this->getMenu($tmp->id);
				foreach($menuII as $tmpII){
					if($tmpII->group_id!=$tmpGroup->group_id) continue;
					$cache.="\$menu_2[".$tmp->id."][]=array('menu_id'=>".$tmpII->id.",'menu_name'=>'".$tmpII->menu_name."','menu_url'=>'".$tmpII->menu_url."','menu_parent'=>".$tmpII->parent_id.");\r\n";
					$menuIII	=	$this->getMenu($tmpII->id);
					foreach($menuIII as $tmpIII){
						if($tmpIII->group_id!=$tmpGroup->group_id) continue;
						$cache.="\$menu_3[".$tmpII->id."][]=array('menu_id'=>".$tmpIII->id.",'menu_name'=>'".$tmpIII->menu_name."','menu_url'=>'".$tmpIII->menu_url."','menu_parent'=>".$tmpIII->parent_id.");\r\n";
					}
				}
			}
			if($fp=fopen(DATADIR."/cache/menu-$tmpGroup->group_name.tmp.php","w")){
				fwrite($fp,$cache);
				fclose($fp);;
			}
			else{

			}
		}
	}
	function order($id,$order){
		$this->db->where("id",$id);
		return $this->db->update("menu",array("menu_order"=>$order));
	}
} //End Menu