clqms-be/app/Controllers/Contact/ContactController.php

106 lines
4.1 KiB
PHP
Raw Normal View History

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;
use App\Controllers\BaseController;
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
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;
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();
$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);
2025-09-17 15:50:55 +07:00
if (empty($rows)) {
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => [] ], 200);
2025-09-17 15:50:55 +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']);
}
}
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();
$row = $model->getContactWithDetail($ContactID);
2025-09-24 16:15:55 +07:00
if (empty($row)) {
return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => null ], 200);
2025-09-17 15:50:55 +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']);
}
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"];
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());
}
}
public function create() {
2025-09-25 13:30:15 +07:00
$input = $this->request->getJSON(true);
if (!$this->validateData($input, $this->rules)) { return $this->failValidationErrors($this->validator->getErrors()); }
2025-09-25 13:30:15 +07:00
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());
}
}
2025-09-25 13:30:15 +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) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
2025-09-24 16:15:55 +07:00
}
2025-09-17 15:50:55 +07:00
}
}