From 82c6bec793933bee6c7a3564c1babe0538236395 Mon Sep 17 00:00:00 2001 From: Dr Masroor Ehsan Date: Sat, 11 Jan 2025 22:20:15 +0600 Subject: [PATCH] radio profile --- .../Controllers/Staff/LanguageController.php | 16 ++ app/Models/RadiologistProfile.php | 18 ++ app/Models/Traits/HasOneFile.php | 175 ++++++++++++++++++ app/Models/User.php | 6 + ...07_904595_create_radiologist_profiles.php} | 6 +- 5 files changed, 218 insertions(+), 3 deletions(-) create mode 100644 app/Http/Controllers/Staff/LanguageController.php create mode 100644 app/Models/RadiologistProfile.php create mode 100644 app/Models/Traits/HasOneFile.php rename database/migrations/{2025_01_07_904595_create_radiologists.php => 2025_01_07_904595_create_radiologist_profiles.php} (71%) diff --git a/app/Http/Controllers/Staff/LanguageController.php b/app/Http/Controllers/Staff/LanguageController.php new file mode 100644 index 0000000..30628af --- /dev/null +++ b/app/Http/Controllers/Staff/LanguageController.php @@ -0,0 +1,16 @@ + $request->language_code]); + + return response(['status' => 'success']); + } +} diff --git a/app/Models/RadiologistProfile.php b/app/Models/RadiologistProfile.php new file mode 100644 index 0000000..c8ebcb6 --- /dev/null +++ b/app/Models/RadiologistProfile.php @@ -0,0 +1,18 @@ +belongsTo(User::class); + } +} diff --git a/app/Models/Traits/HasOneFile.php b/app/Models/Traits/HasOneFile.php new file mode 100644 index 0000000..e5a2830 --- /dev/null +++ b/app/Models/Traits/HasOneFile.php @@ -0,0 +1,175 @@ +deleteFile(); + }); + } + + /** + * Store a new file. + * This will also update the file name field in the database with the new file name and save the model. + * If the file is an instance of UploadedFile, the file name will be the ClientOriginalName. + * If the file is an instance of File, the file name will be the filename of the file. + * + * @param File $file The file to store. + * @return string The storage path of the file + */ + public function storeFile(File $file, bool $save = true): string + { + $storageDirectory = $this->getStorageDirectory(); + + // delete any existing file + Storage::disk($this->getFileStorageDisk())->deleteDirectory($storageDirectory); + + if ($file instanceof UploadedFile) { + $fileName = $file->getClientOriginalName(); + } else { + $fileName = $file->getFilename(); + } + $path = Storage::disk($this->getFileStorageDisk())->putFileAs($storageDirectory, $file, $fileName); + + // record the file name in the database + $this->{$this->getFileNameField()} = $fileName; + if ($save) { + $this->save(); + } + + return $path; + } + + /** + * Delete the associated file. + * This will also clear the file name field in the database and save the model. + * + * @throws Exception If file deletion fails + */ + public function deleteFile(bool $save = true): bool + { + $storagePath = $this->getStorageDirectory(); + $result = Storage::disk($this->getFileStorageDisk())->deleteDirectory($storagePath); + + if (! $result) { + return $result; + } + + $this->{$this->getFileNameField()} = null; + if ($save) { + $this->save(); + } + + return $result; + } + + /** + * Check if a file exists for this model. + */ + public function fileExists(): bool + { + return ! empty($this->{$this->getFileNameField()}); + } + + /** + * Get the contents of the file. + */ + public function getFile(): string + { + return Storage::disk($this->getFileStorageDisk())->get($this->getFilePath()); + } + + /** + * Get the storage disk to use for file operations. + */ + public function getFileStorageDisk(): string + { + // return the disk the user specified if they have it set + if ($this->fileStorageDisk) { + return $this->fileStorageDisk; + } + + // otherwise return the default disk + return config('filesystems.default'); + } + + /** + * Get the full storage path to the file. + */ + public function getFilePath(): string + { + $filePath = $this->getStorageDirectory() . $this->{$this->getFileNameField()}; + + return $filePath; + } + + /** + * Get the URL for accessing the file. + */ + protected function fileUrl(): Attribute + { + return Attribute::make( + get: function () { + if (empty($this->{$this->getFileNameField()})) { + return null; + } + + $disk = Storage::disk($this->getFileStorageDisk()); + + // Check if the disk supports URL generation + if (method_exists($disk, 'url')) { + return $disk->url($this->getStorageDirectory() . $this->{$this->getFileNameField()}); + } + + // Return null if URL generation is not supported + return null; + }, + ); + } + + /** + * Get the storage directory path for the file. + */ + protected function getStorageDirectory(): string + { + return '/' . $this->getTable() . '/' . $this->{$this->primaryKey} . '/'; + } + + /** + * Get the database field name that stores the filename. + */ + protected function getFileNameField(): string + { + // return the file name field the user specified if they have it set + if ($this->fileNameField) { + return $this->fileNameField; + } + + // otherwise return the default file name field + return 'file_name'; + } +} diff --git a/app/Models/User.php b/app/Models/User.php index f6a0b0c..edd73fa 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -11,6 +11,7 @@ use Database\Factories\UserFactory; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Storage; @@ -149,6 +150,11 @@ public function assignedStudies(): BelongsToMany return $this->belongsToMany(Study::class, 'study_assignments'); } + public function radiologistProfile(): HasOne + { + $this->hasOne(RadiologistProfile::class); + } + /** * Get the attributes that should be cast. * diff --git a/database/migrations/2025_01_07_904595_create_radiologists.php b/database/migrations/2025_01_07_904595_create_radiologist_profiles.php similarity index 71% rename from database/migrations/2025_01_07_904595_create_radiologists.php rename to database/migrations/2025_01_07_904595_create_radiologist_profiles.php index aba5ac2..2e76304 100644 --- a/database/migrations/2025_01_07_904595_create_radiologists.php +++ b/database/migrations/2025_01_07_904595_create_radiologist_profiles.php @@ -9,10 +9,10 @@ { public function up(): void { - Schema::create('radiologists', static function (Blueprint $table) { + Schema::create('radiologist_profiles', static function (Blueprint $table) { $table->id(); $table->foreignIdFor(User::class)->unique()->constrained()->cascadeOnDelete(); - $table->text('signature_image_path')->nullable(); + $table->string('signature_image_path')->nullable(); $table->text('signature_text')->nullable(); $table->timestamps(); }); @@ -20,6 +20,6 @@ public function up(): void public function down(): void { - Schema::dropIfExists('radiologists'); + Schema::dropIfExists('radiologist_profiles'); } };