Merge pull request 'Added upload daily report page' (#1036) from ranjith-dev into master
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 24s
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 24s
Reviewed-on: #1036
This commit was merged in pull request #1036.
This commit is contained in:
136
app/Filament/Pages/UploadDailyReport.php
Normal file
136
app/Filament/Pages/UploadDailyReport.php
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Filament\Pages;
|
||||||
|
|
||||||
|
use Filament\Pages\Page;
|
||||||
|
use Filament\Forms;
|
||||||
|
use Filament\Forms\Form;
|
||||||
|
use Filament\Forms\Concerns\InteractsWithForms;
|
||||||
|
use Filament\Forms\Contracts\HasForms;
|
||||||
|
use Filament\Notifications\Notification;
|
||||||
|
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
||||||
|
use Storage;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
|
class UploadDailyReport extends Page implements HasForms
|
||||||
|
{
|
||||||
|
use InteractsWithForms;
|
||||||
|
|
||||||
|
protected static ?string $navigationIcon = 'heroicon-o-document-arrow-up';
|
||||||
|
protected static string $view = 'filament.pages.upload-daily-report';
|
||||||
|
|
||||||
|
protected static ?string $navigationGroup = 'Task Upload';
|
||||||
|
|
||||||
|
public ?array $data = [];
|
||||||
|
|
||||||
|
public array $filters = [];
|
||||||
|
|
||||||
|
public function mount(): void
|
||||||
|
{
|
||||||
|
$this->form->fill();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function form(Form $form): Form
|
||||||
|
{
|
||||||
|
return $form->schema([
|
||||||
|
Forms\Components\Section::make('Upload Daily Task Report')
|
||||||
|
->schema([
|
||||||
|
Forms\Components\TextInput::make('file_name')
|
||||||
|
->label('File Name')
|
||||||
|
->reactive(),
|
||||||
|
Forms\Components\FileUpload::make('report_pdf')
|
||||||
|
->label('Daily Report PDF')
|
||||||
|
->disk('local')
|
||||||
|
->directory('task_updates')
|
||||||
|
->acceptedFileTypes(['application/pdf'])
|
||||||
|
->required()
|
||||||
|
->preserveFilenames(),
|
||||||
|
])
|
||||||
|
->columns(2),
|
||||||
|
])->statePath('filters');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function uploadTaskPdf(): void
|
||||||
|
{
|
||||||
|
$state = $this->form->getState(); // triggers upload/validation
|
||||||
|
$currentFile = $state['report_pdf'];
|
||||||
|
$directory = 'task_updates';
|
||||||
|
$filename = trim(basename($currentFile));
|
||||||
|
|
||||||
|
$path = 'task_updates/' . $filename;
|
||||||
|
|
||||||
|
// $duplicateExists = Storage::disk('local')->exists($path);
|
||||||
|
|
||||||
|
// if ($duplicateExists) {
|
||||||
|
// // Remove the just-uploaded file since it's a duplicate
|
||||||
|
// // Storage::disk('local')->delete($currentFile);
|
||||||
|
|
||||||
|
// Notification::make()
|
||||||
|
// ->title('File already exists')
|
||||||
|
// ->body("A file named \"{$filename}\" is already uploaded.")
|
||||||
|
// ->danger()
|
||||||
|
// ->send();
|
||||||
|
|
||||||
|
// $this->form->fill(['report_pdf' => null]);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// else{
|
||||||
|
Notification::make()
|
||||||
|
->title('PDF uploaded successfully')
|
||||||
|
->success()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
$this->form->fill(['report_pdf' => null]);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeTaskPdf(){
|
||||||
|
$fileName = $this->filters['file_name'] ?? null;
|
||||||
|
|
||||||
|
if (!$fileName) {
|
||||||
|
Notification::make()
|
||||||
|
->title('File Name Required')
|
||||||
|
->body('Please enter a file name before removing the PDF.')
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$filePath = "task_updates/{$fileName}.pdf";
|
||||||
|
|
||||||
|
if (!Storage::disk('local')->exists($filePath)) {
|
||||||
|
Notification::make()
|
||||||
|
->title('PDF Not Found')
|
||||||
|
->body('The selected PDF does not exist.')
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
$this->form->fill(['file_name' => null]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Storage::disk('local')->exists($filePath)) {
|
||||||
|
if (Storage::disk('local')->delete($filePath)) {
|
||||||
|
Notification::make()
|
||||||
|
->title('PDF Removed Successfully')
|
||||||
|
->body('The PDF was removed successfully.')
|
||||||
|
->success()
|
||||||
|
->send();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
Notification::make()
|
||||||
|
->title('PDF Removed Failed')
|
||||||
|
->body('Unable to remove the PDF. Please try again...')
|
||||||
|
->success()
|
||||||
|
->send();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function canAccess(): bool
|
||||||
|
{
|
||||||
|
return Auth::check() && Auth::user()->can('view upload daily report page');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
20
resources/views/filament/pages/upload-daily-report.blade.php
Normal file
20
resources/views/filament/pages/upload-daily-report.blade.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<x-filament-panels::page>
|
||||||
|
{{ $this->form }}
|
||||||
|
|
||||||
|
<div class="flex flex-row gap-2 mt-4">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
wire:click="uploadTaskPdf"
|
||||||
|
class="px-3 py-1 border border-primary-500 text-primary-600 rounded hover:bg-primary-50 hover:border-primary-700 transition text-sm"
|
||||||
|
>
|
||||||
|
Upload Task PDF
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
wire:click="removeTaskPdf"
|
||||||
|
class="px-3 py-1 border border-primary-500 text-primary-600 rounded hover:bg-primary-50 hover:border-primary-700 transition text-sm"
|
||||||
|
>
|
||||||
|
Remove Task PDF
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</x-filament-panels::page>
|
||||||
Reference in New Issue
Block a user