196 lines
6.5 KiB
PHP
196 lines
6.5 KiB
PHP
<?php
|
|
class Carousel_upload extends MY_Controller
|
|
{
|
|
var $url;
|
|
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
//$this->url= "https://devregonline.pramita.co.id/one-api/tools/regonline/r_carousel";
|
|
$this->url= "https://mobile.pramita.co.id/one-api/tools/regonline/r_carousel";
|
|
}
|
|
function step_debug($rows)
|
|
{
|
|
print_r($rows);
|
|
exit();
|
|
}
|
|
function index() {
|
|
$branchCode = $this->get_branch();
|
|
$date = $this->get_heartbeat();
|
|
$sql = "select max(M_CarouselLastUpdated) LastUpdated, max(M_CarouselCreated) LastCreated
|
|
from m_carousel";
|
|
$qry = $this->db->query($sql,[$date]);
|
|
if (!$qry) {
|
|
echo date("Y-m-d H:i:s") .
|
|
" Err : " .
|
|
$this->db->error()["message"] .
|
|
"\n";
|
|
exit();
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
$this->log("No Carousel updated");
|
|
exit;
|
|
}
|
|
if ($rows[0]["LastUpdated"] < $date && $rows[0]["LastCreated"] < $date) {
|
|
$this->log("No Carousel updated or created neeed to be uploaded");
|
|
exit;
|
|
}
|
|
$sql = "select M_CarouselID, M_CarouselImageUrl, ifnull(M_CarouselOrder,0) M_CarouselOrder
|
|
from m_carousel where M_CarouselIsActive='Y' and M_CarouselStatus = 'Y'
|
|
order by M_CarouselOrder, M_CarouselID ";
|
|
|
|
$qry = $this->db->query($sql,[$date]);
|
|
if (!$qry) {
|
|
echo date("Y-m-d H:i:s") .
|
|
" Err : " .
|
|
$this->db->error()["message"] .
|
|
"\n";
|
|
exit();
|
|
}
|
|
$rows = $qry->result_array();
|
|
$data = [];
|
|
$counter_data =0;
|
|
$all_name = "";
|
|
$arr_real_name = [];
|
|
foreach($rows as $r) {
|
|
$order = $r["M_CarouselOrder"];
|
|
$id = $r["M_CarouselID"];
|
|
$name = sprintf("%02d",$order) . "-" . sprintf("%03d",$id) . "-" . $r["M_CarouselImageUrl"];
|
|
$real_name = "/home/one/project/one/one-media/one-regonline/" . $r["M_CarouselImageUrl"];
|
|
if(! file_exists($real_name)) {
|
|
$this->log("File $real_name not exits");
|
|
} else {
|
|
$all_name .= $name;
|
|
$data[] = array(
|
|
"name" => $name,
|
|
"branchCode" => $branchCode,
|
|
"content" => base64_encode(file_get_contents($real_name))
|
|
);
|
|
$counter_data++;
|
|
$arr_real_name[] = $real_name;
|
|
}
|
|
}
|
|
if ($counter_data == 0) {
|
|
$this->log("No Carousel to upload");
|
|
exit;
|
|
}
|
|
$msg = "Uploaded {$counter_data} image.";
|
|
$j_data = json_encode(array(
|
|
"data" => $data,
|
|
"md5" => md5($all_name . $branchCode . "545123")
|
|
));
|
|
$j_resp = $this->post($this->url, $j_data);
|
|
$resp = json_decode($j_resp,true);
|
|
$json_error = json_last_error_msg();
|
|
if (json_last_error() != 0) {
|
|
$msg .= " [JSON ERR] $json_error " ;
|
|
}
|
|
if($resp["status"] == "OK") {
|
|
$msg .= " [Success] ";
|
|
$this->update_heartbeat("OK","");
|
|
} else {
|
|
$msg .= " [ERR] " ;
|
|
if($resp["message"] != "") $msg .= " " . $resp["message"];
|
|
$this->update_heartbeat("ERR",$msg);
|
|
}
|
|
$this->log($msg);
|
|
$this->log("Uploaded files : " . implode(", ", $arr_real_name));
|
|
}
|
|
function log($msg) {
|
|
$date = date("Y-m-d H:i:s");
|
|
echo "$date $msg\n";
|
|
}
|
|
function get_branch() {
|
|
$sql = "select M_BranchCode from m_branch where M_BranchIsActive = 'Y' and M_BranchIsDefault = 'Y'";
|
|
$qry = $this->db->query($sql);
|
|
if (!$qry) {
|
|
echo date("Y-m-d H:i:s") .
|
|
" Err : " .
|
|
$this->db->error()["message"] .
|
|
"\n";
|
|
exit();
|
|
}
|
|
$rows = $qry->result_array();
|
|
if(count($rows) == 0) {
|
|
echo date("Y-m-d H:i:s") .
|
|
" Err : No Default branch" .
|
|
"\n";
|
|
exit();
|
|
}
|
|
return $rows[0]["M_BranchCode"];
|
|
}
|
|
function get_heartbeat() {
|
|
|
|
$sql="select * from regonline.heartbeat where heartbeatCode='CAROUSEL'";
|
|
$qry = $this->db->query($sql);
|
|
if (!$qry) {
|
|
echo date("Y-m-d H:i:s") .
|
|
" Err : " .
|
|
$this->db->error()["message"] .
|
|
"\n";
|
|
exit();
|
|
}
|
|
$rows = $qry->result_array();
|
|
if (count($rows) == 0) {
|
|
$sql = "insert into regonline.heartbeat(heartbeatCode,heartbeatStatus,heartbeatExecuted,heartbeatMessage)
|
|
values('CAROUSEL','OK','1971-01-01 00:00:01','')";
|
|
$qry = $this->db->query($sql);
|
|
if (!$qry) {
|
|
echo date("Y-m-d H:i:s") .
|
|
" Err : " .
|
|
$this->db->error()["message"] .
|
|
"\n";
|
|
exit();
|
|
}
|
|
return "1971-01-01 00:00:01";
|
|
}
|
|
return $rows[0]["heartbeatExecuted"];
|
|
}
|
|
function update_heartbeat( $status, $msg)
|
|
{
|
|
$type = "CAROUSEL";
|
|
if ($status == "OK") {
|
|
$sql = "update regonline.heartbeat set heartbeatExecuted = now(), heartbeatStatus = ? , heartbeatMessage = ?
|
|
where heartbeatCode = ?";
|
|
$qry = $this->db->query($sql, array($status, $msg, $type));
|
|
} else {
|
|
$sql = "update regonline.heartbeat set heartbeatStatus = ? , heartbeatMessage = ?
|
|
where heartbeatCode = ?";
|
|
$qry = $this->db->query($sql, array($status, $msg, $type));
|
|
}
|
|
if (!$qry) {
|
|
echo date("Y-m-d H:i:s") . " \t Error $type : {$this->db->error()['message']}\n";
|
|
}
|
|
}
|
|
|
|
function post($url, $data, $timeout = 60, $c_timeout = 5)
|
|
{
|
|
$zdata = gzcompress($data);
|
|
$this->log("Upload to $url : " . ( round(strlen($zdata) / 1024) ) . " kB");
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $c_timeout);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $zdata);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Content-Type: application/octet",
|
|
"Content-Length: " . strlen($zdata),
|
|
]);
|
|
$result = curl_exec($ch);
|
|
$err_msg = curl_error($ch);
|
|
if ($err_msg != "") {
|
|
return json_encode([
|
|
"status" => "ERR",
|
|
"message" => $err_msg,
|
|
"url" => $url,
|
|
"data" => json_decode($data, true),
|
|
]);
|
|
}
|
|
return $result;
|
|
}
|
|
}
|
|
|