<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pesan_masuk extends CI_Model {
	function parseOrderby($col){
		if($col==1){
			return "tanggal_kirim"; 
		}elseif($col==2){
			return "dari_userid_nama"; 
		}elseif($col==3){
			return "subjek"; 
		}else{
			return "tanggal_kirim";
		}
	}

	function get_pesan_keluar_list($col=1,$asc_desc='DESC',$begin=0,$key='',$userid){
		if(empty($userid)){
			return '';
		}
		$orderby = $this->parseOrderby($col);
		$where = '';
		if(!empty($key)){
			$where .= ' AND (tanggal_kirim LIKE "%'.$key.'%" OR subjek LIKE "%'.$key.'%" OR dari_userid_nama COLLATE latin1_swedish_ci LIKE "%'.$key.'%") ';
		}
		try {
			return $this->db->query("SELECT SQL_CALC_FOUND_ROWS id,subjek,tanggal_kirim,dari_userid_nama,status
				FROM v_pesan_masuk WHERE dari_userid!=".$userid." AND status > 0 AND ke_userid=".$userid." ".$where." ORDER BY tanggal_kirim ".$asc_desc." LIMIT ".$begin.",50;");
		} catch (Exception $e) {
			
		}
	}

	function fecth_data_num(){
		$query = $this->db->query("SELECT FOUND_ROWS() as cnt;");
		if($query->num_rows>0){
			foreach ($query->result() as $row) {
				return $row->cnt;
			}
    		return $query->num_rows;
    	}else{
    		return 0;
    	}
	}
	

	function get_pesan_detil($id){
		if(empty($id)){
			return "";
		}
		try {
			$this->db->select('id,subjek,kepada,tanggal_kirim,dari_userid,dari_userid_nama,isi_pesan,ke_userid_text');
			$this->db->where('id',$id);
			$this->db->where('ke_userid',$this->session->userdata('userid'));
			return $this->db->get('v_pesan_masuk');
		} catch (Exception $e) {
			return '';
		}
	}

	function get_list_user(){
		try {
			return $this->db->query('SELECT userid, fullname,group_name,group_id
								FROM v_users 
								WHERE userid <> '.$this->session->userdata('userid').' AND group_id >0 AND block=0
								ORDER BY group_name,fullname ASC;');
		} catch (Exception $e) {
			
		}
	}

	function add_data($id,$data){
		try {
			$this->db->insert('pesan', $data);
			$datas = array(
               'status' => 3,
               'tanggal_balas' => date("Y-m-d h:i:s",time())
            );
			$this->update_pesan_tujuan($data['parent_id'],$datas);

			$row = $this->get_id($data);
			$id_pesan = $row->id;

			$tmp = explode(',', $data['ke_userid']);
			for ($i=0; $i < count($tmp); $i++) {
				$data_pesan = array(
	               'pesan_id' => $id_pesan,
	               'ke_userid' => $tmp[$i],
	               'status' => 1,
	               'tanggal_kirim' => $data['tanggal_kirim']
	            );
				$this->db->insert('pesan_tujuan', $data_pesan);
			}
			
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}
	}

	function get_id($data){
		try {
			$this->db->select('id');
			$this->db->where('dari_userid',$data['dari_userid']);
			$this->db->where('tanggal_kirim',$data['tanggal_kirim']);
			$this->db->where('ke_userid',$data['ke_userid']);
			$query = $this->db->get('pesan');
			if ($query->num_rows() > 0){
				return $query->row();
			}else{
				return 0;
			}		
		} catch (Exception $e) {
			
		}
	}

	function update_data($id){
		try {
			$data = array(
               'diperbaharui_oleh' => $this->session->userdata('username'),
               'diperbaharui_tanggal' => date("Y-m-d h:i:s",time()),
               'status' => -1
            );
			$this->db->where('id',$id);
			$this->db->update('pesan',$data);
			$data = array(
               'status' => -1,
               'tanggal_hapus' => date("Y-m-d h:i:s",time())
            );
			$this->update_pesan_tujuan($id,$data);
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}
	}

	function update_pesan_tujuan($idpesan,$data){
		try {
			$this->db->where('pesan_id',$idpesan);
			$this->db->where('ke_userid',$this->session->userdata('userid'));
			$this->db->update('pesan_tujuan',$data);
			return TRUE;
		} catch (Exception $e) {
			return FALSE;
		}
	}
}
?>