<?php 
//error_reporting(E_ALL);
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Banner_master extends REF_Controller {
	 public function __construct() {		  
        parent::__construct();
		
		$this->load->library('upload');

        $this->load->model('banner_master_model', '', TRUE);

        $this->uri_key_index = 0;
	
	}

	 public function index() {
        $data = array();

        $data['promotions']	= $this->banner_master_model->get_paged_list()->result();

        if (!empty($this->session->flashdata('success'))) 
            $data['success'] = $this->session->flashdata('success');
        else 
            $data['error'] = $this->session->flashdata('error');        

        $actions 			= $this->actions();

        $data['add_btn']    = 'Create New Item';

        $data['view_link']  = $actions['view'] . '/';

        $data['add_link']   = $actions['add'];

        $data['edit_link']  = $actions['edit'] . '/';

        $data['delete_link']= $actions['delete'] . '/';        

        $service_menu_id = get_master_controller_id($actions['controller']);

        $data['service_menu_id'] = array('service_menu_id' => $service_menu_id, 'menu_type' => '1');

        $this->render($data, 'banners/promotionList');
    }

 

    function _createThumbnail($filename) 
    {
        $config['image_library']    = "gd2";
       $config['source_image']     	= $_SERVER['DOCUMENT_ROOT'].'/demo/kiosk/uploads/display/'.$filename;
        $config['create_thumb']     = TRUE;
        $config['maintain_ratio']   = TRUE;
        $config['width'] 			= "80";
        $config['height'] 			= "80";
		
        $this->load->library('image_lib',$config);
        if(!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }		
    }

	public function add() {
        $data 					= array();		
		$data['title'] 			= 'Add New Display Banner';
        $data['action'] 		= site_url('banner_master/addPromotion');
        $this->render($data, 'banners/promotionAdd');
    }

	function addPromotion()
	{
		$data['title'] 		= 'Add new Promotion';
		
		//echo "<pre>"; print_r($this->input->post()); exit;

		 if($this->input->post('csubmit')!=''){

			$this->load->helper('form');
			
			$dd		= $this->input->post('fromDate');
			
			$ex		= explode('-',$dd);
			
			$dDate1	= $ex[2].'-'.$ex[1].'-'.$ex[0];
			
			$td		= $this->input->post('toDate');
			
			$exd	= explode('-',$td);
			
			$dDate2	= $exd[2].'-'.$exd[1].'-'.$exd[0];
			
			$d		= date('Y-m-d H:s:i'); 
			
			$userr 	= array(			
					'promotion_name'		=> $this->input->post('promotionName'),			
					'from_date'				=> $dDate1,			
					'to_date'				=> $dDate2,
					'user_id'				=> $this->user_data['user_id'],			
					'rstatus'				=> $this->input->post('status'),			
					'rdate'					=> $d,			
					'view_time'				=> $this->input->post('dis_time')			
				);


			 if($_FILES["imagefile"]["name"]!=""){

				 // config for all files in array the same

				 $config['upload_path']   =   $_SERVER['DOCUMENT_ROOT'].'/demo/kiosk/uploads/display/'; 

				 $config['allowed_types'] =   "gif|jpg|jpeg|png"; 

				 $config['max_size']      =   "5000";

				 $config['max_width']     =   "1754";

				 $config['max_height']    =   "800";

				//$tim=time();

				$newFileName = $_FILES['imagefile']['name'];

				$fileExt = array_pop(explode(".", $newFileName));

				$filename = md5(time()).".".$fileExt;

				$filename1=md5(time());

				$config['file_name'] = $filename;
				$this->upload->initialize($config);
				$this->load->library('upload',$config);

				$ti=$_FILES["imagefile"]["name"];

				$tii="imagefile";

				$upload = $this->upload->do_upload($tii);
				
				$this->_createThumbnail($config['file_name']);

				#$data['uploadInfo'] = $finfo;

				$data['thumbnail_name'] = $filename1. '_thumb'.'.'.$fileExt;
				
				$userr['imagefile']				= $filename;			
				$userr['promotion_image_thumb']	= $data['thumbnail_name'];
			 }

			$id = $this->banner_master_model->save($userr);
			$this->session->set_flashdata('success', 'Display banner added successfully'); 
		}
		redirect('banner_master','refresh');
	}
	
	public function edit($id = '') {
        if (empty($id)) {
            show_400_error();
        }        

        $data 					= array();
		$data['person'] 		= $this->banner_master_model->get_by_id($id)->row();
		//echo "<pre>"; print_r($data['person']); echo "</pre>";
		
		$data['title'] 			= 'Update Banner';
		$data['form_action'] 	= site_url('banner_master/updateItem');
        $this->render($data, 'banners/promotionEdit');
    }	
	
	

	function updateItem()
	{
		$data['title'] 			= 'Update Item';
	    if($this->input->post('csubmit') != ''){
			
			$dd		= $this->input->post('fromDate');
			$ex		= explode('-',$dd);
			$dDate1	= $ex[2].'-'.$ex[1].'-'.$ex[0];
			$td		= $this->input->post('toDate');
			$exd	= explode('-',$td);
			$dDate2 = $exd[2].'-'.$exd[1].'-'.$exd[0];
			$id 	= $this->input->post('id');			
			
			$banner	= $this->banner_master_model->get_by_id($id)->row();
			
			$person = array(
				'promotion_name'=>$this->input->post('promotionName'),
				'from_date'=>$dDate1,
				'to_date'=>$dDate2,
				'rstatus'=>$this->input->post('status'),
				'view_time'=>$this->input->post('edi_distime')
			);
			
			if($_FILES["imagefile"]["name"] != ""){
				$config['upload_path']   =   $_SERVER['DOCUMENT_ROOT'].'/demo/kiosk/uploads/display/';
				$config['allowed_types'] =   "gif|jpg|jpeg|png";
				$config['max_size']      =   "5000"; 
				$config['max_width']     =   "1754";
				$config['max_height']    =   "800";	 
				//$tim=time();

				$newFileName 			= $_FILES['imagefile']['name'];
				$fileExt 				= array_pop(explode(".", $newFileName));
				$filename 				= md5(time()).".".$fileExt;
				$filename1				= md5(time());
				$config['file_name'] 	= $filename;
				$this->upload->initialize($config);
				$this->load->library('upload',$config);
				$ti						= $_FILES["imagefile"]["name"];
				$tii					= "imagefile";
				$upload 				= $this->upload->do_upload($tii); 
				
				if($this->upload->display_errors()){
					$this->session->set_flashdata('error', $this->upload->display_errors());
				}
	   			//exit;
				$this->_createThumbnail($config['file_name']);

				$data['thumbnail_name']	= $filename1. '_thumb'.'.'.$fileExt;
								

				$person['imagefile']	 				= $filename;
				$person['promotion_image_thumb']	 	= $data['thumbnail_name'];
				
				if (!empty($banner->imagefile)) {
					if (file_exists($path . $banner->imagefile)) {
						unlink($path . $banner->imagefile);
						unlink($path . $banner->promotion_image_thumb);
					}
				}
			}
			$this->banner_master_model->update($id,$person);
			$this->session->set_flashdata('success', 'Banner details updated successfully'); 
		}
		redirect('banner_master/index/','refresh');
	}

 	public function delete($id = '') {
        if (empty($id)) {
            show_400_error();
        }
		
		$banner	= $this->banner_master_model->get_by_id($id)->row();
		
		$path	= $_SERVER['DOCUMENT_ROOT'].'/demo/kiosk/uploads/display/';
		
		 if ($this->banner_master_model->delete($id) > 0) {
            $this->session->set_flashdata('success', 'Display Banner deleted successfully');
            if (!empty($banner->imagefile)) {
                if (file_exists($path . $banner->imagefile)) {
                    unlink($path . $banner->imagefile);
					unlink($path . $banner->promotion_image_thumb);
                }
            }
        }
        else 
            $this->session->set_flashdata('error', 'Display Banner not deleted!');
		
		
		redirect('banner_master/index/','refresh');
	}
	
	public function view($id = '') {
        if (empty($id)) {
            show_400_error();
        }        

        $data 						= array();
		$data['title'] 				= 'Banner Details';
        $data['promotionperson'] 	= $this->banner_master_model->get_by_id($id)->row();
        $this->render($data, 'banners/promotionView');
    }
}

?>