203 lines
6.4 KiB
PHP
203 lines
6.4 KiB
PHP
<?php
|
|
class Synctable extends MY_Controller
|
|
{
|
|
var $db;
|
|
var $hostname = "devcpone.aplikasi.web.id";
|
|
public function index()
|
|
{
|
|
echo "RECEIVER API";
|
|
}
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function post($url)
|
|
{
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Content-Type: application/text",
|
|
"Content-Length: ",
|
|
]);
|
|
$result = curl_exec($ch);
|
|
if (curl_error($ch) != "") {
|
|
echo json_encode([
|
|
"status" => "ERR",
|
|
"message" => "Http Error : " . curl_error($ch),
|
|
]);
|
|
curl_close($ch);
|
|
exit();
|
|
}
|
|
curl_close($ch);
|
|
return $result;
|
|
}
|
|
|
|
function get_param_z()
|
|
{
|
|
$body_z = file_get_contents("php://input");
|
|
$body = gzuncompress($body_z);
|
|
return json_decode($body, true);
|
|
}
|
|
|
|
function reply($resp)
|
|
{
|
|
echo json_encode($resp);
|
|
}
|
|
|
|
public function update()
|
|
{
|
|
$this->db->trans_begin();
|
|
$prm = $this->sys_input;
|
|
$param = $this->get_param_z();
|
|
|
|
// print_r($param['data']);
|
|
// exit;
|
|
$datas = $param['data'];
|
|
$table = $param['table'];
|
|
|
|
$total_received = count($datas);
|
|
$total_success = 0;
|
|
|
|
if (!empty($datas)) {
|
|
foreach ($datas as $row) {
|
|
// Insert/Update data ke tabel lokal sesuai nama tabel
|
|
$ok = $this->db->replace("one_lab." . $table, $row);
|
|
if ($ok) {
|
|
$total_success++;
|
|
}
|
|
}
|
|
}
|
|
|
|
// cek apakah tabel sudah ada di sync_update
|
|
$sql = "SELECT count(*) as total FROM one_lab.sync_update WHERE Sync_UpdateNameTable = ?";
|
|
$qry = $this->db->query($sql, [$table]);
|
|
if (!$qry) {
|
|
$this->db->trans_rollback();
|
|
$this->sys_error_db("Failed get data sync_update", $this->db);
|
|
exit;
|
|
}
|
|
$row = $qry->row()->total;
|
|
if ($row > 0) {
|
|
$sql_update = "UPDATE one_lab.sync_update SET
|
|
Sync_UpdateNameTable = ?,
|
|
Sync_UpdateLastDownload = NOW()
|
|
WHERE Sync_UpdateNameTable = ?";
|
|
$qry_update = $this->db->query($sql_update, [
|
|
$table,
|
|
$table
|
|
]);
|
|
if (!$qry_update) {
|
|
$this->db->trans_rollback();
|
|
$this->sys_error_db("Error update sync_update", $this->db);
|
|
exit;
|
|
}
|
|
|
|
$this->reply(
|
|
[
|
|
"status" => "OK",
|
|
"message" => "Berhasil sync update tabel " . $table,
|
|
"total_received" => $total_received,
|
|
"total_success" => $total_success
|
|
]
|
|
);
|
|
} else {
|
|
$sql_insert = "INSERT INTO one_lab.sync_update(
|
|
Sync_UpdateNameTable,
|
|
Sync_UpdateLastDownload) VALUES(?,NOW())";
|
|
$qry_insert = $this->db->query($sql_insert, [
|
|
$table
|
|
]);
|
|
if (!$qry_insert) {
|
|
$this->db->trans_rollback();
|
|
$this->sys_error_db("Error insert sync_update", $this->db);
|
|
exit;
|
|
}
|
|
|
|
$this->reply(
|
|
[
|
|
"status" => "OK",
|
|
"message" => "Berhasil sync insert tabel " . $table,
|
|
"total_received" => $total_received,
|
|
"total_success" => $total_success
|
|
]
|
|
);
|
|
}
|
|
$this->db->trans_commit();
|
|
}
|
|
|
|
public function update_bkp()
|
|
{
|
|
$prm = $this->sys_input;
|
|
|
|
$table = $prm['table'];
|
|
$colupdated = $prm['colupdated'];
|
|
$lastdownload = $prm['lastdownload'];
|
|
|
|
$url = "https://" . $this->hostname . "/one-api/tools/syncupdate/getdata/" . $table . "/" . $colupdated . "/" . $lastdownload;
|
|
$postdata = $this->post($url);
|
|
$rst = json_decode($postdata, true);
|
|
|
|
if ($rst['status'] == 'OK') {
|
|
$records = $rst['data']['record'];
|
|
} else {
|
|
echo $rst['message'];
|
|
exit;
|
|
}
|
|
|
|
if (!empty($records)) {
|
|
foreach ($records as $row) {
|
|
// Insert/Update data ke tabel lokal sesuai nama tabel
|
|
$this->db->replace($table, $row);
|
|
}
|
|
}
|
|
|
|
// cek apakah tabel sudah ada di sync_update
|
|
$sql = "SELECT count(*) as total FROM one_lab.sync_update WHERE Sync_UpdateNameTable = ?";
|
|
$qry = $this->db->query($sql, [$table]);
|
|
if (!$qry) {
|
|
$this->db->trans_rollback();
|
|
$this->sys_error_db("Failed get data ", $this->db);
|
|
exit;
|
|
}
|
|
|
|
$row = $qry->row()->total;
|
|
|
|
if ($row > 0) {
|
|
$sql_update = "UPDATE one_lab.sync_update SET
|
|
Sync_UpdateNameTable = ?,
|
|
Sync_UpdateLastDownload = NOW()
|
|
WHERE Sync_UpdateNameTable = ?";
|
|
$qry_update = $this->db->query($sql_update, [
|
|
$table,
|
|
$table
|
|
]);
|
|
if (!$qry_update) {
|
|
$this->db->trans_rollback();
|
|
$this->sys_error_db("Error update sync_update", $this->db);
|
|
exit;
|
|
}
|
|
|
|
$this->sys_ok("Berhasil update sync data tabel " . $table);
|
|
} else {
|
|
$sql_insert = "INSERT INTO one_lab.sync_update(
|
|
Sync_UpdateNameTable,
|
|
Sync_UpdateLastDownload) VALUES(?,NOW())";
|
|
$qry_insert = $this->db->query($sql_insert, [
|
|
$table
|
|
]);
|
|
if (!$qry_insert) {
|
|
$this->db->trans_rollback();
|
|
$this->sys_error_db("Error update sync_update", $this->db);
|
|
exit;
|
|
}
|
|
|
|
$this->sys_ok("Berhasil insert sync data tabel " . $table);
|
|
}
|
|
}
|
|
}
|