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


class Subcategory_admin extends CI_Controller {
	
	private $id				=	''		; 
	private $category_id	=	''		; 
	private $title			=	''		; 
	private $error			=	'' 		;

	
	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/subcategory_form',$data);
		$this->load->view('admin/footer');
		
	}
	
	
	public function create()
	{
					
		if($this->db_model->get_all_by_order('shop_category','title','asc') === FALSE )
		$category_result = array() ;
		
		else
		$category_result = $this->db_model->get_all_by_order('shop_category','title','asc') ;
		
				
		//check if submit button clicked
		if($this->input->post('submit'))
		{
			$this->form_validation->set_rules('category_id', 'Category', 'trim|required|numeric');
			$this->form_validation->set_rules('title', 'Subcategory', 'trim|required|is_unique[shop_subcategory.title]');
			
			//if validation errors
			if($this->form_validation->run() === FALSE)
			{
				
		
				$data = array(
							'page_name' =>  ' Subcategory' ,
							'heading' => ' Subcategory' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('subcategory_admin/view') ,
							'category_id' => $this->input->post('category_id'),
							'category_result' => $category_result,
							'title' => $this->input->post('title'),
							'str' => ''
							);
				
				$this->_page($data);
				
			}

			else 
			{
								
				$data = array(
							  'category_id' => $this->input->post('category_id',TRUE) ,
							  'title' => $this->input->post('title',TRUE) 
							);
				
				//insert data
				$insert_id = $this->db_model->insert('shop_subcategory',$data) ;
				
				redirect('subcategory_admin/view');

			}
			
		}//if($this->input->post('submit'))
		
		else
		{
			
			/**
			*
			*	show form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$data = array(
							'page_name' =>  ' Subcategory' ,
							'heading' => ' Subcategory' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('subcategory_admin/view') ,
							'category_id' => '',
							'category_result' => $category_result,
							'title' => '',
							'str' => ''
							);
				
			$this->_page($data);
		}
		
	
		
	}
	
	/**
	*
	*   Subcategory 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 row from database matching to the id
		$details = $this->db_model->get_by_id('shop_subcategory','id',$this->id);
		
		if($details!==FALSE)
		{
		
		//$this->id =  $details->id ;
		$this->category_id = $details->category_id ;
		$this->title = $details->title ;
		
		}
		
		if($this->db_model->get_all_by_order('shop_category','title','asc') === FALSE )
		{
			$category_result = array() ;
		}
		else
		{
			$category_result = $this->db_model->get_all_by_order('shop_category','title','asc') ;
		}
		
		$data = array(
				'page_name' => ' Subcategory' ,
				'heading' => ' Subcategory - Edit' ,
				'top_link_name' => 'View' ,
				'top_link' => site_url('subcategory_admin/view') ,
				'category_result' => $category_result,
				'id' => $this->id,
				'category_id' => $this->category_id,
				'title' => $this->title,
				'str' => ''
				);
		
		
		//check if submit button clicked
		if($this->input->post('submit'))
		{
			
			$this->form_validation->set_rules('category_id', 'Category', 'trim|required|numeric');
			$this->form_validation->set_rules('title', 'Subcategory', 'trim|required');




			//if validation errors
			if ($this->form_validation->run() === FALSE)
			{
				
				$data = array(
							'page_name' => ' Subcategory' ,
							'heading' => ' Subcategory - Edit' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('subcategory_admin/view') ,
							'category_result' => $category_result,
							'category_id' => $this->input->post('category_id'),
							'title' => $this->input->post('title'),
							'str' => ''
							);

				
				$this->_page($data);
				
			}
			
			
			else 
			{
								
				$data = array(
							   'category_id' => $this->input->post('category_id',TRUE) ,
							  'title' => $this->input->post('title',TRUE) 
							 );
				
				
				//update data
				$this->db_model->update('shop_subcategory',$data,array('id'=>$this->id)) ;
				
				redirect('subcategory_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 helper for the get_category_by_id() in the subcategory_vie
		$this->load->helper('basic_helper') ;

		if( $this->db_model->get_all_by_order('shop_subcategory','id','desc') !== FALSE )
		{
			$result_array = $this->db_model->get_all_by_order('shop_subcategory','id','desc') ;
		}
		
		if( $this->db_model->get_all_by_order('shop_category','id','desc') !== FALSE )
		{
			$category_result = $this->db_model->get_all_by_order('shop_category','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' => 'Subcategory Management' ,
					  'heading' => 'Subcategory Management' ,
					  'top_link_name' => 'Create' ,
					  'top_link' => site_url('subcategory_admin/create') ,
					  'result_array' => $result_array,
					  'category_result' => $category_result,
					  '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/subcategory_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,'');
				
				$this->db_model->delete('shop_subcategory',array('id'=>$this->id));
				
				//redirect page to hide the id
				redirect('subcategory_admin/view') ;
	
			}
			
		}
		else
		{
			redirect('subcategory_admin/view') ;
		}
		
	}
	
	/*public function get_category_by_id($cat_id)
	{
		$category='';
		
		$cat_row = $this->db_model->get_by_id('shop_category','id',$cat_id) ;
		
		if($cat_row!==FALSE)
		{
			$category = $cat_row->title ;	
		}
		
		return $category ;
	}*/
	
	


	

	
}//end class 

