<?php



class Dynamic_menu

{



  function __construct()

  {

    $this->ci = &get_instance();

  }



  function build_menu()

  {

    $menu = array();

    $controller = $this->ci->router->fetch_class();

    $method = $this->ci->router->fetch_method();

    $usuario = $this->ci->session->userdata('usuario');

    $usuarios = $this->ci->db->where('id', $usuario)->get('usuario')->row();

    $perfil = $this->ci->session->userdata('perfil');

    $perfiles = $this->ci->db->where('id', $perfil)->get('perfil')->row();

    $perfilmenus = $this->ci->db->where('pm.perfil', $perfil)->join('menu m', 'm.id=pm.menu')->select('pm.*')->order_by("m.posicion", "asc")->get('perfilmenu pm')->result();

    $html_out  = '<div class="left-side-menu">';

    $html_out .= '<div class="slimscroll-menu">';

    $html_out .= '<div id="sidebar-menu">';

    $html_out .= '<div class="user-box"><div class="float-left">

    <img src="' . base_url() . RECURSOS . 'images/users/avatar-1.png" alt="" class="avatar-md rounded-circle"></div>

    <div class="user-info"><div class="dropdown">

    <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' . $usuarios->nombre . '</a>

    </div><p class="font-13 text-muted m-0">' . $perfiles->nombre . '</p></div></div>';

    $html_out .= '<ul class="metismenu" id="side-menu">';

    foreach ($perfilmenus as $data) {

      $id = $data->id;

      $menu = $data->menu;

      $parent_id = $data->parent_id;

      $is_parent = $data->is_parent;

      $menus = $this->ci->db->where('id', $menu)->get('menu')->row();

      $title = $menus->nombre;

      $url = $menus->url;

      $icon = $menus->icono;

      if ($parent_id == 0) {

        if ($is_parent == TRUE) {

          $html_out .= '<li>';

          $html_out .= '<a href="#" class="waves-effect">';

          $html_out .= '<i class="' . $icon . '"></i><span>' . $title . '</span><span class="menu-arrow"></span>';

          $html_out .= '</a>';

          $html_out .= $this->get_childs($id);

          $html_out .= '</li>';

        } else {

          $html_out .= '<li>' . anchor($url, '<i class="' . $icon . '"></i><span>' . $title . '</span>', 'name="' . $title . '" id="' . $id . '"') . '</li>';

        }

      }

    }

    $html_out .= '</ul></div></div></div>';

    return $html_out;

  }



  function get_childs($id)

  {

    $has_subcats = FALSE;

    $controller = $this->ci->router->fetch_class();

    $method = $this->ci->router->fetch_method();

    $perfil = $this->ci->session->userdata('perfil');

    $html_out  = '';

    // query q me ejecuta el submenu filtrando por usuario y para buscar el submenu segun el id que traigo

    $perfilmenus = $this->ci->db->where('pm.perfil', $perfil)->where('pm.parent_id', $id)->join('menu m','m.id=pm.menu')->select('pm.*')->order_by("m.posicion", "asc")->get('perfilmenu pm')->result();

//    $perfilmenus = $this->ci->db->where('perfil', $perfil)->where('parent_id', $id)->order_by("posicion", "asc")->get('perfilmenu')->result();

    $html_out .= '<ul class="nav-second-level" aria-expanded="false">';

    foreach ($perfilmenus as $data) {

      $menus = $this->ci->db->where('id', $data->menu)->get('menu')->row();

      $title = $menus->nombre;

      $url = $menus->url;

      $has_subcats = TRUE;

      $html_out .= '<li>' . anchor($url, '<span>' . $title . '</span>', 'name="' . $title . '" id="' . $data->id . '"') . '</li>';

    }

    $html_out .= '</ul>';

    return ($has_subcats) ? $html_out : FALSE;

  }

}

