<?php
class Log_mdl extends Base_Model {
    function __construct() {
        parent::__construct();
		$this->table = 'log';
		$this->table2 = 'user';
		$this->pk_field = 'no';
    }
	function getAll($page,$rows,$key='',$dtdari='',$dtsampai=''){
		$kd_cmpy=$this->session->userdata('kd_cmpy');
		$grup=$this->session->userdata('grup');
		
		$offset = ($page-1)*$rows;
		$result = array();
		
		$q = "SELECT *,no AS id FROM ".$this->table." a
			  INNER JOIN ".$this->table2." b ON b.nama_user=a.nama_user
				WHERE 1=1";
		
		if($dtdari != '' && $dtsampai != ''):
			$q .= " AND a.tgl BETWEEN '".$this->dmys2ymd($dtdari)."' AND '".$this->dmys2ymd($dtsampai)."'";
		endif;
				
		if ($kd_cmpy != '9221'){
			  $q .= " AND b.kd_cmpy = '".$kd_cmpy."'";
		}
		if ($grup != 'ADM' && $grup != 'BC'){
			  $q .= " AND b.grup = '".$grup."'";
		}	  
		$q .= " AND (a.nama_user LIKE '%$key%' 
			  OR ket LIKE '%$key%')
			  ORDER BY no DESC";
		$rstot=$this->db->query($q)->result();
	
		$q .= " LIMIT $offset,$rows";
		$rs=$this->db->query($q)->result();
		
		$result["total"] = count($rstot);
		$result["rows"] = $rs;
		
		return $result;
	}
	
	function add($data){
		$run = $this->db->insert($this->table, $data); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($data['nama_user'],"Tambah User Activity ".$data['nama_user']);	
		} else {
			$rs=false;
		}
		
		return $rs;
	}
	
	function edit($data,$id){
		$run = $this->db->update($this->table, $data, array($this->pk_field => $id)); 
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($id,"Ubah User Activity ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;
	}
	
	function del($id){
		$this->db->delete($this->table, array($this->pk_field => $id)); 		
		if ($this->db->affected_rows()>0){
			$rs=true;
			$log=$this->addlog($id,"Hapus User Activity ".$id);	
		} else {
			$rs=false;
		}		
		
		return $rs;		
	}
}
