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


class User_admin extends CI_Controller {
	
	private $id			=	''	; 
	private $uname		=	''	; 
	private $pword		=	''	; 
	private $mdate		=	''	; 
	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->helper('security');
			$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/user_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('uname', 'Username', 'trim|required|is_unique[shop_user.uname]|min_length[6]|max_length[20]');
			$this->form_validation->set_rules('pword', 'Password', 'trim|required|min_length[8]|max_length[20]');
			$this->form_validation->set_rules('pword2', 'Confirm Password', 'trim|required|matches[pword]');

			
			
			//if validation errors
			if($this->form_validation->run() === FALSE)
			{
			
				$data = array(
							'page_name' =>  'User' ,
							'heading' => 'User' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('User_admin/view') ,
							'uname' => $this->input->post('uname'),
							'pword' => $this->input->post('pword'),
							'pword2' => $this->input->post('pword2'),
							'mdate' => $this->input->post('mdate'),
							'str' => ''
							);
				
				$this->_page($data);
				
			}

			else 
			{
								
				$data = array(
							  'uname' => $this->input->post('uname',TRUE) ,
							  'pword' => md5($this->input->post('pword',TRUE)),
							  'mdate' => md5($this->input->post('mdate',TRUE))
							);
				
				//insert data
				$insert_id = $this->Db_model->insert('shop_user',$data) ;
				
				redirect('User_admin/view');

			}
			
		}//if($this->input->post('submit'))
		
		else
		{
			
			/**
			*
			*	show form at first time page loads,
			*	ie, before clicking submit button
			*
			*/
			
			$data = array(
							'page_name' =>  'User' ,
							'heading' => 'User' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('User_admin/view') ,
							'uname' => '',
							'pword' => '',
							'pword2' => '',
							'str' => ''
							);
				
			$this->_page($data);
		}
		
		
		
	}
	
	/**
	*
	*   User 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_user','uid',$this->id);
		
		if($details!==FALSE)
		{
		
		//$this->id =$details->id ;
		$this->uname = $details->uname ;
		
		}
		
		$data = array(
				'page_name' => 'User' ,
				'heading' => 'User - Edit' ,
				'top_link_name' => 'View' ,
				'top_link' => site_url('User_admin/view') ,
				'uname' => $this->uname,
				'pword' => '',
				'pword2' => '',
				'str' => ''
				);
		
		
		//check if submit button clicked
		if($this->input->post('submit'))
		{
		
			$this->form_validation->set_rules('uname', 'Username', 'trim|required|min_length[6]|max_length[20]');
			$this->form_validation->set_rules('pword', 'Password', 'trim|required|min_length[8]|max_length[20]');
			$this->form_validation->set_rules('pword2', 'Confirm Password', 'trim|required|matches[pword]');	

			//if validation errors
			if ($this->form_validation->run() === FALSE)
			{
				
				$data = array(
							'page_name' => 'User' ,
							'heading' => 'User - Edit' ,
							'top_link_name' => 'View' ,
							'top_link' => site_url('User_admin/view') ,
							'uname' => $this->input->post('uname'),
							'pword' => $this->input->post('pword'),
							'pword2' => $this->input->post('pword2'),
							'mdate' => $this->input->post('mdate'),
							'str' => ''
							);

				
				$this->_page($data);
				
			}
			
			
			else 
			{
								
				$data = array(
							  'uname' => $this->input->post('uname',TRUE) ,
							  'pword' => md5($this->input->post('pword',TRUE)), 
							  'mdate' => $this->input->post('mdate',TRUE)
							 );
				
				
				//update data
				$this->Db_model->update('shop_user',$data,array('uid'=>$this->id)) ;
				
				redirect('User_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_user','uid','desc') !== FALSE )
		{
			$result_array = $this->Db_model->get_all_by_order('shop_user','uid','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' => 'User Management' ,
					  'heading' => 'User Management' ,
					  'top_link_name' => 'Create' ,
					  'top_link' => site_url('User_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/user_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_user',array('uid'=>$this->id));
				
				//redirect page to hide the id
				redirect('User_admin/view') ;
	
			}
		
		}
		else
		{
			redirect('User_admin/view') ;
		}
			
	}
	
	


	

	
}//end class

