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


class Login_model extends CI_Model { 
	
	function __construct()
    {
        parent::__construct();
    }

    function loginFunctionForAllUsers (){
        
        $email = html_escape($this->input->post('email'));			
        $password = html_escape($this->input->post('password'));	
        $credential = array('email' => $email, 'password' => sha1($password));	
  
        $query = $this->db->get_where('admin', $credential);
        if ($query->num_rows() > 0) {
            $row = $query->row();
  
            $this->session->set_userdata('login_type', 'admin');
            $this->session->set_userdata('admin_login', '1');
            $this->session->set_userdata('admin_id', $row->admin_id);
            $this->session->set_userdata('login_user_id', $row->admin_id);
            $this->session->set_userdata('name', $row->name);

           	$this->db->where('admin_id', $row->admin_id);
			$this->db->update('admin', array('login_status' => '1'));
							
			$this->session->set_flashdata('flash_message', $row->name. ' '.get_phrase('Successfully Login'));
			redirect(base_url() . 'admin/dashboard', 'refresh');
			
        }

        $query = $this->db->get_where('hrm', $credential);
        if ($query->num_rows() > 0) {
            $row = $query->row();
  
            $this->session->set_userdata('login_type', 'hrm');
            $this->session->set_userdata('hrm_login', '1');
            $this->session->set_userdata('hrm_id', $row->hrm_id);
            $this->session->set_userdata('login_user_id', $row->hrm_id);
            $this->session->set_userdata('name', $row->name);

           	$this->db->where('hrm_id', $row->hrm_id);
			$this->db->update('hrm', array('login_status' => '1'));
							
			$this->session->set_flashdata('flash_message', $row->name. ' '.get_phrase('Successfully Login'));
			redirect(base_url() . 'hrm/dashboard', 'refresh');
        }

        $query = $this->db->get_where('hostel', $credential);
        if ($query->num_rows() > 0) {
            $row = $query->row();
  
            $this->session->set_userdata('login_type', 'hostel');
            $this->session->set_userdata('hostel_login', '1');
            $this->session->set_userdata('hostel_id', $row->hostel_id);
            $this->session->set_userdata('login_user_id', $row->hostel_id);
            $this->session->set_userdata('name', $row->name);

            $this->db->where('hostel_id', $row->hostel_id);
			$this->db->update('hostel', array('login_status' => '1'));
							
			$this->session->set_flashdata('flash_message', $row->name. ' '.get_phrase('Successfully Login'));
			redirect(base_url() . 'hostel/dashboard', 'refresh');
			
        }

        $query = $this->db->get_where('accountant', $credential);
        if ($query->num_rows() > 0) {
            $row = $query->row();
  
            $this->session->set_userdata('login_type', 'accountant');
            $this->session->set_userdata('accountant_login', '1');
            $this->session->set_userdata('accountant_id', $row->accountant_id);
            $this->session->set_userdata('login_user_id', $row->accountant_id);
            $this->session->set_userdata('name', $row->name);

           	$this->db->where('accountant_id', $row->accountant_id);
			$this->db->update('accountant', array('login_status' => '1'));
							
			$this->session->set_flashdata('flash_message', $row->name. ' '.get_phrase('Successfully Login'));
			redirect(base_url() . 'accountant/dashboard', 'refresh');
        }

        $query = $this->db->get_where('librarian', $credential);
        if ($query->num_rows() > 0) {
            $row = $query->row();
  
            $this->session->set_userdata('login_type', 'librarian');
            $this->session->set_userdata('librarian_login', '1');
            $this->session->set_userdata('librarian_id', $row->librarian_id);
            $this->session->set_userdata('login_user_id', $row->librarian_id);
            $this->session->set_userdata('name', $row->name);

            
			$this->db->where('librarian_id', $row->librarian_id);
			$this->db->update('librarian', array('login_status' => '1'));
							
			$this->session->set_flashdata('flash_message', $row->name. ' '.get_phrase('Successfully Login'));
			redirect(base_url() . 'librarian/dashboard', 'refresh');
        }

        $query = $this->db->get_where('parent', $credential);
        if ($query->num_rows() > 0) {
            $row = $query->row();
  
            $this->session->set_userdata('login_type', 'parent');
            $this->session->set_userdata('parent_login', '1');
            $this->session->set_userdata('parent_id', $row->parent_id);
            $this->session->set_userdata('login_user_id', $row->parent_id);
            $this->session->set_userdata('name', $row->name);

            
			$this->db->where('parent_id', $row->parent_id);
			$this->db->update('parent', array('login_status' => '1'));
							
			$this->session->set_flashdata('flash_message', $row->name. ' '.get_phrase('Successfully Login'));
			redirect(base_url() . 'parents/dashboard', 'refresh');
			
        }

        $query = $this->db->get_where('student', $credential);
        if ($query->num_rows() > 0) {
            $row = $query->row();
  
            $this->session->set_userdata('login_type', 'student');
            $this->session->set_userdata('student_login', '1');
            $this->session->set_userdata('student_id', $row->student_id);
            $this->session->set_userdata('login_user_id', $row->student_id);
            $this->session->set_userdata('name', $row->name);

            
			$this->db->where('student_id', $row->student_id);
			$this->db->update('student', array('login_status' => '1'));
							
			$this->session->set_flashdata('flash_message', $row->name. ' '.get_phrase('Successfully Login'));
			redirect(base_url() . 'student/dashboard', 'refresh');
			
        }

        $query = $this->db->get_where('teacher', $credential);
        if ($query->num_rows() > 0) {
            $row = $query->row();
  
            $this->session->set_userdata('login_type', 'teacher');
            $this->session->set_userdata('teacher_login', '1');
            $this->session->set_userdata('teacher_id', $row->teacher_id);
            $this->session->set_userdata('login_user_id', $row->teacher_id);
            $this->session->set_userdata('name', $row->name);

            
			$this->db->where('teacher_id', $row->teacher_id);
			$this->db->update('teacher', array('login_status' => '1'));
							
			$this->session->set_flashdata('flash_message', $row->name. ' '.get_phrase('Successfully Login'));
			redirect(base_url() . 'teacher/dashboard', 'refresh');
        }
		else{
				
			$this->session->set_flashdata('error_message', get_phrase('Wrong email or password'));
			redirect(base_url() . 'login', 'refresh');
		}
				

    }


    function logout_model_for_admin(){
        return  $this->db->set('login_status', ('0'))
                    ->where('admin_id', $this->session->userdata('admin_id'))
                    ->update('admin');
    }

    function logout_model_for_hrm(){
        return  $this->db->set('login_status', ('0'))
                    ->where('hrm_id', $this->session->userdata('hrm_id'))
                    ->update('hrm');
    }

    function logout_model_for_hostel(){
        return  $this->db->set('login_status', ('0'))
                    ->where('hostel_id', $this->session->userdata('hostel_id'))
                    ->update('hostel');
    }

    function logout_model_for_accountant(){
        return  $this->db->set('login_status', ('0'))
                    ->where('accountant_id', $this->session->userdata('accountant_id'))
                    ->update('accountant');
    }

    function logout_model_for_librarian(){
        return  $this->db->set('login_status', ('0'))
                    ->where('librarian_id', $this->session->userdata('librarian_id'))
                    ->update('librarian');
    }

    function logout_model_for_parent(){
        return  $this->db->set('login_status', ('0'))
                    ->where('parent_id', $this->session->userdata('parent_id'))
                    ->update('parent');
    }

    function logout_model_for_teacher(){
        return  $this->db->set('login_status', ('0'))
                    ->where('teacher_id', $this->session->userdata('teacher_id'))
                    ->update('teacher');
    }

    function logout_model_for_student(){
        return  $this->db->set('login_status', ('0'))
                    ->where('student_id', $this->session->userdata('student_id'))
                    ->update('student');
    }
	
	
	


	
	
}
