2025-10-01 12:40:05 +07:00
|
|
|
<?php
|
2025-10-15 11:01:52 +07:00
|
|
|
namespace App\Models\Patient;
|
2025-10-01 12:40:05 +07:00
|
|
|
|
2025-10-15 11:01:52 +07:00
|
|
|
use App\Models\BaseModel;
|
2026-01-12 16:53:41 +07:00
|
|
|
use App\Libraries\ValueSet;
|
2025-10-16 10:50:09 +07:00
|
|
|
|
|
|
|
|
use App\Models\Patient\PatAttModel;
|
|
|
|
|
use App\Models\Patient\PatComModel;
|
|
|
|
|
use App\Models\Patient\PatIdtModel;
|
|
|
|
|
|
2025-10-14 18:53:06 +07:00
|
|
|
class PatientModel extends BaseModel {
|
2025-10-01 12:40:05 +07:00
|
|
|
protected $table = 'patient';
|
|
|
|
|
protected $primaryKey = 'InternalPID';
|
2026-01-12 16:53:41 +07:00
|
|
|
protected $allowedFields = ['PatientID', 'AlternatePID', 'Prefix', 'NameFirst', 'NameMiddle', 'NameMaiden', 'NameLast', 'Suffix', 'NameAlias', 'Sex', 'Birthdate', 'PlaceOfBirth', 'Street_1', 'Street_2', 'Street_3',
|
2025-10-01 12:40:05 +07:00
|
|
|
'City', 'Province', 'ZIP', 'EmailAddress1', 'EmailAddress2', 'Phone', 'MobilePhone', 'Custodian', 'AccountNumber', 'Country', 'Race', 'MaritalStatus', 'Religion', 'Ethnic', 'Citizenship',
|
2026-01-12 16:53:41 +07:00
|
|
|
'DeathIndicator', 'TimeOfDeath', 'LinkTo', 'CreateDate', 'DelDate' ];
|
2025-10-13 15:09:55 +07:00
|
|
|
|
|
|
|
|
protected $useTimestamps = true;
|
|
|
|
|
protected $createdField = 'CreateDate';
|
|
|
|
|
protected $updatedField = '';
|
2025-10-14 10:48:20 +07:00
|
|
|
protected $useSoftDeletes = true;
|
|
|
|
|
protected $deletedField = 'DelDate';
|
2025-10-10 16:47:39 +07:00
|
|
|
|
2025-10-01 12:40:05 +07:00
|
|
|
public function getPatients($filters = []) {
|
2025-11-24 15:17:33 +07:00
|
|
|
$qname = "CONCAT_WS(' ', IFNULL(Prefix,''), IFNULL(NameFirst,''), IFNULL(NameMiddle,''), IFNULL(NameLast,''), IFNULL(NameMaiden,''), IFNULL(Suffix,''))";
|
2025-10-01 12:40:05 +07:00
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
$this->select("InternalPID, PatientID, $qname as FullName, Sex, Birthdate, EmailAddress1 as Email, MobilePhone");
|
2025-10-01 12:40:05 +07:00
|
|
|
|
|
|
|
|
if (!empty($filters['Name'])) {
|
2025-10-16 13:22:28 +07:00
|
|
|
$this->like($qname, $filters['Name'], 'both');
|
2025-10-01 12:40:05 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($filters['InternalPID'])) {
|
2025-10-16 13:22:28 +07:00
|
|
|
$this->where('InternalPID', $filters['InternalPID']);
|
2025-10-01 12:40:05 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($filters['PatientID'])) {
|
2025-10-16 13:22:28 +07:00
|
|
|
$this->like('PatientID', $filters['PatientID'], 'both');
|
2025-10-01 12:40:05 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($filters['Birthdate'])) {
|
2025-10-16 13:22:28 +07:00
|
|
|
$this->where('Birthdate', $filters['Birthdate']);
|
2025-10-01 12:40:05 +07:00
|
|
|
}
|
|
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
$rows = $this->findAll();
|
|
|
|
|
$rows = ValueSet::transformLabels($rows, [
|
2026-01-20 13:20:37 +07:00
|
|
|
'Sex' => 'sex',
|
2026-01-12 16:53:41 +07:00
|
|
|
]);
|
|
|
|
|
return $rows;
|
2025-10-01 12:40:05 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getPatient($InternalPID) {
|
2025-10-16 13:22:28 +07:00
|
|
|
$rows = $this->select("
|
|
|
|
|
patient.*,
|
2025-10-01 12:40:05 +07:00
|
|
|
patcom.Comment as Comment,
|
|
|
|
|
patidt.IdentifierType,
|
|
|
|
|
patidt.Identifier,
|
2025-10-23 09:41:49 +07:00
|
|
|
patatt.Address,
|
2025-12-01 16:47:52 +07:00
|
|
|
areageo1.AreaGeoID as ProvinceID,
|
|
|
|
|
areageo1.AreaName as Province,
|
|
|
|
|
areageo2.AreaGeoID as CityID,
|
|
|
|
|
areageo2.AreaName as City
|
2025-10-23 10:20:55 +07:00
|
|
|
|
2025-10-01 12:40:05 +07:00
|
|
|
")
|
2025-10-16 13:22:28 +07:00
|
|
|
->join('patcom', 'patcom.InternalPID = patient.InternalPID', 'left')
|
|
|
|
|
->join('patidt', 'patidt.InternalPID = patient.InternalPID', 'left')
|
|
|
|
|
->join('patatt', 'patatt.InternalPID = patient.InternalPID and patatt.DelDate is null', 'left')
|
2025-12-01 16:47:52 +07:00
|
|
|
->join('areageo areageo1', 'areageo1.AreaGeoID = patient.Province', 'left')
|
|
|
|
|
->join('areageo areageo2', 'areageo2.AreaGeoID = patient.City', 'left')
|
2025-10-16 13:22:28 +07:00
|
|
|
->where('patient.InternalPID', (int) $InternalPID)
|
|
|
|
|
->findAll();
|
2025-10-01 12:40:05 +07:00
|
|
|
|
|
|
|
|
if (empty($rows)) { return null; }
|
|
|
|
|
|
|
|
|
|
$patient = $rows[0];
|
2025-10-06 09:47:15 +07:00
|
|
|
|
|
|
|
|
if (method_exists($this, 'transformPatientData')) { $patient = $this->transformPatientData($patient); }
|
2025-10-01 15:36:55 +07:00
|
|
|
unset($patient['Address']);
|
2025-10-02 14:11:32 +07:00
|
|
|
unset($patient['IdentifierType']);
|
|
|
|
|
unset($patient['Identifier']);
|
2025-10-03 10:46:23 +07:00
|
|
|
unset($patient['Comment']);
|
2025-10-01 12:40:05 +07:00
|
|
|
|
2026-01-12 16:53:41 +07:00
|
|
|
$patient = ValueSet::transformLabels([$patient], [
|
2026-01-20 13:20:37 +07:00
|
|
|
'Sex' => 'sex',
|
2026-01-12 16:53:41 +07:00
|
|
|
'Country' => 'country',
|
|
|
|
|
'Race' => 'race',
|
|
|
|
|
'Religion' => 'religion',
|
|
|
|
|
'Ethnic' => 'ethnic',
|
|
|
|
|
'DeathIndicator' => 'death_indicator',
|
|
|
|
|
'MaritalStatus' => 'marital_status',
|
|
|
|
|
])[0];
|
|
|
|
|
|
2025-10-01 15:36:55 +07:00
|
|
|
$patient['PatIdt'] = null;
|
|
|
|
|
$patient['PatAtt'] = [];
|
2025-10-02 09:41:29 +07:00
|
|
|
|
2025-10-01 12:40:05 +07:00
|
|
|
foreach ($rows as $row) {
|
2025-10-01 15:36:55 +07:00
|
|
|
if ($row['IdentifierType'] && $row['Identifier'] && !$patient['PatIdt']) {
|
|
|
|
|
$patient['PatIdt'] = [
|
2025-10-01 12:40:05 +07:00
|
|
|
'IdentifierType' => $row['IdentifierType'],
|
|
|
|
|
'Identifier' => $row['Identifier'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($row['Address']) {
|
2025-10-01 15:36:55 +07:00
|
|
|
$patient['PatAtt'][] = ['Address' => $row['Address']];
|
2025-10-01 12:40:05 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-01 15:36:55 +07:00
|
|
|
if (empty($patient['PatIdt'])) { $patient['PatIdt'] = null; }
|
|
|
|
|
if (empty($patient['PatAtt'])) { $patient['PatAtt'] = null; }
|
2025-10-01 12:40:05 +07:00
|
|
|
|
|
|
|
|
return $patient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function createPatient($input) {
|
|
|
|
|
$db = \Config\Database::connect();
|
2025-10-16 10:50:09 +07:00
|
|
|
|
2025-10-16 13:22:28 +07:00
|
|
|
$modelPatAtt = new PatAttModel();
|
|
|
|
|
$modelPatCom = new PatComModel();
|
|
|
|
|
$modelPatIdt = new PatIdtModel();
|
feat(patient): handle array format for Custodian and LinkTo fields
- PatientModel: Convert Custodian from array to integer InternalPID when received as object
- PatientModel: Convert LinkTo from array of objects to comma-separated InternalPID string
- API docs: Add LinkedPatient, Custodian, and PatAttEntry schema definitions
- API docs: Extend Patient schema with DeathIndicator, TimeOfDeath, PatCom,
PatAtt, Province, City, Country, Race, MaritalStatus, Religion, Ethnic fields
- Add AGENTS.md to .gitignore
2026-01-29 11:21:34 +07:00
|
|
|
|
2026-02-10 16:43:52 +07:00
|
|
|
// Extract nested data before filtering
|
|
|
|
|
$patIdt = $input['PatIdt'] ?? null;
|
|
|
|
|
$patCom = $input['PatCom'] ?? null;
|
|
|
|
|
$patAtt = $input['PatAtt'] ?? null;
|
|
|
|
|
|
|
|
|
|
// Remove nested arrays that don't belong to patient table
|
|
|
|
|
unset($input['PatIdt'], $input['PatCom'], $input['PatAtt']);
|
|
|
|
|
|
feat(patient): handle array format for Custodian and LinkTo fields
- PatientModel: Convert Custodian from array to integer InternalPID when received as object
- PatientModel: Convert LinkTo from array of objects to comma-separated InternalPID string
- API docs: Add LinkedPatient, Custodian, and PatAttEntry schema definitions
- API docs: Extend Patient schema with DeathIndicator, TimeOfDeath, PatCom,
PatAtt, Province, City, Country, Race, MaritalStatus, Religion, Ethnic fields
- Add AGENTS.md to .gitignore
2026-01-29 11:21:34 +07:00
|
|
|
if (!empty($input['Custodian'])) {
|
2026-02-10 16:43:52 +07:00
|
|
|
if (is_array($input['Custodian'])) {
|
|
|
|
|
$input['Custodian'] = $input['Custodian']['InternalPID'] ?? null;
|
|
|
|
|
if ($input['Custodian'] !== null) {
|
|
|
|
|
$input['Custodian'] = (int) $input['Custodian'];
|
|
|
|
|
}
|
feat(patient): handle array format for Custodian and LinkTo fields
- PatientModel: Convert Custodian from array to integer InternalPID when received as object
- PatientModel: Convert LinkTo from array of objects to comma-separated InternalPID string
- API docs: Add LinkedPatient, Custodian, and PatAttEntry schema definitions
- API docs: Extend Patient schema with DeathIndicator, TimeOfDeath, PatCom,
PatAtt, Province, City, Country, Race, MaritalStatus, Religion, Ethnic fields
- Add AGENTS.md to .gitignore
2026-01-29 11:21:34 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($input['LinkTo']) && is_array($input['LinkTo'])) {
|
|
|
|
|
$internalPids = array_column($input['LinkTo'], 'InternalPID');
|
|
|
|
|
$input['LinkTo'] = implode(',', $internalPids);
|
|
|
|
|
}
|
2025-10-03 10:46:23 +07:00
|
|
|
$input['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo'];
|
2025-10-16 10:50:09 +07:00
|
|
|
|
2025-10-14 15:50:22 +07:00
|
|
|
$db->transBegin();
|
|
|
|
|
|
2025-10-01 12:40:05 +07:00
|
|
|
try {
|
|
|
|
|
$this->insert($input);
|
|
|
|
|
$newInternalPID = $this->getInsertID();
|
2025-10-06 00:24:55 +07:00
|
|
|
$this->checkDbError($db, 'Insert patient');
|
2025-10-01 12:40:05 +07:00
|
|
|
|
2026-02-10 16:43:52 +07:00
|
|
|
if (!empty($patIdt)) {
|
|
|
|
|
$modelPatIdt->createPatIdt($patIdt, $newInternalPID);
|
2025-10-16 10:50:09 +07:00
|
|
|
$this->checkDbError($db, 'Insert PatIdt');
|
2025-10-01 12:40:05 +07:00
|
|
|
}
|
|
|
|
|
|
2026-02-10 16:43:52 +07:00
|
|
|
if (!empty($patCom)) {
|
|
|
|
|
$modelPatCom->createPatCom($patCom, $newInternalPID);
|
2025-10-16 10:50:09 +07:00
|
|
|
$this->checkDbError($db, 'Insert PatCom');
|
2025-10-01 15:36:55 +07:00
|
|
|
}
|
2025-10-16 10:50:09 +07:00
|
|
|
|
2026-02-10 16:43:52 +07:00
|
|
|
if (!empty($patAtt)) {
|
|
|
|
|
$modelPatAtt->createPatAtt($patAtt, $newInternalPID);
|
2025-10-16 10:50:09 +07:00
|
|
|
$this->checkDbError($db, 'Insert PatAtt');
|
2025-10-01 12:40:05 +07:00
|
|
|
}
|
|
|
|
|
|
2025-10-14 15:50:22 +07:00
|
|
|
$db->transCommit();
|
2025-10-16 10:50:09 +07:00
|
|
|
|
2025-10-01 12:40:05 +07:00
|
|
|
return $newInternalPID;
|
2025-10-01 15:36:55 +07:00
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
2025-10-14 15:50:22 +07:00
|
|
|
$db->transRollback();
|
2025-10-01 15:36:55 +07:00
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function updatePatient($input) {
|
|
|
|
|
$db = \Config\Database::connect();
|
2025-10-16 13:22:28 +07:00
|
|
|
$modelPatIdt = new PatIdtModel();
|
|
|
|
|
$modelPatCom = new PatComModel();
|
|
|
|
|
$modelPatAtt = new PatAttModel();
|
2025-10-16 10:50:09 +07:00
|
|
|
|
feat(patient): handle array format for Custodian and LinkTo fields
- PatientModel: Convert Custodian from array to integer InternalPID when received as object
- PatientModel: Convert LinkTo from array of objects to comma-separated InternalPID string
- API docs: Add LinkedPatient, Custodian, and PatAttEntry schema definitions
- API docs: Extend Patient schema with DeathIndicator, TimeOfDeath, PatCom,
PatAtt, Province, City, Country, Race, MaritalStatus, Religion, Ethnic fields
- Add AGENTS.md to .gitignore
2026-01-29 11:21:34 +07:00
|
|
|
if (!empty($input['Custodian'])) {
|
2026-02-10 16:43:52 +07:00
|
|
|
if (is_array($input['Custodian'])) {
|
|
|
|
|
$input['Custodian'] = $input['Custodian']['InternalPID'] ?? null;
|
|
|
|
|
if ($input['Custodian'] !== null) {
|
|
|
|
|
$input['Custodian'] = (int) $input['Custodian'];
|
|
|
|
|
}
|
feat(patient): handle array format for Custodian and LinkTo fields
- PatientModel: Convert Custodian from array to integer InternalPID when received as object
- PatientModel: Convert LinkTo from array of objects to comma-separated InternalPID string
- API docs: Add LinkedPatient, Custodian, and PatAttEntry schema definitions
- API docs: Extend Patient schema with DeathIndicator, TimeOfDeath, PatCom,
PatAtt, Province, City, Country, Race, MaritalStatus, Religion, Ethnic fields
- Add AGENTS.md to .gitignore
2026-01-29 11:21:34 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($input['LinkTo']) && is_array($input['LinkTo'])) {
|
|
|
|
|
$internalPids = array_column($input['LinkTo'], 'InternalPID');
|
|
|
|
|
$input['LinkTo'] = implode(',', $internalPids);
|
|
|
|
|
}
|
2025-10-03 10:46:23 +07:00
|
|
|
$input['LinkTo'] = empty($input['LinkTo']) ? null : $input['LinkTo'];
|
2025-10-02 09:41:29 +07:00
|
|
|
|
2025-10-14 15:50:22 +07:00
|
|
|
$db->transBegin();
|
|
|
|
|
|
2025-10-01 15:36:55 +07:00
|
|
|
try {
|
2025-10-16 10:50:09 +07:00
|
|
|
|
2025-10-01 15:36:55 +07:00
|
|
|
$InternalPID = $input['InternalPID'];
|
|
|
|
|
$this->where('InternalPID',$InternalPID)->set($input)->update();
|
2025-10-06 00:24:55 +07:00
|
|
|
$this->checkDbError($db, 'Update patient');
|
|
|
|
|
|
2025-10-16 10:50:09 +07:00
|
|
|
if (!empty($input['PatIdt'])) {
|
2025-10-16 13:22:28 +07:00
|
|
|
$modelPatIdt->updatePatIdt($input['PatIdt'], $InternalPID);
|
2025-10-16 10:50:09 +07:00
|
|
|
$this->checkDbError($db, 'Update patIdt');
|
2025-10-01 15:36:55 +07:00
|
|
|
} else {
|
2025-10-16 13:22:28 +07:00
|
|
|
$modelPatIdt->deletePatIdt($InternalPID);
|
2025-10-06 00:24:55 +07:00
|
|
|
$this->checkDbError($db, 'Update patidt');
|
2025-10-01 15:36:55 +07:00
|
|
|
}
|
|
|
|
|
|
2025-10-16 10:50:09 +07:00
|
|
|
if (!empty($input['PatCom'])) {
|
2025-10-16 13:22:28 +07:00
|
|
|
$modelPatCom->updatePatCom($input['PatCom'], $InternalPID);
|
2025-10-16 10:50:09 +07:00
|
|
|
$this->checkDbError($db, 'Update PatCom');
|
2025-10-01 15:36:55 +07:00
|
|
|
} else {
|
2025-10-16 13:22:28 +07:00
|
|
|
$modelPatCom->deletePatCom($InternalPID);
|
2025-10-06 00:24:55 +07:00
|
|
|
$this->checkDbError($db, 'Update patcom');
|
2025-10-01 15:36:55 +07:00
|
|
|
}
|
2025-10-02 09:41:29 +07:00
|
|
|
|
2025-10-16 10:50:09 +07:00
|
|
|
if (!empty($input['PatAtt'])) {
|
2025-10-16 13:22:28 +07:00
|
|
|
$modelPatAtt->updatePatAtt($input['PatAtt'], $InternalPID);
|
2025-10-16 10:50:09 +07:00
|
|
|
$this->checkDbError($db, 'Update PatAtt');
|
2025-10-01 15:36:55 +07:00
|
|
|
} else {
|
2025-10-16 13:22:28 +07:00
|
|
|
$modelPatAtt->deletePatAtt($InternalPID);
|
2025-10-16 10:50:09 +07:00
|
|
|
$this->checkDbError($db, 'Update/Delete patatt');
|
2025-10-01 15:36:55 +07:00
|
|
|
}
|
2025-10-02 09:41:29 +07:00
|
|
|
|
2025-10-14 15:50:22 +07:00
|
|
|
$db->transCommit();
|
2025-10-16 10:50:09 +07:00
|
|
|
|
2025-10-01 15:36:55 +07:00
|
|
|
return $InternalPID;
|
2025-10-14 15:50:22 +07:00
|
|
|
|
2025-10-01 12:40:05 +07:00
|
|
|
} catch (\Exception $e) {
|
2025-10-14 15:50:22 +07:00
|
|
|
$db->transRollback();
|
2025-10-01 15:36:55 +07:00
|
|
|
throw $e;
|
2025-10-01 12:40:05 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function transformPatientData(array $patient): array {
|
2025-10-21 10:27:31 +07:00
|
|
|
$patient["Age"] = $this->calculateAgeFromBirthdate($patient["Birthdate"], $patient["TimeOfDeath"]);
|
|
|
|
|
$patient["TimeOfDeath"] = $this->formattedDate($patient["TimeOfDeath"]);
|
2025-10-10 10:19:52 +07:00
|
|
|
$patient["CreateDate"] = $this->formattedDate($patient["CreateDate"]);
|
2025-10-01 12:40:05 +07:00
|
|
|
$patient["BirthdateConversion"] = $this->formatedDateForDisplay($patient["Birthdate"]);
|
|
|
|
|
$patient["LinkTo"] = $this->getLinkedPatients($patient['LinkTo']);
|
2025-10-21 09:33:38 +07:00
|
|
|
$patient["Custodian"] = $this->getCustodian($patient['Custodian']);
|
2025-10-06 09:47:15 +07:00
|
|
|
$patient['PatCom'] = $patient['Comment'];
|
2025-10-01 12:40:05 +07:00
|
|
|
|
|
|
|
|
return $patient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getLinkedPatients(?string $linkTo): ?array {
|
|
|
|
|
if (empty($linkTo)) { return null; }
|
|
|
|
|
|
|
|
|
|
$ids = array_filter(explode(',', $linkTo));
|
|
|
|
|
|
|
|
|
|
return $this->db->table('patient')
|
|
|
|
|
->select('InternalPID, PatientID')
|
|
|
|
|
->whereIn('InternalPID', $ids)
|
|
|
|
|
->get()
|
|
|
|
|
->getResultArray() ?: null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getCustodian($custodianId): ?array {
|
|
|
|
|
if (empty($custodianId)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-16 13:22:28 +07:00
|
|
|
return $this->select('InternalPID, PatientID')
|
2025-10-01 12:40:05 +07:00
|
|
|
->where('InternalPID', (int) $custodianId)
|
2025-10-21 09:25:48 +07:00
|
|
|
->first() ?: null;
|
2025-10-01 12:40:05 +07:00
|
|
|
}
|
|
|
|
|
|
2025-10-21 09:25:48 +07:00
|
|
|
private function calculateAgeFromBirthdate($birthdate, $deathdate) {
|
2025-10-01 12:40:05 +07:00
|
|
|
$dob = new \DateTime($birthdate);
|
|
|
|
|
|
2025-10-21 09:25:48 +07:00
|
|
|
if ($deathdate == null) {
|
2025-10-03 10:46:23 +07:00
|
|
|
$today = new \DateTime();
|
|
|
|
|
} else {
|
2025-10-21 09:25:48 +07:00
|
|
|
$deathdate = new \DateTime($deathdate);
|
|
|
|
|
$today = $deathdate;
|
2025-10-03 10:46:23 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$diff = $today->diff($dob);
|
2025-10-01 12:40:05 +07:00
|
|
|
$formattedAge = "";
|
|
|
|
|
if ($diff->y > 0){
|
|
|
|
|
$formattedAge .= "{$diff->y} Years ";
|
|
|
|
|
}
|
|
|
|
|
if ($diff->m > 0){
|
|
|
|
|
$formattedAge .= "{$diff->m} Months ";
|
|
|
|
|
}
|
|
|
|
|
if ($diff->d > 0){
|
|
|
|
|
$formattedAge .= "{$diff->d} Days";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $formattedAge;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-06 13:25:53 +07:00
|
|
|
private function formattedDate(?string $dateString): ?string {
|
|
|
|
|
try {
|
2025-10-14 15:50:22 +07:00
|
|
|
if (empty($dateString)) {return null;}
|
2025-10-01 12:40:05 +07:00
|
|
|
|
2025-10-06 13:25:53 +07:00
|
|
|
$dt = new \DateTime($dateString, new \DateTimeZone("UTC"));
|
2026-01-12 16:53:41 +07:00
|
|
|
return $dt->format('Y-m-d\TH:i:s\Z');
|
2025-10-06 13:25:53 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-10-06 13:23:18 +07:00
|
|
|
}
|
2025-10-01 12:40:05 +07:00
|
|
|
|
|
|
|
|
private function formatedDateForDisplay($dateString) {
|
|
|
|
|
$date = \DateTime::createFromFormat('Y-m-d H:i', $dateString);
|
|
|
|
|
|
|
|
|
|
if (!$date) {
|
|
|
|
|
$timestamp = strtotime($dateString);
|
|
|
|
|
if ($timestamp) {
|
|
|
|
|
return date('j M Y', $timestamp);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $date->format('j M Y');
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-06 00:24:55 +07:00
|
|
|
private function checkDbError($db, string $context) {
|
|
|
|
|
$error = $db->error();
|
|
|
|
|
if (!empty($error['code'])) {
|
|
|
|
|
throw new \Exception(
|
|
|
|
|
"{$context} failed: {$error['code']} - {$error['message']}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function isValidDateTime($datetime) {
|
2025-10-10 10:19:52 +07:00
|
|
|
if (empty($datetime) || $datetime=="") {return null; }
|
2025-10-06 12:36:01 +07:00
|
|
|
try {
|
|
|
|
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $datetime)) {
|
|
|
|
|
$dt = \DateTime::createFromFormat('Y-m-d', $datetime);
|
2026-01-12 16:53:41 +07:00
|
|
|
return $dt ? $dt->format('Y-m-d') : null;
|
2025-10-06 12:36:01 +07:00
|
|
|
}
|
2025-10-06 00:24:55 +07:00
|
|
|
|
2025-10-06 12:36:01 +07:00
|
|
|
$dt = new \DateTime($datetime);
|
|
|
|
|
return $dt->format('Y-m-d H:i:s');
|
2025-10-06 00:24:55 +07:00
|
|
|
|
2025-10-06 12:36:01 +07:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-06 00:24:55 +07:00
|
|
|
|
2025-10-01 12:40:05 +07:00
|
|
|
}
|