2025-09-17 15:50:55 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
|
use CodeIgniter\Controller;
|
2025-09-24 16:15:55 +07:00
|
|
|
|
|
|
|
|
use App\Models\ContactModel;
|
2025-09-17 15:50:55 +07:00
|
|
|
|
|
|
|
|
class Contact extends Controller {
|
|
|
|
|
use ResponseTrait;
|
|
|
|
|
|
2025-09-24 16:15:55 +07:00
|
|
|
protected $contactModel;
|
|
|
|
|
protected $contactRule;
|
|
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
public function __construct() {
|
2025-09-24 16:15:55 +07:00
|
|
|
$this->contactModel = new ContactModel();
|
|
|
|
|
$this->contactRule = [ 'NameFirst' => 'required' ];
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
2025-09-24 16:15:55 +07:00
|
|
|
$model = new ContactModel();
|
|
|
|
|
$rows = $model->getContactsWithDetail();
|
2025-09-17 15:50:55 +07:00
|
|
|
|
|
|
|
|
if (empty($rows)) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => "no Data.",
|
2025-09-18 15:52:37 +07:00
|
|
|
'data' => $rows,
|
2025-09-17 15:50:55 +07:00
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message'=> "fetch success",
|
|
|
|
|
'data' => $rows,
|
|
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show($ContactID = null) {
|
2025-09-24 16:15:55 +07:00
|
|
|
$model = new ContactModel();
|
|
|
|
|
$rows = $model->getContactWithDetail($ContactID);
|
|
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
if (empty($rows)) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => "Data not found.",
|
|
|
|
|
'data' => [],
|
|
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->respond([
|
2025-09-24 16:15:55 +07:00
|
|
|
'status' => 'success',
|
2025-09-17 15:50:55 +07:00
|
|
|
'message'=> "Data fetched successfully",
|
2025-09-24 16:15:55 +07:00
|
|
|
'data' => $rows,
|
2025-09-17 15:50:55 +07:00
|
|
|
], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create() {
|
2025-09-24 16:15:55 +07:00
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
$dataContact = $this->prepareContactData($input);
|
|
|
|
|
$dataContactDetail = $this->prepareContactDetailData($input);
|
2025-09-17 15:50:55 +07:00
|
|
|
|
2025-09-24 16:15:55 +07:00
|
|
|
if (!$this->validateData($dataContact, $this->contactRule)) {
|
|
|
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
|
|
|
}
|
2025-09-17 15:50:55 +07:00
|
|
|
|
2025-09-24 16:15:55 +07:00
|
|
|
try{
|
|
|
|
|
$result = $this->contactModel->create($dataContact, $dataContactDetail);
|
|
|
|
|
if ($result) {
|
|
|
|
|
return $this->respondCreated([
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
'message' => 'Contact created successfully',
|
|
|
|
|
'data' => $dataContact,
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
return $this->failServerError('Failed to create contact');
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
|
|
|
|
} catch (\Throwable $e) {
|
2025-09-24 16:15:55 +07:00
|
|
|
return $this->failServerError('Error: ' . $e->getMessage());
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-24 16:15:55 +07:00
|
|
|
public function update() {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
$ContactID = $input['ContactID'];
|
|
|
|
|
$dataContact = $this->prepareContactData($input);
|
|
|
|
|
$dataContactDetail = $this->prepareContactDetailData($input);
|
2025-09-17 15:50:55 +07:00
|
|
|
|
2025-09-24 16:15:55 +07:00
|
|
|
if (!$this->validateData($dataContact, $this->contactRule)) {
|
|
|
|
|
return $this->failValidationErrors( $this->validator->getErrors());
|
|
|
|
|
}
|
2025-09-17 15:50:55 +07:00
|
|
|
|
2025-09-24 16:15:55 +07:00
|
|
|
try{
|
|
|
|
|
$result = $this->contactModel->update($ContactID, $dataContact, $dataContactDetail);
|
|
|
|
|
if ($result) {
|
|
|
|
|
return $this->respondCreated([
|
|
|
|
|
'status' => 'success',
|
2025-09-25 09:42:17 +07:00
|
|
|
'message' => 'Contact updated successfully',
|
2025-09-24 16:15:55 +07:00
|
|
|
'data' => $dataContact,
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
return $this->failServerError('Failed to create contact');
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
|
|
|
|
} catch (\Throwable $e) {
|
2025-09-24 16:15:55 +07:00
|
|
|
return $this->failServerError('Error: ' . $e->getMessage());
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
2025-09-24 16:15:55 +07:00
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete() {
|
|
|
|
|
try {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
2025-09-18 15:52:37 +07:00
|
|
|
$ContactID = $input["ContactID"];
|
|
|
|
|
if (!$ContactID) {
|
|
|
|
|
return $this->failValidationError('ContactID is required.');
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 15:52:37 +07:00
|
|
|
$contact = $this->db->table('contact')->where('ContactID', $ContactID)->get()->getRow();
|
|
|
|
|
if (!$contact) {
|
|
|
|
|
return $this->failNotFound("data with {$ContactID} not found.");
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 15:52:37 +07:00
|
|
|
$this->db->table('contact')->where('ContactID', $ContactID)->update(['EndDate' => NOW()]);
|
2025-09-17 15:50:55 +07:00
|
|
|
|
|
|
|
|
return $this->respondDeleted([
|
|
|
|
|
'status' => 'success',
|
2025-09-18 15:52:37 +07:00
|
|
|
'message' => "Contact with {$ContactID} deleted successfully."
|
2025-09-17 15:50:55 +07:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
// Ensure rollback if something goes wrong
|
|
|
|
|
if ($this->db->transStatus() !== false) {
|
|
|
|
|
$this->db->transRollback();
|
|
|
|
|
}
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-18 15:52:37 +07:00
|
|
|
private function prepareContactData(array $input): array {
|
2025-09-17 15:50:55 +07:00
|
|
|
$data = [
|
2025-09-18 15:52:37 +07:00
|
|
|
"NameFirst" => $input['NameFirst'] ?? null,
|
|
|
|
|
"NameLast" => $input['NameLast'] ?? null,
|
|
|
|
|
"Title" => $input['Title'] ?? null,
|
|
|
|
|
"Initial" => $input['Initial'] ?? null,
|
|
|
|
|
"Birthdate" => $input['Birthdate'] ?? null,
|
|
|
|
|
"EmailAddress1" => $input['EmailAddress1'] ?? null,
|
|
|
|
|
"EmailAddress2" => $input['EmailAddress2'] ?? null,
|
|
|
|
|
"Phone" => $input["Phone"] ?? null,
|
|
|
|
|
"MobilePhone1" => $input["MobilePhone1"] ?? null,
|
|
|
|
|
"MobilePhone2" => $input["MobilePhone2"] ?? null,
|
|
|
|
|
"Specialty" => $input["Specialty"] ?? null,
|
|
|
|
|
"SubSpecialty" => $input["SubSpecialty"] ?? null,
|
2025-09-17 15:50:55 +07:00
|
|
|
];
|
|
|
|
|
|
2025-09-18 15:52:37 +07:00
|
|
|
if(!empty($input["ContactID"])) { $data["ContactID"] = $input["ContactID"]; }
|
2025-09-17 15:50:55 +07:00
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-18 15:52:37 +07:00
|
|
|
private function prepareContactDetailData(array $input): array {
|
2025-09-24 16:15:55 +07:00
|
|
|
foreach($input['ContactDetail'] as $detail) {
|
|
|
|
|
$data[] = [
|
2025-09-25 09:42:17 +07:00
|
|
|
"SiteID" => $detail['SiteID'] ?? null,
|
2025-09-24 16:15:55 +07:00
|
|
|
"ContactCode" => $detail['ContactCode'] ?? null,
|
|
|
|
|
"ContactEmail" => $detail['ContactEmail'] ?? null,
|
|
|
|
|
"OccupationID" => $detail['OccupationID'] ?? null,
|
|
|
|
|
"JobTitle" => $detail['JobTitle'] ?? null,
|
|
|
|
|
"Department" => $detail['Department'] ?? null,
|
|
|
|
|
"ContactStartDate" => $detail['ContactStartDate'] ?? null,
|
|
|
|
|
"ContactEndDate" => $detail['ContactEndDate'] ?? null,
|
|
|
|
|
];
|
|
|
|
|
}
|
2025-09-17 15:50:55 +07:00
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
}
|