2025-09-17 15:50:55 +07:00
|
|
|
<?php
|
2025-10-16 11:09:36 +07:00
|
|
|
namespace App\Controllers\Contact;
|
2025-09-17 15:50:55 +07:00
|
|
|
|
|
|
|
|
use CodeIgniter\API\ResponseTrait;
|
2025-10-15 11:01:52 +07:00
|
|
|
use App\Controllers\BaseController;
|
2026-01-28 17:31:00 +07:00
|
|
|
use App\Libraries\ValueSet;
|
2025-10-14 18:53:06 +07:00
|
|
|
use App\Models\Contact\ContactModel;
|
2025-09-17 15:50:55 +07:00
|
|
|
|
2026-01-05 16:55:34 +07:00
|
|
|
class ContactController extends BaseController {
|
2025-09-17 15:50:55 +07:00
|
|
|
use ResponseTrait;
|
|
|
|
|
|
2025-09-25 13:30:15 +07:00
|
|
|
protected $db;
|
2025-10-13 13:59:52 +07:00
|
|
|
protected $model;
|
2025-10-15 11:01:52 +07:00
|
|
|
protected $rules;
|
2025-09-24 16:15:55 +07:00
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
public function __construct() {
|
2025-09-25 13:30:15 +07:00
|
|
|
$this->db = \Config\Database::connect();
|
2025-10-13 13:59:52 +07:00
|
|
|
$this->model = new ContactModel();
|
2025-10-15 11:01:52 +07:00
|
|
|
$this->rules = [ 'NameFirst' => 'required' ];
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index() {
|
2025-09-26 16:41:55 +07:00
|
|
|
$ContactName = $this->request->getVar('ContactName');
|
|
|
|
|
$Specialty = $this->request->getVar('Specialty');
|
2025-10-13 13:59:52 +07:00
|
|
|
$rows = $this->model->getContacts($ContactName, $Specialty);
|
2026-01-28 17:31:00 +07:00
|
|
|
|
2025-09-17 15:50:55 +07:00
|
|
|
if (empty($rows)) {
|
2025-10-15 11:01:52 +07:00
|
|
|
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200);
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
|
|
|
|
|
refactor(api): standardize ValueSet label transformation across controllers
Replace manual label lookup code with ValueSet::transformLabels() helper
for consistent API responses across all controllers.
Updated controllers:
- ContactController: Specialty, Occupation
- OrderTestController: Priority, OrderStatus
- PatientController: Sex
- ContainerDefController: ConCategory, CapColor, ConSize
- SpecimenCollectionController: CollectionMethod, Additive, SpecimenRole
- SpecimenController: SpecimenType, SpecimenStatus, BodySite
- SpecimenStatusController: Status, Activity
- DemoOrderController: Priority, OrderStatus
- TestMapController: HostType, ClientType
- TestsController: Reference range fields
Also updated api-docs.yaml field naming convention to PascalCase
2026-01-29 09:05:40 +07:00
|
|
|
$rows = ValueSet::transformLabels($rows, [
|
|
|
|
|
'Specialty' => 'specialty',
|
|
|
|
|
'Occupation' => 'occupation',
|
|
|
|
|
]);
|
2026-01-28 17:31:00 +07:00
|
|
|
|
2025-10-15 11:01:52 +07:00
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200);
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show($ContactID = null) {
|
2025-09-24 16:15:55 +07:00
|
|
|
$model = new ContactModel();
|
2025-12-29 12:55:31 +07:00
|
|
|
$row = $model->getContactWithDetail($ContactID);
|
2025-09-24 16:15:55 +07:00
|
|
|
|
2025-12-29 12:55:31 +07:00
|
|
|
if (empty($row)) {
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200);
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
|
|
|
|
|
refactor(api): standardize ValueSet label transformation across controllers
Replace manual label lookup code with ValueSet::transformLabels() helper
for consistent API responses across all controllers.
Updated controllers:
- ContactController: Specialty, Occupation
- OrderTestController: Priority, OrderStatus
- PatientController: Sex
- ContainerDefController: ConCategory, CapColor, ConSize
- SpecimenCollectionController: CollectionMethod, Additive, SpecimenRole
- SpecimenController: SpecimenType, SpecimenStatus, BodySite
- SpecimenStatusController: Status, Activity
- DemoOrderController: Priority, OrderStatus
- TestMapController: HostType, ClientType
- TestsController: Reference range fields
Also updated api-docs.yaml field naming convention to PascalCase
2026-01-29 09:05:40 +07:00
|
|
|
$row = ValueSet::transformLabels([$row], [
|
|
|
|
|
'Specialty' => 'specialty',
|
|
|
|
|
'Occupation' => 'occupation',
|
|
|
|
|
])[0];
|
2026-01-28 17:31:00 +07:00
|
|
|
|
2025-12-29 12:55:31 +07:00
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $row ], 200);
|
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"];
|
2025-10-15 11:01:52 +07:00
|
|
|
if (!$ContactID) { return $this->failValidationErrors('ContactID is required.'); }
|
|
|
|
|
$this->model->delete($ContactID);
|
|
|
|
|
return $this->respondDeleted([ 'status' => 'success', 'message' => "Contact with {$ContactID} deleted successfully."]);
|
2025-09-17 15:50:55 +07:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-15 11:01:52 +07:00
|
|
|
public function create() {
|
2025-09-25 13:30:15 +07:00
|
|
|
$input = $this->request->getJSON(true);
|
2025-10-15 11:01:52 +07:00
|
|
|
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
|
2025-09-25 13:30:15 +07:00
|
|
|
try {
|
2025-10-15 11:01:52 +07:00
|
|
|
$id = $this->model->saveContact($input,true);
|
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => 'data created successfully', 'data' => $id ], 201);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-25 13:30:15 +07:00
|
|
|
|
2025-10-15 11:01:52 +07:00
|
|
|
public function update() {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
|
|
|
|
|
try {
|
|
|
|
|
$this->model->saveContact($input);
|
|
|
|
|
$id = $input['ContactID'];
|
|
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $id ], 201);
|
2025-09-25 13:30:15 +07:00
|
|
|
} catch (\Throwable $e) {
|
2025-10-15 11:01:52 +07:00
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
2025-09-24 16:15:55 +07:00
|
|
|
}
|
2025-09-17 15:50:55 +07:00
|
|
|
}
|
2026-01-05 16:55:34 +07:00
|
|
|
}
|