<?php
class MY_Controller  extends CI_Controller{
    
    public function __construct(){
        parent::__construct();
        session_start();
        $this->url = $this->config->item("base_url");
        if(!$this->checkIn()){
            header("Location:" . $this->url . "login");
        }
    }
    
    public function checkIn(){
        if (isset($_SESSION['name'])){
           return true;
        } else{
            return false;            
        }
    }
    
    public function navButton($_navcode){
        
        $_url=$_SERVER['PHP_SELF'];
        $_nav_url=substr($_url,14);
        $_url_arr=explode('/',$_nav_url);
        if(count($_url_arr)>1){
            $_url_arr[1]=ucfirst($_url_arr[1]);
            $_nav_url=implode('/', $_url_arr);
        }

        if(!$_SESSION['admin']){
                foreach($_SESSION['power'] as $_key=>$_value){
                    if($_value->nav_code==$_navcode){
                        foreach ($_value->children as $_keys=>$_values){
                            if($_values->nav_url==$_nav_url){
                                $_button=$_values->button;
                            }
                        }
                    }
                }
        
                if($_button){
                    foreach($_button as $_key=>$_value){
                        foreach($_SESSION['button'] as $_keys=>$_values){
                            if($_value==$_values->button_id){
                                $_data['button'][]=$_SESSION['button'][$_keys];
                            }
                        }
                    }
                    foreach ($_data['button'] as $_key => $_value) {
                        $_tmp[$_key] = $_value->button_level;
                    }
                    array_multisort($_tmp,SORT_ASC,$_data['button']);
                    return $_data['button'];            
                }else{
                    return false;
                }
        }else{
            return $_SESSION['button'];
        }
        
    }
}