<?php
if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}

require_once 'modules/Administration/Forms.php';
require_once 'modules/MySettings/TabController.php';

class Viewwebdashboard_module_configuration extends SugarView
{
    /**
     * @see SugarView::_getModuleTitleParams() - to set the title of the WebPage
     */
    protected function _getModuleTitleParams($browserTitle = false)
    {
        global $mod_strings;

        return array(
            "<a href='index.php?module=Administration&action=index'>" . $mod_strings['LBL_MODULE_NAME'] . "</a>",
            $mod_strings['LBL_WEBDASHBOARD_CONFIGURATION'],
        );
    }

    /**
     * @see SugarView::preDisplay()
     */
    public function preDisplay()
    {
        global $current_user;

        if (!is_admin($current_user)) {
            sugar_die("Unauthorized access to administration.");
        }
    }

    /**
     * @see SugarView::display()
     */
    public function display()
    {
        global $mod_strings, $app_list_strings, $app_strings, $sugar_config;

        $excluded_module_array       = array();
        $available_module_array      = array();
        $enabled_module_config_array = array();
        $enabled_module_array        = array();

        $enabled_module_config_array = json_decode($sugar_config['web_dashboard']['enabled_modules'], true);

        $re_suite_version = '/(7\.?(\d+\.)?(\*|\d+)$)/';
        if ($sugar_config['suitecrm_version'] != '' && preg_match($re_suite_version, $sugar_config['suitecrm_version'])) {
            $excluded_module_array = array('Home', 'Calendar', 'Users', 'Forecasts', 'Products', 'Emails', 'AM_ProjectTemplates', 'AOBH_BusinessHours', 'AOK_KnowledgeBase', 'AOK_Knowledge_Base_Categories', 'AOR_Reports', 'AOR_Scheduled_Reports', 'AOS_Contracts', 'AOS_Invoices', 'AOS_PDF_Templates', 'AOS_Product_Categories', 'AOS_Quotes', 'AOW_WorkFlow', 'FP_Event_Locations', 'FP_events', 'ResourceCalendar', 'SecurityGroups', 'Spots', 'jjwg_Address_Cache', 'jjwg_Areas', 'jjwg_Maps', 'jjwg_Markers');
        } else {
            $excluded_module_array = array('Home', 'Calendar', 'Users', 'Forecasts', 'Emails', 'Campaigns', 'Targets', 'Documents', 'KBDocuments', 'Prospects', 'ProspectLists', 'Quotes', 'ab_SMS_Template', 'Reports', 'sca_Support_CallBack_App', 'oppor_Opportunity_Line_Item', 'dp_Duplicate_Data', 'eil_Interlinks', 'eua_UsersActivity');
        }

        //To get the system module list
        $controller = new TabController();
        $tabs       = $controller->get_tabs_system();
        $tabs[0]['EQM_Line_Item']="EQM_Line_Item";
        foreach ($tabs[0] as $key => $value) {
            if (!in_array($key, array_merge((array) $excluded_module_array, (array) $enabled_module_config_array))) {
                $available_module_array[] = array("module" => $key, 'label' => translate($key));
            }
        }

        //to create the name-value array of the already enabled modules
        foreach ($enabled_module_config_array as $key => $value) {
            $enabled_module_array[] = array("module" => $value, 'label' => translate($value));
        }
        $this->ss->assign('APP', $GLOBALS['app_strings']);
        $this->ss->assign('MOD', $GLOBALS['mod_strings']);
        $this->ss->assign('available_modules', json_encode($available_module_array));
        $this->ss->assign('enabled_modules', json_encode($enabled_module_array));
        // $this->ss->assign('title', $this->getModuleTitle(false));
        $this->ss->assign('title', 'Web Dashboard Modules Configuration');

        echo $this->ss->fetch('custom/modules/Administration/templates/webdashboard_module_configuration.tpl');
    }
}
