<?php
	namespace App\Helpers;
	//if (!defined('BASEPATH')) exit('No direct script access allowed');

	class Globals
	{
		static $usr_modul;
		static $usr_menu;

		public static function get_modul() {
			if (empty(self::$usr_modul)) {
				$id_user = session()->get('user')['id'];
				//$db = \Config\Database::connect();
				$db = db_connect();
	        	$query = $db->query("SELECT u.group_id, g.nama_group, g.id_modul, m.fname, m.nama_modul, m.ikon FROM usergroups u LEFT JOIN rolegroups as g ON u.group_id = g.id_group LEFT JOIN moduls m ON g.id_modul = m.id_modul WHERE u.user_id = $id_user ORDER BY m.urut ASC");

	        	$results = $query->getResultArray();
				self::$usr_modul = $results; 
			}
			return self::$usr_modul;
		}

		public static function get_menu() {
			if (self::$usr_menu == null) {
				$id_user = session()->get('user')['id'];
				$db = \Config\Database::connect();
	        	$query = $db->query("SELECT a.group_id, z.id_parent, z.nama_menu, z.method, a.menu_id, a.rekam, a.ubah, a.hapus, a.exp, m.fname FROM usergroups x LEFT JOIN rolegroups as g ON x.group_id = g.id_group LEFT JOIN rolemenus a ON a.group_id = g.id_group LEFT JOIN menus z ON a.menu_id = z.id_menu LEFT JOIN moduls m ON z.id_modul = m.id_modul WHERE x.user_id = $id_user ORDER BY z.urut ASC");

	        	$results = $query->getResultArray();
				self::$usr_menu = $results; 
			}
			return self::$usr_menu;
		}

		public static function set_menu($modul) {
			$data_modul = self::get_modul();
			$key = array_search($modul, array_column($data_modul, 'fname'));
			$user_group_id = $data_modul[$key]['group_id'];
			
			$data_menu = self::get_menu();
			$arr_menu = array_filter($data_menu, function ($var) use ($user_group_id) {
                return ($var['group_id'] == $user_group_id);
            });
            return $arr_menu;
		}
	}