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

class Simple_login
{
	protected $CI;

	public function __construct()
	{
        $this->CI =& get_instance();
        //load data model user
        $this->CI->load->model('user_model');
        $this->CI->load->model('setting_model');
	}

	//fungsi login
	public function login($userid,$password)
	{
		$check = $this->CI->user_model->login($userid,$password);
		//jika ada user, maka create session login
		if($check) {
			$id      = $check->id;
			$nama	 = $check->nama;
			$userid	 = $check->userid;
			$level	 = $check->level;
			$email	 = $check->email;
			//create session
			$this->CI->session->set_userdata('id',$id);
			$this->CI->session->set_userdata('nama',$nama);
			$this->CI->session->set_userdata('userid',$userid);
			$this->CI->session->set_userdata('level',$level);
			$this->CI->session->set_userdata('email',$email);			
			
			// Jika ga ada, default masuk ke halaman dasbor
			$this->CI->session->set_flashdata('sukses', 'Anda berhasil login');
			redirect(base_url('dashboard'),'refresh');			
		}else{
			//kalau tidak ada atau salah maka suruh login lagi
			$this->CI->session->set_flashdata('warning', 'Userid atau password salah');
			redirect(base_url('login'),'refresh');
		}

		$chec2 = $this->CI->setting_model->setting();
		//jika ada user, maka create session login
		if($check2) {
			$id      			 = $check2->id;
			$tgl_sosialisasi	 = $check2->date('d-m-Y H:i:s');
			
			//create session
			$this->CI->session->set_userdata('id',$id);
			$this->CI->session->set_userdata('tgl_sosialisasi',$tgl_sosialisasi);				
						
		}
	}

	public function setting($id)
	{
		$check2 = $this->CI->setting_model->setting($id);
		//jika ada user, maka create session login
		if($check2) {
			$id      			 = $check2->id;
			$tgl_sosialisasi	 = $check2->tgl_sosialisasi;
			
			//create session
			$this->CI->session->set_userdata('id',$id);
			$this->CI->session->set_userdata(date('Y-m-d H:i:s',strtotime('tgl_sosialisasi')));				
						
		}
	}

	// Fungsi check login: seseorang sudah login atau belum
	public function check_login()
	{
		// Check status login (kita ambil status username dan akses level)
		if($this->CI->session->userdata('userid') == "" && 
			$this->CI->session->userdata('level') == "")
		{
			$this->CI->session->set_flashdata('warning', 'Anda belum login');
			redirect(base_url('login'),'refresh');
		}
	}

	//fungsi logout
	public function logout()
	{
		//membuang semua session yang telah diset pada login
		$this->CI->session->unset_userdata('id');
		$this->CI->session->unset_userdata('nama');
		$this->CI->session->unset_userdata('userid');
		$this->CI->session->unset_userdata('level');
		$this->CI->session->unset_userdata('email');
		//setelah session dibuang, maka redirect login
		$this->CI->session->set_flashdata('sukses', 'Anda berhasil logout');
		redirect(base_url('login'),'refresh');
	}

}

/* End of file Simple_login.php */





/* End of file Simple_login.php */
/* Location: ./application/libraries/Simple_login.php */
