<?php

class Testlandingcontentmultistyle 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('Landingmain_Model','landingmain_model'); 
        $this->load->model('Landingsubmenu_Model','landingsubmenu_model'); 
        $this->load->model('Landingcontentmultistyle_Model','landingcontentmultistyle_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['landingcontentmultistyle'] = $this->landingcontentmultistyle_model->get();
        $data['selectedmenu']="landingcontentmultistyle";
        $this->load->view('admin/landingcontentmultistyle_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['partaimage1']['tmp_name'])){
                $result=$this->uploadfile('partaimage1');
            //     $result['message']="";
            // $result['url']="";
            // $result['status']="";
                // $arr['partcimage'] =$url;
                if($result['status']=='fail'){
                    echo $result['message'];
                    $passtest=false;                    
                }else{
                    $arr['partaimage1'] =$result['url'];
                    if(!empty($this->input->post('partaimage1olddata'))){
                       $replacestring=str_replace(base_url(),"",$this->input->post('partaimage1olddata'));
                       unlink($replacestring); 
                    }      
                }
            }else{
                $arr['partaimage1'] =$this->input->post('partaimage1olddata');
            }

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


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

            //part d image
            echo $_FILES['partdimage']['tmp_name'];

            if(is_uploaded_file($_FILES['partdimage']['tmp_name'])){
                $result=$this->uploadfile('partdimage');
                if($result['status']=='fail'){
                    echo $result['message'];
                    $passtest=false;                    
                }else{
                    $arr['partdimage'] =$result['url'];
                    if(!empty($this->input->post('partdimageolddata'))){
                       $replacestring=str_replace(base_url(),"",$this->input->post('partdimageolddata'));
                       unlink($replacestring); 
                    }      
                }
            }else{
                $arr['partdimage'] =$this->input->post('partdimageolddata');
            }
            //end part d image

            //part f image
            echo $_FILES['partfimage']['tmp_name'];

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

            //end part f image


            //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;
            $inputlandingpage="0";
            if($this->input->post('landingpage')==1){
                // echo "checked landingpage";
            	$inputlandingpage="1";
                // echo $inputlandingpage;
            }
            // else{
            //     // echo "not checked landingpage";
            // }
            $inputmainweb="0";
            if($this->input->post('mainweb')==1){
            	$inputmainweb="1";
            }


            $arr['main_id'] = $this->input->post('main_id');
            $arr['submenu_id'] = $this->input->post('submenu_id');
            $arr['landingpage'] = $inputlandingpage;
            // $arr['landingpage'] = "1";
            $arr['mainweb'] = $inputmainweb;
            $arr['h1'] = $this->input->post('h1');
            $arr['h2'] = $this->input->post('h2');
            $arr['meta_title'] = $this->input->post('meta_title');
            $arr['meta_keyword'] = $this->input->post('meta_keyword');
            $arr['meta_description'] = $this->input->post('meta_description');
            // $arr['partbtext'] = $this->input->post('partbtext');
            $arr['partaimage1_alttext'] = $this->input->post('partaimage1_alttext');
            $arr['partaimage2_alttext'] = $this->input->post('partaimage2_alttext');
            $arr['partcimage_alttext'] = $this->input->post('partcimage_alttext');
            $arr['partcpart2'] = $this->input->post('partcpart2');
            $arr['partdpart1'] = $this->input->post('partdpart1');
            $arr['partdimage_alttext'] = $this->input->post('partdimage_alttext');
            $arr['partetext'] = $this->input->post('partetext');
            $arr['partftext'] = $this->input->post('partftext');
            $arr['partfimage_alttext'] = $this->input->post('partfimage_alttext');
            // $arr['main_content'] = $this->input->post('main_content');
            $arr['displayorder'] = $this->input->post('displayorder');
            // $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->landingcontentmultistyle_model->insert($arr);
                    header('location:'.base_url().'admin/landingcontentmultistyle/edit?save=1&id='.$id);
                    exit;
                }else{
                    $this->landingcontentmultistyle_model->update($id, $arr);
                    header('location:'.base_url().'admin/landingcontentmultistyle/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['landingsubmenu'] = $this->landingsubmenu_model->get($id);
        //
        $data['save'] = $this->input->get('save');
        $data['selectedmenu']="landingcontent";    
        $this->load->view('admin/testlandingcontentmultistyle_edit_view.php', $data);    
        
    }
    function delete($id)
    {
       //$this->db->delete('items', array('id' => $id));
       $this->landingcontentmultistyle_model->delete($id);
       echo 'Deleted successfully.';
    }
    function getsubmenu(){
        $id=$this->input->post('id',TRUE);
        $data=$this->landingcontentmultistyle_model->getsubmenu($id);
        echo json_encode($data);
    }
    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_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;
    }
    
}

?>