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


class Type_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/type_form',$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', 'Type', 'trim|required|is_unique[shop_type.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(
							'page_name' =>  'Type' ,
							'heading' => 'Type' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Type_admin/view') ,
							'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) ,
							  'img' => $this->img ,
							  'order' => $this->input->post('order',TRUE) 
							);
				
				//insert data
				$insert_id = $this->Db_model->insert('shop_type',$data) ;
				
				redirect('Type_admin/view');

			}
			
		}//if($this->input->post('submit'))
		
		else
		{
			
			/**
			*
			*	show form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$data = array(
							'page_name' =>  'Type' ,
							'heading' => 'Type' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Type_admin/view') ,
							'id' => '',
							'order' => '',
							'title' => '',
							'image' => '',
							'str' => ''
							);
				
			$this->_page($data);
		}
		
		
		
	}
	
	/**
	*
	*   Type modification
	*
	*/
	
	public function edit($id='')
	{
	
		$this->id = $this->uri->segment(3,'');//select 3rd field(ie, id) from uri , if not set set default to ''
		
		//retrieve details from database matching to the id
		$details = $this->Db_model->get_by_id('shop_type','id',$this->id);
		
		if($details!==FALSE)
		{
		
		//$this->id =$details->id ;
		$this->order = $details->order ;
		$this->title = $details->title ;
		$this->img = $details->img ;
		}
		
		$data = array(
				'page_name' => 'Type' ,
				'heading' => 'Type - Edit' ,
				'top_link_name' => 'View' ,
				'top_link' => site_url('Type_admin/view') ,
				'id' => $this->id,
				'order' => $this->order,
				'title' => $this->title,
				'image' => $this->img,
				'str' => ''
				);
		
		
		//check if submit button clicked
		if($this->input->post('submit'))
		{
		
			$this->form_validation->set_rules('title', 'Type', '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' => 'Type' ,
							'heading' => 'Type - Edit' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('Type_admin/view') ,
							'id' => $this->id,
							'order' => $this->order,
							'title' => $this->input->post('title',$this->title),
							'image' => $this->img,
							'str' => ''
							);

				
				$this->_page($data);
				
			}
			
			
			else 
			{
								
				$data = array(
							  'title' => $this->input->post('title',TRUE) ,
							  'img' => $this->img ,
							  'order' => $this->input->post('order',TRUE) 
							 );
				
				
				//update data
				$this->Db_model->update('shop_type',$data,array('id'=>$this->id)) ;
				
				redirect('Type_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('shop_type','id','desc') !== FALSE )
		{
			$result_array = $this->Db_model->get_all_by_order('shop_type','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' => 'Type Management' ,
					  'heading' => 'Type Management' ,
					  'top_link_name' => 'Create' ,
					  'top_link' => site_url('Type_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/type_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','shop_type',array('id'=>$this->id)) ;
				
				if($details !== FALSE )
				{
					
					if(  $details[0]['img'] != '' )
					{
						unlink('assets/images/'.$details[0]['img']) ;
					}
				
				}
				
				$this->Db_model->delete('shop_type',array('id'=>$this->id));
				
				//redirect page to hide the id
				redirect('Type_admin/view') ;
	
			}
		
		}
		else
		{
			redirect('Type_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','shop_type',array('id'=>$id)) ;
			
			if($details !== FALSE )
			{
				
				if(  $details[0]['img'] != '' )
				{
					unlink('assets/images/'.$details[0]['img']) ;
				}
			
			}
			
			$this->db->update('shop_type',array('img'=>''),array('id'=>$id)) ;
			
			//redirect page to hide the id
			redirect('Type_admin/edit/'.$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)
			*	return value is an array
			*	if $arr['upload']==1 uploaded , $arr['filename'] = name of file
			*	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;
		}
				
	}
	

	
}//end class

