load->library('session'); $this->load->library('rhlib'); } function retValOrNull($val){ //especially for combo & looukup with no item selected $val = ($val=='')? null : $val; return $val; } function get_tbpst(){ $q = "SELECT * FROM v_tbpst ORDER BY kdpsttbpst"; $this->rhlib->jsonFromQueryWLimit($q); } function get_jenjangtbkod(){ $q = "SELECT kdkodtbkod AS kode, nmkodtbkod AS nama FROM tbkod where kdapltbkod='01' LIMIT 10"; $this->rhlib->jsonFromQuery($q); } function insert_tbpst(){ $dataArray = $this->getFieldsAndValues(); $ret['tbpstpriv'] = $this->rhlib->insertRecord('tbpst',$dataArray); $ret['tbpstpub'] = $this->rhlib->insertRecordPublic('tbpst',$dataArray); if ($ret['tbpstpriv'] && $ret['tbpstpub']) { $ret["success"]=true; } else { $ret["success"]=false; } echo json_encode($ret); } function update_tbpst(){ $fieldsAndValues = $this->getFieldsAndValues(); //UPDATE $where['kdpsttbpst'] = $_POST['kdpsttbpst']; $this->db->where('kdpsttbpst', $_POST['kdpsttbpst']); $this->db->update('tbpst', $fieldsAndValues); $ret["success"]=true; if($this->db->affected_rows()){ $ret["success"]=true; $ret["msg"]='Update Data Berhasil'; $ret["num"]=$this->db->affected_rows(); $this->rhlib->updateRecordPublic('tbpst',$fieldsAndValues,$where); } echo json_encode($ret); } function delete_tbpst(){ $where['kdpsttbpst'] = $_POST['kdpsttbpst']; $ret = $this->rhlib->deleteRecord('tbpst',$where); $this->rhlib->deleteRecordPublic('tbpst',$where); return $ret; } function getFieldsAndValues(){ $dataArray = array( 'kdpsttbpst'=> $this->retValOrNull($_POST['kdpsttbpst']), 'kdjentbpst'=> $this->retValOrNull($_POST['kdjentbpst']), 'nmpsttbpst'=> $this->retValOrNull($_POST['nmpsttbpst']), 'kdbiltbpst'=> $this->retValOrNull($_POST['kdbiltbpst']), 'kdgrutbpst'=> $this->retValOrNull($_POST['kdgrutbpst']), 'kdasntbpst'=> $this->retValOrNull($_POST['kdasntbpst']), 'nomortbpst'=> $this->retValOrNull($_POST['nomortbpst']), ); return $dataArray; } //import data excel function getcountdata($where,$tbl){ $this->db->select("*"); $this->db->from($tbl); $this->db->where($where); $query = $this->db->get(); return $query->num_rows(); } function getcountdatapublic($where,$tbl){ $KEDUA= $this->load->database('second', TRUE); $KEDUA->select("*"); $KEDUA->from($tbl); $KEDUA->where($where); $query = $KEDUA->get(); return $query->num_rows(); } function update_import($data,$where,$tbl){ $this->db->where($where); $this->db->update($tbl, $data); if($this->db->affected_rows()){ $ret["success"]=true; $ret["msg"]='Update Data Berhasil'; $ret["num"]=$this->db->affected_rows(); }else{ $ret["success"]=false; $ret["msg"]= 'Update Data Gagal'; } return $ret; } function import_from_Excel(){ $ori_dir = 'resources/files/'; $upload; if ($_FILES['f_excel_import_pst']['name']) { $upload = move_uploaded_file($_FILES['f_excel_import_pst']['tmp_name'], $ori_dir . $_FILES['f_excel_import_pst']['name']); } if ($upload) { include_once ( APPPATH."libraries/excel_reader2.php"); $file = $this->input->post("f_excel"); $data = new Spreadsheet_Excel_Reader('resources/files/'.$file); $baris = $data->rowcount($sheet_index=0); $countinsert = 0; $countupdate = 0; if (strtolower($data->val(1, 1)) =='kdpsttbpst') { for ($i=2; $i<=$baris; $i++) { $dataArray = array( 'kdpsttbpst'=> $this->retValOrNull($data->val($i, 1)), 'kdjentbpst'=> $this->retValOrNull($data->val($i, 2)), 'nmpsttbpst'=> $this->retValOrNull($data->val($i, 3)), 'kdbiltbpst'=> $this->retValOrNull($data->val($i, 4)), 'kdgrutbpst'=> $this->retValOrNull($data->val($i, 5)), 'kdasntbpst'=> $this->retValOrNull($data->val($i, 6)), 'nomortbpst'=> $this->retValOrNull($data->val($i, 7)) ); $where['kdpsttbpst'] = $this->retValOrNull($data->val($i, 1)); //private $countdatapriv = $this->getcountdata($where,'tbpst'); if ($countdatapriv > 0) { $this->update_import($dataArray,$where,'tbpst'); } else { $privinsert = $this->rhlib->insertRecord('tbpst',$dataArray); } //public $countdatapub = $this->getcountdatapublic($where,'tbpst'); if ($countdatapub > 0) { $this->rhlib->updateRecordPublic('tbpst',$dataArray,$where); } else { $pubinsert = $this->rhlib->insertRecordPublic('tbpst',$dataArray); } if ($countdatapriv > 0 && $countdatapub > 0) { $countupdate = $countupdate + 1; } if ($privinsert && $pubinsert) { $countinsert = $countinsert + 1; } } $return["success"]=true; $return["countinsert"]=$countinsert; $return["countupdate"]=$countupdate; unlink('resources/files/'.$file); } else { $return["success"]=false; $return["countinsert"]=0; $return["countupdate"]=0; } echo json_encode($return); } } } ?>