<?php

class Serviceinfo extends CI_Controller {

        var $module = 'serviceinfo_content';
    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('at-admin/Security','security_model');
        $this->load->model('at-admin/Serviceinfo_Model','serviceinfo_model');
        $this->load->model('at-admin/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
        

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


            $arr['slogan_text'] = $this->input->post('slogan_text');
           

            // $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->serviceinfo_model->insert($arr);
                    header('location:'.base_url().'at-admin/serviceinfo/index?save=1&id='.$id);
                    exit;
                }else{
                    $this->serviceinfo_model->update($id, $arr);
                    header('location:'.base_url().'at-admin/serviceinfo/index?save=1&id='.$id);
                    exit;
                }

        }
        

        //end add save feature


        //$data['sess'] = $sess;  
        //$data['property'] = $property;
        // $data['module'] = $this->module;
        // //$data['landingsubmenu'] = $this->landingsubmenu_model->get();
        // $data['save']="";
        // $data['feature'] = $this->serviceinfo_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=1;
        }
        $data['languages'] = $this->language_model->get_all();
        $data['module'] = $this->module;
        
        $data['serviceinfo'] = $this->serviceinfo_model->get_id($id);
        //submenu by selected content menu

        //
        $data['save'] = $this->input->get('save');
        $data['selectedmenu']="feature";    
         $this->load->view('at-admin/serviceinfo_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['feature_image']['tmp_name'])){
                $result=$this->uploadfile('feature_image');
            //     $result['message']="";
            // $result['url']="";
            // $result['status']="";
                // $arr['partcimage'] =$url;
                if($result['status']=='fail'){
                    echo $result['message'];
                    $passtest=false;                    
                }else{
                    $arr['feature_image'] =$result['url'];
                    if(!empty($this->input->post('feature_imageeolddata'))){
                       $replacestring=str_replace(base_url(),"",$this->input->post('feature_imageeolddata'));
                       unlink($replacestring); 
                    }      
                }
            }else{
                $arr['feature_image'] =$this->input->post('feature_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['feature_text'] = $this->input->post('feature_text');
            $arr['displayorder'] = $this->input->post('displayorder');
            $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->featurecontent_model->insert($arr);
                    header('location:'.base_url().'at-admin/shopfeature/edit?save=1&id='.$id);
                    exit;
                }else{
                    $this->featurecontent_model->update($id, $arr);
                    header('location:'.base_url().'at-admin/shopfeature/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['featurecontent'] = $this->featurecontent_model->get_id($id);
        //
        $data['save'] = $this->input->get('save');
        $data['selectedmenu']="shopfeature";    
        $this->load->view('at-admin/shopfeature_edit_view.php', $data);
        
    }
    function delete($id)
    {
       //$this->db->delete('items', array('id' => $id));
       $this->featurecontent_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/at-admin/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/at-admin/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/at-admin/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;
    }
    
}

?>