85 lines
3.0 KiB
PHP
85 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class PatientModel extends Model
|
|
{
|
|
protected $table = 'patients';
|
|
protected $primaryKey = 'id';
|
|
protected $useAutoIncrement = true;
|
|
protected $returnType = 'array';
|
|
protected $useSoftDeletes = false;
|
|
protected $protectFields = true;
|
|
protected $allowedFields = ['user_id','age','gender','phone'];
|
|
|
|
protected bool $allowEmptyInserts = false;
|
|
protected bool $updateOnlyChanged = true;
|
|
|
|
protected array $casts = [];
|
|
protected array $castHandlers = [];
|
|
|
|
// 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 findByUserId(int $userId): ?array
|
|
{
|
|
$row = $this->where('user_id', $userId)->first();
|
|
|
|
return $row ?: null;
|
|
}
|
|
|
|
public function getAdminPatientList(string $sortBy = 'patient_id', string $sortDir = 'asc'): array
|
|
{
|
|
$fullNameSql = "TRIM(CONCAT(COALESCE(users.first_name, ''), ' ', COALESCE(users.last_name, '')))";
|
|
|
|
$sortColumns = [
|
|
'patient_id' => 'users.id',
|
|
'name' => $fullNameSql,
|
|
'email' => 'users.email',
|
|
'phone' => 'patients.phone',
|
|
'status' => 'users.status',
|
|
];
|
|
|
|
$sortColumn = $sortColumns[$sortBy] ?? 'users.id';
|
|
$sortDir = strtolower($sortDir) === 'desc' ? 'DESC' : 'ASC';
|
|
|
|
return $this->asObject()
|
|
->select("users.id as user_id, CASE WHEN users.formatted_user_id IS NULL OR users.formatted_user_id = '' OR users.formatted_user_id LIKE 'PHY%' THEN CONCAT('PAT', LPAD(users.id, 7, '0')) ELSE users.formatted_user_id END as formatted_user_id, {$fullNameSql} as name, users.email, users.first_name, users.last_name, users.status, patients.id, patients.phone")
|
|
->join('users', 'users.id = patients.user_id')
|
|
->where('users.role', 'patient')
|
|
->orderBy($sortColumn, $sortDir)
|
|
->findAll();
|
|
}
|
|
|
|
public function getLatestPatients(int $limit = 5): array
|
|
{
|
|
return $this->select("TRIM(CONCAT(COALESCE(users.first_name, ''), ' ', COALESCE(users.last_name, ''))) as name, patients.phone")
|
|
->join('users', 'users.id = patients.user_id')
|
|
->orderBy('patients.id', 'DESC')
|
|
->findAll($limit);
|
|
}
|
|
}
|