<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Hotel_facilitates extends CI_Controller
{
    public $userDetails;
    function __construct()
    {
        parent::__construct();
        $this->load->model('Hotel_facilitates_model');
        $this->load->library('form_validation');
        $this->load->model('ApiModel');
        $this->userDetails = $this->session->userdata('isUserLoggedIn');
    }

    public function ajexfile(){

        $type = $_GET['type'];
        $CKEditor = $_GET['CKEditor'];
        $funcNum  = $_GET['CKEditorFuncNum'];

        //print_r($_FILES);

        // Image upload
        if($type == 'image'){
            
            $allowed_extension = array(
                "png",
                "jpg",
                "jpeg"
            );
            
            // Get image file extension
            $file_extension = pathinfo($_FILES["upload"]["name"], PATHINFO_EXTENSION);
            
            if(in_array(strtolower($file_extension),$allowed_extension)){
                echo $_FILES['upload']['tmp_name'];
                if( move_uploaded_file($_FILES['upload']['tmp_name'], "assets/uploads/".$_FILES['upload']['name'])){

                    // File path
                    if(isset($_SERVER['HTTPS'])){
                        $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
                    }
                    else{
                        $protocol = 'http';
                    }
                    $url = base_url()."assets/uploads/".$_FILES['upload']['name'];

                    echo '<script>window.parent.CKEDITOR.tools.callFunction('.$funcNum.', "'.$url.'", "'.$message.'")</script>';
                }
                
            }
            
            exit;
        }

        // File upload
        if($type == 'file'){
            
            $allowed_extension = array(
                "doc",
                "pdf",
                "docx"
            );
            
            // Get image file extension
            $file_extension = pathinfo($_FILES["upload"]["name"], PATHINFO_EXTENSION);
            
            if(in_array(strtolower($file_extension),$allowed_extension)){

                if(move_uploaded_file($_FILES['upload']['tmp_name'], "assets/uploads/".$_FILES['upload']['name'])){

                    // File path
                    if(isset($_SERVER['HTTPS'])){
                        $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
                    }
                    else{
                        $protocol = 'http';
                    }

                    $url = base_url()."assets/uploads/".$_FILES['upload']['name'];

                    echo '<script>window.parent.CKEDITOR.tools.callFunction('.$funcNum.', "'.$url.'", "'.$message.'")</script>';
                }
                 
            }
            
            exit;
        }
    }   



    public function menus($menu_id)
    {
        $categoryid = isset($_REQUEST['categoryid'])?$_REQUEST['categoryid']:'';
        $q = urldecode($this->input->get('q', TRUE));
        $start = intval($this->input->get('start'));
        
        if ($q <> '') {
            $config['base_url'] = base_url() . 'hotel_facilitates/?q=' . urlencode($q);
            $config['first_url'] = base_url() . 'hotel_facilitates/?q=' . urlencode($q);
        } else {
            $config['base_url'] = base_url() . 'hotel_facilitates/';
            $config['first_url'] = base_url() . 'hotel_facilitates/';
        }

        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['total_rows'] = $this->Hotel_facilitates_model->total_rows($q,$menu_id,$categoryid,$this->userDetails['Id']);
        $hotel_facilitates = $this->Hotel_facilitates_model->get_limit_data($config['per_page'], $start, $q,$menu_id,$categoryid,$this->userDetails['Id']);

        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $data = array(
            'hotel_facilitates_data' => $hotel_facilitates,
            'q' => $q,
            'pagination' => $this->pagination->create_links(),
            'total_rows' => $config['total_rows'],
            'start' => $start,
            'menu_id'=>$menu_id,
            'category_id'=> $categoryid,
        );

        $sigleMeu = $this->ApiModel->getMenDetails($menu_id);
        $HotelMenu = $sigleMeu[0];

        $pageData = array(
            'header'=>array('pageTitle'=>$HotelMenu['fsTitle'].' Facilities'),
            'body'=>$data,
            'footer'=>array(),
        );
        loadView('hotel_facilitates/hotel_facilitates_list',$pageData);
    }


    public function create($menu_id,$isDirectCall = false) 
    {
        if($isDirectCall==true){
            $_REQUEST['categoryid'] = $menu_id['fiCategoryId'];
            $menu_id = $menu_id['fiMenuId'];
        }
       
        $data = array(
            'button' => 'Create',
            'action' => site_url('hotel_facilitates/create_action'),
        'id' => set_value('id'),
        'fiMenuId' => set_value('fiMenuId'),
        'fiCategoryId' => $_REQUEST['categoryid'],
        'fiHotelsId' => set_value('fiHotelsId'),
        'fsTitle' => set_value('fsTitle'),
        'fsImage' => set_value('fsImage'),
        'fsBannerImage' => set_value('fsBannerImage'),
        'fsDescription' => set_value('fsDescription'),
        'fiStatus' => set_value('fiStatus'),
        'fdCreateDate' => set_value('fdCreateDate'),
        'fdUpdateDate' => set_value('fdUpdateDate'),
        'menu_id'=>$menu_id
    );
        $query = $this->db->select('*')->from('hotel_menu')->get();
        $data['menuLists'] = $query->result_array(); 

        $query = $this->db->select('*')->from('hotel_category')->where('hotel_category.fiMenuId',$menu_id)->get();
        $data['categoryLists'] = $query->result_array(); 

        $pageData = array(
            'header'=>array('pageTitle'=>'Hotels create'),
            'body'=>$data,
            'footer'=>array()
        );
         loadView('hotel_facilitates/hotel_facilitates_form',$pageData);
    }
    
    public function create_action() 
    {

        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->create($_REQUEST,true);
        } else {
            $data = array(
        'fiMenuId' => $this->input->post('fiMenuId',TRUE),
        'fiCategoryId' => $this->input->post('fiCategoryId',TRUE),
        'fiHotelsId' => $this->input->post('fiHotelsId',TRUE),
        'fsTitle' => $this->input->post('fsTitle',TRUE),
        'fsImage' => $this->input->post('fsImage',TRUE),
        'fsBannerImage' => $this->input->post('fsBannerImage',TRUE),
        'fsDescription' => $this->input->post('fsDescription',TRUE),
        'fiStatus' => $this->input->post('fiStatus',TRUE),
        'fdCreateDate' => $this->input->post('fdCreateDate',TRUE),
        'fdUpdateDate' => $this->input->post('fdUpdateDate',TRUE),
        ); 

            // Upload Image

                $SampleBannerImage = "";
                $ext = pathinfo($_FILES["fsImage"]["name"], PATHINFO_EXTENSION);
                $target_file = "assets/uploads/facilities_image/icon/" . time().'-'.rand(1111,9999).'.'.$ext;
                if (move_uploaded_file($_FILES["fsImage"]["tmp_name"], $target_file)) {
                }
                $SampleBannerImage = $target_file;
                $data['fsImage'] = $SampleBannerImage;

             // End

            // Upload Image

                $SampleBannerImage = "";
                $ext = pathinfo($_FILES["fsBannerImage"]["name"], PATHINFO_EXTENSION);
                $target_file = "assets/uploads/facilities_image/" . time().'-'.rand(1111,9999).'.'.$ext;
                if (move_uploaded_file($_FILES["fsBannerImage"]["tmp_name"], $target_file)) {
                }
                $SampleBannerImage = $target_file;
                $data['fsBannerImage'] = $SampleBannerImage;

             // End


            $this->Hotel_facilitates_model->insert($data);
            $this->session->set_flashdata('message', 'Create Record Success');
            redirect(site_url('hotel_facilitates/menus/'.$this->input->post('fiMenuId',TRUE).'?categoryid='.$_REQUEST['fiCategoryId']));
            
        }
    }
    
    public function update($id,$isDirectCall = false) 
    {

        if($isDirectCall==true){
            $_REQUEST['categoryId'] = $id['fiCategoryId'];
            $id = $id['id'];
        }

        $row = $this->Hotel_facilitates_model->get_by_id($id);

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('hotel_facilitates/update_action'),
        'id' => set_value('id', $row->id),
        'fiMenuId' => set_value('fiMenuId', $row->fiMenuId),
        'fiCategoryId' => $_REQUEST['categoryId'],
        'fiHotelsId' => set_value('fiHotelsId', $row->fiHotelsId),
        'fsTitle' => set_value('fsTitle', $row->fsTitle),
        'fsImage' => set_value('fsImage', $row->fsImage),
        'fsBannerImage' => set_value('fsBannerImage', $row->fsBannerImage),
        'fsDescription' => set_value('fsDescription', $row->fsDescription),
        'fiStatus' => set_value('fiStatus', $row->fiStatus),
        'fdCreateDate' => set_value('fdCreateDate', $row->fdCreateDate),
        'fdUpdateDate' => set_value('fdUpdateDate', $row->fdUpdateDate),
        'menu_id'=>$row->fiMenuId

        );

            $query = $this->db->select('*')->from('hotel_menu')->get();
            $data['menuLists'] = $query->result_array(); 

            $query = $this->db->select('*')->from('hotel_category')->where('hotel_category.fiMenuId',$row->fiMenuId)->get();
            $data['categoryLists'] = $query->result_array(); 

            $pageData = array(
                'header'=>array('pageTitle'=>'Hotels update'),
                'body'=>$data,
                'footer'=>array(),
            );
            loadView('hotel_facilitates/hotel_facilitates_form',$pageData);
        } else {
             // print_r($_REQUEST['categoryid']); die();
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('hotel_facilitates/menus/'.$this->input->post('fiMenuId',TRUE).'?categoryid='.$_REQUEST['categoryid']));
        }
    }
    
    public function update_action() 
    {

        
            $data = array(
        'fiMenuId' => $this->input->post('fiMenuId',TRUE),
        'fiCategoryId' => $this->input->post('fiCategoryId',TRUE),
        'fiHotelsId' => $this->input->post('fiHotelsId',TRUE),
        'fsTitle' => $this->input->post('fsTitle',TRUE),
        'fsImage' => $this->input->post('fsImage',TRUE),
        // 'fsBannerImage' => $this->input->post('fsBannerImage',TRUE),
        'fsDescription' => $this->input->post('fsDescription',TRUE),
        'fiStatus' => $this->input->post('fiStatus',TRUE),
        'fdCreateDate' => $this->input->post('fdCreateDate',TRUE),
        'fdUpdateDate' => $this->input->post('fdUpdateDate',TRUE),
        ); 

            // Upload Image
                if($_FILES["fsImage"]["name"]!=""){
                $SampleBannerImage = "";
                $ext = pathinfo($_FILES["fsImage"]["name"], PATHINFO_EXTENSION);
                $target_file = "assets/uploads/facilities_image/icon/" . time().'-'.rand(1111,9999).'.'.$ext;
                // echo $target_file; die();
                if (move_uploaded_file($_FILES["fsImage"]["tmp_name"], $target_file)) {
                }
                $SampleBannerImage = $target_file;
                $data['fsImage'] = $SampleBannerImage;
            }
             // End

            // Upload Image
                if($_FILES["fsBannerImage"]["name"]!=""){
                    $SampleBannerImage = "";
                    $ext = pathinfo($_FILES["fsBannerImage"]["name"], PATHINFO_EXTENSION);
                    $target_file = "assets/uploads/facilities_image/" . time().'-'.rand(1111,9999).'.'.$ext;
                    // echo $target_file; die();
                    if (move_uploaded_file($_FILES["fsBannerImage"]["tmp_name"], $target_file)) {
                    }
                    $SampleBannerImage = $target_file;
                    $data['fsBannerImage'] = $SampleBannerImage;
                }

             // End

            // print_r($data); die();
            $this->Hotel_facilitates_model->update($this->input->post('id', TRUE), $data);
            $this->session->set_flashdata('message', 'Update Record Success');
             redirect(site_url('hotel_facilitates/menus/'.$this->input->post('fiMenuId',TRUE).'?categoryid='.$_REQUEST['fiCategoryId']));
        
    }
    
    public function delete($id) 
    {
        $row = $this->Hotel_facilitates_model->get_by_id($id);

        if ($row) {
            $this->Hotel_facilitates_model->delete($id);
            $this->session->set_flashdata('message', 'Delete Record Success');
            redirect(site_url('hotel_facilitates/menus/'.$row->fiMenuId.'?categoryid='.$row->fiCategoryId));
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('hotel_facilitates/menus/'.$row->fiMenuId.'?categoryid='.$row->fiCategoryId));
        }
    }

    public function _rules() 
    {
    // $this->form_validation->set_rules('fiMenuId', 'fimenuid', 'trim|required');
    // $this->form_validation->set_rules('fiHotelsId', 'fihotelsid', 'trim|required');
    $this->form_validation->set_rules('fsTitle', 'fstitle', 'trim|required');
    $this->form_validation->set_rules('fsDescription', 'fsdescription', 'trim|required');

    $this->form_validation->set_rules('id', 'id', 'trim');
    $this->form_validation->set_error_delimiters('<span class="text-danger">', '</span>');
    }

}

/* End of file Hotel_facilitates.php */
/* Location: ./application/controllers/Hotel_facilitates.php */
/* Please DO NOT modify this information : */
/* Generated by Harviacode Codeigniter CRUD Generator 2020-03-18 05:35:31 */
/* http://harviacode.com */