Initial import
This commit is contained in:
226
one-api/application/controllers/v1/master/Sex.php
Normal file
226
one-api/application/controllers/v1/master/Sex.php
Normal file
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
/*
|
||||
### Sex API
|
||||
- Functions
|
||||
- search
|
||||
- add
|
||||
- edit
|
||||
- delete
|
||||
|
||||
template function {
|
||||
$this->sys_debug();
|
||||
try {
|
||||
if (! $this->isLogin) {
|
||||
$this->sys_error("Invalid Token");
|
||||
exit;
|
||||
}
|
||||
$prm = $this->sys_input;
|
||||
|
||||
} catch(Exception $exc) {
|
||||
$message = $exc->getMessage();
|
||||
$this->sys_error($message);
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
class Sex extends MY_Controller {
|
||||
function index() {
|
||||
echo "Sex Api";
|
||||
}
|
||||
function search() {
|
||||
//$this->sys_debug();
|
||||
try {
|
||||
//1. cek token valid
|
||||
if (! $this->isLogin) {
|
||||
$this->sys_error("Invalid Token");
|
||||
exit;
|
||||
}
|
||||
|
||||
//2. ambil parameter input
|
||||
$prm = $this->sys_input;
|
||||
$s_query = "%" . $prm["query"] . "%";
|
||||
|
||||
//2A. jumlah baris per page default 25 jika tidak di set
|
||||
$row_per_page = 25;
|
||||
if (isset($prm["row_per_page"])) $row_per_page = $prm["row_per_page"];
|
||||
$page = 1;
|
||||
if (isset($prm["page"])) $page = $prm["page"];
|
||||
$tot_count = 0;
|
||||
$sql_param = array($s_query);
|
||||
|
||||
//3. hitung total rows
|
||||
$sql = "select count(*) as tot
|
||||
from m_sex
|
||||
where M_SexName like ? and M_SexIsActive='Y'";
|
||||
$query = $this->db->query($sql,$sql_param);
|
||||
if ($query) {
|
||||
$tot_count = $query->result_array()[0]["tot"];
|
||||
} else {
|
||||
$this->sys_error_db("m_sex count");
|
||||
exit;
|
||||
}
|
||||
|
||||
//4. cari records jika total count > 0
|
||||
$rows = array();
|
||||
if ($tot_count > 0) {
|
||||
//4A. start_limit set ke 0 jika negative atau > total count
|
||||
$start_limit = ($page - 1) * $row_per_page;
|
||||
if ($start_limit > $tot_count) {
|
||||
$start_limit = 0;
|
||||
}
|
||||
if ($start_limit < 0) {
|
||||
$start_limit = 0;
|
||||
}
|
||||
$sql = "select *
|
||||
from m_sex
|
||||
where M_SexName like ? and M_SexIsActive='Y'
|
||||
limit $start_limit,$row_per_page";
|
||||
$query = $this->db->query($sql,$sql_param);
|
||||
if ($query) {
|
||||
$rows = $query->result_array();
|
||||
} else {
|
||||
$this->sys_error_db("m_sex rows");
|
||||
exit;
|
||||
}
|
||||
}
|
||||
$result = array ("total" => $tot_count, "records" => $rows);
|
||||
$this->sys_ok($result);
|
||||
|
||||
} catch(Exception $exc) {
|
||||
$message = $exc->getMessage();
|
||||
$this->sys_error($message);
|
||||
}
|
||||
}
|
||||
function add() {
|
||||
//$this->sys_debug();
|
||||
try {
|
||||
//1. cek token valid
|
||||
if (! $this->isLogin) {
|
||||
$this->sys_error("Invalid Token");
|
||||
exit;
|
||||
}
|
||||
//1a. ambil userID dari token
|
||||
$tokenM_UserID = $this->sys_user["M_UserID"];
|
||||
|
||||
//2. ambil parameter input
|
||||
$prm = $this->sys_input;
|
||||
|
||||
//3. validasi input jika di perlukan, contoh di sini cek M_SexCode harus belum ada
|
||||
$sql = "select count(*) tot
|
||||
from m_sex
|
||||
where M_SexIsActive='Y' and M_SexCode=?";
|
||||
$query = $this->db->query($sql,array($prm["M_SexCode"]));
|
||||
//cek jika query error kirim pesan agar tidak crash
|
||||
if ($query) {
|
||||
$tot_count = $query->result_array()[0]["tot"];
|
||||
if ($tot_count > 0) {
|
||||
$message = "SexCode : " . $prm["M_SexCode"] . " already exists.";
|
||||
$this->sys_error($message);
|
||||
exit;
|
||||
}
|
||||
} else {
|
||||
$this->sys_error_db("m_sex sexCode validation");
|
||||
exit;
|
||||
}
|
||||
//4. insert statement menggunakan explicit field name di sebutkan
|
||||
// untuk menghindari kesalahan karena penambahan field baru
|
||||
$sql = "insert into m_sex(M_SexCode, M_SexName, M_SexHISDefault,
|
||||
M_SexUserID, M_SexLastUpdate) values( ?, ?, ?, ?, now())";
|
||||
//4a. Create statment
|
||||
$query = $this->db->query($sql,
|
||||
array($prm["M_SexCode"], $prm["M_SexName"], $prm["M_SexHisIsDefault"],
|
||||
$tokenM_UserID)
|
||||
);
|
||||
if ($query) {
|
||||
echo json_encode(array(
|
||||
"status" => "OK",
|
||||
"affected_rows" => $this->db->affected_rows(),
|
||||
"inserted_id" => $this->db->insert_id(),
|
||||
"message" => "",
|
||||
"data" => array()
|
||||
));
|
||||
} else {
|
||||
$this->sys_error_db("m_sex insert");
|
||||
exit;
|
||||
}
|
||||
} catch(Exception $exc) {
|
||||
$message = $exc->getMessage();
|
||||
$this->sys_error($message);
|
||||
}
|
||||
}
|
||||
function edit() {
|
||||
//$this->sys_debug();
|
||||
try {
|
||||
//1. cek token valid
|
||||
if (! $this->isLogin) {
|
||||
$this->sys_error("Invalid Token");
|
||||
exit;
|
||||
}
|
||||
//1a. ambil userID dari token
|
||||
$tokenM_UserID = $this->sys_user["M_UserID"];
|
||||
|
||||
//2. ambil parameter input
|
||||
$prm = $this->sys_input;
|
||||
$sql = "update m_sex
|
||||
set M_SexCode = ? , M_SexName = ?, M_SexHISDefault = ?,
|
||||
M_SexLastUpdate = now() , M_SexUserID = ?
|
||||
where M_SexID = ? ";
|
||||
$query = $this->db->query($sql, array(
|
||||
$prm["M_SexCode"], $prm["M_SexName"], $prm["M_SexHISDefault"],
|
||||
$tokenM_UserID, $prm["M_SexID"]
|
||||
));
|
||||
if ($query) {
|
||||
echo json_encode(array(
|
||||
"status" => "OK",
|
||||
"affected_rows" => $this->db->affected_rows(),
|
||||
"message" => "",
|
||||
"data" => array()
|
||||
));
|
||||
} else {
|
||||
$this->sys_error_db("m_sex update");
|
||||
exit;
|
||||
}
|
||||
} catch(Exception $exc) {
|
||||
$message = $exc->getMessage();
|
||||
$this->sys_error($message);
|
||||
}
|
||||
}
|
||||
function delete() {
|
||||
//$this->sys_debug();
|
||||
try {
|
||||
//1. cek token valid
|
||||
if (! $this->isLogin) {
|
||||
$this->sys_error("Invalid Token");
|
||||
exit;
|
||||
}
|
||||
//1a. ambil userID dari token
|
||||
$tokenM_UserID = $this->sys_user["M_UserID"];
|
||||
|
||||
//2. ambil parameter input
|
||||
$prm = $this->sys_input;
|
||||
$sql = "update m_sex
|
||||
set M_SexIsActive = 'N', M_SexLastUpdate = now(),
|
||||
M_SexUserID = ?
|
||||
where M_SexID = ? ";
|
||||
$query = $this->db->query($sql, array(
|
||||
$tokenM_UserID, $prm["M_SexID"]
|
||||
));
|
||||
if ($query) {
|
||||
echo json_encode(array(
|
||||
"status" => "OK",
|
||||
"affected_rows" => $this->db->affected_rows(),
|
||||
"message" => "",
|
||||
"data" => array()
|
||||
));
|
||||
} else {
|
||||
$this->sys_error_db("m_sex delete");
|
||||
exit;
|
||||
}
|
||||
} catch(Exception $exc) {
|
||||
$message = $exc->getMessage();
|
||||
$this->sys_error($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user