clqms-be/app/Controllers/Contact.php

222 lines
7.7 KiB
PHP
Raw Normal View History

2025-09-17 15:50:55 +07:00
<?php
namespace App\Controllers;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller;
use CodeIgniter\Database\RawSql;
class Contact extends Controller {
use ResponseTrait;
public function __construct() {
$this->db = \Config\Database::connect();
$this->rulesContact = [
'NameFirst' => 'required'
];
}
public function index() {
2025-09-18 15:52:37 +07:00
$sql = $this->db->table('contact');
$sql->select("*")
->join("contactdetail", "contact.ContactID=contactdetail.ContactID", "left")
->join("occupation","contactdetail.OccupationID=occupation.OccupationID", "left");
$rows = $sql->get()->getRowArray();
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-18 15:52:37 +07:00
$rows=$this->db->table('contact')
->select("*")
->join("contactdetail", "contact.ContactID=contactdetail.ContactID", "left")
->join("occupation","occupation.OccupationID=contactdetail.OccupationID", "left")
2025-09-17 15:50:55 +07:00
->where('contact.ContactID', (int) $ContactID)
->get()->getRowArray();
if (empty($rows)) {
return $this->respond([
'status' => 'success',
'message' => "Data not found.",
'data' => [],
], 200);
}
return $this->respond([
'status' => 'success',
'message'=> "Data fetched successfully",
'data' => $rows,
], 200);
}
public function create() {
try {
$input = $this->request->getJSON(true);
// Prepare data
2025-09-18 15:52:37 +07:00
$dataContact = $this->prepareContactData($input);
$dataContactDetail = $this->prepareContactDetailData($input);
2025-09-17 15:50:55 +07:00
2025-09-18 15:52:37 +07:00
if (!$this->validateData($dataContact, $this->rulesContact)) {
2025-09-17 15:50:55 +07:00
return $this->failValidationErrors($this->validator->getErrors());
}
// Start transaction
$this->db->transStart();
2025-09-18 15:52:37 +07:00
$this->db->table('contact')->insert($dataContact);
$newContactID = $this->db->insertID();
2025-09-17 15:50:55 +07:00
2025-09-18 15:52:37 +07:00
if (!empty($dataContactDetail)) {
$dataContactDetail['ContactID'] = $newContactID;
$this->db->table('contactdetail')->insert($dataContactDetail);
2025-09-17 15:50:55 +07:00
}
if ($this->db->transStatus() === false) {
$dbError = $this->db->error();
return $this->failServerError(
2025-09-18 15:52:37 +07:00
'Failed to create data (transaction rolled back): ' . ( $dbError['message'] ?? 'Unknown database error')
2025-09-17 15:50:55 +07:00
);
}
2025-09-18 15:52:37 +07:00
// Complete transaction
$this->db->transComplete();
2025-09-17 15:50:55 +07:00
return $this->respondCreated([
'status' => 'success',
2025-09-18 15:52:37 +07:00
'message' => 'Contact created successfully',
'data' => $dataContact,
2025-09-17 15:50:55 +07:00
], 201);
} 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());
}
}
public function update() {
try {
$input = $this->request->getJSON(true);
// Prepare data
2025-09-18 15:52:37 +07:00
$dataContact = $this->prepareContactData($input);
$dataContactDetail = $this->prepareContactDetailData($input);
2025-09-17 15:50:55 +07:00
2025-09-18 15:52:37 +07:00
if (!$this->validateData($dataContact, $this->rulesContact)) {
2025-09-17 15:50:55 +07:00
return $this->failValidationErrors( $this->validator->getErrors());
}
// Start transaction
$this->db->transStart();
2025-09-18 15:52:37 +07:00
$this->db->table('contact')->where('ContactID', $dataContact["ContactID"])->update($dataContact);
2025-09-17 15:50:55 +07:00
// Insert address if available
2025-09-18 15:52:37 +07:00
if (!empty($dataContactDetail)) {
$dataContactDetail['ContactID'] = $input["ContactID"];
$this->db->table('contactdetail')->upsert($dataContactDetail);
2025-09-17 15:50:55 +07:00
}
// Complete transaction
$this->db->transComplete();
if ($this->db->transStatus() === false) {
$dbError = $this->db->error();
return $this->failServerError(
2025-09-18 15:52:37 +07:00
'Failed to update contact data (transaction rolled back): ' . ($dbError['message'] ?? 'Unknown database error')
2025-09-17 15:50:55 +07:00
);
}
return $this->respondCreated([
'status' => 'success',
2025-09-18 15:52:37 +07:00
'message' => 'Contact updated successfully',
'data' => $dataContact,
2025-09-17 15:50:55 +07:00
], 201);
} 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());
}
}
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-17 15:50:55 +07:00
$data = [
2025-09-18 15:52:37 +07:00
"Code" => $input['Code'] ?? null,
"ContactEmail" => $input['ContactEmail'] ?? null,
"OccupationID" => $input['OccupationID'] ?? null,
"JobTitle" => $input['JobTitle'] ?? null,
"Department" => $input['Department'] ?? null,
"ContactStartDate" => $input['ContactStartDate'] ?? null,
"ContactEndDate" => $input['ContactEndDate'] ?? null,
2025-09-17 15:50:55 +07:00
];
return $data;
}
}