clqms-be/app/Models/ContactModel.php

103 lines
3.1 KiB
PHP
Raw Normal View History

2025-09-24 16:15:55 +07:00
<?php
namespace App\Models;
use CodeIgniter\Model;
class ContactModel extends Model {
2025-09-25 13:30:15 +07:00
protected $table = 'contact';
2025-09-24 16:15:55 +07:00
protected $primaryKey = 'ContactID';
protected $allowedFields = ['NameFirst', 'NameLast', 'Title', 'Initial', 'Birthdate', 'EmailAddress1', 'EmailAddress2', 'Phone', 'MobilePhone1', 'MobilePhone2', 'Specialty', 'SubSpecialty'];
public function getContactsWithDetail() {
2025-09-25 13:30:15 +07:00
$rows = $this->select("contact.ContactID, cd.SiteID, cd.ContactCode, NameFirst, NameLast, Specialty")
->join("contactdetail cd", "contact.ContactID=cd.ContactID", "left")
2025-09-24 16:15:55 +07:00
->join("occupation o","cd.OccupationID=o.OccupationID", "left")
->get()->getResultArray();
return $rows;
}
public function getContactWithDetail($ContactID) {
2025-09-25 13:30:15 +07:00
$rows = $this->where('contact.ContactID', $ContactID)->join('contactdetail cd', 'contact.ContactID=cd.ContactID','left')->get()->getResultArray();
2025-09-24 16:15:55 +07:00
$contact = [
'ContactID' => $rows[0]['ContactID'],
'NameFirst' => $rows[0]['NameFirst'] ?? null,
'NameLast' => $rows[0]['NameLast'] ?? null,
'Title' => $rows[0]['Title'] ?? null,
'Initial' => $rows[0]['Initial'] ?? null,
'Birthdate' => $rows[0]['Birthdate'] ?? null,
'EmailAddress1' => $rows[0]['EmailAddress1'] ?? null,
'EmailAddress2' => $rows[0]['EmailAddress2'] ?? null,
'Phone' => $rows[0]['Phone'] ?? null,
'MobilePhone1' => $rows[0]['MobilePhone1'] ?? null,
'MobilePhone2' => $rows[0]['MobilePhone2'] ?? null,
'Specialty' => $rows[0]['Specialty'] ?? null,
'SubSpecialty' => $rows[0]['SubSpecialty'] ?? null,
'Details' => []
];
foreach ($rows as $row) {
if (!empty($row['ContactDetID'])) {
$contact['Details'][] = [
2025-09-25 09:42:17 +07:00
'SiteID' => $row['SiteID'] ?? null,
2025-09-24 16:15:55 +07:00
'ContactDetID' => $row['ContactDetID'],
'ContactCode' => $row['ContactCode'] ?? null,
'ContactEmail' => $row['DetailPhone'] ?? null,
'OccupationID' => $row['OccupationID'] ?? null,
'JobTitle' => $row['JobTitle'] ?? null,
'Department' => $row['Department'] ?? null,
'ContactStartDate' => $row['ContactStartDate'] ?? null,
'ContactEndDate' => $row['ContactEndDate'] ?? null
];
}
}
return $contact;
}
2025-09-25 13:30:15 +07:00
public function saveWithDetails(array $data): array {
$db = \Config\Database::connect();
$db->transStart();
2025-09-24 16:15:55 +07:00
2025-09-25 13:30:15 +07:00
try {
if (!empty($data['ContactID'])) {
$contactId = $data['ContactID'];
$this->update($contactId, $data);
} else {
$contactId = $this->insert($data, true);
2025-09-24 16:15:55 +07:00
}
2025-09-25 13:30:15 +07:00
if (!$contactId) {
throw new \RuntimeException('Failed to save contact');
2025-09-24 16:15:55 +07:00
}
2025-09-25 13:30:15 +07:00
if (!empty($data['Details'])) {
$detailModel = new \App\Models\ContactDetailModel();
$result = $detailModel->syncDetails($contactId, $data['Details']);
2025-09-24 16:15:55 +07:00
2025-09-25 13:30:15 +07:00
if ($result['status'] !== 'success') {
throw new \RuntimeException('SyncDetails failed: ' . $result['message']);
}
2025-09-24 16:15:55 +07:00
}
$db->transComplete();
2025-09-25 13:30:15 +07:00
return [
'status' => 'success',
'ContactID' => $contactId,
];
} catch (\Throwable $e) {
2025-09-24 16:15:55 +07:00
$db->transRollback();
2025-09-25 13:30:15 +07:00
log_message('error', 'saveWithDetails error: ' . $e->getMessage());
return [
'status' => 'error',
'message' => $e->getMessage(),
];
}
2025-09-24 16:15:55 +07:00
}
2025-09-25 13:30:15 +07:00
2025-09-24 16:15:55 +07:00
}