<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Menus_model extends Ion_auth_model {
	
    public function __construct()
    {
        parent::__construct();
	}
	
	/**
	 * menus
	 *
	 * @return object
	 * @author Raquel Romero and Tanya Paredes
	 **/
	public function menus($id = NULL)
	{
		$this->trigger_events('menus');

		$this->response = $this->db->get('v_menus_jerarquia');

		return $this;
	}

	/**
	 * menus
	 *
	 * @return object
	 * @author Raquel Romero and Tanya Paredes
	 **/
	/*public function menu($tabla = NULL, $where = NULL)
	{
		$this->trigger_events('menus');

		if (isset($id))
		{
			$this->where($this->tables['menus'].'.id', $id);
		}

		$this->limit(1);
		$this->order_by('id', 'nombre_vista');

		return $this->menus();
	}*/

	public function menu($tabla = NULL, $where = NULL) 
	{
		$this->trigger_events('menu');
		if (isset($where)) {
			$this->response = $this->db->get_where($this->tables[$tabla], $where,1);			
		}else {
			$this->response = $this->db->get($this->tables[$tabla]);
		}
		
		
		return $this;
	}

	public function get_vista ($concepto = ''){
		$nombre = $this->db->get_where($this->tables['v_habilitado'], array('concepto' => $concepto));
		return $habilitado;
	}

	public function get_vista_2 ($id = ''){
		$description = $this->db->get_where($this->tables['groups'], array('id' => $id));
		return $groups;
	}


	public function get_vista_3 ($id = ''){
		$nombre_vista = $this->db->get_where($this->tables['vistas'], array('id' => $id));
		return $vistas;
	}
	

	

	/**
	 * create_vista
	 *
	 * @author Raquel Romero and Tanya Paredes
	*/
	
	public function create_menu($data, $additional_data = array())
	{

		// Verifica si ya se encuentra insertado el nombre de la menu.
		$existing_menus = $this->db->get_where($this->tables['menus'], $data)->num_rows();
		//show_error($existing_menus);

		if($existing_menus !== 0)
		{
			$this->ion_auth->set_error('menu_already_exists');
			return FALSE;
		}
		
		// insertar nueva menus
		$this->db->insert($this->tables['menus'], $data);
		$vista_id = $this->db->insert_id();

		// report success
		$this->ion_auth->set_message('menu_creation_successful');
		// return the brand new vista id
		return $vista_id;

	}	

	public function update_menu($vid,$data){
		
		// Verifica si ya se encuentra insertado el nombre del menus.
		$existing_menus = $this->db->get_where($this->tables['menus'], $data)->num_rows();

		
		if($existing_menus !== 0)
		{
			$this->ion_auth->set_error('menu_already_exists');
			return FALSE;
		}

		$this->db->update($this->tables['menus'], $data, array('id' => $vid));

		$this->set_message('menus_update_successful');

		return TRUE;
	}

	public function v_habilitado()
	{
		$this->response = $this->db->get($this->tables['v_habilitado']);

		return $this;
	}

	public function groups()
	{
		$this->response = $this->db->get($this->tables['groups']);

		return $this;
	}

	public function vistas()
	{
		$this->response = $this->db->get($this->tables['vistas']);

		return $this;
	}

	public function delete_menu($vid){

		$this->db->delete($this->tables['menus'], array('id' => $vid));

		return TRUE;

	}

	
}