Files
pds/app/Filament/Pages/UploadDailyReport.php
dhanabalan b7d7640cdb
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 14s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 14s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 20s
Laravel Pint / pint (pull_request) Successful in 3m8s
Laravel Larastan / larastan (pull_request) Failing after 4m27s
Added download button in upload daily report
2026-10-27 09:06:59 +05:30

203 lines
5.9 KiB
PHP

<?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()
->maxSize(10240)
->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;
$expectedEmployees = [
'Dhanabalan Saravanan',
'Ranjith',
'Srimathi Ramamoorthy',
];
$filename = pathinfo(basename($currentFile), PATHINFO_FILENAME);
$isValid = collect($expectedEmployees)->contains(
fn ($name) => strcasecmp(trim($filename), $name) == 0
);
if (! $isValid) {
Notification::make()
->title('Invalid PDF filename')
->body(
'Please upload a PDF with one of these names: '
. implode(', ', array_map(
fn ($name) => $name . '.pdf',
$expectedEmployees
))
)
->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 function downloadTaskPdf(){
$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($filePath){
$fullPath = Storage::disk('local')->path($filePath);
Notification::make()
->title('PDF Downloaded Successfully')
->body('The PDF was downloaded successfully.')
->success()
->send();
return response()->download($fullPath, "{$fileName}.pdf");
}
else{
Notification::make()
->title('PDF downloaded Failed')
->body('Unable to download the PDF. Please try again...')
->success()
->send();
return;
}
}
}
public static function canAccess(): bool
{
return Auth::check() && Auth::user()->can('view upload daily report page');
}
}