<?php
class M_user extends CI_Model{

		function datapegsdm(){
		//$password2=md5($password);
		$db2 = $this->load->database('db2', TRUE);
		$query = $db2->query(" SELECT a.userid, a.username, b.idlogin, b.nik, b.nidn, b.namadepan, b.idtipepeg, b.namatengah, b.namabelakang, b.gelardepan, b.gelarbelakang
											FROM gate.sc_user as a 
											LEFT JOIN sdm.ms_pegawai as b ON b.idlogin = a.username 
											WHERE b.idtipepeg = 'P' OR b.idtipepeg = 'D'  ");						
		return $query;
		}
		
		function dataadmin(){
		$query = $this->db->query("SELECT * FROM `admin` ");		
		return $query;
		}

		function edit($username){
		$query = $this->db->query(" 	SELECT  a.username , c.akses, a.id_fakultas, a.id_prodi, a.id_biro, b.id_role, a.id_admin, c.deskripsi, c.domain, c.log_url, c.nama_akses
													FROM  admin as a 
													LEFT JOIN role as b On b.id_admin = a.id_admin 
													LEFT JOIN akses as c ON c.id_akses = b.id_akses
													WHERE a.username = '$username' ");		
		return $query;
		}

		function akses(){
		$query = $this->db->query("SELECT `id_akses`, `akses` FROM `akses` ORDER BY akses ASC ");		
		return $query;
		}

		function usertu(){
		//$password2=md5($password);
		$db2 = $this->load->database('db2', TRUE);
		$query = $db2->query("
								SELECT  c.namaunit, c.idunit, a.username, d.namadepan
								FROM  gate.sc_user as a
								JOIN gate.sc_userrole as b ON a.userid = b.userid
								JOIN sdm.ms_unit as c ON c.kodeunit = b.idsatker
								LEFT JOIN sdm.ms_pegawai as d ON d.nik = a.username
								WHERE c.namaunit LIKE '%TU%' AND a.username !=''
								ORDER BY c.level ASC    ");						
		return $query;
		}

		function dataprodi($namaprodi){
		$query = $this->db->query("SELECT * FROM `prodi` WHERE nama_prodi = '$namaprodi'  ");	
		return $query;
		}
		
		function cekadmin($username){
		$query = $this->db->query("SELECT * FROM `admin` WHERE username = '$username'  ");	
		return $query;
		}

		function cekrole($id_admin){
		$query = $this->db->query("SELECT * FROM `role` WHERE id_admin = '$id_admin'  ");	
		return $query;
		}

		function cekakses($username){
		$query = $this->db->query("SELECT * FROM `admin` WHERE username = '$username'  AND id_prodi = '517' AND id_fakultas = '517' AND id_biro = '517' ");	
		return $query;
		}
		
		function fakultas(){
			$query = $this->db->query("SELECT `id_fakultas`, `nama_fakultas`, `status`,`url_fakultas` FROM `fakultas` WHERE `status`='1' ORDER BY id_fakultas ASC");								
			return $query;		
		}
		function prodi(){
			$query = $this->db->query("SELECT `id_prodi`,`nama_prodi`, `status`, `url_prodi` FROM `prodi` WHERE `status`='1' AND id_prodi != '0' ORDER BY nama_prodi ASC");								
			return $query;		
		}
		function biro(){
			$query = $this->db->query("SELECT `id_biro`,`nama_biro`, `status`,`url_biro` FROM `biro` WHERE `status`='1' AND flag='1'   ORDER BY nama_biro ASC");								
			return $query;		
		}
		
		function getidfakultas($id_prodi){
		$query = $this->db->query("SELECT  id_fakultas FROM prodi WHERE id_prodi = '$id_prodi' ");		
		$row = $query->row();
		return $row->id_fakultas;
		}

		function ambilidadmin($username){
		$query = $this->db->query("SELECT  id_admin FROM admin WHERE username = '$username' ");		
		$row = $query->row();
		return $row->id_admin;
		}

		function getasal($username){
		$query = $this->db->query("SELECT  id_admin, id_prodi, id_fakultas, id_biro, id_pusdi FROM admin WHERE username = '$username' ");	
		$row = $query->row();
		$cek = $query->num_rows();
		if($cek >0){
			$row = $query->row();
			return $row->id_prodi.'-'.$row->id_fakultas.'-'.$row->id_biro.'-'.$row->id_pusdi;
		}else{
			return 0;
		}
		}
		
		function aksesprodi($username){
		$query = $this->db->query(" SELECT a.username, a.password, b.nama_prodi, b.url_prodi FROM admin as a  
		JOIN prodi as b On b.id_prodi = a.id_prodi 
		WHERE a.username = '$username'
		");	
		return $query;
		}

		function aksesbiro($username){
		$query = $this->db->query("SELECT a.username, a.password, b.nama_biro, b.url_biro FROM admin as a 
		JOIN biro as b On b.id_biro = a.id_biro
		WHERE a.username = '$username'
		");	
		return $query;
		}

		function aksesaplikasi($username){
		$query = $this->db->query("SELECT a.username, a.password, b.nama_biro, b.url_biro FROM admin as a 
		JOIN biro as b On b.id_biro = a.id_biro
		WHERE a.username = '$username'
		");	
		return $query;
		}
		
		
		function in($data_save,$table){
					$this->db->insert($table,$data_save);
		} 	
		function update($where,$data,$table){
				$this->db->where($where);
				$this->db->update($table,$data);
		}	
			function hapus($where,$table){
				$this->db->where($where);
				$this->db->delete($table);
		}			
		
}