File: /home/cafsindia/allyindian_com/backend/application/models/Employee.php
<?php
class Employee extends Person{
public function exists($person_id)
{
$this->db->from('employees');
$this->db->join('people', 'people.person_id = employees.person_id');
$this->db->where('employees.person_id', $person_id);
return ($this->db->get()->num_rows() == 1);
}
public function get_state() {
$this->db->from('state');
$this->db->order_by('state_name', 'asc');
return $this->db->get();
}
public function get_role(){
$this->db->from('role');
$this->db->where('status = ',1);
$this->db->order_by('role_id', 'asc');
return $this->db->get();
}
public function get_reporter() {
$this->db->from('employees');
$this->db->join('people', 'people.person_id = employees.person_id');
$this->db->join('role', 'employees.role = role.role_id');
$this->db->where('role != ',3,FALSE);
return $this->db->get();
}
public function get_total_rows() {
$this->db->from('employees');
$this->db->where('deleted', 0);
return $this->db->count_all_results();
}
public function get_all($limit = 10000, $offset = 0){
$this->db->from('employees');
$this->db->where('deleted', 0);
$this->db->join('people', 'employees.person_id = people.person_id');
$this->db->order_by('last_name', 'asc');
$this->db->limit($limit);
$this->db->offset($offset);
return $this->db->get();
}
public function get_info_empid($employee_id){
$this->db->from('employees');
$this->db->join('people', 'people.person_id = employees.person_id');
$this->db->where('employees.id', $employee_id);
$query = $this->db->get();
if($query->num_rows() == 1){
return $query->row();
}
return false;
}
public function get_info($employee_id){
$this->db->from('employees');
$this->db->join('people', 'people.person_id = employees.person_id');
$this->db->where('employees.person_id', $employee_id);
$query = $this->db->get();
if($query->num_rows() == 1){
return $query->row();
}else{
$person_obj = parent::get_info(-1);
return $person_obj;
}
}
public function get_multiple_info($employee_ids){
$this->db->from('employees');
$this->db->join('people', 'people.person_id = employees.person_id');
$this->db->where_in('employees.person_id', $employee_ids);
$this->db->order_by('first_name', 'asc');
return $this->db->get();
}
public function save_employee(&$person_data, &$employee_data, &$grants_data, $employee_id = FALSE){
$success = FALSE;
$this->db->trans_start();
if((int)$employee_id === -1){
$query = $this->db->query("SELECT count(*) as count FROM sblttweb_employees where username = '".$employee_data['username']."'");
$row = $query->row_array();
$empCount = $row['count'];
if((int)$empCount === 0){
parent::save($person_data, $employee_id);
$employee_data['person_id'] = $employee_id = $person_data['person_id'];
$success = $this->db->insert('employees', $employee_data);
}
}else{
parent::save($person_data, $employee_id);
$this->db->where('person_id', $employee_id);
$success = $this->db->update('employees', $employee_data);
}
if($success){
$success = $this->db->delete('grants', array('person_id' => $employee_id));
if($success){
foreach($grants_data as $permission_id){
$success = $this->db->insert('grants', array('permission_id' => $permission_id, 'person_id' => $employee_id));
}
}
}
$this->db->trans_complete();
$success &= $this->db->trans_status();
return $success;
}
public function delete($employee_id){
$success = FALSE;
if($employee_id == $this->get_logged_in_employee_info()->person_id){
return FALSE;
}
$this->db->trans_start();
if($this->db->delete('grants', array('person_id' => $employee_id))){
$this->db->where('person_id', $employee_id);
$success = $this->db->update('employees', array('deleted' => 1));
}
$this->db->trans_complete();
return $success;
}
public function delete_list($employee_ids){
$success = FALSE;
if(in_array($this->get_logged_in_employee_info()->person_id, $employee_ids)){
return FALSE;
}
$this->db->trans_start();
$this->db->where_in('person_id', $employee_ids);
if($this->db->delete('grants')){
$this->db->where_in('person_id', $employee_ids);
$success = $this->db->update('employees', array('deleted' => 1));
}
$this->db->trans_complete();
return $success;
}
public function get_found_rows($search){
$this->db->from('employees');
$this->db->join('people', 'employees.person_id = people.person_id');
$this->db->group_start();
$this->db->like('first_name', $search);
$this->db->or_like('last_name', $search);
$this->db->or_like('email', $search);
$this->db->or_like('phone_number', $search);
$this->db->or_like('username', $search);
$this->db->or_like('CONCAT(first_name, " ", last_name)', $search);
$this->db->group_end();
$this->db->where('deleted', 0);
return $this->db->get()->num_rows();
}
public function search($search, $rows = 0, $limit_from = 0, $sort = 'employees.id', $order = 'asc'){
$this->db->from('employees');
$this->db->join('people', 'employees.person_id = people.person_id');
$this->db->group_start();
$this->db->like('first_name', $search);
$this->db->or_like('last_name', $search);
$this->db->or_like('email', $search);
$this->db->or_like('phone_number', $search);
$this->db->or_like('username', $search);
$this->db->or_like('CONCAT(first_name, " ", last_name)', $search);
$this->db->group_end();
$this->db->where('deleted', 0);
$this->db->order_by($sort, $order);
if($rows > 0){
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
public function login($username, $password){
$query = $this->db->get_where('employees', array('username' => $username, 'password' => md5($password), 'deleted' => 0), 1);
if($query->num_rows() == 1){
$row = $query->row();
$this->session->set_userdata('person_id', $row->person_id);
$this->session->set_userdata('shop_id', $row->shop_id);
$this->session->set_userdata('emp_id', $row->id);
$this->session->set_userdata('emp_role', $row->role);
return TRUE;
}
return FALSE;
}
public function logout(){
$this->session->sess_destroy();
redirect('login');
}
public function is_logged_in(){
return ($this->session->userdata('person_id') != FALSE);
}
public function get_logged_in_employee_info(){
if($this->is_logged_in()){
return $this->get_info($this->session->userdata('person_id'));
}
return FALSE;
}
public function has_module_grant($permission_id, $person_id){
$this->db->from('grants');
$this->db->like('permission_id', $permission_id, 'after');
$this->db->where('person_id', $person_id);
$result_count = $this->db->get()->num_rows();
if($result_count != 1){
return ($result_count != 0);
}
return $this->has_subpermissions($permission_id);
}
public function has_subpermissions($permission_id){
$this->db->from('permissions');
$this->db->like('permission_id', $permission_id.'_', 'after');
return ($this->db->get()->num_rows() == 0);
}
public function has_grant($permission_id, $person_id){
if($permission_id == null){
return TRUE;
}
$query = $this->db->get_where('grants', array('person_id' => $person_id, 'permission_id' => $permission_id), 1);
return ($query->num_rows() == 1);
}
public function get_employee_grants($person_id){
$this->db->from('grants');
$this->db->where('person_id', $person_id);
return $this->db->get()->result_array();
}
}
?>