<?php
    class MY_Controller extends CI_Controller {

        public $userModules = array();
        public $isSuperUser = false;

        function __construct()
        {
            parent::__construct();
            $this->_setupPermissions();

            //Automatic check the user has permission to use this controller or not
			if ($this->session->userdata('user_type')) {
				$this->_checkModulePass();
			}	
        }

        function _setupPermissions()
        {
            if ($this->session->userdata('user_type')) {
                if ($this->session->userdata('user_type') == SUPERADMIN) {
                    $this->isSuperUser = true;
                }

                if ($this->session->userdata('user_type') == ADMIN) {
                    $userModules = $this->db->get_where('sw_user_modules', array('user_id' => $this->session->userdata('user_id')))->result();
                    $modules = array();
                    foreach ($userModules as $key => $var) {
                        $modules[] = $var->module;
                    }
                    $this->userModules = $modules;
                }
            }
        }

        function _checkModulePass()
        {
            $module = $this->uri->segment(1);
            $method = $this->uri->segment(2);

            //Skip checking for some controller to prevent unlimited redirect
            if (($module != 'error') && ($module != 'home') && ($module != 'language') && ($module != 'login') && ($module != 'logout') && ($module != 'profile')) {
                //Superuser no checking
                if ($this->isSuperUser !== true) {
                    $this->lang->load('account_general', get_language());

                    if (!in_array($module, $this->userModules)) {
                        $this->session->set_userdata(array('form_msg_type' => 200, 'form_msg' => $this->lang->line('permission_denied')));
                        redirect('home', 'refresh');
                    }
                }
            }
        }

        public function getModuleLists() {
			$this->lang->load('account_general', get_language());
			
            return array(
				'adjustment' => $this->lang->line('hf_adjustment'),
                'admin' => $this->lang->line('hf_admin'),
				'announcement' => $this->lang->line('hf_announcement'),
				'convert' => $this->lang->line('hf_convert'),
				'insurance' => $this->lang->line('hf_insurance'),
				// 'game' => $this->lang->line('hf_game'),
                'member' => $this->lang->line('hf_member_manager'),
                'message' => $this->lang->line('hf_private_message'),
				// 'product' => $this->lang->line('hf_product'),
				// 'payout' => $this->lang->line('hf_payout'),
				'report' => $this->lang->line('hf_report'),
				'setting' => $this->lang->line('hf_setting'),
				'topup' => $this->lang->line('hf_topup'),
				'transfer' => $this->lang->line('hf_transfer'),
				'withdrawal' => $this->lang->line('hf_withdrawal'),
            );
        }

    }