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


class Banner extends CI_Controller {
	
	private $id				=	''		; 
	private $filename		=	''		;
	private $order			=	''		; 
	private $title			=	''		; 
	private $text			=	''		; 
	private $img			=	''		;
	private $type			=	''		; 
	private $active			=	''		;
	private $error			=	'' 		;
	private $error_count	=	0		;
	private $upload_arr		=	array() ; 

	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('loginfunctions');
		$this->load->helper('security');
		//logged out
		if($this->loginfunctions->checkloggedin()===FALSE)
		{
			redirect('admin/') ;
		}
		else
		{
			$this->load->model('Db_model') ;
			
			$this->load->library(array('form_validation'));
		}
		
	}
	
	public function index()
	{
		$this->view();
	}


	private function _page( $data=array()  )
	{
		
		$this->load->view('admin/about_header',$data);
		$this->load->view('admin/menu');
		//$this->load->view('admin/top_links',$data);
		$this->load->view('admin/banner_content',$data);
		$this->load->view('admin/footer');
		
	}
	
	
	public function create()
	{

		//check if submit button clicked
		if($this->input->post('submit'))
		{
			$this->form_validation->set_rules('link', 'Link', 'trim|required|xss_clean');

			$this->form_validation->set_rules('img', 'Image', 'callback_image_upload');
			

			//if validation errors
			if($this->form_validation->run() === FALSE)
			{
		
				$data = array(
							'page_name' =>  'Banner' ,
							'heading' => 'Banner' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Banner/view') ,
							'id' => '',
							'link' => $this->input->post('link'),
							'image' =>'',
							'status' => '1',
							'str' => ''
							);
				
				$this->_page($data);
				
			}
			
			else
			{
				
					$data = array(
								  
								  'link' => $this->input->post('link',TRUE),
								  'image' => $this->img,
								  'status' => '1'
								);
					
					//insert data
					$insert_id = $this->Db_model->insert('shop_banner',$data) ;
					
					redirect('Banner/view');
			}
			
		}//if($this->input->post('submit'))
		
		else
		{
			
			/**
			*
			*	show form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$data = array(
							'page_name' =>  'Banner' ,
							'heading' => 'Banner' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Banner/view') ,
							'id' => '',
							'link' => '',
							'image' =>'',
							'status' => '1',
							'str' => ''
							);
				
			$this->_page($data);
		}

		
	}
	
	/**
	*
	*   Services modification
	*
	*/
	
	public function edit($id='')
	{

		$this->id = $this->uri->segment(3,'');
		
		//retrieve details from database matching to the id
		$details = $this->Db_model->get_where('shop_banner',array('id'=>$this->id));
		
		if($details!==FALSE)
		{
		
		//$this->aid = $details[0]['aid'] ;
		$this->link = $details[0]['link'] ;
		$this->img = $details[0]['image'] ;
		$this->status = $details[0]['status'] ;

		}
		
		$data = array(
				'page_name' => 'Banner' ,
				'heading' => 'Banner - Edit' ,
				'top_link_name' => 'View' ,
				'top_link' => site_url('Banner/view') ,
				'id' => $this->id,
				'link' => $this->link,
				'image' => $this->img,
				//'active' => 'Y',
				'str' => ''
				);
		
		
		//check if submit button clicked
		if($this->input->post('submit'))
		{
			
			$this->form_validation->set_rules('link', 'Link', 'trim|required|xss_clean');
			
			$this->form_validation->set_rules('img', 'Image', 'callback_image_upload');


			//if validation errors
			if ($this->form_validation->run() === FALSE)
			{
				
				$data = array(
							'page_name' => 'Banner' ,
							'heading' => 'Banner - Edit' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Banner/view') ,
							'id' => $this->id,
							'link' => $this->input->post('link'),
							'image' => $this->img,
							//'active' => 'Y',
							'str' => ''
							);
				
			
				$this->_page($data);
				
			}
			else
			{
				
				$data = array(
							 
							  'link' => $this->input->post('link',TRUE) ,
							  'image' => $this->img ,
							  );
				
			
				
				//update data
				$this->Db_model->update('shop_banner',$data,array('id'=>$this->id)) ;
				
				redirect('Banner/view');
			}
		
		}//if($this->input->post('submit'))
		
		else
		{
			/**
			*
			*	show form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$this->_page($data);
		}
		
			
	

	}
	
	
	public function view()
	{
		
		$result_array = array() ;
		
	
		if( $this->Db_model->get_all_by_order('shop_banner','id','desc') !== FALSE )
		{
			$result_array = $this->Db_model->get_all_by_order('shop_banner','id','desc') ;
		}
		
		/***
		*
		*	set uniq_id field & store it in session to make security while deletion
		*
		*/
		
		$uniq_id = uniqid();
		
		$this->load->library('session');
		
		$this->session->set_userdata('uniq_id',$uniq_id);
		
		$data=  array(
					  
					  'page_name' => 'Banner Management' ,
					  'heading' => 'Banner Management' ,
					  'top_link_name' => 'Create' ,
					  'top_link' => site_url('Banner/create') ,
					  'result_array' => $result_array,
					  'uniq_id' => $uniq_id
					 );
	
		
		$this->load->view('admin/home_header',$data);
		$this->load->view('admin/menu');
		//$this->load->view('admin/top_links',$data);
		$this->load->view('admin/banner_view',$data);
		$this->load->view('admin/footer');
		
	}
	
	
	public function delete($id='',$del=0,$uniq_id='')
	{
		$this->load->library('session');
		
		$uniq_id_uri = $this->uri->segment(5,'') ;
		
		//delete iff session matches with uniq_id in uri
		if($this->session->userdata('uniq_id') == $uniq_id_uri)
		{
			
			if ($this->uri->segment(4,'')==1)
			{	
			
				$this->id = $this->uri->segment(3,'');
				
				$details = $this->Db_model->get_field('image','shop_banner',array('id'=>$this->id)) ;
				
				if($details !== FALSE )
				{
					
					if(  $details[0]['image'] != '' )
					{
						unlink('assets/images/'.$details[0]['image']) ;
					}
				
				}
				
				$this->Db_model->delete('shop_banner',array('id'=>$this->id));
				
				//redirect page to hide the id
				redirect('Banner/view') ;
	
			}
			
		}
		else
		{
			redirect('Banner/view') ;
		}
			
	}
	
	
	
	public function image_delete($id='',$del=0)
	{
			
		if ($this->uri->segment(4,'')==1)
		{	
		
			$this->id = $this->uri->segment(3,'');
			
			$details = $this->Db_model->get_field('image','shop_banner',array('id'=>$this->id)) ;
			
			if($details !== FALSE )
			{
				
				if(  $details[0]['image'] != '' )
				{
					unlink('assets/images/'.$details[0]['image']) ;
				}
			
			}
			
			$this->db->update('shop_banner',array('image'=>''),array('id'=>$this->id)) ;
			
			//redirect page to hide the id
			redirect('Banner/edit/'.$this->id);


		}
			
	}
	
	
	
	public function image_upload()
	{

		if (isset($_FILES['img']['name']) && !empty($_FILES['img']['name'])) 
		{
							
			$this->load->library('single_file_upload') ;
			
			/**
			*
			*	my do_upload method : do_upload(field_name,max_size,max_width,max_height,$thumb)
			*	return value is an array
			*	if $arr['upload']==1 uploaded , $arr['filename'] = name of file,$arr[''thumb'] = thumb filename
			*	if $arr['upload']==0 not uploaded , $arr['errors'] = upload errors
			*
			*/
			
			$this->upload_arr = $this->single_file_upload->do_upload('img') ;
			
			if($this->upload_arr['upload']==1)
			{ 
				$this->img = $this->upload_arr['filename'] ;
				return true;
			}
			
			else
			{
				$this->form_validation->set_message('image_upload', $this->upload_arr['errors'] );
        		return false;
			}
			
		}
		else
		{
			return true;
		}
				
	}
	
	
	public function record_not_exist()
	{
		if($this->Db_model->get_where( 'shop_pages', array('type'=>'service','title'=> $this->input->post('title'))) === FALSE)
		{
			return true;
		}
		else
		{
			$this->form_validation->set_message('record_not_exist', 'Title name exists.');
			
			return false;
		}
	}
	
	
	
	
	


	

	
}//end class Service_admin

