jahjh21a
jahjh21a

Reputation: 89

how can I do a menu list with permission on codeigniter?

I want to display a menu list with the respetives modules that has permission. I have my model,and controller for it, but I dont know how to pass it to my home view and display it. either for php directly or ajax.

controller

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

class Login extends MY_Controller {
    public function __construct(){
        parent::__construct();
    }

    public function index(){
        $data['module'] = 'Login';
        $this->load->view('login',$data);
    }

    public function getAccess(){
        if ($this->session->userdata('logged_in') == TRUE){
            redirect('home');
        }else{
            $username = $this->security->xss_clean($this->input->post('username'));
            $password = $this->security->xss_clean($this->input->post('password'));
            $array = $this->user->login($username,$password);
            if ($array[0] == 0) {
                echo 0;
            }else{
                $data_session = array(
                    'id' => $array[0]['id'],
                    'name' => $array[0]['nombre'],
                    'last_name' => $array[0]['apellido'],
                    'type' => $array[0]['id_perfil'],
                    'logged_in' => TRUE 
                );
                $this->session->set_userdata('log',$data_session);
            }
        }
    } 

    public function logout(){
        $this->session->sess_destroy();
        redirect('login');
    }

    public function getModules($module_id){
        if ($this->session->userdata('log')){
            $data = $this->session->userdata('log');
            $menu = array();
            $seccions = $this->module->get_rows();
            foreach ($seccions as $index => $seccion) {
                //echo json_encode($seccion);
                $modules = $this->module->query("SELECT concat('".$seccion['id']."',storelte_modulo.id) AS id, storelte_modulo.modulo AS VALUE,storelte_modulo.seccion_id,concat('".base_url()."',storelte_modulo.url) as url FROM storelte_modulo INNER JOIN storelte_modulo_perfil ON  storelte_modulo_perfil.modulo_id = storelte_modulo.id WHERE seccion_id = $seccion[id] AND storelte_modulo_perfil.perfiles_id =  $data[id] AND storelte_modulo_perfil.status = 1");
                $seccions[$index]['data']= $modules;
                if(!count($seccions[$index]['data']))
                    unset($seccions[$index]);
            }
            foreach($seccions as $item)
                array_push($menu,$item);
            $this->json($menu);
        }
    }
}

module_model

class Module extends CI_Model {
    public function __construct(){
        parent::__construct();

    }

    public function get_rows(){
        $this->db->select('id,seccion');
        $this->db->from('storelte_seccion');
        return $this->db->get()->result_array();
    }

    public function query($query){
        return $this->db->query($query)->result_array();
    }

}

html where will be displayed the modules with permissions

<div class="row">
              <h3 class="text-center">Welcome to storeLTE, click a module below to get started!</h3>
              <div class="home_module_list">

              </div>
          </div>

Upvotes: 0

Views: 1209

Answers (1)

Geo Halkiadakis
Geo Halkiadakis

Reputation: 380

In order to write the modules list via php, you have to pass the $menu array to the view file (html). If you do so, function getModules() should:

  • return the $menu array, if the getModules() is called from another method of your Controler

-- or --

  • pass the array straight to the view file (easiest way for your code example). So, in this case, change your function getModules() on the Controler to:
    public function getModules($module_id){
        if ($this->session->userdata('log')){
          $data = $this->session->userdata('log');
          $menu = array();
          $seccions = $this->module->get_rows();
          foreach ($seccions as $index => $seccion) {
                $modules = $this->module->query("SELECT concat('". $seccion['id'] ."',storelte_modulo.id) AS id, storelte_modulo.modulo AS VALUE, storelte_modulo.seccion_id, concat('". base_url() ."',storelte_modulo.url) as url FROM storelte_modulo INNER JOIN storelte_modulo_perfil ON storelte_modulo_perfil.modulo_id = storelte_modulo.id WHERE seccion_id = $seccion[id] AND storelte_modulo_perfil.perfiles_id =  $data[id] AND storelte_modulo_perfil.status = 1");
                 // assuming you get 1 row only from the above query
                 if (!empty($modules;)) {
                    $modules['index'] = $index;
                    $menu['seccions'] = $modules;
                 }
           }
           $this->load->view('modules_view',$menu);
         }
    }

Then in your view file (modules_view.php) you can loop through the $menu items:

<div class="row">
<h3 class="text-center">Welcome to storeLTE, click a module below to get started!</h3>
<div class="home_module_list">
  <ul>
  <?php
     foreach($seccions as $session) {
        echo '<li><a href="'. $session['url'] .'">'. $session['index'] .' - '. $session['id'] .' - '. $session['VALUE'] .'</a></li>';
     }
  ?>
  </ul>
</div>
</div>

...

If you want to pass the menu via ajax, you have to do it with javascript in your html file, but first your function getModules() should echo the JSON array (or the html you are about to embed in the "home_module_list" div). In this case I suggest you give an id to the div.

Upvotes: 1

Related Questions