<?php

class Landingsubmenu extends CI_Controller {

        var $module = 'landingsubmenu';
    public function __construct()
    {
        // $this->load does not exist until after you call this
        parent::__construct(); // Construct CI's core so that you can use it

        $this->load->database();
        $this->load->helper('language'); 
        $this->load->library('session'); 
        $this->load->model('Security','security_model');
        $this->load->model('Landingsubmenu_Model','landingsubmenu_model');
        $this->load->model('Language','language_model');
        $this->load->model('Landingmain_Model','landingmain_model');

         //require_once(APPPATH.'controllers/admin/Security.php'); //include controller
            //$security = new Security();  //create object 
            //$security->permission(); //call function    

        //check user have login or not
        $this->security_model->admin($this->session->userdata('session_id'),$this->session->userdata('user_id'),$this->session->userdata('username'));
        //end check user have login or not
    }

    function index()
    {

        $data['users'] = $this->db->get('_users')->num_rows();
        $data['_inactive_loading'] = true;
        $data['page'] = $this->module;
        $data['controller'] = $this->router->fetch_class();
        $data['landingmainmenu'] = $this->landingmain_model->get();
        $data['_view'] = 'landingsubmenu';
        $data['csrf'] = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $data['web']=$this->uri->segment(4);
        if(empty($data['web'])){
            $data['web']=$data['landingmainmenu'][0]->id;
        }
        $data['landingsubmenu'] = $this->landingsubmenu_model->getbymainid($data['web']);
        $data['selectedmenu']="landingsubmenu";
        $this->load->view('backend/index', $data);

    }

    function index_old()
    {
        
        $session_id = $this->security_model->getcode();
        //$sess = $this->security_model->admin($session_id);
        //$property = $this->Property->get();
        

        //check user have login or not
        // $this->security_model->admin($this->session->userdata('session_id'),$this->session->userdata('user_id'),$this->session->userdata('username'));
        //end check user have login or not
        
        //$data['sess'] = $sess;  
        //$data['property'] = $property;

        $data['landingmainmenu']=$this->landingmain_model->get();
         $data['web']=$this->uri->segment(4);
        if(empty($data['web'])){
            $data['web']=$data['landingmainmenu'][0]->id;
        }

        $data['module'] = $this->module;
        // $data['landingsubmenu'] = $this->landingsubmenu_model->get();
        $data['landingsubmenu'] = $this->landingsubmenu_model->getbymainid($data['web']);
        $data['selectedmenu']="landingsubmenu";
        $this->load->view('backend/landingsubmenu_view.php', $data);
    
    }

    function edit()
    {

        if($this->input->post('save')){
            //echo "post save";
            $request = $this->input->post();
//            $id = $this->input->post('id');

            $arr['main_id'] = $this->input->post('main_id');
            $arr['title'] = $this->input->post('title');
            $arr['title_en'] = $this->input->post('title_en');
            $arr['uri'] = $this->input->post('uri');
            $arr['displayorder'] = $this->input->post('displayorder');
            // $arr['keywords'] = $this->input->post('keywords');
            // $arr['lang'] = $this->input->post('lang');

            if (empty($request['id'])) {
                $arr['entered'] = date('Y-m-d H:i:s');
                $id = $this->landingsubmenu_model->insert($arr);
            } else {
                $this->landingsubmenu_model->update($request['id'], $arr);
            }
            header('location:'.base_url().'backend/landingsubmenu/edit?save=1&id='.$id);
            exit;
        }

        $request = $this->input->get();
        if (array_key_exists('id', $request)) {
            $id = $request['id'];
            $data['languages'] = $this->language_model->get_all();
            $data['landingsubmenu'] = $this->landingsubmenu_model->get_id($id);
            $this->load->model('Page_objects_model', 'page_object');
            $data['landingsubmenu_objects'] = $this->page_object->load_objects(0, $id);
            $data['object_type'] = $this->page_object->object_type;
        }
        else {
            $data['landingsubmenu'] = [ [] ];
        }
        $data['save'] = array_key_exists('save', $request) ? $request['save'] : '';
        $data['_inactive_loading'] = true;
        $data['selectedmenu']="landingsubmenu";
        $data['landingmain'] = $this->landingmain_model->get();
        $data['_view'] = 'landingsubmenu_edit';
        $data['csrf'] = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $this->load->view('backend/index', $data);

    }

    function edit_old()
    {

        // echo "1";
        //$session_id = $this->security_model->getcode();
        //$this->security_model->admin($this->session->userdata('session_id'),$this->session->userdata('user_id'),$this->session->userdata('username'));
        //$property = $this->Property->get();
        
        //echo $this->input->post('save');

        if($this->input->post('save')){
            //echo "post save";
            $id = $this->input->post('id');
            
            $arr['main_id'] = $this->input->post('main_id');
            $arr['title'] = $this->input->post('title');
            $arr['displayorder'] = $this->input->post('displayorder');
            // $arr['keywords'] = $this->input->post('keywords');
            // $arr['lang'] = $this->input->post('lang');
            
            if($id == false){
                $arr['entered'] = date('Y-m-d H:i:s');
                $id = $this->landingsubmenu_model->insert($arr);
                header('location:'.base_url().'backend/landingsubmenu/edit?save=1&id='.$id);
                exit;
            }else{
                $this->landingsubmenu_model->update($id, $arr);
                header('location:'.base_url().'backend/landingsubmenu/edit?save=1&id='.$id);
                exit;
            }
        }
        
        $id = $this->input->get('id');
        
        $data['languages'] = $this->language_model->get_all();
        //$data['sess'] = $sess;  
        //$data['sess'] = $session_id;  
        //$data['property'] = $property;
        //$data['module'] = $this->module;
         $data['landingmain'] = $this->landingmain_model->get();
        $data['landingsubmenu'] = $this->landingsubmenu_model->get_id($id);
        $data['save'] = $this->input->get('save');
        $data['selectedmenu']="landingsubmenu";    
        $this->load->view('backend/landingsubmenu_edit_view.php', $data);
        
    }
    function delete($id)
    {
       //$this->db->delete('items', array('id' => $id));
       $this->landingsubmenu_model->delete($id);
       echo 'Deleted successfully.';
    }
    function update_rank(){
        // $table = txt_only($_GET['sortList']);
        // $r_name = txt_only($_GET['rank_name']);
        // $id = txt_only($_GET['id_name']);
        // $item_name = $_GET['listItem'];

        $table_name = $this->input->post('table_name');
        $rank_name = $this->input->post('rank_name');
        $id_name = $this->input->post('id_name');
        $order = $this->input->post('order');
        // var_dump($order);
        $params = array();
        // parse_str($order, $params);
        parse_str($order, $searcharray);
        // print_r($searcharray);
        $rank=1;
        // var_dump($searcharray['listItem']);
        foreach ($searcharray['listItem'] as $value) {
            // echo "<br>";
            // echo 'value';
            // echo $value;
            $this->landingsubmenu_model->update_rank($value,$rank);
            $rank=$rank+1;
        }



        return "";


    }
    
}

?>