Initial import
This commit is contained in:
363
application/controllers/tools/satu_sehat/Resource.php
Normal file
363
application/controllers/tools/satu_sehat/Resource.php
Normal file
@@ -0,0 +1,363 @@
|
||||
<?php
|
||||
|
||||
class Resource extends MY_Controller
|
||||
{
|
||||
var $base_url, $base_consent_url, $base_oauth_url;
|
||||
var $is_staging, $organizationID;
|
||||
var $dbname;
|
||||
var $tz;
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->tz = "+07:00";
|
||||
$this->is_staging = false;
|
||||
$this->db_onedev = $this->load->database("default", true);
|
||||
$this->base_url = "https://api-satusehat.kemkes.go.id/fhir-r4/v1";
|
||||
$this->base_oauth_url = "https://api-satusehat.kemkes.go.id/oauth2/v1";
|
||||
$this->base_consent_url = "https://api-satusehat.dto.kemkes.go.id/consent/v1";
|
||||
$this->dbname = "one_health";
|
||||
if ($this->is_staging) {
|
||||
$this->base_url = "https://api-satusehat-stg.kemkes.go.id/fhir-r4/v1";
|
||||
$this->base_oauth_url = "https://api-satusehat-stg.kemkes.go.id/oauth2/v1";
|
||||
$this->base_consent_url = "https://api-satusehat-stg.dto.kemkes.go.id/consent/v1";
|
||||
$this->dbname = "one_health_dev";
|
||||
}
|
||||
$this->get_organization_id();
|
||||
}
|
||||
// function get_organization
|
||||
function organization()
|
||||
{
|
||||
$sql = "select M_BranchName, M_BranchAddress
|
||||
from m_branch where M_BranchIsDefault ='Y' and M_BranchIsActive = 'Y'";
|
||||
$qry = $this->db->query($sql);
|
||||
if (!$qry) {
|
||||
echo "ERR : " . $this->db->error()["message"];
|
||||
echo $this->db->last_query();
|
||||
exit;
|
||||
}
|
||||
$rows = $qry->result_array();
|
||||
if (count($rows) > 0) {
|
||||
$this->print_table($rows, array_keys($rows[0]));
|
||||
}
|
||||
$o_resp = $this->ss_get("/Organization/{$this->organizationID}");
|
||||
$resp = $this->objToArray($o_resp);
|
||||
// "Type" => $resp["type"][0]["coding"][0]["display"]
|
||||
$id = $resp["id"];
|
||||
$name = $resp["name"];
|
||||
$x_type = $resp["type"][0]["coding"][0];
|
||||
$type = $x_type["display"];
|
||||
$code = $x_type["code"];
|
||||
$system= $x_type["system"];
|
||||
$rows = [
|
||||
[
|
||||
"ID" => $resp["id"],
|
||||
"Name" => $resp["name"],
|
||||
"Type" => $type . "<br/>" .
|
||||
$code . "<br/>" .
|
||||
$system
|
||||
]
|
||||
];
|
||||
echo "<br/>";
|
||||
echo "Organization ID: " . $this->organizationID ;
|
||||
echo "<br/>";
|
||||
$this->print_table($rows, array_keys($rows[0]));
|
||||
}
|
||||
// get subject in a date
|
||||
function get_encounter_by_date($date = "", $start = 0, $limit = 10)
|
||||
{
|
||||
if ($date == "") $date = date("Y-m-d");
|
||||
$sdate = $date . " 00:00:01";
|
||||
$edate = $date . " 23:59:59";
|
||||
$sql = "select T_OrderHeaderLabNumber LabNo, T_OrderHeaderDate Date,
|
||||
OHPatientMapIhsNumber PatientIHSNumber, M_PatientName,
|
||||
EncounterResponseID EncounterID
|
||||
from t_orderheader
|
||||
join {$this->dbname}.oh_doctor_map
|
||||
on T_OrderHeaderPjM_DoctorID = OHDoctorMapM_DoctorID
|
||||
and T_OrderHeaderDate >= ?
|
||||
and T_OrderHeaderDate <= ?
|
||||
join m_doctor on T_OrderHeaderPjM_DoctorID = M_DoctorID
|
||||
join {$this->dbname}.oh_patient_map
|
||||
on T_OrderHeaderM_PatientID = OHPatientMapM_PatientID
|
||||
join m_patient on T_OrderHeaderM_PatientID = M_PatientID
|
||||
left join {$this->dbname}.encounter
|
||||
on T_OrderHeaderID = EncounterT_OrderHeaderID
|
||||
where EncounterID is not null
|
||||
limit $start,$limit
|
||||
";
|
||||
$qry = $this->db->query($sql, [$sdate, $edate]);
|
||||
if (!$qry) {
|
||||
echo "ERR : " . $this->db->error()["message"];
|
||||
echo $this->db->last_query();
|
||||
exit;
|
||||
}
|
||||
$rows = $qry->result_array();
|
||||
if (count($rows) > 0) {
|
||||
$this->print_table($rows, array_keys($rows[0]));
|
||||
}
|
||||
}
|
||||
|
||||
// helper
|
||||
function get_env()
|
||||
{
|
||||
if ($this->is_staging) {
|
||||
echo "Environment is Staging \n";
|
||||
} else {
|
||||
echo "Environment is Production \n";
|
||||
}
|
||||
}
|
||||
function change_env()
|
||||
{
|
||||
$this->reset_token();
|
||||
$this->put_token();
|
||||
}
|
||||
function ss_post($service, $data)
|
||||
{
|
||||
$token = $this->get_token();
|
||||
$authorization = "Authorization: Bearer " . $token;
|
||||
$xbase_url = $this->base_url;
|
||||
$url = $xbase_url . "$service";
|
||||
$ch = curl_init($url);
|
||||
# Setup request to send json via POST.
|
||||
$payload = json_encode($data);
|
||||
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
|
||||
# Return response instead of printing.
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
# Send request.
|
||||
$result = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
# Print response.
|
||||
$data_rst = json_decode($result);
|
||||
return $data_rst;
|
||||
}
|
||||
function ss_get($service, $debug = "")
|
||||
{
|
||||
$token = $this->get_token();
|
||||
$authorization = "Authorization: Bearer " . $token;
|
||||
$xbase_url = $this->base_url;
|
||||
$url = $xbase_url . "$service";
|
||||
$ch = curl_init($url);
|
||||
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
|
||||
# Return response instead of printing.
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
# Send request.
|
||||
$result = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
# Print response.
|
||||
if ($debug != "") {
|
||||
echo "url : $url \n";
|
||||
print_r($result);
|
||||
}
|
||||
$data_rst = json_decode($result);
|
||||
return $data_rst;
|
||||
}
|
||||
|
||||
function get_organization_id()
|
||||
{
|
||||
|
||||
$sql = "SELECT organizationID
|
||||
FROM {$this->dbname}.organization
|
||||
JOIN m_branch ON organizationM_BranchID = M_BranchID AND M_BranchIsDefault = 'Y' AND M_BranchIsActive = 'Y'
|
||||
WHERE organizationIsActive = 'Y'";
|
||||
$qry = $this->db->query($sql);
|
||||
if (!$qry) {
|
||||
return;
|
||||
}
|
||||
$rows = $qry->result_array();
|
||||
if (count($rows) > 0) {
|
||||
$this->organizationID = $rows[0]["organizationID"];
|
||||
}
|
||||
}
|
||||
function get_client_key($debug = "")
|
||||
{
|
||||
$sql = "select * from {$this->dbname}.client where clientIsActive = 'Y'";
|
||||
$qry = $this->db->query($sql);
|
||||
if (!$qry) {
|
||||
return [false, "", ""];
|
||||
}
|
||||
$rows = $qry->result_array();
|
||||
if (count($rows) == 0) {
|
||||
if ($debug != "") {
|
||||
print_r([false, "", ""]);
|
||||
}
|
||||
return [false, "", ""];
|
||||
}
|
||||
if ($debug != "") {
|
||||
print_r([true, $rows[0]["clientKey"], $rows[0]["clientSecret"]]);
|
||||
}
|
||||
|
||||
return [true, $rows[0]["clientKey"], $rows[0]["clientSecret"]];
|
||||
}
|
||||
|
||||
function reset_token()
|
||||
{
|
||||
$sql = "delete from {$this->dbname}.token ";
|
||||
$qry = $this->db->query($sql);
|
||||
if (!$qry) {
|
||||
echo "ERR : " . $this->db->error()["message"];
|
||||
echo " " . $this->db->last_query();
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function put_token()
|
||||
{
|
||||
$auth_url = $this->base_oauth_url;
|
||||
//api url
|
||||
$url = $auth_url . "/accesstoken?grant_type=client_credentials";
|
||||
list($status, $key, $secret) = $this->get_client_key();
|
||||
$data = [
|
||||
"client_id" => $key,
|
||||
"client_secret" => $secret
|
||||
];
|
||||
$ch = curl_init($url);
|
||||
# setup request to send json via post.
|
||||
$post_data = http_build_query($data);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
// suppress return header
|
||||
// curl_setopt(
|
||||
// $ch,
|
||||
// CURLOPT_HEADER,
|
||||
// array(
|
||||
// 'content-type: application/x-www-form-urlencoded'
|
||||
// )
|
||||
// );
|
||||
# return response instead of printing.
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
# send request.
|
||||
$result = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
# print response.
|
||||
//echo $token_rst->access_token;
|
||||
if ($result) {
|
||||
$token_rst = json_decode($result);
|
||||
|
||||
$sql = "select count(*) as xcount, tokenID
|
||||
from {$this->dbname}.token
|
||||
where
|
||||
tokenIsActive = 'y'
|
||||
";
|
||||
$qry = $this->db_onedev->query($sql);
|
||||
if (!$qry) {
|
||||
echo "get count token error";
|
||||
exit;
|
||||
}
|
||||
|
||||
$rst_count = $qry->row_array();
|
||||
// print_r($token_rst);
|
||||
if ($rst_count['xcount'] > 0) {
|
||||
$sql = "update {$this->dbname}.token set tokenValue = ?, tokenExpired = date_add(now(), interval 50 minute)
|
||||
where tokenID = ?";
|
||||
$qry = $this->db_onedev->query($sql, [$token_rst->access_token, $rst_count['tokenID']]);
|
||||
if (!$qry) {
|
||||
$this->sys_error_db("refresh token error", $this->db_onedev->last_query());
|
||||
exit;
|
||||
}
|
||||
} else {
|
||||
$sql = "update {$this->dbname}.token set tokenIsActive = 'N' where tokenIsActive = 'Y'";
|
||||
$qry = $this->db_onedev->query($sql);
|
||||
if (!$qry) {
|
||||
echo "nonactive token error";
|
||||
exit;
|
||||
}
|
||||
|
||||
$sql = "insert into {$this->dbname}.token(tokenValue,tokenExpired) values(?,date_add(now(), interval 50 minute))";
|
||||
$qry = $this->db_onedev->query($sql, [$token_rst->access_token]);
|
||||
if (!$qry) {
|
||||
echo "insert token error";
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
$sql = "select tokenValue
|
||||
from {$this->dbname}.token
|
||||
where
|
||||
tokenIsActive = 'Y' limit 1
|
||||
";
|
||||
$qry = $this->db_onedev->query($sql);
|
||||
if (!$qry) {
|
||||
echo "get token error";
|
||||
exit;
|
||||
}
|
||||
|
||||
return $qry->row()->tokenValue;
|
||||
}
|
||||
}
|
||||
|
||||
function get_token()
|
||||
{
|
||||
$sql = "SELECT COUNT(*) as xcount, tokenValue
|
||||
FROM {$this->dbname}.token
|
||||
WHERE tokenIsActive = 'Y' AND NOW() < tokenExpired AND tokenValue IS NOT NULL
|
||||
";
|
||||
$qry = $this->db_onedev->query($sql);
|
||||
if (!$qry) {
|
||||
echo "select token error";
|
||||
exit;
|
||||
}
|
||||
|
||||
$data_token = $qry->row_array();
|
||||
//print_r($data_token);
|
||||
if ($data_token['xcount'] > 0) {
|
||||
return $data_token['tokenValue'];
|
||||
} else {
|
||||
return $this->put_token();
|
||||
}
|
||||
}
|
||||
|
||||
public function print_table_style()
|
||||
{
|
||||
echo "
|
||||
<style>
|
||||
th, td {
|
||||
padding: 15px;
|
||||
text-align: left;
|
||||
}
|
||||
tr:nth-child(even) {background-color: #f2f2f2;}
|
||||
table {
|
||||
border: solid 1px ;
|
||||
min-width:600px;
|
||||
}
|
||||
</style>
|
||||
";
|
||||
}
|
||||
public function print_table($rows, $keys)
|
||||
{
|
||||
$this->print_table_style();
|
||||
echo "<table>";
|
||||
echo "<tr>";
|
||||
foreach ($keys as $k) {
|
||||
echo "<td>$k</td>";
|
||||
}
|
||||
echo "</tr>\n";
|
||||
foreach ($rows as $r) {
|
||||
echo "<tr>";
|
||||
foreach ($keys as $k) {
|
||||
echo "<td>" . $r[$k] . "</td>";
|
||||
}
|
||||
echo "</tr>";
|
||||
}
|
||||
echo "</table>";
|
||||
}
|
||||
protected function objToArray($obj)
|
||||
{
|
||||
// Not an object or array
|
||||
if (!is_object($obj) && !is_array($obj)) {
|
||||
return $obj;
|
||||
}
|
||||
|
||||
// Parse array
|
||||
foreach ($obj as $key => $value) {
|
||||
$arr[$key] = $this->objToArray($value);
|
||||
}
|
||||
|
||||
// Return parsed array
|
||||
return $arr;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user