<?php
class CT_app_menu_man extends CI_Controller{
    function __construct()
    {
        parent::__construct();
		if (!$this->dx_auth->is_logged_in()) {
            redirect('');  
            die(); 
        }
        
        $this->uid = $this->session->userdata('DX_user_id');
        
        if(!permissionAuth(2) && $this->uid != RID){ 
            header('HTTP/1.1 401 Unauthorize');die('noaccess');
        } 
        $this->load->model("MDL_main", 'mdlMain');
    }

    function index(){
        if($this->uid != RID){ 
            header('HTTP/1.1 401 Unauthorize');die('noaccess');
        } 
        $data = array();
        $this->load->view('settings/view_app_menu_management', $data);
    }
    
    function loadMenuTypeData(){
        
        $page = isset($_POST['page'])?$_POST['page']:1;
        $limit = isset($_POST['rows'])?$_POST['rows']:20; 
        $sidx = isset($_POST['sidx'])?$_POST['sidx']:'id'; 
        $sord = isset($_POST['sord'])?$_POST['sord']:''; 
        $start = $limit*$page - $limit;

        if ($start<0) $start=0;
        
        
        $records = $this->mdlMain->getAllMenuTypeData($limit,$start,$sidx,$sord);
        $count = $this->mdlMain->getAllMenuTypeCount();
        if( $count > 0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } 
        if ($page > $total_pages) $page=$total_pages;
        
        $response->page = $page;
		$response->total = $total_pages;
		$response->records =  $count;
        $i=0;
        foreach($records as $r){
            
            $response->rows[$i]['id']=$r->id; 
            $response->rows[$i]['menutype']=$r->menutype; 
            $response->rows[$i]['menutitle']=$r->title; 
            $response->rows[$i]['menudesc']=$r->description; 
            $response->rows[$i]['menupublished']=$r->published_menutype; 
            $response->rows[$i]['menufp']=$r->frontpage_desc; 
            $response->rows[$i]['menuorder']=$r->order; 
 
            $i++;
        }
        echo json_encode($response);
    }
    
        
    function getAllMenu(){
        if($this->uid != RID){ 
            header('HTTP/1.1 401 Unauthorize');die('noaccess');
        } 
        header('Content-Type: application/json');
        
        
        $mr = $this->mdlMain->getAllMenuType();
        
        
        
        $count0 = count($mr['data']);
        $h = 0;
        echo '[';
        
        foreach($mr['data'] as $r){
            echo '{"title": "'.str_replace("'"," ",$r->title).'", "pid": "'.$r->id.'", "isFolder": true,"hideCheckbox": true,"unselectable": true, "key": "'.$r->menutype.'"';
            echo ',"children": [';
            
            
            $records = $this->mdlMain->getAllMenu($r->id);
            
            $count = count($records['data']);
            $i = 0;
            foreach($records['data'] as $record){
               //echo $record->title . ' >>> ' . $record->alias . '<br />';
               $is_folder = ($record->note != 'folder')? '':'"isFolder": true,';
               $is_link = ($record->link == " ")? '':'"url": "'.$record->link.'",';
               $is_icon = ($record->img == " ")? '':', "icon": "'.$record->img.'"';
               $is_check = ($record->published == 0)? ', "select": false ':', "select": true ';
               
               echo '{"title": "'.str_replace("'"," ",$record->title).'", '.$is_link.' "pid": "'.$record->id.'", '. $is_folder .'"key": "'.$record->alias.'"' . $is_icon . $is_check;
               
                    
                    $childs = $this->mdlMain->getAllChildMenu($record->id);
                    $count2 = count($childs['data']);
                    
                    
                    if($count2 > 0){
                        echo ',"children": [';
                        $j = 0;
                        // child loop start
                        foreach($childs['data'] as $child){
                            $is_icon = ($child->img == " ")? '':', "icon": "'.$child->img.'"';
                            $is_check = ($record->published == 0)? ', "select": false ':', "select": true ';
                            echo '{"title": "'.str_replace("'"," ",$child->title).'", "url": "'.$child->link.'", "pid": "'.$child->id.'", "key": "'.$child->alias.'" ' . $is_icon . $is_check;      
                            
                            
                            $grandChilds = $this->mdlMain->getAllChildMenu($child->id);
                            $count3 = count($grandChilds['data']);
                            
                            if($count3 > 0){
                                echo ',"children": [';
                                
                                $k = 0;
                                // grandchild loop start
                                foreach($grandChilds['data'] as $grandChild){
                                    $is_icon = ($grandChild->img == " ")? '':', "icon": "'.$grandChild->img.'"';
                                    $is_check = ($record->published == 0)? ', "select": false ':', "select": true ';
                                    
                                    echo '{"title": "'.str_replace("'"," ",$grandChild->title).'", "url": "'.$grandChild->link.'", "pid": "'.$grandChild->id.'", "key": "'.$grandChild->alias .'"'. $is_icon . $is_check;                    
                                    
                                    
                                    $gGrandChilds = $this->mdlMain->getAllChildMenu($grandChild->id);
                                    $count4 = count($gGrandChilds['data']);
                                    
                                    if($count4 > 0){
                                        echo ',"children": [';
                                        
                                        $l = 0;
                                        // grandgrandchild loop start
                                        foreach($gGrandChilds['data'] as $gGrandChild){
                                            $is_icon = ($gGrandChild->img == " ")? '':', "icon": "'.$gGrandChild->img.'"';
                                            $is_check = ($record->published == 0)? ', "select": false ':', "select": true ';
                                            echo '{"title": "'.str_replace("'"," ",$gGrandChild->title).'", "url": "'.$gGrandChild->link.'", "pid": "'.$gGrandChild->id.'", "key": "'.$gGrandChild->alias.'"' . $is_icon . $is_check;                    
                                            
                                            $l++;
                                            if ($l < $count4){
                                              echo '},';
                                            } else {
                                              echo '}';   
                                            }
                                        }
                                        // grandgrandchild loop end
                                        
                                        $l = 0;
                                        
                                        echo ']';
                                    }
                                    
                                    
                                    
                                    $k++;
                                    if ($k < $count3){
                                      echo '},';
                                    } else {
                                      echo '}';   
                                    }
                                }
                                // grandchild loop end
                                
                                $k = 0;
                                
                                echo ']';
                            }
                            
                                          
                            $j++;
                            if ($j < $count2){
                              echo '},';
                            } else {
                              echo '}';   
                            }
                        }
                        // child loop end
                        $j = 0;
                        
                        echo ']';
                    }
                    
                    
               
               $i++;
               if ($i < $count){
                 echo '},';
               } else {
                 echo '}';   
               }
               
               
            }
           // child loop end
           $i = 0;
            
           echo ']';
           
           $h++;
           if ($h < $count0){
             echo '},';
           } else {
             echo '}';   
           }
               
        }
        echo ']';
        
    }
    
    function saveappmenu(){
        
        $data = $this->input->post('data');        
        
        $arr_data = explode(",",$data);
        
        $db_string = "'". implode("','",$arr_data)."'";
        $this->mdlMain->updateMenuTree($db_string);
        
        die('{"sts":"00","desc":"success"}');
        
    }
    
    function publishappmenutype(){
        if(!permissionAuth(3)){ 
            //header('HTTP/1.1 401 Unauthorize');die('noaccess');
        } 
        
        $this->publish      = $this->input->post('publish');
        $this->id           = $this->input->post('id');       
        
        $this->mdlMain->publishMenu($this->publish ,$this->id);
        
        die('{"sts":"00","desc":"success"}'); 
    }
    
}