<?php 
    error_reporting(0);
    
    function menu_hmis($link1){
        $cacheKey = 'hmis_menu_' . (isset($_SESSION['hmis_userid']) ? $_SESSION['hmis_userid'] : '');
        if (isset($_SESSION[$cacheKey]) && is_array($_SESSION[$cacheKey])) {
            return $_SESSION[$cacheKey];
        }
    	$qry="SELECT `RolemasterDetail`.`id` AS RolemasterDetail_id, `Module`.`id` AS Module_id, `Module`.`description`, `Category`.`id` AS Category_id,
 `Category`.`level`, `Form`.`lebel`, `Form`.`url` 
FROM `rolemaster_details` AS `RolemasterDetail` 
INNER JOIN modules AS `Module` ON (`Module`.`id` = `RolemasterDetail`.`module_id`) 
INNER JOIN categories AS `Category` ON (`Category`.`id` = `RolemasterDetail`.`category_id`) 
INNER JOIN forms AS `Form` ON (`Form`.`id` = `RolemasterDetail`.`page_id`)
INNER JOIN `userrolls` AS Userroll ON(Userroll.rolemaster_id=RolemasterDetail.rolemaster_id) 
WHERE `Userroll`.`id` = '".$_SESSION['hmis_userid']."' 
AND `Module`.`description` NOT IN ('Security & Administrator', 'Master Setup') 
ORDER BY `Module`.`sequence` ASC, `Category`.`category_seq` ASC, `Category`.`page_seq` ASC";
     $sql=mysqli_query($link1,$qry);
     $menuData=array();
     if(($sql)) {
         while ($value = mysqli_fetch_assoc($sql)) {
             $mod = str_replace('&', '0', trim($value['description']));
             $mod = str_replace(' ', '_', trim($mod));
             $cat = str_replace('&', '0', trim($value['level']));
             $cat = str_replace(' ', '_', trim($cat));
             $modId = trim($value['Module_id']);
             $menuData[$mod][$modId . '|' . $cat][] = $value['lebel'] . '%' . $value['url'];
         }
     }
     $_SESSION[$cacheKey] = $menuData;
     return $menuData;
    }
    function menu_hmis_setting($link1){
        $cacheKey = 'hmis_setting_menu_' . (isset($_SESSION['hmis_userid']) ? $_SESSION['hmis_userid'] : '');
        if (isset($_SESSION[$cacheKey]) && is_array($_SESSION[$cacheKey])) {
            return $_SESSION[$cacheKey];
        }
    	$qry="SELECT `RolemasterDetail`.`id` AS RolemasterDetail_id, `Module`.`id` AS Module_id, `Module`.`description`, `Category`.`id` AS Category_id,
 `Category`.`level`, `Form`.`lebel`, `Form`.`url` 
FROM `rolemaster_details` AS `RolemasterDetail` 
INNER JOIN modules AS `Module` ON (`Module`.`id` = `RolemasterDetail`.`module_id`) 
INNER JOIN categories AS `Category` ON (`Category`.`id` = `RolemasterDetail`.`category_id`) 
INNER JOIN forms AS `Form` ON (`Form`.`id` = `RolemasterDetail`.`page_id`)
INNER JOIN `userrolls` AS Userroll ON(Userroll.rolemaster_id=RolemasterDetail.rolemaster_id) 
WHERE `Userroll`.`id` = '".$_SESSION['hmis_userid']."' 
AND `Module`.`description` IN ('Security & Administrator', 'Master Setup') 
ORDER BY `Module`.`sequence` ASC, `Category`.`category_seq` ASC, `Category`.`page_seq` ASC";
     $sql=mysqli_query($link1,$qry);
     $menuData=array();
     if($sql) {
         while ($value = mysqli_fetch_assoc($sql)) {
             $mod = str_replace('&', '0', trim($value['description']));
             $mod = str_replace(' ', '_', trim($mod));
             $cat = str_replace('&', '0', trim($value['level']));
             $cat = str_replace(' ', '_', trim($cat));
             $modId = trim($value['Module_id']);
             $menuData[$mod][$modId . '|' . $cat][] = $value['lebel'] . '%' . $value['url'];
         }
     }
     $_SESSION[$cacheKey] = $menuData;
     return $menuData;
    }
    function page_name($link1,$formName=NULL){
      if(!empty($formName)){
        $qry="SELECT `RolemasterDetail`.`id` AS RolemasterDetail_id, `Module`.`id` AS Module_id, `Module`.`description`, `Category`.`id` AS Category_id,
   `Category`.`level`, `Form`.`lebel`, `Form`.`url` 
  FROM `rolemaster_details` AS `RolemasterDetail` 
  INNER JOIN modules AS `Module` ON (`Module`.`id` = `RolemasterDetail`.`module_id`) 
  INNER JOIN categories AS `Category` ON (`Category`.`id` = `RolemasterDetail`.`category_id`) 
  INNER JOIN forms AS `Form` ON (`Form`.`id` = `RolemasterDetail`.`page_id`)
  INNER JOIN `userrolls` AS Userroll ON(Userroll.rolemaster_id=RolemasterDetail.rolemaster_id) 
  WHERE `Userroll`.`id` = '".$_SESSION['hmis_userid']."' 
  AND `Form`.`lebel` ='".trim($formName)."'
  ORDER BY `Module`.`sequence` ASC, `Category`.`category_seq` ASC, `Category`.`page_seq` ASC";
      } else {
        $qry="SELECT `RolemasterDetail`.`id` AS RolemasterDetail_id, `Module`.`id` AS Module_id, `Module`.`description`, `Category`.`id` AS Category_id,
   `Category`.`level`, `Form`.`lebel`, `Form`.`url` 
  FROM `rolemaster_details` AS `RolemasterDetail` 
  INNER JOIN modules AS `Module` ON (`Module`.`id` = `RolemasterDetail`.`module_id`) 
  INNER JOIN categories AS `Category` ON (`Category`.`id` = `RolemasterDetail`.`category_id`) 
  INNER JOIN forms AS `Form` ON (`Form`.`id` = `RolemasterDetail`.`page_id`)
  INNER JOIN `userrolls` AS Userroll ON(Userroll.rolemaster_id=RolemasterDetail.rolemaster_id) 
  WHERE `Userroll`.`id` = '".$_SESSION['hmis_userid']."' 
  ORDER BY `Module`.`sequence` ASC, `Category`.`category_seq` ASC, `Category`.`page_seq` ASC"; 
      }
     $sql=mysqli_query($link1,$qry);
     $menuData=array();
     if(($sql) && mysqli_num_rows($sql)>0){
        $value=mysqli_fetch_assoc($sql);
        $GLOBALS['Module']=$value['description'];
        $GLOBALS['Category']=$value['level'];
        $GLOBALS['Form']=$value['lebel'];
        $GLOBALS['formPageUrl']=$value['url'];

            /*$mod=str_replace('&','0',trim($value['description']));
            $mod=str_replace(' ','_',trim($mod));
            $cat=str_replace('&','0',trim($value['level']));
            $cat=str_replace(' ','_',trim($cat));
            $modId=trim($value['Module_id']);
            $menuData[$mod][$modId.'|'.$cat][]=$value['lebel'].'%'.$value['url'];          */
       
     }
    }
    function getNotice($link1){
        $cacheKey = 'hmis_notice_' . date('Y-m-d');
        if (isset($_SESSION[$cacheKey]) && is_array($_SESSION[$cacheKey])) {
            return $_SESSION[$cacheKey];
        }
    	$from_date=date('Y-m-d');
    	$qry="SELECT Generalnoticeboard.subject,Generalnoticeboard.central_notice,Generalnoticeboard.notice_by,Generalnoticeboard.notice_type FROM generalnoticeboards AS Generalnoticeboard   
 WHERE Generalnoticeboard.from_date <= '".$from_date."' AND Generalnoticeboard.to_date >= '".$from_date."' ORDER BY Generalnoticeboard.id DESC LIMIT 20";
        $sql=mysqli_query($link1,$qry);
        $notices=array();
        if($sql){
            while($row=mysqli_fetch_assoc($sql)){
                $notices[]=$row;
            }
        }
        $_SESSION[$cacheKey]=$notices;
        return $notices;
    }

