Files
2026-05-18 14:20:43 +07:00

237 lines
7.8 KiB
PHP

<?php
class Template extends MY_Controller
{
var $db_onedev;
var $db_onedev_log;
public function index()
{
echo "INFORM CONSENT TEMPLATE API";
}
public function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("onedev", true);
$this->db_onedev_log = $this->load->database("one_lab_log", true);
}
public function types()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT
M_InformConsentType,
M_InformConsentTitle,
M_InformConsentID
FROM m_informconsent
WHERE M_InformConsentIsActive = 'Y'
ORDER BY M_InformConsentType ASC";
$rows = $this->db_onedev->query($sql)->result_array();
$result = array(
"total" => count($rows),
"records" => $rows
);
$this->sys_ok($result);
} catch (Exception $exc) {
$this->sys_error($exc->getMessage());
}
}
public function detail()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$type = isset($prm["type"]) ? trim($prm["type"]) : "";
if ($type === "") {
$this->sys_error("type harus diisi");
exit;
}
$sql = "SELECT
M_InformConsentID,
M_InformConsentType,
M_InformConsentTitle,
M_InformConsentContent,
M_InformConsentIsActive,
M_InformConsentCreated,
M_InformConsentLastUpdated
FROM m_informconsent
WHERE M_InformConsentType = ?
AND M_InformConsentIsActive = 'Y'
LIMIT 1";
$row = $this->db_onedev->query($sql, array($type))->row_array();
if (!$row) {
$this->sys_ok(array("total" => 0, "records" => array()));
exit;
}
$this->sys_ok(array("total" => 1, "records" => $row));
} catch (Exception $exc) {
$this->sys_error($exc->getMessage());
}
}
public function add()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$userID = intval($this->sys_user["M_UserID"]);
$type = isset($prm["type"]) ? trim($prm["type"]) : "";
$title = isset($prm["title"]) ? trim($prm["title"]) : "";
$content = isset($prm["content"]) ? trim($prm["content"]) : "";
if ($type === "" || $title === "" || $content === "") {
$this->sys_error("type, title, content wajib diisi");
exit;
}
$this->db_onedev->trans_begin();
$sql = "INSERT INTO m_informconsent (
M_InformConsentType,
M_InformConsentTitle,
M_InformConsentContent,
M_InformConsentIsActive,
M_InformConsentCreated,
M_InformConsentCreatedUserID,
M_InformConsentLastUpdated,
M_InformConsentLastUpdatedUserID
) VALUES (?, ?, ?, 'Y', NOW(), ?, NOW(), ?)";
$qry = $this->db_onedev->query($sql, array($type, $title, $content, $userID, $userID));
if (!$qry) {
$this->db_onedev->trans_rollback();
$this->sys_error_db("insert m_informconsent", $this->db_onedev);
exit;
}
$newID = intval($this->db_onedev->insert_id());
$after = array(
"M_InformConsentID" => $newID,
"M_InformConsentType" => $type,
"M_InformConsentTitle" => $title,
"M_InformConsentContent" => $content,
"M_InformConsentIsActive" => "Y"
);
$this->writeLog($newID, $type, array(), $after, $userID);
$this->db_onedev->trans_commit();
$this->sys_ok(array("total" => 1, "records" => array("M_InformConsentID" => $newID)));
} catch (Exception $exc) {
if ($this->db_onedev->trans_status() === false) {
$this->db_onedev->trans_rollback();
}
$this->sys_error($exc->getMessage());
}
}
public function save()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$userID = intval($this->sys_user["M_UserID"]);
$id = isset($prm["id"]) ? intval($prm["id"]) : 0;
$title = isset($prm["title"]) ? trim($prm["title"]) : "";
$content = isset($prm["content"]) ? trim($prm["content"]) : "";
if ($id <= 0 || $title === "" || $content === "") {
$this->sys_error("id, title, content wajib diisi");
exit;
}
$before = $this->db_onedev->query(
"SELECT * FROM m_informconsent WHERE M_InformConsentID = ? LIMIT 1",
array($id)
)->row_array();
if (!$before) {
$this->sys_error("Template tidak ditemukan");
exit;
}
$this->db_onedev->trans_begin();
$sql = "UPDATE m_informconsent
SET
M_InformConsentTitle = ?,
M_InformConsentContent = ?,
M_InformConsentLastUpdated = NOW(),
M_InformConsentLastUpdatedUserID = ?
WHERE M_InformConsentID = ?";
$qry = $this->db_onedev->query($sql, array($title, $content, $userID, $id));
if (!$qry) {
$this->db_onedev->trans_rollback();
$this->sys_error_db("update m_informconsent", $this->db_onedev);
exit;
}
$after = $this->db_onedev->query(
"SELECT * FROM m_informconsent WHERE M_InformConsentID = ? LIMIT 1",
array($id)
)->row_array();
$this->writeLog(
$id,
$before["M_InformConsentType"],
$before,
$after,
$userID
);
$this->db_onedev->trans_commit();
$this->sys_ok(array(
"total" => 1,
"affected_rows" => $this->db_onedev->affected_rows()
));
} catch (Exception $exc) {
if ($this->db_onedev->trans_status() === false) {
$this->db_onedev->trans_rollback();
}
$this->sys_error($exc->getMessage());
}
}
private function writeLog($informConsentID, $type, $before, $after, $userID)
{
$sql = "INSERT INTO log_m_informconsent (
Log_M_InformConsentM_InformConsentID,
Log_M_InformConsentType,
Log_M_InformConsentDate,
Log_M_InformConsentBeforeJSON,
Log_M_InformConsentAfterJSON,
Log_M_InformConsentUserID
) VALUES (?, ?, NOW(), ?, ?, ?)";
$this->db_onedev_log->query($sql, array(
intval($informConsentID),
strval($type),
json_encode($before),
json_encode($after),
intval($userID)
));
}
}