clqms-be/app/Models/ContactModel.php

111 lines
3.3 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;
}
2025-09-25 16:43:58 +07:00
public function getContacts() {
$rows = $this->select("ContactID, NameFirst, NameLast, Title, Initial, Specialty")->get()->getResultArray();
return $rows;
}
2025-09-24 16:15:55 +07:00
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-26 10:00:06 +07:00
$contact = [];
foreach ($rows as $row) {
if(empty($contact['NameFirst'])) {
$contact = [
'ContactID' => $ContactID,
'NameFirst' => $row['NameFirst'] ?? null,
'NameLast' => $row['NameLast'] ?? null,
'Title' => $row['Title'] ?? null,
'Initial' => $row['Initial'] ?? null,
'Birthdate' => $row['Birthdate'] ?? null,
'EmailAddress1' => $row['EmailAddress1'] ?? null,
'EmailAddress2' => $row['EmailAddress2'] ?? null,
'Phone' => $row['Phone'] ?? null,
'MobilePhone1' => $row['MobilePhone1'] ?? null,
'MobilePhone2' => $row['MobilePhone2'] ?? null,
'Specialty' => $row['Specialty'] ?? null,
'SubSpecialty' => $row['SubSpecialty'] ?? null,
'Details' => []
];
}
if (!empty($row['ContactDetID'])) {
$contact['Details'][] = [
'SiteID' => $row['SiteID'] ?? null,
'ContactDetID' => $row['ContactDetID'],
'ContactCode' => $row['ContactCode'] ?? null,
2025-09-26 13:08:30 +07:00
'ContactEmail' => $row['ContactEmail'] ?? null,
'OccupationID' => $row['OccupationID'] ?? null,
'JobTitle' => $row['JobTitle'] ?? null,
'Department' => $row['Department'] ?? null,
'ContactStartDate' => $row['ContactStartDate'] ?? null,
'ContactEndDate' => $row['ContactEndDate'] ?? null
];
2025-09-24 16:15:55 +07:00
}
}
2025-09-24 16:15:55 +07:00
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
}