feat: initialize Dr-Panel project with patient file number support

This commit is contained in:
SajjadMahmoody
2026-05-06 02:18:23 +03:30
parent f8f174690a
commit 8b84d1780f
460 changed files with 113960 additions and 1 deletions
+49
View File
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace App\Observers;
use App\Models\Patient;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class PatientObserver
{
private const FILE_FIELDS = ['photos_before', 'photos_after', 'videos', 'audio_files'];
public function creating(Patient $patient): void
{
if (empty($patient->file_number)) {
$max = DB::table('patients')->max('file_number');
$patient->file_number = max(100, ($max ?? 99) + 1);
}
}
public function updating(Patient $patient): void
{
foreach (self::FILE_FIELDS as $field) {
$rawOld = $patient->getRawOriginal($field);
$oldFiles = ! empty($rawOld) ? (json_decode($rawOld, true) ?? []) : [];
$newFiles = array_values((array) ($patient->getAttribute($field) ?? []));
$removed = array_diff($oldFiles, $newFiles);
foreach ($removed as $path) {
if (! empty(trim((string) $path))) {
Storage::disk('public')->delete($path);
}
}
}
}
public function deleting(Patient $patient): void
{
foreach (self::FILE_FIELDS as $field) {
$files = $patient->getAttribute($field) ?? [];
foreach ((array) $files as $path) {
Storage::disk('public')->delete($path);
}
}
}
}