2026-03-16 07:24:50 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Controllers\Contact;
|
|
|
|
|
|
|
|
|
|
use App\Traits\ResponseTrait;
|
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
|
use App\Libraries\ValueSet;
|
|
|
|
|
use App\Models\Contact\ContactModel;
|
|
|
|
|
|
2026-04-06 15:38:30 +07:00
|
|
|
class ContactController extends BaseController {
|
2026-03-16 07:24:50 +07:00
|
|
|
use ResponseTrait;
|
|
|
|
|
|
2026-04-06 15:38:30 +07:00
|
|
|
protected $db;
|
|
|
|
|
protected $model;
|
|
|
|
|
protected $rules;
|
|
|
|
|
protected $patchRules;
|
2026-03-16 07:24:50 +07:00
|
|
|
|
2026-04-06 15:38:30 +07:00
|
|
|
public function __construct() {
|
|
|
|
|
$this->db = \Config\Database::connect();
|
|
|
|
|
$this->model = new ContactModel();
|
|
|
|
|
$this->rules = [ 'NameFirst' => 'required' ];
|
|
|
|
|
$this->patchRules = [ 'NameFirst' => 'permit_empty' ];
|
|
|
|
|
}
|
2026-03-16 07:24:50 +07:00
|
|
|
|
|
|
|
|
public function index() {
|
|
|
|
|
$ContactName = $this->request->getVar('ContactName');
|
|
|
|
|
$Specialty = $this->request->getVar('Specialty');
|
|
|
|
|
$rows = $this->model->getContacts($ContactName, $Specialty);
|
|
|
|
|
|
|
|
|
|
if (empty($rows)) {
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rows = ValueSet::transformLabels($rows, [
|
|
|
|
|
'Specialty' => 'specialty',
|
|
|
|
|
'Occupation' => 'occupation',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show($ContactID = null) {
|
|
|
|
|
$model = new ContactModel();
|
|
|
|
|
$row = $model->getContactWithDetail($ContactID);
|
|
|
|
|
|
|
|
|
|
if (empty($row)) {
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$row = ValueSet::transformLabels([$row], [
|
|
|
|
|
'Specialty' => 'specialty',
|
|
|
|
|
'Occupation' => 'occupation',
|
|
|
|
|
])[0];
|
|
|
|
|
|
|
|
|
|
return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $row ], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete() {
|
|
|
|
|
try {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
$ContactID = $input["ContactID"];
|
|
|
|
|
if (!$ContactID) { return $this->failValidationErrors('ContactID is required.'); }
|
|
|
|
|
$this->model->delete($ContactID);
|
|
|
|
|
return $this->respondDeleted([ 'status' => 'success', 'message' => "Contact with {$ContactID} deleted successfully."]);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create() {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
|
|
|
|
|
try {
|
|
|
|
|
$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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 15:58:56 +07:00
|
|
|
public function update($ContactID = null) {
|
|
|
|
|
$input = $this->request->getJSON(true);
|
|
|
|
|
if (!$ContactID || !ctype_digit((string) $ContactID)) {
|
|
|
|
|
return $this->respond([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'ContactID is required and must be a valid integer',
|
|
|
|
|
'data' => []
|
|
|
|
|
], 400);
|
|
|
|
|
}
|
2026-04-06 15:38:30 +07:00
|
|
|
if (empty($input) || !is_array($input)) {
|
|
|
|
|
return $this->failValidationErrors('No data provided for update.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$validationInput = array_intersect_key($input, $this->patchRules);
|
|
|
|
|
if (!empty($validationInput) && !$this->validateData($validationInput, $this->patchRules)) {
|
|
|
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 15:58:56 +07:00
|
|
|
$input['ContactID'] = (int) $ContactID;
|
|
|
|
|
try {
|
|
|
|
|
$this->model->saveContact($input);
|
|
|
|
|
$id = $input['ContactID'];
|
2026-03-16 07:24:50 +07:00
|
|
|
return $this->respondCreated([ 'status' => 'success', 'message' => 'data updated successfully', 'data' => $id ], 201);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|