<?php

class Usebrand extends CI_Controller {

        var $module = 'feature';
    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('Usebrand_Model','usebrand_model');  
        $this->load->model('Language','language_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['landingsubmenu'] = $this->landingsubmenu_model->get();
        // $data['save']="";
        // $data['feature'] = $this->feature_model->get();
        // $data['featurecontent'] = $this->featurecontent_model->get();
        // $data['selectedmenu']="feature";
        // $this->load->view('admin/feature_view.php', $data); 


         $id = $this->input->get('id');
        if(empty($id)){
            $id=9;
        }
        $data['languages'] = $this->language_model->get_all();
        $data['module'] = $this->module;
  
        //submenu by selected content menu
        //it may change at later 2020-11-20
        $data['usebrand'] = $this->usebrand_model->get();
        //
        $data['save'] = $this->input->get('save');
        $data['selectedmenu']="usebrand";    
         $this->load->view('admin/usebrand_view.php', $data);    
    
    }

    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['use_brand_image']['tmp_name'])){
                $result=$this->uploadfile('use_brand_image');
            //     $result['message']="";
            // $result['url']="";
            // $result['status']="";
                // $arr['partcimage'] =$url;
                if($result['status']=='fail'){
                    echo $result['message'];
                    $passtest=false;                    
                }else{
                    $arr['use_brand_image'] =$result['url'];
                    if(!empty($this->input->post('use_brand_imageeolddata'))){
                       $replacestring=str_replace(base_url(),"",$this->input->post('use_brand_imageeolddata'));
                       unlink($replacestring); 
                    }      
                }
            }else{
                $arr['use_brand_image'] =$this->input->post('use_brand_imageeolddata');
            }











            //handle upload file

            // $source = $_FILES['partcimage']['tmp_name'];
            // $file = $_FILES['partcimage']['name'];
            // //var_dump($source." ".$file);

            // $ext = pathinfo($file, PATHINFO_EXTENSION);

            // $config['upload_path'] = $_SERVER['DOCUMENT_ROOT']."/assets/images/uploads/";
            // $config['allowed_types'] = 'gif|jpg|jpeg|png';
            // $config['overwrite'] = FALSE;

            // $newName = date('YmdHis').rand(1000, 9999); 
            // $config["file_name"] = $newName.$ext;
            // $this->load->library('upload',$config);
            // if (!$this->upload->do_upload('partcimage')){ // upload the file, 'upload' is the name of the field from CKEditor
            //  // failed upload
            //     $message = "Upload failed on blog manager server.";
            //     $url = '';
            //     //echo $message;
            //     //echo $this->upload->display_errors();
            //     // $error = array('error' => $this->upload->display_errors());
         
            // }else{

            //     echo "succeful upload";
            //     $upload_result = base_url() . 'assets/images/uploads/'. $this->upload->data()['file_name'];
            //     $upload_name = $this->upload->data()['file_name'];
            //     $url=$upload_result;

            //     //resize image
            //         $config1['source_image'] = $this->upload->upload_path.$this->upload->file_name;
            //         $config1['new_image'] = $this->upload->upload_path.$newName."s.".$ext;
            //         $config1['maintain_ratio'] = TRUE;
            //         $config1['width'] = 500;
            //          $message = 'Upload success!';
            //         $this->load->library('image_lib', $config1); 
            //         if ( ! $this->image_lib->resize()){ 
            //             // $message=$message."resize fail".$this->image_lib->display_errors('', '')." ".$config1['new_image']." ".$config1['source_image'];
            //             $message=$message."resize fail".$this->image_lib->display_errors('', '')." ";
            //             $url="";
            //         }
            //         else{
            //             //$message=$message."resize successful";
            //             unlink($this->upload->upload_path.$this->upload->file_name);
            //             $url=base_url() . 'assets/images/uploads/'.$newName."s.".$ext;
            //         }
            //     //end resize image

            // }
            //end handle upload file

            //$arr['partcimage'] =$url;
            // $arr['use_brand_text'] = $this->input->post('use_brand_text');
            $arr['displayorder'] = $this->input->post('displayorder');
            $arr['alt_text'] = $this->input->post('alt_text');
            $arr['id'] = $this->input->post('id');

            // $arr['keywords'] = $this->input->post('keywords');
            // $arr['lang'] = $this->input->post('lang');
            if($passtest==true){
                if($id == false){
                    $arr['entered'] = date('Y-m-d H:i:s');
                    $id = $this->usebrand_model->insert($arr);
                    header('location:'.base_url().'admin/usebrand/edit?save=1&id='.$id);
                    exit;
                }else{
                    $this->usebrand_model->update($id, $arr);
                    header('location:'.base_url().'admin/usebrand/edit?save=1&id='.$id);
                    exit;
                }
            }//if($passtest==true){
        }
        
        $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['landingcontentmultistyle'] = $this->landingcontentmultistyle_model->get_id($id);
        //submenu by selected content menu
        //it may change at later 2020-11-20
        $data['usebrandcontent'] = $this->usebrand_model->get_id($id);
        //
        $data['save'] = $this->input->get('save');
        $data['selectedmenu']="use_brand";    
        $this->load->view('admin/use_brand_edit_view.php', $data);    
        
    }
    function delete($id)
    {
       //$this->db->delete('items', array('id' => $id));
       $this->usebrand_model->delete($id);
       echo 'Deleted successfully.';
    }

    function uploadfile($filename){
        $result=array();
        $result['message']="";
        $result['url']="";
        $result['status']="";

        //upload code
         //handle upload file
            $status="success";
            $source = $_FILES[$filename]['tmp_name'];
            $file = $_FILES[$filename]['name'];
            //var_dump($source." ".$file);

            $ext = pathinfo($file, PATHINFO_EXTENSION);

            $config['upload_path'] = $_SERVER['DOCUMENT_ROOT']."/assets/images/uploads/";
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['overwrite'] = FALSE;

            $newName = date('YmdHis').rand(1000, 9999); 
            $config["file_name"] = $newName.$ext;
            $this->load->library('upload',$config);

            $sourceUPLPath = $_FILES[$filename]['tmp_name'];
            $targetUPLPath = $_SERVER['DOCUMENT_ROOT']."/assets/images/uploads/".  $newName.".".$ext;

            if (!move_uploaded_file($sourceUPLPath,$targetUPLPath)){ // upload the file, 'upload' is the name of the field from CKEditor
             // failed upload
                $message = "Upload failed on blog manager server.";
                $url = '';
                //echo $message;
                //echo $this->upload->display_errors();
                //$message = $message .$this->upload->display_errors();
                // $error = array('error' => $this->upload->display_errors());
                $status="fail";
         
            }else{

                //echo "succeful upload";
                // $upload_result = base_url() . 'assets/images/uploads/'. $newName.".".$ext;
                $upload_result = 'assets/images/uploads/'. $newName.".".$ext;
                $upload_name = $newName.$ext;
                $url=$upload_result;
                 $message ="successful upload";
                //resize image fail at result 2020-11-23
                    // $config1['source_image'] ='assets/images/uploads/'. $newName.".".$ext;
                    // $config1['new_image'] = 'assets/images/uploads/'.$newName."s.".$ext;
                    // $config1['maintain_ratio'] = TRUE;
                    // $config1['width'] = 500;
                    //  $message = 'Upload success!';
                    // $this->load->library('image_lib', $config1); 
                    // if ( ! $this->image_lib->resize()){ 
                    //     // $message=$message."resize fail".$this->image_lib->display_errors('', '')." ".$config1['new_image']." ".$config1['source_image'];
                    //     $message=$message."resize fail".$this->image_lib->display_errors('', '')." ";
                    //     $url="";
                    //     $status="fail";
                    // }
                    // else{
                    //     //$message=$message."resize successful";
                    //     unlink('assets/images/uploads/'. $newName.".".$ext);
                    //     $url=base_url() . 'assets/images/uploads/'.$newName."s.".$ext;
                    // }
                //end resize image



            }
            //end handle upload file
        //end upload code


        $result['message']=$message;
        $result['url']=$url;
        $result['status']=$status;

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



        return "";


    }
    
}

?>