nhance/app/Models/ThzMasterNotesModel.php

118 lines
3.7 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class ThzMasterNotesModel extends Model
{
protected $table = 'thz_master_notes';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = ['thz_id','notes','notes_by','created_by','created_at','notes_type'];
protected bool $allowEmptyInserts = false;
// Dates
protected $useTimestamps = false;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
public function ticketConversationLists($thz_id){
if(empty($thz_id)){
return false;
}
$sql = "SELECT
thz_master.*,
thz_master_notes.id,
thz_master_notes.thz_id,
thz_master_notes.notes,
thz_master_notes.notes_by,
thz_master_notes.created_by,
thz_master_notes.created_at
FROM
thz_master_notes
Join thz_master ON thz_master.thz_id = :thz_id:
WHERE
thz_master_notes.thz_id = :thz_id:
ORDER BY
thz_master_notes.created_at DESC
";
$binds = ["thz_id" => $thz_id ];
$result = $this->db->query($sql,$binds)->getResultArray() ;
return $result;
}
public function ticketConversationList($thz_id , $returnType)
{
$ticketTypeFilter = "";
if ($returnType == 'api') {
//user only see his message
$ticketTypeFilter = "AND thz_master_notes.notes_type = 'External' ";
}
$notes_sql = "SELECT
thz_master_notes.*,
CASE
WHEN LOWER(thz_master_notes.notes_by) = 'staff'
THEN CONCAT(user_profiles.first_name, ' ', user_profiles.last_name)
WHEN LOWER(thz_master_notes.notes_by) = 'user'
THEN thz_master.name
ELSE thz_master_notes.notes_by
END AS name
FROM thz_master_notes
LEFT JOIN user_profiles
ON user_profiles.id = thz_master_notes.created_by
AND LOWER(thz_master_notes.notes_by) = 'staff'
LEFT JOIN thz_master
ON thz_master.thz_id = thz_master_notes.thz_id
AND LOWER(thz_master_notes.notes_by) = 'user'
WHERE thz_master_notes.thz_id = :integerTicketId:
$ticketTypeFilter
ORDER BY thz_master_notes.created_at DESC";
$binds = ["integerTicketId" => (int)$thz_id];
$result = $this->db->query($notes_sql,$binds)->getResultArray();
return $result;
}
}