<?php
if(!defined('BASEPATH')) exit('Hacking Attempt : Will Kill You Do Not Worry..!!');

class Users extends MY_Controller
{

	var $model ='Users_model';
	
	public function __construct(){
		parent::__construct();
		if($this->session->userdata('isLogin') == FALSE ){ redirect(base_url('admin/login/login_form'));}	
		$this->load->model('admin/'.$this->model); 
		
		$config['upload_path']          = 'uploads/';
		$config['allowed_types']        = 'gif|jpg|png|jpeg';
		$config['max_size'] 			= 2000;
        $config['max_width'] 			= 1500;
        $config['max_height'] 			= 1500;
		$this->upload->initialize($config);
	}

	/*######################################################################################################################################*/
/*###################################################### USERS START ########################################################*/
/*######################################################################################################################################*/
	
/*##########################################################*/
/*################ TO PRINT LIST ####################*/
/*##########################################################*/
	public function index(){
			
			$data['accounttypeoption']=array();
			$data['accounttypeoption'][0]=array('value'=>0,'title'=>'Super Admin');
			$data['accounttypeoption'][1]=array('value'=>1,'title'=>'Admin');
			$data['accounttypeoption'][2]=array('value'=>2,'title'=>'Executive');
			
			$data['pagetitle']='System Users List';
			if(isset($_POST['status'])){$data['status'] = $_POST['status'];}else{$data['status']='-1';}			
			$data['response'] = $this->Users_model->get_users($data['status']);			
			$this->adminview('users/list',$data);
	}
	
/*##########################################################*/
/*################ TO ADD DATA ####################*/
/*##########################################################*/
	
	public function publish($para=null){
		
			$formerror=0;
			
			//print_r($_POST);
			if(isset($_POST['submit'])){
				$rules = array(
									array(
											'field' => 'email',
											'label' => 'Email',
											'rules' => 'required'
									),
									array(
											'field' => 'name',
											'label' => 'Name',
											'rules' => 'required'
									)
							);

				$this->form_validation->set_rules($rules);
				if ($this->form_validation->run() == FALSE) { 
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> '.validation_errors().'</div>');
				}else{$formerror = 0; }
				
				if($_POST['avtarold'] != $_FILES['avtar']['name'] && $_FILES['avtar']['name'] !=''){
					if (!$this->upload->do_upload('avtar')) {    
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> '.$this->upload->display_errors().'</div>');
						$formerror = 1; 
					} 				
					else {  
						$imgdata = $this->upload->data('file_name');  $formerror = 0; 
						$data['avtar']=$imgdata;		
					}
				}
								
				if($_POST['id'] ==''){
					if(checkunique('email',array('email'=>$_POST['email']),'admin_login')){
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> USer Already In Database...</div>');
							$formerror = 1; 
					}
				}
				
				if(!$formerror){
					$id = $_POST['id'];
					$data['name']=$_POST['name'];
					$data['email']=$_POST['email'];
					$data['contact']=$_POST['contact'];
					if($id ==''){$data['password']=md5($_POST['password']);}
					$data['role']=$_POST['role'];
					if(isset($_POST['status'])){$data['status']=1;}else{$data['status'] = 0;}
					if($id ==''){$data['createdon']=date('Y-m-d H:i:s');}					
					if($data['avtar']==''){$data['avtar']='dummy-user.png';}
					$id = $this->Users_model->set_user($data,$id);
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> User Created Successfully...</div>');
					redirect(base_url('admin/users'));
				}else{
					$data['name']=$_POST['name'];
					$data['email']=$_POST['email'];
					$data['contact']=$_POST['contact'];
					$data['password']=md5($_POST['password']);
					$data['role']=$_POST['role'];
					if(@$_POST['status'] == 'on'){$data['status']=1;}else{$data['status'] = 0;}
				}
			
			}
			
			if($para >0){
					
					$response = $this->Users_model->get_single_user($para);
					$data['id']=$response[0]->id;
					$data['name']=$response[0]->name;
					$data['contact']=$response[0]->contact;
					$data['email']=$response[0]->email;
					$data['role']=$response[0]->role;
					$data['avtar']=$response[0]->avtar;
					$data['status']=$response[0]->status;
			}
			
			$data['accounttypeoption']=array();
			$data['accounttypeoption'][0]=array('value'=>0,'title'=>'Super Admin');
			$data['accounttypeoption'][1]=array('value'=>1,'title'=>'Admin');
			$data['accounttypeoption'][2]=array('value'=>2,'title'=>'Executive');
			
			$data['pagetitle']='Add Blog Category';			
			$this->adminview('users/publish',$data);
	}

/*##########################################################*/
/*################ TO Update DATA ####################*/
/*##########################################################*/
	
	public function update($para=null){
		
			$formerror=0;
			
			//print_r($_POST);
			if(isset($_POST['submit'])){
				$rules = array(
									array(
											'field' => 'email',
											'label' => 'Email',
											'rules' => 'required'
									),
									array(
											'field' => 'name',
											'label' => 'Name',
											'rules' => 'required'
									)
							);

				$this->form_validation->set_rules($rules);
				if ($this->form_validation->run() == FALSE) { 
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> '.validation_errors().'</div>');
				}else{$formerror = 0; }
				
				if($_POST['avtarold'] != $_FILES['avtar']['name'] && $_FILES['avtar']['name'] !=''){
					if (!$this->upload->do_upload('avtar')) {    
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> '.$this->upload->display_errors().'</div>');
						$formerror = 1; 
					} 				
					else {  
						$imgdata = $this->upload->data('file_name');  $formerror = 0; 
						$data['avtar']=$imgdata;		
					}
				}
								
				if($_POST['id'] ==''){
					if(checkunique('email',array('email'=>$_POST['email']),'admin_login')){
						$this->session->set_flashdata('image_error', '<div class="alert alert-danger text-center"> USer Already In Database...</div>');
							$formerror = 1; 
					}
				}
				
				if(!$formerror){
					$id = $_POST['id'];
					$data['name']=$_POST['name'];
					$data['email']=$_POST['email'];
					$data['contact']=$_POST['contact'];
					$data['role']=$_POST['role'];
					if($data['avtar']==''){$data['avtar']='dummy-user.png';}
					if(isset($_POST['status'])){$data['status']=1;}else{$data['status'] = 0;}
					if($id ==''){$data['createdon']=date('Y-m-d H:i:s');}					
					
					if($data['status'] == 0){
						delcisession($data['email']);
					}
					$id = $this->Users_model->set_user($data,$id);
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> User Updated Successfully...</div>');
					redirect(base_url('admin/users'));
				}else{
					$data['name']=$_POST['name'];
					$data['email']=$_POST['email'];
					$data['contact']=$_POST['contact'];
					$data['role']=$_POST['role'];
					if(@$_POST['status'] == 'on'){$data['status']=1;}else{$data['status'] = 0;}
				}
			
			}
			
			if($para >0){
					
					$response = $this->Users_model->get_single_user($para);
					$data['id']=$response[0]->id;
					$data['name']=$response[0]->name;
					$data['contact']=$response[0]->contact;
					$data['email']=$response[0]->email;
					$data['role']=$response[0]->role;
					$data['avtar']=$response[0]->avtar;
					$data['status']=$response[0]->status;
			}
			
			$data['accounttypeoption']=array();
			$data['accounttypeoption'][0]=array('value'=>0,'title'=>'Super Admin');
			$data['accounttypeoption'][1]=array('value'=>1,'title'=>'Admin');
			$data['accounttypeoption'][2]=array('value'=>2,'title'=>'Executive');
			
			$data['pagetitle']='Add Blog Category';			
			$this->adminview('users/publish',$data);
	}
	
/*##########################################################*/
/*################ TO Update DATA ####################*/
/*##########################################################*/
	
	public function changepassword($para=null){
		
			$formerror=0;
			
			//print_r($_POST);
			if(isset($_POST['submit'])){
				$rules = array(
									array(
											'field' => 'password',
											'label' => 'Password',
											'rules' => 'required'
									)
							);

				$this->form_validation->set_rules($rules);
				if ($this->form_validation->run() == FALSE) { 
					$formerror = 1; 
					$this->session->set_flashdata('form_error', '<div class="alert alert-danger text-center"> '.validation_errors().'</div>');
				}else{$formerror = 0; }
				
				
				
				if(!$formerror){
					$id = $_POST['id'];
					$data['password']=md5($_POST['password']);					
					delcisession($_POST['email']);
					$id = $this->Users_model->set_user($data,$id);
					$this->session->set_flashdata('page_message', '<div class="alert alert-success text-center"> User Password Updated Successfully...</div>');
					redirect(base_url('admin/users'));
				}else{
					$data['name']=$_POST['name'];
					$data['email']=$_POST['email'];
					$data['contact']=$_POST['contact'];
					$data['password']=$_POST['password'];
					$data['role']=$_POST['role'];
					if(@$_POST['status'] == 'on'){$data['status']=1;}else{$data['status'] = 0;}
				}
			
			}
			
			if($para >0){
					
					$response = $this->Users_model->get_single_user($para);
					$data['id']=$response[0]->id;
					$data['name']=$response[0]->name;
					$data['contact']=$response[0]->contact;
					$data['email']=$response[0]->email;
					$data['password']=$response[0]->password;
			}
			
			$data['accounttypeoption']=array();
			$data['accounttypeoption'][0]=array('value'=>0,'title'=>'Super Admin');
			$data['accounttypeoption'][1]=array('value'=>1,'title'=>'Admin');
			$data['accounttypeoption'][2]=array('value'=>2,'title'=>'Executive');
			
			$data['pagetitle']='Change Password';			
			$this->adminview('users/changepassword',$data);
	}	
	
/*##########################################################*/
/*################ TO DELETE SINGLE ENTRY ####################*/
/*##########################################################*/
	
	public function delete($para = null){
		$this->Users_model->delete_user($para);
		$this->session->set_flashdata('page_message', '<div class="alert alert-danger text-center"> Selected Entry Deleted Successfully</div>');
		redirect(base_url('admin/users'));
	}
	

/*######################################################################################################################################*/
/*###################################################### USERS END ########################################################*/
/*######################################################################################################################################*/
}	

?>