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

class Side_event extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('common_model');
		$this->load->model('users_model');
		if(get_cookie('admin_id'))
		{
			$this->session->set_userdata('admin_id',get_cookie('admin_id'));
		}
		if(!$this->session->userdata('admin_id'))
		{
			redirect('admin/login');
		}
	}
	public function index()
	{
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['events'] = $this->common_model->getAllMultipleJoin('side_events','','','side_events.created DESC');
		//echo $this->db->last_query();die;
		$data['content'] = $this->load->view('admin/side_events/list', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function delete($enc_userID='')
	{
		if($enc_userID!='')
		{
			$eventId=base64_decode($enc_userID);
			$this->common_model->deleteData('side_events',array('id'=>$eventId));
			$this->common_model->deleteData('side_events_lang_values',array('side_event_id'=>$eventId));
			$this->common_model->deleteData('side_events_to_event',array('side_event_id'=>$eventId));
			$this->utility->setMsg('Side event deleted','SUCCESS');	
			redirect(base_url().'admin/side_event');
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/side_event');
		}
	}
	public function visibility($enc_userID='',$status='')
	{
		if($enc_userID!='')
		{
			$eventId=base64_decode($enc_userID);
			if($status==1)
			{
				$newstatus = 1;
			}
			else
			{
				$newstatus = 0;
			}
			$this->common_model->UpdateData('side_events',array('is_visible'=>$newstatus),array('id'=>$eventId));
			$this->utility->setMsg('Side event status changed','SUCCESS');	
			redirect(base_url().'admin/side_event');
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/side_event');
		}
	}
	public function add()
	{
		if($this->input->post())
		{
			$filename = '';
			if($_FILES['image']['name']!='')
			{
				$imagefolder = 'side_events/';
		        $uploadpath = 'assets/userfiles/'. $imagefolder;
				$config = array(
		            'upload_path' 		=> FCPATH . $uploadpath . '/',
		            'allowed_types' 	=> 'JPEG|jpeg|JPG|jpg|PNG|png',
		            'encrypt_name' 		=> false,
		            'file_ext_tolower' 	=> true,
		            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
		        );

		        $this->load->library('upload', $config);
		        $this->upload->initialize($config);
		        if(!$this->upload->do_upload('image')) {

		            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
					redirect(base_url().'admin/side_event/add/');
		        }
		        $uploaddata = $this->upload->data();
		        $this->utility->pic_resize($uploadpath.$uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1),$uploadpath.$uploaddata['raw_name'].'_thumb'.'.'.substr($uploaddata['file_ext'], 1),'275','160'); 
				$filename = $uploadpath.$uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1);
			}
			$evtArr = array(
							'title'=>$this->input->post('title_2'),
							'sub_title'=>$this->input->post('sub_title2'),
							'description'=>$this->input->post('description2'),
							'created'=>time(),
							'is_visible'=>1,
							'image'=>$filename,
							'link'=>$this->input->post('link')
				);//print_r($evtArr); die;
			$evtId = $this->common_model->insertData('side_events',$evtArr);
			$languages = $this->common_model->getAll('sys_languages');
			$arr= array();
			if(!empty($languages))
			{
				foreach ($languages as $value) {
					$arr['side_event_id'] = $evtId;
					$arr['lang_id'] = $value['id'];
					$arr['title'] = $this->input->post('title_'.$value['id']);
					$arr['sub_title'] = $this->input->post('sub_title'.$value['id']);
					$arr['description'] = $this->input->post('description'.$value['id']);
					$instLngVal = $this->common_model->insertData('side_events_lang_values',$arr);
				}
			}
			if($this->input->post('event_id'))
			{
				foreach ($this->input->post('event_id') as $value) {
					$arr['side_event_id'] = $evtId;
					$arr['event_id'] = $value;
					$instLngVal = $this->common_model->insertData('side_events_to_event',$arr);
				}
			}
			redirect(base_url().'admin/side_event');
		}
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$content_data['languages'] = $this->common_model->getAll('sys_languages') ;
		$content_data['locations'] = $this->common_model->getAll('loc_locations',array('name !='=>''),'','name ASC') ;
		$content_data['events'] = $this->common_model->getAll('evt_events',array('visible'=>1,'time_from >'=>time())) ;
		//pr($content_data);
		$data['content'] = $this->load->view('admin/side_events/add', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function edit($enc_userID='')
	{
		if($enc_userID!='')
		{
			$evtId=base64_decode($enc_userID);
			if($this->input->post())
			{
				$filename = '';
				if($_FILES['image']['name']!='')
				{
					$imagefolder = 'side_events/';
			        $uploadpath = 'assets/userfiles/'. $imagefolder;
					$config = array(
			            'upload_path' 		=> FCPATH . $uploadpath . '/',
			            'allowed_types' 	=> 'JPEG|jpeg|JPG|jpg|PNG|png',
			            'encrypt_name' 		=> false,
			            'file_ext_tolower' 	=> true,
			            //'max_size' 			=> (int)MSysSettings::getValue('upload_max_filesize') * 1024,
			        );

			        $this->load->library('upload', $config);
			        $this->upload->initialize($config);
			        if(!$this->upload->do_upload('image')) {

			            $this->utility->setMsg($this->upload->display_errors(),'ERROR');	
						redirect(base_url().'admin/side_event/edit/'.$enc_userID);
			        }
			        $uploaddata = $this->upload->data();
			        $this->utility->pic_resize($uploadpath.$uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1),$uploadpath.$uploaddata['raw_name'].'_thumb'.'.'.substr($uploaddata['file_ext'], 1),'275','160'); 
					$filename = $uploadpath.$uploaddata['raw_name'].'.'.substr($uploaddata['file_ext'], 1);
				}
				$evtArr = array(
								'title'=>$this->input->post('title_2'),
								'sub_title'=>$this->input->post('sub_title2'),
								'description'=>$this->input->post('description2'),
								'link'=>$this->input->post('link')
					);//print_r($evtArr); die;
				if($filename)
				{
					$evtArr = array_merge(array('image'=>$filename),$evtArr);
				}
				$this->common_model->UpdateData('side_events',$evtArr,array('id'=>$evtId));
				$languages = $this->common_model->getAll('sys_languages');
				$arr= array();
				if(!empty($languages))
				{
					foreach ($languages as $value) {
						if($this->input->post('title_'.$value['id']))
						{
							$this->common_model->deleteData('side_events_lang_values',array('side_event_id'=>$evtId,'lang_id'=>$value['id']));
							$arr['side_event_id'] = $evtId;
							$arr['lang_id'] = $value['id'];
							$arr['title'] = $this->input->post('title_'.$value['id']);
							$arr['sub_title'] = $this->input->post('sub_title'.$value['id']);
							$arr['description'] = $this->input->post('description'.$value['id']);
							$instLngVal = $this->common_model->insertData('side_events_lang_values',$arr);
						}
					}
				}
				if($this->input->post('event_id'))
				{
					$this->common_model->deleteData('side_events_to_event',array('side_event_id'=>$evtId));
					
					foreach ($this->input->post('event_id') as $value) {
						$arrEvent['side_event_id'] = $evtId;
						$arrEvent['event_id'] = $value;
						$instLngVal = $this->common_model->insertData('side_events_to_event',$arrEvent);
					}
				}
				redirect(base_url().'admin/side_event/edit/'.$enc_userID);
			}
			$content_data = array();
			$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
			$content_data['languages'] = $this->common_model->getAll('sys_languages') ;
			$content_data['events'] = $this->common_model->getAll('evt_events',array('visible'=>1,'time_from >'=>time())) ;
			$content_data['eventDetail'] = $this->common_model->getAllRowMultipleJoin('side_events',array('side_events.id'=>$evtId));
			$evts = array();
			if($content_data['eventDetail'])
			{
				$events = $this->common_model->getAll('side_events_to_event',array('side_event_id'=>$content_data['eventDetail']['id'])) ;
				if($events)
				{
					foreach ($events as $key => $value) {
						array_push($evts, $value['event_id']);
					}
				}
				$content_data['eventDetail']['events'] = $evts;
			}
			//pr($content_data['eventDetail']); die;
			$data['content'] = $this->load->view('admin/side_events/edit', $content_data, TRUE);
			$this->load->view('admin/layout', $data);
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/side_event');
		}
	}
}