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


class About_admin 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');
		
		//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/about_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('title', 'Title', 'trim|required|callback_record_not_exist');
			$this->form_validation->set_rules('order', 'Order', 'trim|required|numeric');
			//$this->form_validation->set_rules('text', 'Description', '');
			$this->form_validation->set_rules('img', 'Image', 'callback_image_upload');
			

			//if validation errors
			if($this->form_validation->run() === FALSE)
			{
		
				$data = array(
							'page_name' =>  'Delivery Area' ,
							'heading' => 'Delivery Area' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('About_admin/view') ,
							'id' => '',
							'type' => 'delivery'   ,
							'order' => $this->input->post('order'),
							'title' => $this->input->post('title'),
							//'text' => $this->input->post('text'),
							'img' =>'',
							'active' => 'Y',
							'str' => ''
							);
				
				$this->_page($data);
				
			}
			
			else
			{
				
					$data = array(
								  'type' => 'delivery' ,
								  'order' => $this->input->post('order',TRUE),
								  'title' => $this->input->post('title',TRUE),
								 // 'text' => $this->input->post('text',TRUE),
								  'img' => $this->img,
								  'active' => 'Y'
								);
					
					//insert data
					$insert_id = $this->Db_model->insert('delivery',$data) ;
					
					redirect('About_admin/view');
			}
			
		}//if($this->input->post('submit'))
		
		else
		{
			
			/**
			*
			*	show form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$data = array(
							'page_name' =>  'Delivery Area' ,
							'heading' => 'Delivery Area' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('About_admin/view') ,
							'id' => '',
							'type' => 'delivery' ,
							'order' => '',
							'title' => '',
							'text' => '',
							'img' =>'',
							'active' => 'Y',
							'str' => ''
							);
				
			$this->_page($data);
		}

		
	}
	
	/**
	*
	*   About Us 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('delivery',array('aid'=>$this->id,'type'=>'delivery'));
		
		if($details!==FALSE)
		{
		
		//$this->aid = $details[0]['aid'] ;
		$this->order = $details[0]['order'] ;
		$this->title = $details[0]['title'] ;
		//$this->text = $details[0]['text'] ;
		//$this->img = $details[0]['img'] ;
		$this->type = $details[0]['type'] ;
		$this->active = $details[0]['active'] ;

		}
		
		$data = array(
				'page_name' => 'Delivery Area' ,
				'heading' => 'Delivery Area - Edit' ,
				'top_link_name' => 'View' ,
				'top_link' => site_url('About_admin/view') ,
				'id' => $this->id,
				'type' => 'about' ,
				'order' => $this->order,
				'title' => $this->title,
				'text' => $this->text,
				'img' => $this->img,
				//'active' => 'Y',
				'str' => ''
				);
		
		
		//check if submit button clicked
		if($this->input->post('submit'))
		{
			
			$this->form_validation->set_rules('title', 'Title', 'trim|required');
			$this->form_validation->set_rules('order', 'Order', 'trim|required|numeric');
			//$this->form_validation->set_rules('text', 'Description', 'trim|required');
			//$this->form_validation->set_rules('img', 'Image', 'callback_image_upload');


			//if validation errors
			if ($this->form_validation->run() === FALSE)
			{
				
				$data = array(
							'page_name' => 'Delivery Area' ,
							'heading' => 'Delivery Area - Edit' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('About_admin/view') ,
							'id' => $this->id,
							'type' => 'delivery' ,
							'order' => $this->input->post('order'),
							'title' => $this->input->post('title'),
							'text' => $this->input->post('text'),
							'img' => $this->img,
							//'active' => 'Y',
							'str' => ''
							);
				
			
				$this->_page($data);
				
			}
			else
			{
				
				$data = array(
							  'type' => 'delivery' ,
							  'title' => $this->input->post('title',TRUE) ,
							  'text' => $this->input->post('text',TRUE) ,
							  'order' => $this->input->post('order',TRUE) ,
							  'img' => $this->img ,
							  );
				
			
				
				//update data
				$this->Db_model->update('delivery',$data,array('aid'=>$this->id)) ;
				
				redirect('About_admin/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() ;
		
		$this->load->model('Db_model') ;

		if( $this->Db_model->get_where_by_order('delivery',array('type'=>'delivery'),'aid','desc') !== FALSE )
		{
			$result_array = $this->Db_model->get_where_by_order('delivery',array('type'=>'delivery'),'aid','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(
					  'type'=>'about',
					  'page_name' => 'Delivery Area Management' ,
					  'heading' => 'Delivery Area Management' ,
					  'top_link_name' => 'Create' ,
					  'top_link' => site_url('About_admin/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/about_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('img','delivery',array('aid'=>$this->id)) ;
				
				if($details !== FALSE )
				{
					
					if(  $details[0]['img'] != '' )
					{
						unlink('assets/images/'.$details[0]['img']) ;
					}
				
				}
				
				$this->Db_model->delete('delivery',array('aid'=>$this->id));
				
				//redirect page to hide the id
				redirect('About_admin/view') ;
	
	
			}
		
		}
		else
		{
			redirect('About_admin/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('img','delivery',array('aid'=>$this->id)) ;
			
			if($details !== FALSE )
			{
				
				if(  $details[0]['img'] != '' )
				{
					unlink('assets/images/'.$details[0]['img']) ;
				}
			
			}
			
			$this->db->update('delivery',array('img'=>''),array('aid'=>$this->id)) ;
			
			//redirect page to hide the id
			redirect('About_admin/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( 'delivery', array('type'=>'delivery','title'=> $this->input->post('title'))) === FALSE)
		{
			return true;
		}
		else
		{
			$this->form_validation->set_message('record_not_exist', 'Title name exists.');
			
			return false;
		}
	}
	
	
	
	
	


	

	
}//end class About_admin

