<?php

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

class Programe extends CI_Controller 
{
	 function __construct() 
	 {
        parent::__construct();
        $this->load->database();
        $this->load->model('User_Model');
        $this->load->model('Common');
         $x = $this->session->userdata('logged');
         if ($x['email'] == '') 
         {
                return redirect('Welcome/index', 'refresh');
          }
    }

    public function index()
    {
        $page =(isset($_GET['page'])) ? $_GET['page'] : 0;
        $query=$this->db->select('programe.*')->from('programe')->order_by('id','desc')->limit(20,($page*20))->get();
        $num_rows=$this->db->order_by('id', 'desc')->get('programe')->num_rows();
        $data['programe']=$query->result_array();
        $data['links']=$this->pagi->pagination1('admin/programe',$num_rows,20);
        $this->load->view('programe/index',$data);
    }

    public function add()
    {
        if ($this->input->server('REQUEST_METHOD') == 'POST') 
        {
         $this->load->helper(array('form'));
         $this->load->library('form_validation');
         $this->form_validation->set_rules('tittle', 'Title', 'required'); 
         $this->form_validation->set_rules('shortdesc', 'Short Desc', 'required'); 
         $this->form_validation->set_rules('package', 'package', 'required');
         $this->form_validation->set_rules('venue', 'venue', 'required'); 
         $this->form_validation->set_rules('price', 'price', 'required'); 
         $this->form_validation->set_rules('terms', 'terms', 'required'); 
        $this->form_validation->set_rules('seat', 'seat', 'required'); 


            if ($this->form_validation->run() == FALSE) 
             { 
                 $this->load->view('programe/add');
             } 
             else 
             { 
                $journalName = str_replace(' ', '_', $_FILES['image']['name']);
                $config['file_name'] = time() . $journalName;
                $config['upload_path'] = './uploads/programe/';
                $config['allowed_types'] = 'gif|jpg|png|JPG|JPEG|jpeg';
                $this->upload->initialize($config);
                $this->upload->do_upload('image');

            $dataInfo = array();
        // If file upload form submitted
            $files= $_FILES;
                    $cpt = count($_FILES['slider']['name']);
                if ($cpt > 0 && $_FILES['slider']['name'][0]!='') {
            for($i = 0; $i < $cpt; $i++){
                  $journal = str_replace(' ', '_', $files['slider']['name'][$i]);
                        $_FILES['slider']['name'] = time() . $journal;
                        $_FILES['slider']['type'] = $files['slider']['type'][$i];
                        $_FILES['slider']['tmp_name'] = $files['slider']['tmp_name'][$i];
                        $_FILES['slider']['error'] = $files['slider']['error'][$i];
                        $_FILES['slider']['size'] = $files['slider']['size'][$i];
               $this->upload->initialize($this->set_upload_options());
                        $this->upload->do_upload('slider');
                        $dataInfo[] = $_FILES['slider']['name'];
                    }
                    $_POST['slider'] = json_encode($dataInfo);
                }
          
    



               
                $_POST['image'] = $config['file_name'];
                if($this->db->insert('programe',$_POST))
                {
                    $this->session->set_flashdata('msg', 'programe Successfully Added');
                    return redirect(base_url('admin/programe'),'refresh');
                }
                else
                {
                    $this->session->set_flashdata('err', 'Please try Again After SomeTimes');
                    return redirect(base_url('admin/programe/add'),'refresh');
                }
             } 
        }
        else
        {
            $this->load->view('programe/add');
        }
    }

    public function edit($id='')
    {
        if ($this->input->server('REQUEST_METHOD') == 'POST') 
        {
         $this->load->helper(array('form'));
         $this->load->library('form_validation');
         $this->form_validation->set_rules('tittle', 'Title', 'required'); 
         $this->form_validation->set_rules('shortdesc', 'Short Desc', 'required'); 
         $this->form_validation->set_rules('package', 'package', 'required');
         $this->form_validation->set_rules('venue', 'venue', 'required'); 
         $this->form_validation->set_rules('price', 'price', 'required'); 
         $this->form_validation->set_rules('terms', 'terms', 'required'); 
         $this->form_validation->set_rules('seat', 'seat', 'required'); 
            if ($this->form_validation->run() == FALSE) 
             { 
                 $data['programe']=$this->db->where('id',$id)->get('programe')->row_array();
                 $this->load->view('programe/edit',$data);
             } 
             else 
             { 
                if($_FILES['image']['name']!='')
                {
                     $journalName = str_replace(' ', '_', $_FILES['image']['name']);
                $config['file_name'] = time() . $journalName;
                $config['upload_path'] = './uploads/programe/';
                $config['allowed_types'] = 'gif|jpg|png|JPG|JPEG|jpeg';
                $this->upload->initialize($config);
                $this->upload->do_upload('image');
  $dataInfo = array();
        // If file upload form submitted
            $files= $_FILES;
                    $cpt = count($_FILES['slider']['name']);
                if ($cpt > 0 && $_FILES['slider']['name'][0]!='') {
            for($i = 0; $i < $cpt; $i++){
                  $journal = str_replace(' ', '_', $files['slider']['name'][$i]);
                        $_FILES['slider']['name'] = time() . $journal;
                        $_FILES['slider']['type'] = $files['slider']['type'][$i];
                        $_FILES['slider']['tmp_name'] = $files['slider']['tmp_name'][$i];
                        $_FILES['slider']['error'] = $files['slider']['error'][$i];
                        $_FILES['slider']['size'] = $files['slider']['size'][$i];
               $this->upload->initialize($this->set_upload_options());
                        $this->upload->do_upload('slider');
                        $dataInfo[] = $_FILES['slider']['name'];
                    }
                    $_POST['slider'] = json_encode($dataInfo);
                }
          
                $_POST['image'] = $config['file_name'];
                }
                if($this->db->where('id',$id)->update('programe',$_POST))
                {
                    $this->session->set_flashdata('msg', 'programe Successfully Added');
                    return redirect(base_url('admin/programe'),'refresh');
                }
                else
                {
                    $this->session->set_flashdata('err', 'Please try Again After SomeTimes');
                    return redirect(base_url('admin/programe/edit/'.$id),'refresh');
                }
             }
        }
        else
        {
            $data['programe']=$this->db->where('id',$id)->get('programe')->row_array();
            $this->load->view('programe/edit',$data);
        }
    }

    public function view($id='')
    {
         $data['programe']=$this->db->where('id',$id)->get('programe')->row_array();
         $this->load->view('programe/view',$data);
    }

    public function delete($id='')
    {
        if($this->db->where('id',$id)->delete('programe'))
        {
            $this->session->set_flashdata('msg', 'Success Deleted programe');
        }
        else
        {
            $this->session->set_flashdata('err', 'Please try After Sometimes...');
        }
        return redirect(base_url('admin/programe'),'refresh');
    }
private function set_upload_options()
{   
    //upload an image options
    $config = array();
    $config['upload_path'] = './uploads/programe/';
    $config['allowed_types'] = 'gif|jpg|png|JPG|JPEG|jpeg';
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE;

    return $config;
}
}




?>