<?php
/* Sidebar menu — HERP ACG permission system (modules/module_form_categories/modules_form).
   All DB_query calls use TrapErrors=false so a query failure never kills the page. */

$_mm_curMod = isset($_REQUEST['modid']) ? intval($_REQUEST['modid']) : (isset($_SESSION['modid']) ? intval($_SESSION['modid']) : 0);
$_mm_curFrm = isset($_REQUEST['frmid']) ? intval($_REQUEST['frmid']) : (isset($_SESSION['frmid']) ? intval($_SESSION['frmid']) : 0);

/* ---- permitted module IDs from session ---- */
$_mm_modIds = array();
if (!empty($_SESSION['ACG_ModulePermission'])) {
    $src = $_SESSION['ACG_ModulePermission'];
    $arr = is_array($src) ? $src : explode(',', $src);
    foreach ($arr as $v) {
        $v = intval(trim($v));
        if ($v > 0) { $_mm_modIds[] = $v; }
    }
}

$_mm_erpMenu = array();

if (!empty($_mm_modIds)) {
    $_mm_modIn = implode(',', $_mm_modIds);
    $_mm_permKey = md5($_mm_modIn . '|' . (isset($_SESSION['ACG_FRMPermission']) ? $_SESSION['ACG_FRMPermission'] : ''));
    $_mm_cacheKey = 'erp_main_menu_' . $_mm_permKey;

    if (isset($_SESSION[$_mm_cacheKey]) && is_array($_SESSION[$_mm_cacheKey])) {
        $_mm_erpMenu = $_SESSION[$_mm_cacheKey];
    } else {
        $_mm_frmFilter = '';
        if (!empty($_SESSION['ACG_FRMPermission'])) {
            $_mm_frmFilter = " AND f.id IN('".$_SESSION['ACG_FRMPermission']."')";
        }

        $_mm_menuQ = DB_query(
            "SELECT
                m.id AS module_id,
                m.modulename,
                COALESCE(c.id, 0) AS category_id,
                COALESCE(c.cat_desc, '') AS category_name,
                f.id AS form_id,
                f.frm_name,
                f.frm_url
             FROM modules m
             INNER JOIN modules_form f ON f.moduleid = m.id AND f.status = 'Y' $_mm_frmFilter
             LEFT JOIN module_form_categories c ON c.id = f.module_form_category_id AND c.module_id = m.id
             WHERE m.status = 'Y' AND m.id IN($_mm_modIn)
             ORDER BY m.sequence ASC, c.sequence ASC, c.id ASC, f.sequence ASC",
            $db, '', '', false, false
        );

        $_mm_modIndex = array();
        if ($_mm_menuQ) {
            while ($_mm_row = DB_fetch_assoc($_mm_menuQ)) {
                $_mm_modId = intval($_mm_row['module_id']);
                $_mm_catId = intval($_mm_row['category_id']);

                if (!isset($_mm_modIndex[$_mm_modId])) {
                    $_mm_erpMenu[] = array(
                        'id' => $_mm_modId,
                        'name' => $_mm_row['modulename'],
                        'cats' => array(),
                        '_catIndex' => array(),
                    );
                    $_mm_modIndex[$_mm_modId] = count($_mm_erpMenu) - 1;
                }

                $_mm_mPos = $_mm_modIndex[$_mm_modId];
                if (!isset($_mm_erpMenu[$_mm_mPos]['_catIndex'][$_mm_catId])) {
                    $_mm_erpMenu[$_mm_mPos]['cats'][] = array(
                        'id' => $_mm_catId,
                        'name' => $_mm_row['category_name'],
                        'forms' => array(),
                    );
                    $_mm_erpMenu[$_mm_mPos]['_catIndex'][$_mm_catId] = count($_mm_erpMenu[$_mm_mPos]['cats']) - 1;
                }

                $_mm_cPos = $_mm_erpMenu[$_mm_mPos]['_catIndex'][$_mm_catId];
                $_mm_erpMenu[$_mm_mPos]['cats'][$_mm_cPos]['forms'][] = array(
                    'id' => $_mm_row['form_id'],
                    'frm_name' => $_mm_row['frm_name'],
                    'frm_url' => $_mm_row['frm_url'],
                );
            }
        }

        foreach ($_mm_erpMenu as $_mm_i => $_mm_item) {
            unset($_mm_erpMenu[$_mm_i]['_catIndex']);
        }
        $_SESSION[$_mm_cacheKey] = $_mm_erpMenu;
    }
}

/* ---- HMIS menu (only when hmis_userid is present) ---- */
$_mm_hmisMenu = array();
if (!empty($_SESSION['hmis_userid'])) {
    $_mm_hmisMenu = menu_hmis($link1);
}

$_mm_hroot = defined('_ROOT') ? _ROOT : ('http://' . $_SERVER['HTTP_HOST'] . '/git/PHP_564/mimhans-2.0/');
?>
<div class="nav-side-menu">
    <div class="menu-list" id="accordion">
        <ul id="menu-content" class="menu-content collapse out">

<?php if (!empty($_mm_erpMenu)): ?>
    <?php foreach ($_mm_erpMenu as $_mm_m):
        $_mm_isActiveMod = ($_mm_curMod == intval($_mm_m['id']));
        $_mm_modKey = 'erpmod' . intval($_mm_m['id']);
    ?>
    <li data-toggle="collapse" data-parent="#accordion"
        data-target="#<?php echo $_mm_modKey; ?>"
        class="<?php echo $_mm_isActiveMod ? '' : 'collapsed'; ?>">
        <a href="#" class="<?php echo $_mm_isActiveMod ? 'parent-active' : ''; ?>">
            <?php echo htmlspecialchars($_mm_m['name']); ?>
            <span class="arrow"></span>
        </a>
    </li>
    <ul class="sub-menu collapse <?php echo $_mm_isActiveMod ? 'in' : ''; ?>"
        id="<?php echo $_mm_modKey; ?>" role="tabpanel">

        <?php foreach ($_mm_m['cats'] as $_mm_c):
            $_mm_hasFrm = false;
            foreach ($_mm_c['forms'] as $_f) {
                if (intval($_f['id']) === $_mm_curFrm) { $_mm_hasFrm = true; break; }
            }
            $_mm_catKey = 'erpcat' . intval($_mm_m['id']) . '_' . intval($_mm_c['id']);
        ?>
            <?php if (!empty($_mm_c['name'])): ?>
            <li data-toggle="collapse" data-target="#<?php echo $_mm_catKey; ?>"
                class="<?php echo $_mm_hasFrm ? '' : 'collapsed'; ?>">
                <a href="#" class="<?php echo $_mm_hasFrm ? 'submenu-active' : ''; ?>">
                    <?php echo htmlspecialchars($_mm_c['name']); ?>
                </a>
            </li>
            <ul class="sub-child-menu collapse <?php echo $_mm_hasFrm ? 'in' : ''; ?>"
                id="<?php echo $_mm_catKey; ?>">
            <?php else: ?>
            <ul class="sub-child-menu collapse in" id="<?php echo $_mm_catKey; ?>">
            <?php endif; ?>

                <?php foreach ($_mm_c['forms'] as $_mm_f):
                    $_mm_furl = 'index.php?route=' . ltrim($_mm_f['frm_url'], '/');
                    $_mm_furl .= '&modid=' . intval($_mm_m['id']) . '&frmid=' . intval($_mm_f['id']);
                    $_mm_active = (intval($_mm_f['id']) === $_mm_curFrm);
                ?>
                <li>
                    <a href="<?php echo htmlspecialchars($_mm_furl); ?>"
                       class="<?php echo $_mm_active ? 'child-active' : ''; ?>">
                        <?php echo htmlspecialchars($_mm_f['frm_name']); ?>
                    </a>
                </li>
                <?php endforeach; ?>

            </ul>
        <?php endforeach; ?>
    </ul>
    <?php endforeach; ?>
<?php endif; ?>

<?php if (!empty($_mm_hmisMenu)): ?>
    <li style="padding:4px 12px; font-size:10px; color:#888; border-top:1px solid #444; margin-top:4px; text-transform:uppercase; letter-spacing:1px;">HMIS</li>
    <?php foreach ($_mm_hmisMenu as $_hk => $_hv):
        $_hmMod  = str_replace(array('0','_'), array('&',' '), trim($_hk));
        $_hmKey  = 'hmis_' . preg_replace('/[^a-zA-Z0-9]/', '_', $_hk);
        $_hmActive = (trim($_hmMod) == trim(isset($GLOBALS['Module']) ? $GLOBALS['Module'] : ''));
    ?>
    <li data-toggle="collapse" data-parent="#accordion"
        data-target="#<?php echo $_hmKey; ?>"
        class="<?php echo $_hmActive ? '' : 'collapsed'; ?>">
        <a href="#" class="<?php echo $_hmActive ? 'parent-active' : ''; ?>">
            <?php echo htmlspecialchars($_hmMod); ?> <span class="arrow"></span>
        </a>
    </li>
    <ul class="sub-menu collapse <?php echo $_hmActive ? 'in' : ''; ?>"
        id="<?php echo $_hmKey; ?>" role="tabpanel">
        <?php foreach ($_hv as $_hk1 => $_hv1):
            $_hcArr = explode('|', $_hk1);
            $_hcat  = str_replace(array('0','_'), array('&',' '), trim($_hcArr[1]));
            $_hcKey = 'hmis_c_' . preg_replace('/[^a-zA-Z0-9]/', '_', $_hcArr[0].$_hcArr[1]);
            $_hcActive = (trim($_hcat) == trim(isset($GLOBALS['Category']) ? $GLOBALS['Category'] : ''));
        ?>
        <li data-toggle="collapse" data-target="#<?php echo $_hcKey; ?>"
            class="<?php echo $_hcActive ? '' : 'collapsed'; ?>">
            <a href="#" class="<?php echo $_hcActive ? 'submenu-active' : ''; ?>">
                <?php echo htmlspecialchars($_hcat); ?>
            </a>
        </li>
        <ul class="sub-child-menu collapse <?php echo $_hcActive ? 'in' : ''; ?>"
            id="<?php echo $_hcKey; ?>">
            <?php foreach ($_hv1 as $_hv2):
                $_hd = explode('%', $_hv2);
                $_hfActive = (trim($_hd[0]) == trim(isset($GLOBALS['Form']) ? $GLOBALS['Form'] : ''));
            ?>
            <li>
                <a href="<?php echo htmlspecialchars($_mm_hroot . $_hd[1]); ?>"
                   class="<?php echo $_hfActive ? 'child-active' : ''; ?>">
                    <?php echo htmlspecialchars($_hd[0]); ?>
                </a>
            </li>
            <?php endforeach; ?>
        </ul>
        <?php endforeach; ?>
    </ul>
    <?php endforeach; ?>
<?php endif; ?>

<?php if (empty($_mm_erpMenu) && empty($_mm_hmisMenu)): ?>
    <li style="padding:10px 14px; color:#888; font-size:11px;">No menu items available.</li>
<?php endif; ?>

        </ul>
    </div>
</div>
