diff --git a/app/Filament/Resources/TestingTempResource.php b/app/Filament/Resources/TestingTempResource.php index 2c9f88a..7c6cacb 100644 --- a/app/Filament/Resources/TestingTempResource.php +++ b/app/Filament/Resources/TestingTempResource.php @@ -3,20 +3,20 @@ namespace App\Filament\Resources; use App\Filament\Resources\TestingTempResource\Pages; -use App\Filament\Resources\TestingTempResource\RelationManagers; +use App\Models\Plant; use App\Models\TestingTemp; use Filament\Facades\Filament; use Filament\Forms; +use Filament\Forms\Components\Actions\Action; use Filament\Forms\Form; +use Filament\Notifications\Notification; use Filament\Resources\Resource; use Filament\Tables; use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; -use Filament\Forms\Components\Actions\Action; -use Filament\Notifications\Notification; -use Storage; use Livewire\Features\SupportFileUploads\TemporaryUploadedFile; +use Storage; class TestingTempResource extends Resource { @@ -30,12 +30,69 @@ class TestingTempResource extends Resource { return $form ->schema([ + Forms\Components\Select::make('plant_id') + ->label('Plant Name') + ->relationship('plant', 'name') + ->required() + ->searchable() + // ->preload() + // ->nullable(), + ->reactive() + ->columnSpan(1) + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; + + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $plantId = $get('plant_id'); + $set('ivPlantError', null); + $set('name', null); + $set('selected_file', null); + $set('attachment', null); + if (! $plantId) { + $set('ivPlantError', 'Please select a plant first.'); + } + $set('updated_by', Filament::auth()->user()?->name); + }) + ->extraAttributes(fn ($get) => [ + 'class' => $get('ivPlantError') ? 'border-red-500' : '', + ]) + ->hint(fn ($get) => $get('ivPlantError') ? $get('ivPlantError') : null) + ->hintColor('danger'), Forms\Components\TextInput::make('name') - ->label('File Name'), + ->label('File Name') + ->required() + ->reactive() + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $plantId = $get('plant_id'); + $fileNam = $get('name'); + // $set('selected_file', null); + if (! $plantId) { + $set('name', null); + } elseif (! $fileNam) { + $set('attachment', null); + } + $set('updated_by', Filament::auth()->user()?->name); + }), Forms\Components\Select::make('selected_file') ->label('Select Uploaded File') - ->options(function () { - return collect(Storage::disk('local')->files('uploads')) + ->searchable() + ->options(function (callable $get) { + $plantId = $get('plant_id'); + if (! $plantId) { + return collect(Storage::disk('local')->files('uploads/temp')) + ->mapWithKeys(function ($file) { + return [ + $file => basename($file), // value => label + ]; + }) + ->toArray(); + } + + $plantCode = Plant::find($plantId)?->code ?? null; + + return collect(Storage::disk('local')->files("uploads/temp/{$plantCode}")) ->mapWithKeys(function ($file) { return [ $file => basename($file), // value => label @@ -43,101 +100,157 @@ class TestingTempResource extends Resource }) ->toArray(); }) - ->searchable() + ->afterStateUpdated(function ($state, callable $set, callable $get) { + // $plantId = $get('plant_id'); + // if (! $plantId) { + // $set('selected_file', null); + // } + $set('updated_by', Filament::auth()->user()?->name); + }) ->reactive(), + // ->disabled(fn (callable $get) => ! $get('plant_id')), Forms\Components\FileUpload::make('attachment') - ->label('Upload') + ->label('Choose File to Upload') // ->acceptedFileTypes(['application/pdf']) ->storeFiles(false) ->disk('local') - ->directory('uploads/temp') + ->directory(function (callable $get) { + $plantId = $get('plant_id'); + $plantCode = Plant::find($plantId)?->code ?? null; + + return "uploads/temp/{$plantCode}"; + }) ->preserveFilenames() - ->maxSize(20480) - ->reactive(), + ->maxSize(20480) // 20 MB + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $plantId = $get('plant_id'); + if (! $plantId) { + $set('attachment', null); + } + $set('updated_by', Filament::auth()->user()?->name); + }) + ->required(function (callable $get) { + $selFile = $get('selected_file'); + $selAtt = $get('attachment'); + if (! $selAtt && ! $selFile) { + return true; + } + + return false; + }) + ->reactive() + ->disabled(fn (callable $get) => ! $get('plant_id') || ! $get('name')), Forms\Components\Actions::make([ Action::make('uploadNow') - ->label('File Upload') - ->color('success') - ->action(function ($get) { - - $uploadedFiles = $get('attachment'); - - if (is_array($uploadedFiles) && count($uploadedFiles) > 0) { - - $uploaded = reset($uploadedFiles); - - if ($uploaded instanceof TemporaryUploadedFile) { - - $baseName = $get('name'); - $extension = $uploaded->getClientOriginalExtension(); - - $finalFileName = $baseName . '.' . $extension; - - $uploaded->storeAs( - 'uploads', - $finalFileName, - 'local' - ); - - Notification::make() - ->title("File uploaded successfully: {$finalFileName}") - ->success() - ->send(); + ->label('Upload File') + ->color('success') + ->requiresConfirmation(function (callable $get) { + $filePath = $get('attachment'); + if ($filePath) { + return true; } - } else { - Notification::make() - ->title('No file selected to upload') - ->warning() - ->send(); - } - }), + return false; + }) + ->action(function (callable $get, callable $set) { - // Action::make('downloadAttachment') - // ->label('Download File') - // ->action(function ($get) { + $uploadedFiles = $get('attachment'); - // $fileName = basename($get('selected_file')); + if (is_array($uploadedFiles) && count($uploadedFiles) > 0) { - // //dd($fileName); + $uploaded = reset($uploadedFiles); - // // if (!$fileName) { - // // Notification::make() - // // ->title('Enter file name to download') - // // ->danger() - // // ->send(); - // // return; - // // } + if ($uploaded instanceof TemporaryUploadedFile) { - // $files = Storage::disk('local')->files('uploads'); + $baseName = $get('name'); + $extension = $uploaded->getClientOriginalExtension(); - // foreach ($files as $file) { + $finalFileName = $baseName.'.'.$extension; - // if (pathinfo($file, PATHINFO_FILENAME) === $fileName) { - // dd($fileName); - // Notification::make() - // ->title("File downloaded successfully") - // ->success() - // ->send(); + $plantId = $get('plant_id'); + $plantCode = Plant::find($plantId)?->code ?? null; - // return response()->download( - // Storage::disk('local')->path($file) - // ); - // } - // } + if (! $plantCode) { + Notification::make() + ->title('Please select a plant first.') + ->warning() + ->send(); + + return; + } elseif (! $baseName) { + Notification::make() + ->title('Please enter a file name first.') + ->warning() + ->send(); + + return; + } + // return "uploads/temp/{$plantCode}"; + + $uploaded->storeAs( + "uploads/temp/{$plantCode}", + $finalFileName, + 'local' + ); + + $set('updated_by', Filament::auth()->user()?->name); + + Notification::make() + ->title("File uploaded successfully: {$finalFileName}") + ->success() + ->send(); + } + + } else { + if (! $get('plant_id')) { + Notification::make() + ->title('Please select a plant first.') + ->warning() + ->send(); + } elseif (! $get('name')) { + Notification::make() + ->title('Please enter a file name first.') + ->warning() + ->send(); + } else { + Notification::make() + ->title('No file selected to upload') + ->warning() + ->send(); + } + } + }), - // // Notification::make() - // // ->title('File not found') - // // ->danger() - // // ->send(); - // }), Action::make('downloadAttachment') ->label('Download File') - ->action(function ($get) { + ->color('warning') + ->requiresConfirmation(function (callable $get) { + $filePath = $get('selected_file'); + if ($filePath) { + return true; + } + return false; + }) + ->action(function (callable $get, callable $set) { $filePath = $get('selected_file'); // uploads/filename.pdf - if (!$filePath || !Storage::disk('local')->exists($filePath)) { + // if (! $get('plant_id')) { + // Notification::make() + // ->title('Please select a plant first.') + // ->warning() + // ->send(); + + // return; + // } else + if (! $filePath) { + Notification::make() + ->title('Please select a file first.') + ->warning() + ->send(); + + return; + } elseif (! Storage::disk('local')->exists($filePath)) { Notification::make() ->title('File not found') ->danger() @@ -146,6 +259,8 @@ class TestingTempResource extends Resource return; } + $set('updated_by', Filament::auth()->user()?->name); + Notification::make() ->title('File downloaded successfully') ->success() @@ -156,38 +271,65 @@ class TestingTempResource extends Resource ); }), - Action::make('deleteAttachment') + Action::make('deleteAttachment') ->label('Delete File') ->color('danger') - ->requiresConfirmation() - ->action(function ($get) { + ->requiresConfirmation(function (callable $get) { + $filePath = $get('selected_file'); + + if ($filePath) { + return true; + } + + return false; + }) + ->action(function ($get, $set) { $filePath = $get('selected_file'); // uploads/filename.pdf - if (!$filePath || !Storage::disk('local')->exists($filePath)) { - Notification::make() - ->title('File not found') - ->danger() - ->send(); - - return; - } - - Storage::disk('local')->delete($filePath); + // if (! $get('plant_id')) { + // Notification::make() + // ->title('Please select a plant first.') + // ->warning() + // ->send(); + // return; + // } else + if (! $filePath) { Notification::make() - ->title('File deleted successfully') - ->success() + ->title('Please select a file first.') + ->warning() ->send(); - }), - ]), + return; + } elseif (! Storage::disk('local')->exists($filePath)) { + Notification::make() + ->title('File not found') + ->danger() + ->send(); + + return; + } + + Storage::disk('local')->delete($filePath); + + $set('selected_file', null); + + Notification::make() + ->title('File deleted successfully') + ->success() + ->send(); + }), + + ]), Forms\Components\Hidden::make('created_by') ->label('Created By') - ->default(Filament::auth()->user()?->name), + ->default(Filament::auth()->user()?->name) + ->reactive(), Forms\Components\Hidden::make('updated_by') ->label('Updated By') - ->default(Filament::auth()->user()?->name), + ->default(Filament::auth()->user()?->name) + ->reactive(), ]); }