<?php
    $this->session = \Config\Services::session();
    $query_reader = new \App\Models\_query_reader();
    $permissions = $query_reader->get_list('get_permission_details', array('permissions'=>"'".implode("','", $this->session->get('__permissions'))."'"));
    $menu = array();
    $selectedCategory = "";
    if (empty($clear_menu)) {
        if (!$this->session->get('__selected_permission')) {
            $default = $this->session->get('__default_permission');
            $this->session->set('__selected_permission', $default);
        }
    } else {
        $this->session->remove('__selected_permission');
    }
    
    # Generate the menu array with the data results
    foreach ($permissions as $row) {
        $menu[$row['category']] = !empty($menu[$row['category']])? $menu[$row['category']]:array();
        array_push($menu[$row['category']], array('code'=>$row['code'], 'display'=>$row['display'], 'url'=>$row['url']));
            
        if (empty($clear_menu) && $row['code'] == $this->session->get('__selected_permission')) {
            $selectedCategory = $row['category'];
        }
    }
    
        
    # Now generate the menu itself
    foreach ($menu as $item=>$subMenus) {
        echo "<div class='item nav-link text-white".($item == $selectedCategory? ' selected': '')."'>
            <div class='header'>".ucwords(str_replace('_', ' ', $item))."</div>";
        foreach ($subMenus as $sub) {
            echo "<div data-rel='".$sub['url']."'".($this->session->get('__selected_permission') == $sub['code']? " class='selected'": '').">".$sub['display']."</div>";
        }
        echo "</div>";
    }