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
|
|
|
}
|
|
|
|
|
|
2026-01-28 17:31:00 +07:00
|
|
|
// Transform Specialty and Occupation
|
|
|
|
|
foreach ($rows as &$row) {
|
|
|
|
|
if (isset($row['Specialty'])) {
|
|
|
|
|
$row['specialty'] = $row['Specialty'];
|
|
|
|
|
$row['specialtyLabel'] = ValueSet::getLabel('specialty', $row['Specialty']) ?? '';
|
|
|
|
|
unset($row['Specialty']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($row['Occupation'])) {
|
|
|
|
|
$row['occupation'] = $row['Occupation'];
|
|
|
|
|
$row['occupationLabel'] = ValueSet::getLabel('occupation', $row['Occupation']) ?? '';
|
|
|
|
|
unset($row['Occupation']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2026-01-28 17:31:00 +07:00
|
|
|
// Transform Specialty and Occupation
|
|
|
|
|
if (isset($row['Specialty'])) {
|
|
|
|
|
$row['specialty'] = $row['Specialty'];
|
|
|
|
|
$row['specialtyLabel'] = ValueSet::getLabel('specialty', $row['Specialty']) ?? '';
|
|
|
|
|
unset($row['Specialty']);
|
|
|
|
|
}
|
|
|
|
|
if (isset($row['Occupation'])) {
|
|
|
|
|
$row['occupation'] = $row['Occupation'];
|
|
|
|
|
$row['occupationLabel'] = ValueSet::getLabel('occupation', $row['Occupation']) ?? '';
|
|
|
|
|
unset($row['Occupation']);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|