Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 155
MenuService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 12
992.00
0.00% covered (danger)
0.00%
0 / 155
 __construct
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
 __clone
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 getInstance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 5
 loadProductConfiguration
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 15
 getMenuConfigFromCache
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 setMenuConfigInCache
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 canShow
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 45
 getMenuItems
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 15
 getParentMenuItems
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 16
 getSubmenuItems
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 16
 getMenuCodeByMenuAction
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 17
 getExternalLinks
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 10
<?php
namespace com\linways\core\ams\professional\service;
use com\linways\base\cache\LinCache;
use com\linways\core\ams\professional\dto\constant\PackageType;
use com\linways\core\ams\professional\exception\ProfessionalException;
use com\linways\core\ams\professional\service\BaseService;
use com\linways\core\ams\professional\constant\StudentExcludedMenu;
use com\linways\core\ams\professional\constant\StaffExcludedMenu;
use com\linways\core\ams\professional\constant\UserType;
class MenuService extends BaseService
{
    // /Condition 1 - Presence of a static member variable
    private static $_instance = null;
    private $packageType;
    private $staffModuleMenus = [];
    private $linCache = null;
    // /Condition 2 - Locked down the constructor
    private function __construct()
    {
        $this->linCache = new LinCache();
        try {
            $this->loadProductConfiguration();
        } catch (ProfessionalException $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
    // Prevent any oustide instantiation of this class
    // /Condition 3 - Prevent any object or instance of that class to be cloned
    private function __clone()
    {
    }
    // Prevent any copy of this object
    // /Condition 4 - Have a single globally accessible static method
    public static function getInstance()
    {
        if (!is_object(self::$_instance)) // or if( is_null(self::$_instance) ) or if( self::$_instance == null )
            self::$_instance = new self();
        return self::$_instance;
    }
    /**
     * load product package type configurations
     *
     * @param string $forceToLoad
     *            - set true to load confiuration again
     * @throws ProfessionalException
     */
    public function loadProductConfiguration($forceToLoad = false)
    {
        $packageConfig = CommonService::getInstance()->getActiveProductConfig();
        //Check package config already configured or not.if no throw exception
        if (empty($packageConfig)) {
            throw new ProfessionalException(ProfessionalException::ACTIVE_PRODUCT_CONFIG_NOT_FOUND, "Active product configuration not found.");
        } else {
            $this->packageType = $packageConfig->packageType;
            //Load staffModuleMenus from cache
            $this->staffModuleMenus = $this->getMenuConfigFromCache($packageConfig->packageType);
            //If not loaded from memcache load again
            if ($forceToLoad || empty($this->staffModuleMenus)) {
                require_once DOCUMENT_ROOT . $packageConfig->menuConfigPath;
                $this->staffModuleMenus = empty($staffModuleMenus) ? [] : $staffModuleMenus;
                $this->packageType = $packageConfig->packageType;
                $this->setMenuConfigInCache($this->packageType, $this->staffModuleMenus);
            }
        }
    }
    /**
     * Load menu config from memcache if already set
     * @param unknown $key
     * @return NULL|unknown
     */
    public function getMenuConfigFromCache($key)
    {
        return $this->linCache->getValue($key);
    }
    /**
     * Set menu config in memcache
     * @param unknown $key
     * @param unknown $value
     */
    public function setMenuConfigInCache($key, $value)
    {
        $this->linCache->setValue($key, $value);
    }
    /**
     * Can show the current menu/action.This method return true or false depends on the package type .
     * If the menu/action is not present in packageType (eg.lite) return false else true
     *
     * @param string $menu
     * @return boolean
     */
    public function canShow($menu)
    {
        $allMenus = $this->staffModuleMenus;
        if($_SESSION['studentID'] && $_SESSION['deptID']){
            $StudentMenuReflectionClass = new StudentExcludedMenu();
            $excludedMenus = array_values($StudentMenuReflectionClass->getConstants());
            $allMenus = array_filter($this->staffModuleMenus, function($m)  use ($excludedMenus){
                return !in_array($m,$excludedMenus);
            });
            // $allMenus = array_map(function($m) use($excludedMenus){
            //     return !in_array($m,$excludedMenus)?$m;
            // },$this->staffModuleMenus);
            // $allMenus = [];
            // foreach ($this->staffModuleMenus as $key => $m) {
            //     if(!in_array($m,$excludedMenus)){
            //         $allMenus [] = $m ;
            //     }else{
            //         $a = $m ;
            //     }
            // }
            // $allMenus = array_diff($this->staffModuleMenus,$includeClusterIds);
            // $allMenus = array_diff($this->allMenus,)
        }
        if($_SESSION['staffID'] && $_SESSION['deptID']){
            $staffMenuReflectionClass = new StaffExcludedMenu();
            $excludedMenus = array_values($staffMenuReflectionClass->getConstants());
            $allMenus = array_filter($this->staffModuleMenus, function($m)  use ($excludedMenus){
                return !in_array($m,$excludedMenus);
            });
        }
        $staffLeaveManagementModules = [
            'PROFILE_LEAVE_MANGMNT',
            'PROFILE_LEAVE_MANGMNT_DETAILS',
            'PROFILE_LEAVE_MANGMNT_APPLY',
            'PROFILE_LEAVE_MANGMNT_APPLIED_LEAVES',
            'PROFILE_LEAVE_MANGMNT_RECOMMENDORAPPROVE',
            'PROFILE_LEAVE_MANGMNT_RECOMMENDORAPPROVED',
            'PROFILE_LEAVE_MANGMNT_BULK_APPROVE',
            'PROFILE_LEAVE_MANGMNT_LEAVE_REPROT',
            'PROFILE_LEAVE_MANGMNT_SHIFT_CHANGE_REQUEST',
            'PROFILE_LEAVE_MANGMNT_PUNCH_TIME_REQUEST',
            'PROFILE_LEAVE_MANGMNT_APPLIED_REQUEST',
            'PROFILE_LEAVE_MANGMNT_RECOMMENDORAPPROVE_REQUEST',
            'PROFILE_LEAVE_MANGMNT_RECOMMENDORAPPROVED_REQUEST',
            'PROFILE_LEAVE_MANGMNT_MONTHLY_LEAVE_REPORT',
            'PROFILE_MY_ATTENDANCE'
        ];
        $leaveReports = [
            'QUICK_REPORT_LEAVE_REPORT',
            'QUICK_REPORT_ATTENDNACE',
            'QUICK_REPORT_LEAVE_REPORT_APPLIED',
            'CONSOLIDATED_LEAVE_REPORT',
            'LEAVE_REPORT_BALANCE',
            'LEAVE_REPORT_LOP',
            'REQUEST_REPORT',
            'QUICK_REPORT_CONSOLIDATED_LEAVE_REPORT'
        ];
        $allMenus = array_merge($allMenus, $staffLeaveManagementModules);
        $allMenus = array_merge($allMenus, $leaveReports);
        return in_array(strtoupper($menu), $allMenus);
        // $canShow = false;
        // // switch ($this->packageType) {
        // // case PackageType::LITE :
        // // $canShow = in_array ( strtoupper ( $menu ), $this->staffModuleMenus );
        // // break;
        // // case PackageType::PROFESSIONAL :
        // // // TO DO : menu visibility implementation
        // // $canShow = true;
        // // break;
        // // case PackageType::ENTERPRISE :
        // // // TO DO : menu visibility implementation
        // // $canShow = true;
        // // break;
        // // default :
        // // $canShow = true;
        // // break;
        // // }
        // return $canShow;
    }
    /**
     * Function to get side menu corresponds to admin, staff according to their typeId (DB Table - 'menu')
     * @param Integer $menuId - specifies the type of menu ('Admin Menu', 'Staff Batch Menu', 'Staff Profile Menu', ... )
     * @return Array $menuItems
     * @author Vishnu M
     */
    public function getMenuItems($menuId)
    {
        $menuId = $this->realEscapeString($menuId);
        $menuItems = [];
        $sql = "SELECT id, display_name, code, parent_id, menu_id, is_active, order_no, url, auth_permissions, menu, actions, show_in_menu FROM menu_items WHERE menu_id = " . $menuId . " AND is_active = 1 ORDER BY order_no ASC";
        try {
            $menus = $this->executeQueryForList($sql);
            foreach ($menus as $menu) {
                $menu->submenu = $this->getSubmenuItems($menu->id);
                $menuItems[$menu->id] = $menu;
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $menuItems;
    }
    /**
     * Function to get side menu corresponds to admin, staff according to their typeId (DB Table - 'menu')
     * @param Integer $menuId - specifies the type of menu ('Admin Menu', 'Staff Batch Menu', 'Staff Profile Menu', ... )
     * @return Array $menuItems
     * @throws ProfessionalException
     * @author Vishnu M
     */
    public function getParentMenuItems($menuId)
    {
        $menuId = $this->realEscapeString($menuId);
        $menuItems = [];
        $sql = "SELECT id, display_name, code, parent_id, menu_id, is_active, order_no, url, auth_permissions, menu, actions, show_in_menu 
                FROM menu_items WHERE menu_id = " . $menuId . " AND is_active = 1 AND parent_id IS NULL ORDER BY order_no ASC";
        try {
            $menus = $this->executeQueryForList($sql);
            foreach ($menus as $menu) {
                $menu->submenu = $this->getSubmenuItems($menu->id);
                $menuItems[$menu->id] = $menu;
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $menuItems;
    }
    public function getSubmenuItems($parentId)
    {
        $parentId = $this->realEscapeString($parentId);
        $menuItems = [];
        $sql = "SELECT id, display_name, code, parent_id, menu_id, is_active, order_no, url, auth_permissions, menu, actions, show_in_menu 
                FROM menu_items WHERE is_active = 1 AND parent_id = " . $parentId . " ORDER BY order_no ASC";
        try {
            $subMenus = $this->executeQueryForList($sql);
            foreach ($subMenus as $submenu) {
                $submenu->submenu = $this->getSubmenuItems($submenu->id);
                $menuItems[$submenu->id] = $submenu;
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $menuItems;
    }
    public function getMenuCodeByMenuAction($menuId, $menu, $action = NULL)
    {
        $menuCode = null;
        $menu = $this->realEscapeString($menu);
        $menuId = $this->realEscapeString($menuId);
        $action = $this->realEscapeString($action);
        try {
            $sql = "SELECT code FROM menu_items WHERE menu_id = " . $menuId . " AND menu = '" . $menu . "' AND FIND_IN_SET ( '" . $action . "', actions )";
            $menuCode = $this->executeQueryForObject($sql)->code;
            if (empty($menuCode)) {
                $sql = "SELECT GROUP_CONCAT(code) as code FROM menu_items WHERE menu_id = " . $menuId . " AND menu = '" . $menu . "'";
                $menuCode = $this->executeQueryForObject($sql)->code;
            }
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
        return $menuCode;
    }
    
    /**
     * Get label icon and url for external modules
     * @return object $externalLinkItems
     */
    public function getExternalLinks($userType = UserType::STAFF)
    {
        $query  = "SELECT id,label,icon,url 
        from v4_external_links where userType = '$userType'";
        try {
            $externalLinkItems = $this->executeQueryForList($query);
            return $externalLinkItems;
        } catch (\Exception $e) {
            throw new ProfessionalException($e->getCode(), $e->getMessage());
        }
    }
}