Files
pds/app/Filament/Resources/TestingTempResource.php
dhanabalan 2b476e467c
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Gemini PR Review / Gemini PR Review (pull_request) Has been cancelled
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Has been cancelled
Laravel Larastan / larastan (pull_request) Has been cancelled
Laravel Pint / pint (pull_request) Has been cancelled
Added max size in testing temp
2026-01-28 17:48:47 +05:30

256 lines
9.6 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Resources\TestingTempResource\Pages;
use App\Filament\Resources\TestingTempResource\RelationManagers;
use App\Models\TestingTemp;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Form;
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;
class TestingTempResource extends Resource
{
protected static ?string $model = TestingTemp::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $navigationGroup = 'IIOT Temp';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->label('File Name'),
Forms\Components\Select::make('selected_file')
->label('Select Uploaded File')
->options(function () {
return collect(Storage::disk('local')->files('uploads'))
->mapWithKeys(function ($file) {
return [
$file => basename($file), // value => label
];
})
->toArray();
})
->searchable()
->reactive(),
Forms\Components\FileUpload::make('attachment')
->label('Upload')
// ->acceptedFileTypes(['application/pdf'])
->storeFiles(false)
->disk('local')
->directory('uploads/temp')
->preserveFilenames()
->maxSize(20480)
->reactive(),
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();
}
} else {
Notification::make()
->title('No file selected to upload')
->warning()
->send();
}
}),
// Action::make('downloadAttachment')
// ->label('Download File')
// ->action(function ($get) {
// $fileName = basename($get('selected_file'));
// //dd($fileName);
// // if (!$fileName) {
// // Notification::make()
// // ->title('Enter file name to download')
// // ->danger()
// // ->send();
// // return;
// // }
// $files = Storage::disk('local')->files('uploads');
// foreach ($files as $file) {
// if (pathinfo($file, PATHINFO_FILENAME) === $fileName) {
// dd($fileName);
// Notification::make()
// ->title("File downloaded successfully")
// ->success()
// ->send();
// return response()->download(
// Storage::disk('local')->path($file)
// );
// }
// }
// // Notification::make()
// // ->title('File not found')
// // ->danger()
// // ->send();
// }),
Action::make('downloadAttachment')
->label('Download File')
->action(function ($get) {
$filePath = $get('selected_file'); // uploads/filename.pdf
if (!$filePath || !Storage::disk('local')->exists($filePath)) {
Notification::make()
->title('File not found')
->danger()
->send();
return;
}
Notification::make()
->title('File downloaded successfully')
->success()
->send();
return response()->download(
Storage::disk('local')->path($filePath)
);
}),
Action::make('deleteAttachment')
->label('Delete File')
->color('danger')
->requiresConfirmation()
->action(function ($get) {
$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);
Notification::make()
->title('File deleted successfully')
->success()
->send();
}),
]),
Forms\Components\Hidden::make('created_by')
->label('Created By')
->default(Filament::auth()->user()?->name),
Forms\Components\Hidden::make('updated_by')
->label('Updated By')
->default(Filament::auth()->user()?->name),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('id')
->label('ID')
->numeric()
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('deleted_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
Tables\Filters\TrashedFilter::make(),
])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(),
]),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListTestingTemps::route('/'),
'create' => Pages\CreateTestingTemp::route('/create'),
'view' => Pages\ViewTestingTemp::route('/{record}'),
'edit' => Pages\EditTestingTemp::route('/{record}/edit'),
];
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}