260 lines
7.2 KiB
PHP
260 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Enums\Permission;
|
|
use App\Models\Enums\Priority;
|
|
use App\Models\Enums\ReportStatus;
|
|
use App\Models\Enums\StudyLevelStatus;
|
|
use App\Models\Traits\HashableId;
|
|
use App\Services\Pacs\PacsUrlGen;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Study extends BaseModel
|
|
{
|
|
use HashableId;
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'is_locked' => 'boolean',
|
|
'is_archived' => 'boolean',
|
|
'study_status' => StudyLevelStatus::class,
|
|
'report_status' => ReportStatus::class,
|
|
'priority' => Priority::class,
|
|
'received_at' => 'immutable_datetime',
|
|
'reported_at' => 'immutable_datetime',
|
|
'assigned_at' => 'immutable_datetime',
|
|
'study_date' => 'immutable_datetime',
|
|
];
|
|
}
|
|
|
|
public function details(): HasOne
|
|
{
|
|
return $this->hasOne(StudyDetails::class);
|
|
}
|
|
|
|
public function attachments(): HasMany
|
|
{
|
|
return $this->hasMany(StudyAttachment::class);
|
|
}
|
|
|
|
public function reports(): HasMany
|
|
{
|
|
return $this->hasMany(StudyReport::class);
|
|
}
|
|
|
|
public function shares(): HasMany
|
|
{
|
|
return $this->hasMany(SharedStudy::class);
|
|
}
|
|
|
|
public function scopeActive($query)
|
|
{
|
|
return $query->where('is_archived', false);
|
|
}
|
|
|
|
public function scopeArchived($query)
|
|
{
|
|
return $query->where('is_archived', true);
|
|
}
|
|
|
|
public function scopeUnlocked($query)
|
|
{
|
|
return $query->where('is_locked', false);
|
|
}
|
|
|
|
public function getHistoryLink(): string
|
|
{
|
|
$user = auth()->user();
|
|
if ($user->may(Permission::StudyHistoryEdit)) {
|
|
return route('staff.history.edit', $this->hash);
|
|
}
|
|
|
|
if ($user->may(Permission::StudyHistoryView)) {
|
|
return route('staff.history.view', $this->hash);
|
|
}
|
|
|
|
return '#';
|
|
}
|
|
|
|
public function getMetadataLink(): string
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if ($user->may(Permission::StudyMetadataEdit)) {
|
|
return route('staff.meta.edit', $this->hash);
|
|
}
|
|
|
|
if ($user->may(Permission::StudyMetadataView)) {
|
|
return route('staff.meta.view', $this->hash);
|
|
}
|
|
|
|
return '#';
|
|
}
|
|
|
|
public function sexAge(): string
|
|
{
|
|
$dob = $this->patient_birthdate;
|
|
$age = $dob ? (int) Carbon::make($dob)->diffInYears().'Y' : null;
|
|
if (blank($age) && blank($this->patient_sex)) {
|
|
return '~';
|
|
}
|
|
|
|
return sprintf('%s / %s', $age ?? '~', $this->patient_sex);
|
|
}
|
|
|
|
public function numInstances(): string
|
|
{
|
|
return "{$this->image_count} / {$this->series_count}";
|
|
}
|
|
|
|
public function readingPhysician(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'reporting_physician_id');
|
|
}
|
|
|
|
public function getReportStatusLedAttribute(): string
|
|
{
|
|
$color = match ($this->report_status) {
|
|
ReportStatus::Pending => 'bg-dark',
|
|
ReportStatus::Opened => 'bg-warning',
|
|
ReportStatus::Draft => 'bg-warning',
|
|
ReportStatus::Finalized => 'bg-success',
|
|
ReportStatus::Authorized => 'bg-success',
|
|
default => 'bg-light',
|
|
};
|
|
|
|
return sprintf('<span class="badge badge-center rounded-pill %s"><i class="fa-light fa-battery-bolt"></i></span>', $color);
|
|
}
|
|
|
|
public function getArchiveLink(): ?string
|
|
{
|
|
if (auth()->user()->may(Permission::StudyDownload)) {
|
|
return PacsUrlGen::archive($this->study_instance_uid);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function getStoneLink(): ?string
|
|
{
|
|
if (auth()->user()->may(Permission::StudyDownload)) {
|
|
return PacsUrlGen::stoneViewer($this->study_instance_uid);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function getOhifLink(): ?string
|
|
{
|
|
if (auth()->user()->may(Permission::StudyDownload)) {
|
|
return PacsUrlGen::ohifViewer($this->study_instance_uid);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function getOhifSegmentationLink(): ?string
|
|
{
|
|
if (auth()->user()->may(Permission::StudyDownload)) {
|
|
return PacsUrlGen::ohifSegmentation($this->study_instance_uid);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function getOhifMprLink(): ?string
|
|
{
|
|
if (auth()->user()->may(Permission::StudyDownload)) {
|
|
return PacsUrlGen::ohifViewerMpr($this->study_instance_uid);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function links(): array
|
|
{
|
|
return [
|
|
'history' => $this->getHistoryLink(),
|
|
'metadata' => $this->getMetadataLink(),
|
|
'zip' => $this->getArchiveLink(),
|
|
'stone' => $this->getStoneLink(),
|
|
'ohif' => $this->getOhifLink(),
|
|
'ohif.mpr' => $this->getOhifMprLink(),
|
|
'ohif.seg' => $this->getOhifSegmentationLink(),
|
|
];
|
|
}
|
|
|
|
public function allowed(): array
|
|
{
|
|
return [
|
|
'zip' => filled($this->getArchiveLink()),
|
|
'stone' => filled($this->getStoneLink()),
|
|
'ohif' => filled($this->getOhifLink()),
|
|
'ohif.mpr' => filled($this->getOhifMprLink()),
|
|
'ohif.seg' => filled($this->getOhifSegmentationLink()),
|
|
];
|
|
}
|
|
|
|
public function sanitizedPatientName(): string
|
|
{
|
|
$name = preg_replace('/[^[:alnum:][:space:]]/u', ' ', $this->patient_name);
|
|
|
|
return Str::of($name)
|
|
->slug(' ')
|
|
->title();
|
|
}
|
|
|
|
public function sanitizedStudyDescription(): string
|
|
{
|
|
$name = preg_replace('/[^[:alnum:][:space:]\/\-\&]/u', ' ', $this->study_description);
|
|
|
|
$name = Str::of($name)->title();
|
|
$lut = [
|
|
'ct' => 'CT',
|
|
'mr' => 'MR',
|
|
'mri' => 'MRI',
|
|
'hrct' => 'HRCT',
|
|
'kub' => 'KUB',
|
|
'ap' => 'AP',
|
|
'pa' => 'PA',
|
|
'with' => 'w/',
|
|
];
|
|
foreach ($lut as $search => $replace) {
|
|
$name = preg_replace("/\b$search\b/i", $replace, $name);
|
|
}
|
|
|
|
return $name;
|
|
}
|
|
|
|
public function toArray(): array
|
|
{
|
|
return array_merge(parent::toArray(), [
|
|
'disk_size_human' => human_filesize($this->disk_size),
|
|
'reader_name' => $this->readingPhysician?->display_name,
|
|
'reader_photo' => $this->readingPhysician?->profile_photo_url,
|
|
'report_status_led' => $this->getReportStatusLedAttribute(),
|
|
'priority_icon' => $this->getPriorityIcon(),
|
|
'sex_age' => $this->sexAge(),
|
|
'num_instances' => $this->numInstances(),
|
|
'links' => $this->links(),
|
|
'allowed' => $this->allowed(),
|
|
]);
|
|
}
|
|
|
|
public function getPriorityIcon(): string
|
|
{
|
|
return match ($this->priority) {
|
|
Priority::High => '<i class="fa-light fa-triangle-exclamation"></i>',
|
|
Priority::Urgent => '<i class="fa-light fa-bolt"></i>',
|
|
Priority::Low => '<i class="fa-thin fa-chevrons-down"></i>',
|
|
default => '',
|
|
};
|
|
}
|
|
}
|