111 lines
3.4 KiB
PHP
111 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class ContactModel extends Model {
|
|
protected $table = 'contact c';
|
|
protected $primaryKey = 'ContactID';
|
|
protected $allowedFields = ['NameFirst', 'NameLast', 'Title', 'Initial', 'Birthdate', 'EmailAddress1', 'EmailAddress2', 'Phone', 'MobilePhone1', 'MobilePhone2', 'Specialty', 'SubSpecialty'];
|
|
|
|
public function getContactsWithDetail() {
|
|
$rows = $this->select("c.ContactID, cd.ContactCode, c.NameFirst, c.NameLast, c.Specialty")
|
|
->join("contactdetail cd", "c.ContactID=cd.ContactID", "left")
|
|
->join("occupation o","cd.OccupationID=o.OccupationID", "left")
|
|
->get()->getResultArray();
|
|
|
|
return $rows;
|
|
}
|
|
|
|
public function getContactWithDetail($ContactID) {
|
|
$rows = $this->where('c.ContactID', $ContactID)->join('contactdetail cd', 'c.ContactID=cd.ContactID','left')->get()->getResultArray();
|
|
$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'][] = [
|
|
'SiteID' => $row['SiteID'] ?? null,
|
|
'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;
|
|
}
|
|
|
|
public function createContact(array $contactData, array $contactDetails) {
|
|
$db = \Config\Database::connect();
|
|
$db->transStart();
|
|
|
|
try {
|
|
if (!$this->insert($contactData)) {
|
|
throw new \Exception('Failed to insert contact');
|
|
}
|
|
$ContactID = $this->getInsertID();
|
|
|
|
$detailModel = new \App\Models\ContactDetailModel();
|
|
foreach ($contactDetails as $detail) {
|
|
$detail['ContactID'] = $ContactID;
|
|
if (!$detailModel->insert($detail)) {
|
|
throw new \Exception('Failed to insert contact details');
|
|
}
|
|
}
|
|
|
|
$db->transComplete();
|
|
return $db->transStatus();
|
|
} catch (\Exception $e) {
|
|
$db->transRollback();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function updateContact(array $contactData, array $contactDetails) {
|
|
$db = \Config\Database::connect();
|
|
$db->transStart();
|
|
|
|
try {
|
|
if (!$this->update($contactData)) {
|
|
throw new \Exception('Failed to insert contact');
|
|
}
|
|
$ContactID = $this->getInsertID();
|
|
|
|
$detailModel = new \App\Models\ContactDetailModel();
|
|
foreach ($contactDetails as $detail) {
|
|
$detail['ContactID'] = $ContactID;
|
|
if (!$detailModel->insert($detail)) {
|
|
throw new \Exception('Failed to insert contact details');
|
|
}
|
|
}
|
|
|
|
$db->transComplete();
|
|
return $db->transStatus();
|
|
} catch (\Exception $e) {
|
|
$db->transRollback();
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|