Files
pds/app/Filament/Resources/TestingTempResource.php
dhanabalan f0f7bffe74
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Gemini PR Review / review (pull_request) Failing after 28s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 15s
Laravel Pint / pint (pull_request) Successful in 2m25s
Laravel Larastan / larastan (pull_request) Failing after 3m26s
Added testing temp resource files
2026-01-06 18:56:45 +05:30

186 lines
6.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';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->label('File Name'),
Forms\Components\FileUpload::make('attachment')
->label('Upload')
// ->acceptedFileTypes(['application/pdf'])
->storeFiles(false)
->disk('local')
->directory('uploads/temp')
->preserveFilenames()
->reactive(),
Forms\Components\Actions::make([
Action::make('uploadNow')
->label('File Upload')
->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 = $get('name');
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) {
Notification::make()
->title("File downloaded successfully")
->success()
->send();
return response()->download(
Storage::disk('local')->path($file)
);
}
}
Notification::make()
->title('File not found')
->danger()
->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,
]);
}
}