100 lines
2.7 KiB
PHP
Executable File
100 lines
2.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Filament\Pages\Strumenti;
|
|
|
|
use App\Models\User;
|
|
use Filament\Forms\Concerns\InteractsWithForms;
|
|
use Filament\Forms\Contracts\HasForms;
|
|
use Filament\Forms\Form;
|
|
use Filament\Forms\Components\FileUpload;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class ImportInbox extends Page implements HasForms
|
|
{
|
|
use InteractsWithForms;
|
|
|
|
protected static ?string $navigationLabel = 'Inbox import';
|
|
|
|
protected static ?string $title = 'Inbox import';
|
|
|
|
protected static \BackedEnum|string|null $navigationIcon = 'heroicon-o-inbox-arrow-down';
|
|
|
|
protected static \UnitEnum|string|null $navigationGroup = 'Strumenti';
|
|
|
|
protected static ?int $navigationSort = 90;
|
|
|
|
protected static ?string $slug = 'strumenti/inbox-import';
|
|
|
|
protected string $view = 'filament.pages.strumenti.import-inbox';
|
|
|
|
/** @var array<string, mixed> */
|
|
public array $data = [];
|
|
|
|
/** @var array<int, array{name:string,path:string,size:int,modified:int}> */
|
|
public array $files = [];
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$u = Auth::user();
|
|
return $u instanceof User && $u->hasAnyRole(['super-admin', 'admin']);
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->form->fill();
|
|
$this->refreshFiles();
|
|
}
|
|
|
|
public function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->statePath('data')
|
|
->schema([
|
|
FileUpload::make('uploads')
|
|
->label('Carica file (da Windows)')
|
|
->helperText('I file vengono salvati in storage locale: import/inbox')
|
|
->disk('local')
|
|
->directory('import/inbox')
|
|
->multiple()
|
|
->preserveFilenames()
|
|
->required(),
|
|
]);
|
|
}
|
|
|
|
public function submit(): void
|
|
{
|
|
$uploads = $this->data['uploads'] ?? [];
|
|
$count = is_array($uploads) ? count($uploads) : 0;
|
|
|
|
Notification::make()
|
|
->title('Upload completato')
|
|
->body('File caricati: ' . $count)
|
|
->success()
|
|
->send();
|
|
|
|
$this->data['uploads'] = [];
|
|
$this->refreshFiles();
|
|
}
|
|
|
|
public function refreshFiles(): void
|
|
{
|
|
$disk = Storage::disk('local');
|
|
$paths = $disk->files('import/inbox');
|
|
sort($paths);
|
|
|
|
$out = [];
|
|
foreach ($paths as $path) {
|
|
$out[] = [
|
|
'name' => basename($path),
|
|
'path' => $path,
|
|
'size' => (int) $disk->size($path),
|
|
'modified' => (int) $disk->lastModified($path),
|
|
];
|
|
}
|
|
$this->files = $out;
|
|
}
|
|
}
|