<?php

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

class Facilities extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Facilities_model');
        $this->load->library('form_validation');
    }


    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;
        }
    }  


// Ajax to Category and Sub category Menge (Controller, Vivew,)
    public function ajax_category() {
        $query = $this->db->select('*')->from('category')->where('category.fiMenuId', $this->input->post('MenuId'))->get();
        $categoryLists = $query->result_array(); 
        ?>
         <option>Select Category</option> 
         <?php
            if(isset($categoryLists)){
                  foreach($categoryLists as $categorylistvalue){
                ?>
                <option value="<?php echo $categorylistvalue['id'] ?>" <?php echo $categorylistvalue['id']=='fiMenuId'?'selected':''; ?>>  <?php echo $categorylistvalue['fsTitle'] ?></option>
                <!-- <option value="<?php echo $categorylistvalue['id'] ?>"> <?php echo $categorylistvalue['fsTitle'] ?></option> -->
                <?php

                  }
            }
                                
    }  

// Ajax to Category and Sub category Menge End


    public function index()
    {
        $q = urldecode($this->input->get('q', TRUE));
        $start = intval($this->input->get('start'));
        
        if ($q <> '') {
            $config['base_url'] = base_url() . 'facilities/?q=' . urlencode($q);
            $config['first_url'] = base_url() . 'facilities/?q=' . urlencode($q);
        } else {
            $config['base_url'] = base_url() . 'facilities/';
            $config['first_url'] = base_url() . 'facilities/';
        }

        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['total_rows'] = $this->Facilities_model->total_rows($q);
        $facilities = $this->Facilities_model->get_limit_data($config['per_page'], $start, $q);

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

        $data = array(
            'facilities_data' => $facilities,
            'q' => $q,
            'pagination' => $this->pagination->create_links(),
            'total_rows' => $config['total_rows'],
            'start' => $start,
        );

        $pageData = array(
            'header'=>array('pageTitle'=>'Facilities'),
            'body'=>$data,
            'footer'=>array(),
        );
        loadView('facilities/facilities_list',$pageData);
    }


    public function create() 
    {
        $data = array(
            'button' => 'Create',
            'action' => site_url('facilities/create_action'),
        'id' => set_value('id'),
        'fiMenuId' => set_value('fiMenuId'),
        'fiCategoryId' => set_value('fiCategoryId'),
        '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'),
    );

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

        // $query = $this->db->select('*')->from('category')->get();
        // $data['categoryLists'] = $query->result_array(); 

        $pageData = array(
            'header'=>array('pageTitle'=>'Facilities create'),
            'body'=>$data,
            'footer'=>array(),
        );
        loadView('facilities/facilities_form',$pageData);

    }
    
    public function create_action() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->create();
        } else {
            $data = array(
        'fiMenuId' => $this->input->post('fiMenuId',TRUE),
        'fiCategoryId' => $this->input->post('fiCategoryId',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),
        'fdCreateDate' => date('Y-m-d H:i:s'),
        );

            // 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->Facilities_model->insert($data);
            $this->session->set_flashdata('message', 'Create Record Success');
            redirect(site_url('facilities'));
        }
    }
    
    public function update($id) 
    {
        $row = $this->Facilities_model->get_by_id($id);

        if ($row) {
            $data = array(
                'button' => 'Update',
                'action' => site_url('facilities/update_action'),
        'id' => set_value('id', $row->id),
        'fiMenuId' => set_value('id', $row->fiMenuId),
        'fiCategoryId' => set_value('id', $row->fiCategoryId),
        '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),
        );

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

            // $query = $this->db->select('*')->from('category')->get();
            // $data['categoryLists'] = $query->result_array(); 

             $pageData = array(
                'header'=>array('pageTitle'=>'Facilities update'),
                'body'=>$data,
                'footer'=>array(),
            );
            loadView('facilities/facilities_form',$pageData);
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('facilities'));
        }
    }
    
    public function update_action() 
    {
        $this->_rules();

        if ($this->form_validation->run() == FALSE) {
            $this->update($this->input->post('id', TRUE));
        } else {
            $data = array(
        'fiMenuId' => $this->input->post('fiMenuId',TRUE),
        'fiCategoryId' => $this->input->post('fiCategoryId',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),
        'fdUpdateDate' => date('Y-m-d H:i:s'),
        );

            // 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
            

            $this->Facilities_model->update($this->input->post('id', TRUE), $data);
            $this->session->set_flashdata('message', 'Update Record Success');
            redirect(site_url('facilities'));
        }
    }
    
    public function delete($id) 
    {
        $row = $this->Facilities_model->get_by_id($id);

        if ($row) {
            $this->Facilities_model->delete($id);
            $this->session->set_flashdata('message', 'Delete Record Success');
            redirect(site_url('facilities'));
        } else {
            $this->session->set_flashdata('message', 'Record Not Found');
            redirect(site_url('facilities'));
        }
    }

    public function _rules() 
    {
    $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 facilities.php */
/* Location: ./application/controllers/facilities.php */
/* Please DO NOT modify this information : */
/* Generated by Harviacode Codeigniter CRUD Generator 2020-03-13 10:38:38 */
/* http://harviacode.com */