193 lines
8.9 KiB
PHP
193 lines
8.9 KiB
PHP
<?php
|
|
|
|
/*******************************************************************
|
|
|
|
Module : /Web/Menu.php
|
|
Created By : Siau Pheng ([email protected]).
|
|
Created Date : January 16th, 2008.
|
|
Last Modified : October 30th, 2026.
|
|
|
|
(c) 3FONIA Software; WWW.3FONIA.COM.
|
|
|
|
*******************************************************************/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Webapp\Web;
|
|
|
|
use \flight\Engine;
|
|
use \flight\Session;
|
|
use \flight\database\SimplePdo;
|
|
|
|
/**
|
|
* Class Menu
|
|
*
|
|
* Manajemen Menu aplikasi.
|
|
*
|
|
* @license MIT, https://www.3fonia.com
|
|
* @copyright Copyright (c) 2008-2026, Siau Pheng <[email protected]>
|
|
*
|
|
* @package Webapp\Web
|
|
*/
|
|
|
|
final class Menu {
|
|
private array $web_app;
|
|
private array $web_user;
|
|
private array $my_menu;
|
|
protected Engine $app;
|
|
protected SimplePdo $db;
|
|
protected Session $session;
|
|
|
|
/**
|
|
* @param Engine $app
|
|
*/
|
|
public function __construct(Engine $app){
|
|
$this->app = $app;
|
|
$this->db = $app->db();
|
|
$this->session = $app->session();
|
|
$this->web_app = $this->session->get('WEB_APP', ['APP_MODULE'=>'', 'PARENT_SITE'=>'', 'PARENT_SUB'=>false]);
|
|
$this->web_user = $this->session->get('WEB_USER', ['Menu'=>'', 'Menu_Grup'=>'', 'MENU_MODUL'=>[]]);
|
|
$_gmenu = '';
|
|
$_umenu = $this->db->fetchField("SELECT CONVERT(DECODE(Menu,?) USING UTF8MB4) Menu FROM app_operator WHERE UserID=?", [$this->web_app['PARENT_CODE'], $this->web_user['UserID']]) ?? '';
|
|
if ($this->web_user['Grup_MenuID'] > 0) $_gmenu = $this->db->fetchField('SELECT CONVERT(DECODE(Menu,?) USING UTF8MB4) Menu FROM app_menu_grup WHERE Grup_MenuID=?', [$this->web_app['PARENT_CODE'], $this->web_user['Grup_MenuID']]) ?? '';
|
|
$this->my_menu = array_unique(array_filter(explode(",", $_gmenu.",".$_umenu)));
|
|
}
|
|
|
|
public function Sidebar(): string {
|
|
$a_modul = [];
|
|
$is_modul = !empty($this->web_app['APP_MODULE']);
|
|
if ($is_modul === true) {
|
|
$a_modul[] = $this->web_app['APP_MODULE'];
|
|
} else {
|
|
$atmp = $this->db->fetchAll('SELECT * FROM app_menu WHERE Level=0 AND Aktif=1 AND Modul<>"IND" ORDER BY Urut');
|
|
foreach ($atmp as $tmp) $a_modul[] = $tmp['Modul'];
|
|
}
|
|
|
|
$tsql = 'SELECT * FROM app_menu WHERE Aktif=1 AND Modul IN (?) AND Kode IN (?)';
|
|
if ($is_modul == true) $tsql .= ' AND Level>0';
|
|
|
|
$amodul = $this->db->fetchAll($tsql . ' ORDER BY Kode', [$a_modul, $this->my_menu]);
|
|
$thasil = '';
|
|
|
|
$last_level = 0;
|
|
for ($i=0; $i<count($amodul); $i++) {
|
|
if (($i>0) and ($amodul[$i]['Level']<$amodul[$i-1]['Level'])) {
|
|
$thasil .= str_repeat("</ul></li>", $amodul[$i-1]['Level']-$amodul[$i]['Level']);
|
|
}
|
|
$ticon = ($amodul[$i]['Icon']) ? '<i class="me-2 '.$amodul[$i]['Icon'].'"></i>' : "";
|
|
if (($amodul[$i]['Level']>=0) && (isset($amodul[$i+1]) && ($amodul[$i+1]['Level']>$amodul[$i]['Level']))) {
|
|
$thasil .= '<li class="nav-item has-submenu"><a class="nav-link" title="'.$amodul[$i]['Judul'].'">'.$ticon.$amodul[$i]['Menu'].'<i class="bi-caret-down float-end"></i></a><ul class="submenu collapse">';
|
|
} else if (trim($amodul[$i]['Menu'])=="-") {
|
|
$thasil .= '<li class="nav-divider"></li>';
|
|
} else {
|
|
$thasil .= '<li class="nav-item"><a class="nav-link mlink" id="'.$amodul[$i]['Kode'].'Link" title="'.$amodul[$i]['Judul'].'">'.$ticon.$amodul[$i]['Menu'].'</a></li>';
|
|
}
|
|
$last_level = $amodul[$i]['Level'];
|
|
}
|
|
if ($last_level>1) $thasil .= str_repeat("</ul></li>", $last_level-1);
|
|
return $thasil;
|
|
}
|
|
|
|
public function Header(): string {
|
|
$a_modul = [];
|
|
$is_modul = !empty($this->web_app['APP_MODULE']);
|
|
$mulai_level = 0;
|
|
if ($is_modul === true) {
|
|
$a_modul[] = $this->web_app['APP_MODULE'];
|
|
$mulai_level = 1;
|
|
} else {
|
|
$atmp = $this->db->fetchAll('SELECT * FROM app_menu WHERE Level=0 AND Aktif=1 AND Modul<>"IND" ORDER BY Urut');
|
|
foreach ($atmp as $tmp) {
|
|
$a_modul[] = $tmp['Modul'];
|
|
}
|
|
}
|
|
|
|
$tsql = 'SELECT * FROM app_menu WHERE Aktif=1 AND Modul IN (?) AND Kode IN (?)';
|
|
if ($is_modul == true) $tsql .= ' AND Level>0';
|
|
|
|
$amodul = $this->db->fetchAll($tsql . ' ORDER BY Kode', [$a_modul, $this->my_menu]);
|
|
$thasil = '';
|
|
|
|
$last_level = 0;
|
|
for ($i=0; $i<count($amodul); $i++) {
|
|
if (($i>0) and ($amodul[$i]['Level']<$amodul[$i-1]['Level'])) {
|
|
$thasil .= str_repeat("</ul></li>", $amodul[$i-1]['Level']-$amodul[$i]['Level']);
|
|
}
|
|
$ticon = ($amodul[$i]['Icon']) ? '<i class="me-2 '.$amodul[$i]['Icon'].'"></i>' : "";
|
|
if (isset($amodul[$i+1]) && ($amodul[$i+1]['Level']>$amodul[$i]['Level'])) {//have submenu
|
|
if ($amodul[$i]['Level']==$mulai_level) {
|
|
$thasil .= '<li class="nav-item pe-4 dropdown"><a class="nav-link dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" title="'.$amodul[$i]['Judul'].'">'.$ticon.$amodul[$i]['Menu'].'</a><ul class="dropdown-menu" data-bs-popper="none">';
|
|
} else {
|
|
$thasil .= '<li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle slink" data-bs-toggle="dropdown" aria-expanded="false" title="'.$amodul[$i]['Judul'].'">'.$ticon.$amodul[$i]['Menu'].'</a><ul class="dropdown-menu" data-bs-popper="none">';
|
|
}
|
|
} else if (trim($amodul[$i]['Menu'])=="-" && $amodul[$i]['Level']>$mulai_level) {//divider
|
|
$thasil .= '<li class="dropdown-divider"></li>';
|
|
} else if ($amodul[$i]['Level']==$mulai_level) {//level = 1 + no submenu
|
|
$thasil .= '<li class="nav-item pe-4"><a class="nav-link mlink" id="'.$amodul[$i]['Kode'].'Link" title="'.$amodul[$i]['Judul'].'">'.$ticon.$amodul[$i]['Menu'].'</a></li>';
|
|
} else {// level > 1 + no submenu
|
|
$thasil .= '<li><a class="dropdown-item mlink" id="'.$amodul[$i]['Kode'].'Link" title="'.$amodul[$i]['Judul'].'">'.$ticon.$amodul[$i]['Menu'].'</a></li>';
|
|
}
|
|
$last_level = $amodul[$i]['Level'];
|
|
}
|
|
if ($last_level>$mulai_level) $thasil .= str_repeat("</ul></li>", $last_level-$mulai_level);
|
|
return $thasil;
|
|
}
|
|
|
|
public function Modul(): string {
|
|
$t_modul = '';
|
|
if (is_array($this->web_user['MENU_MODUL']) && count($this->web_user['MENU_MODUL'])>0) {
|
|
$t_modul = '<div class="dropdown-divider"></div>';
|
|
if ($this->app->request()->getVar('SERVER_ADDR') === '127.0.0.1') {
|
|
$href1 = '//127.0.0.1/';
|
|
$href2 = "";
|
|
} else {
|
|
$_host = $_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_HOST'] ?? $this->web_app['PARENT_SITE'];
|
|
$_host = str_replace(['http://','https://'], "", $_host);
|
|
if ($this->web_app['PARENT_SUB'] === true) {
|
|
$_subs = explode('.', $_host);
|
|
if (count($_subs) > 2) array_shift($_subs);
|
|
$href1 = "//";
|
|
$href2 = "." . implode('.', $_subs);
|
|
} else {
|
|
$href1 = "//" . $_host . '/';
|
|
$href2 = "";
|
|
}
|
|
}
|
|
foreach ($this->web_user['MENU_MODUL'] as $modul) {
|
|
$href = $href1 . $modul['APP_PATH'] . $href2 . '/' . $this->session->get('APP_TOKEN');
|
|
$t_modul .= '<a class="dropdown-item slink" href="'.$href.'"><i class="'.$modul['Icon'].' me-2"></i> '.$modul['Judul'].'</a>';
|
|
}
|
|
}
|
|
return $t_modul;
|
|
}
|
|
|
|
static function MenuJSON(SimplePdo $db, string $page = "", string $q = ""): array {
|
|
$ajson = ['login' => true];
|
|
$apage = ['Link' => ""];
|
|
|
|
$acus_page = [
|
|
'SYSP' => ['Kode'=>"SYSP", 'Judul'=>"Profil Pengguna", 'Link'=>"system/profile", 'JS'=>"SYSProfileJS", 'Width'=>600, 'Button'=>'[{},{"i":"butBatal"},{"i":"butSave","v":"Simpan","c":"success"}]'],
|
|
'SYSC' => ['Kode'=>"SYSC", 'Judul'=>"Ubah Password", 'Link'=>"system/chpasswd", 'JS'=>"SYSChpasswdJS", 'Width'=>500, 'Button'=>'[{},{"i":"butBatal"},{"i":"butSave","v":"Simpan","c":"success"}]']
|
|
];
|
|
|
|
if ($acus_page[$page] ?? false) {
|
|
$apage = $acus_page[$page];
|
|
} else if ($page <> "") {
|
|
$apage = $db->fetchRow('SELECT * FROM app_menu WHERE Kode=?', [$page]);
|
|
$apage['Width'] = (int) $apage['Width'];
|
|
}
|
|
|
|
$ajson['iswindow'] = ($apage['Width'] > 0) ? true : false;
|
|
$ajson['width'] = $apage['Width'];
|
|
$ajson['title'] = $apage['Judul'];
|
|
$ajson['button'] = json_validate($apage['Button']) ? json_decode($apage['Button']) : $apage['Button'];
|
|
$ajson['php'] = $apage['Link'];
|
|
$ajson['func'] = $apage['JS'];
|
|
$ajson['kode'] = $apage['Kode'];
|
|
|
|
return $ajson;
|
|
}
|
|
}
|
|
|
|
?>
|