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


class City_admin extends CI_Controller {
	
	private $id				=	''		; 
	private $order			=	''		; 
	private $title			=	''		; 
	private $error			=	'' 		;
	//private $img			= 	'' 		;
	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/home_header',$data);
		$this->load->view('admin/menu');
		//$this->load->view('admin/top_links',$data);
		$this->load->view('admin/city_form',$data);
		$this->load->view('admin/footer');
		
	}
	
	
	public function create()
	{
		$dist_result= array();
		
		if($this->Db_model->get_where_by_order('districts',array('state_id'=>'18'),'name','asc') !== FALSE )
		$dist_result = $this->Db_model->get_where_by_order('districts',array('state_id'=>'18'),'name','asc') ;
		
		
		
		//check if submit button clicked
		if($this->input->post('submit'))
		{
			$this->form_validation->set_rules('title', 'unit', 'trim|required|is_unique[city.title]');
			$this->form_validation->set_rules('order', 'Order', 'trim|required|numeric');
			//$this->form_validation->set_rules('img', 'Image', 'callback_image_upload');
			
			//if validation errors
			if($this->form_validation->run() === FALSE)
			{
			
				$data = array(
				             'dist_result' => $dist_result,
							'page_name' =>  'City' ,
							'heading' => 'City' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('City_admin/view') ,
							'id' => '',
							'dist_id'=>$this->input->post('dist_id'),
							'order' => $this->input->post('order'),
							'title' => $this->input->post('title'),
				//			'image' => '', 
							'str' => ''
							);
				
				
				$this->_page($data);
				
			}

			else 
			{
								
				$data = array(
							  'title' => $this->input->post('title',TRUE) ,
							  'dist_id' => $this->input->post('dist_id',TRUE) ,
					//		  'img' => $this->img ,
							  'order' => $this->input->post('order',TRUE) 
							);
				
				//insert data
				$insert_id = $this->Db_model->insert('city',$data) ;
				
				redirect('City_admin/view');

			}
			
		}//if($this->input->post('submit'))
		
		else
		{
			
			/**
			*
			*	show form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$data = array(
			
			
			                'dist_result' => $dist_result,
							'page_name' =>  'City' ,
							'heading' => 'City' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('City_admin/view') ,
							'id' => '',
							'order' => '',
							'dist_id' => '',
							'title' => '',
						//	'image' => '',
							'str' => ''
							);
				
			$this->_page($data);
		}
		
		
		
	}
	
	/**
	*
	*   Category modification
	*
	*/
	
	public function edit($id='')
	{
		
		$dist_result= array();
		

		
	
		$this->id = $this->uri->segment(3,'');//select 3rd field(ie, id) from uri , if not set set default to ''
		
		
		if($this->Db_model->get_where_by_order('districts',array('state_id'=>'18'),'name','asc') !== FALSE )
		$dist_result = $this->Db_model->get_where_by_order('districts',array('state_id'=>'18'),'name','asc') ;
		
		//retrieve details from database matching to the id
		$details = $this->Db_model->get_by_id('city','id',$this->id);
		
		if($details!==FALSE)
		{
		
		//$this->id =$details->id ;
		$this->order = $details->order ;
		$this->title = $details->title ;
		$this->dist_id = $details->dist_id ;
		//$this->img = $details->img ;
		}
		
		$data = array(
		
		        
				'page_name' => 'City' ,
				'heading' => 'City - Edit' ,
				'top_link_name' => 'View' ,
				'top_link' => site_url('City_admin/view') ,
				'dist_result' => $dist_result,
				'id' => $this->id,
				'order' => $this->order,
				'title' => $this->title,
				'dist_id' => $this->dist_id,
			//	'image' => $this->img,
				'str' => ''
				);
		
		
		//check if submit button clicked
		if($this->input->post('submit'))
		{
		
			$this->form_validation->set_rules('title', 'unit', 'trim|required');
			$this->form_validation->set_rules('order', 'Order', 'trim|required|numeric');
			//$this->form_validation->set_rules('img', 'Image', 'callback_image_upload');

			//if validation errors
			if ($this->form_validation->run() === FALSE)
			{
				
				$data = array(
							'page_name' => 'City' ,
							'heading' => 'City - Edit' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('City_admin/view') ,
					
							'id' => $this->id,
							'order' => $this->order,
							'title' => $this->input->post('title',$this->title),
					        'dist_id' => $this->dist_id,
				//			'image' => $this->img,
							'str' => ''
							);

				
				$this->_page($data);
				
			}
			
			
			else 
			{
								
				$data = array(
				             
							  'title' => $this->input->post('title',TRUE) ,
							  'dist_id' => $this->input->post('dist_id',TRUE) ,
					//		  'img' => $this->img ,
							  'order' => $this->input->post('order',TRUE) 
							 );
				
				
				//update data
				$this->Db_model->update('city',$data,array('id'=>$this->id)) ;
				
				redirect('City_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() ;
			
		if( $this->Db_model->get_all_by_order('city','id','desc') !== FALSE )
		{
			$result_array = $this->Db_model->get_all_by_order('city','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' => 'City Management' ,
					  'heading' => 'City Management' ,
					  'top_link_name' => 'Create' ,
					  'top_link' => site_url('City_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/city_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)
			{	
				//delete record on setting 3rd segment, ie: id of row
				$this->id = $this->uri->segment(3,'');
				
				/*$details = $this->Db_model->get_field('img','city',array('id'=>$this->id)) ;
				
				if($details !== FALSE )
				{
					
					if(  $details[0]['img'] != '' )
					{
						unlink('assets/images/'.$details[0]['img']) ;
					}
				
				}
				*/
				$this->Db_model->delete('city',array('id'=>$this->id));
				
				//redirect page to hide the id
				redirect('City_admin/view') ;
	
			}
		
		}
		else
		{
			redirect('City_admin/view') ;
		}
			
	}
	
	



	
}//end class

