From df4bd9874f80a0a17fe3e311bf5dbf0855219d87 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Fri, 10 Oct 2025 15:25:47 +0530 Subject: [PATCH] Added module list file name --- app/Filament/Resources/ModuleListResource.php | 45 ++++++++------ .../Pages/CreateModuleList.php | 60 +++++++++++++++++++ 2 files changed, 87 insertions(+), 18 deletions(-) diff --git a/app/Filament/Resources/ModuleListResource.php b/app/Filament/Resources/ModuleListResource.php index 6783144..155062d 100644 --- a/app/Filament/Resources/ModuleListResource.php +++ b/app/Filament/Resources/ModuleListResource.php @@ -28,24 +28,33 @@ class ModuleListResource extends Resource return $form ->schema([ Section::make('') - ->schema([ - Forms\Components\TextInput::make('module_name') - ->label('Module Name') - ->required(), - Forms\Components\TextInput::make('dashboard_name') - ->label('DashBoard Name') - ->required(), - Forms\Components\TextInput::make('filter_name') - ->label('Filter Name') - ->required(), - 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), - ]) - ->columns(3), + ->schema([ + Forms\Components\TextInput::make('module_name') + ->label('Module Name') + ->required(), + Forms\Components\TextInput::make('dashboard_name') + ->label('DashBoard Name') + ->required(), + Forms\Components\TextInput::make('filter_name') + ->label('Filter Name') + ->required(), + Forms\Components\TextInput::make('file_name') + ->label('File Name') + ->required() + ->reactive() + ->extraAttributes([ + 'x-data' => '{ value: "" }', + 'x-model' => 'value', + 'x-on:keydown.enter.prevent' => '$wire.process(value)', + ]), + 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), + ]) + ->columns(4), ]); } diff --git a/app/Filament/Resources/ModuleListResource/Pages/CreateModuleList.php b/app/Filament/Resources/ModuleListResource/Pages/CreateModuleList.php index 854e619..779e740 100644 --- a/app/Filament/Resources/ModuleListResource/Pages/CreateModuleList.php +++ b/app/Filament/Resources/ModuleListResource/Pages/CreateModuleList.php @@ -3,13 +3,73 @@ namespace App\Filament\Resources\ModuleListResource\Pages; use App\Filament\Resources\ModuleListResource; +use App\Models\ModuleList; use Filament\Actions; +use Filament\Notifications\Notification; use Filament\Resources\Pages\CreateRecord; +use phpseclib3\Net\SFTP; class CreateModuleList extends CreateRecord { protected static string $resource = ModuleListResource::class; + public function process($fileName) + { + //$file = trim($fileName); + if (!$fileName) + { + Notification::make() + ->title('File name is required!') + ->danger() + ->send(); + return; + } + + $ftpHost = env('SFTP_HOST'); + $ftpUser = env('SFTP_USERNAME'); + $ftpPass = env('SFTP_PASSWORD'); + $remoteDir = env('SFTP_ROOT'); + $port = env('SFTP_PORT'); + + $sftp = new SFTP($ftpHost, $port, 10); + if (! $sftp->login($ftpUser, $ftpPass)) { + Notification::make()->title('SFTP login failed')->danger()->send(); + return; + } + + $remoteFile = $remoteDir . '/' . $fileName . '.txt'; + $content = $sftp->get($remoteFile); + + if ($content == false) { + Notification::make() + ->title("File {$remoteFile} not found") + ->danger() + ->send(); + return; + } + + if ($content == false) { + Notification::make() + ->title("Failed to read {$remoteDir} from SFTP") + ->danger() + ->send(); + return; + } + + $lines = array_map('trim', explode("\n", str_replace("\r", "", $content))); + + ModuleList::create([ + 'module_name' => $lines[0] ?? '', + 'dashboard_name' => $lines[1] ?? '', + 'filter_name' => $lines[2] ?? '', + ]); + + Notification::make() + ->title("File {$remoteDir} read and saved successfully!") + ->success() + ->send(); + } + protected function getRedirectUrl(): string { return $this->getResource()::getUrl('create');