<?php

class Dragtest extends CI_Controller {

        var $module = 'dragtag';
    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('Blog_tag_Model','blog_tag_model'); 
        $this->load->model('Language','language_model');  
        $this->load->model('Test_drag_Model','test_drag_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()
    {
        
        $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['module'] = $this->module;
        $data['blog_tag_content'] = $this->blog_tag_model->get();
        $data['selectedmenu']="question_submit";

        $data['test_drag_content']=$this->test_drag_model->get();

        $this->load->view('admin/drag_test_view.php', $data); 
    
    }

    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->test_drag_model->update_rank($value,$rank);
            $rank=$rank+1;
        }



        return "";


    }
    function edit()
    {

        // 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');

            $passtest=true;
            // if(is_uploaded_file($_FILES['blog_image']['tmp_name'])){
            //     $result=$this->uploadfile('blog_image');
            // //     $result['message']="";
            // // $result['url']="";
            // // $result['status']="";
            //     // $arr['partcimage'] =$url;
            //     if($result['status']=='fail'){
            //         echo $result['message'];
            //         $passtest=false;                    
            //     }else{
            //         $arr['blog_image'] =$result['url'];
            //         if(!empty($this->input->post('blog_imageolddata'))){
            //            $replacestring=str_replace(base_url(),"",$this->input->post('blog_imageolddata'));
            //            unlink($replacestring); 
            //         }      
            //     }
            // }else{
            //     $arr['blog_image'] =$this->input->post('blog_imageolddata');
            // }


            
            $arr['color'] = $this->input->post('color');
            // $arr['blog_image'] = $this->input->post('blog_image');
            $arr['name'] = $this->input->post('name');
            $arr['display_order'] = $this->input->post('display_order');

            // $arr['keywords'] = $this->input->post('keywords');
            // $arr['lang'] = $this->input->post('lang');
            
            if($id == false){
                $arr['update_time'] = date('Y-m-d H:i:s');
                $id = $this->blog_tag_model->insert($arr);

                


                header('location:'.base_url().'admin/blogtag/edit?save=1&id='.$id);
                exit;
            }else{
                $this->blog_tag_model->update($id, $arr);
                header('location:'.base_url().'admin/blogtag/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['blog_tag_content'] = $this->blog_tag_model->get_id($id);
        



        $data['save'] = $this->input->get('save');
        $data['selectedmenu']="blog";    
        $this->load->view('admin/blog_tag_edit_view.php', $data);    
        
    }
    function delete($id)
    {
       //$this->db->delete('items', array('id' => $id));
       $this->blog_tag_model->delete($id);
       echo 'Deleted successfully.';
    }
   
    
}

?>