Added import and export for configurations
This commit is contained in:
54
app/Filament/Exports/ConfigurationExporter.php
Normal file
54
app/Filament/Exports/ConfigurationExporter.php
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Exports;
|
||||||
|
|
||||||
|
use App\Models\Configuration;
|
||||||
|
use Filament\Actions\Exports\ExportColumn;
|
||||||
|
use Filament\Actions\Exports\Exporter;
|
||||||
|
use Filament\Actions\Exports\Models\Export;
|
||||||
|
|
||||||
|
class ConfigurationExporter extends Exporter
|
||||||
|
{
|
||||||
|
protected static ?string $model = Configuration::class;
|
||||||
|
|
||||||
|
public static function getColumns(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
ExportColumn::make('no')
|
||||||
|
->label('No')
|
||||||
|
->state(function ($record) use (&$rowNumber) {
|
||||||
|
// Increment and return the row number
|
||||||
|
return ++$rowNumber;
|
||||||
|
}),
|
||||||
|
ExportColumn::make('plant.name')
|
||||||
|
->label('Plant'),
|
||||||
|
ExportColumn::make('line.name')
|
||||||
|
->label('Line'),
|
||||||
|
ExportColumn::make('c_type')
|
||||||
|
->label('Type'),
|
||||||
|
ExportColumn::make('c_group')
|
||||||
|
->label('Group'),
|
||||||
|
ExportColumn::make('c_name')
|
||||||
|
->label('Name'),
|
||||||
|
ExportColumn::make('c_value')
|
||||||
|
->label('Value'),
|
||||||
|
ExportColumn::make('created_at')
|
||||||
|
->label('Created At'),
|
||||||
|
ExportColumn::make('updated_at')
|
||||||
|
->label('Updated At'),
|
||||||
|
ExportColumn::make('deleted_at')
|
||||||
|
->label('Deleted At'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getCompletedNotificationBody(Export $export): string
|
||||||
|
{
|
||||||
|
$body = 'Your configuration export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.';
|
||||||
|
|
||||||
|
if ($failedRowsCount = $export->getFailedRowsCount()) {
|
||||||
|
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
}
|
||||||
78
app/Filament/Imports/ConfigurationImporter.php
Normal file
78
app/Filament/Imports/ConfigurationImporter.php
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Imports;
|
||||||
|
|
||||||
|
use App\Models\Configuration;
|
||||||
|
use Filament\Actions\Imports\ImportColumn;
|
||||||
|
use Filament\Actions\Imports\Importer;
|
||||||
|
use Filament\Actions\Imports\Models\Import;
|
||||||
|
|
||||||
|
class ConfigurationImporter extends Importer
|
||||||
|
{
|
||||||
|
protected static ?string $model = Configuration::class;
|
||||||
|
|
||||||
|
public static function getColumns(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
ImportColumn::make('plant')
|
||||||
|
->requiredMapping()
|
||||||
|
->relationship(resolveUsing: 'name')
|
||||||
|
->exampleHeader('Plant')
|
||||||
|
->example(['Ransar Industries-I'])
|
||||||
|
->label('Plant')
|
||||||
|
->rules(['required']),
|
||||||
|
ImportColumn::make('line')
|
||||||
|
->requiredMapping()
|
||||||
|
->relationship(resolveUsing: 'name')
|
||||||
|
->exampleHeader('Plant')
|
||||||
|
->example(['4 inch pump line'])
|
||||||
|
->label('Line')
|
||||||
|
->rules(['required']),
|
||||||
|
ImportColumn::make('c_type')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Type')
|
||||||
|
->example(['Testing Panel Readings'])
|
||||||
|
->label('Type')
|
||||||
|
->rules(['required']),
|
||||||
|
ImportColumn::make('c_group')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Group')
|
||||||
|
->example(['LMC_WINDED'])
|
||||||
|
->label('Group')
|
||||||
|
->rules(['required']),
|
||||||
|
ImportColumn::make('c_name')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Name')
|
||||||
|
->example(['MOTOR_PHASE'])
|
||||||
|
->label('Name')
|
||||||
|
->rules(['required']),
|
||||||
|
ImportColumn::make('c_value')
|
||||||
|
->requiredMapping()
|
||||||
|
->exampleHeader('Value')
|
||||||
|
->example(['Single'])
|
||||||
|
->label('Value')
|
||||||
|
->rules(['required']),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resolveRecord(): ?Configuration
|
||||||
|
{
|
||||||
|
// return Configuration::firstOrNew([
|
||||||
|
// // Update existing records, matching them by `$this->data['column_name']`
|
||||||
|
// 'email' => $this->data['email'],
|
||||||
|
// ]);
|
||||||
|
|
||||||
|
return new Configuration();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getCompletedNotificationBody(Import $import): string
|
||||||
|
{
|
||||||
|
$body = 'Your configuration import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.';
|
||||||
|
|
||||||
|
if ($failedRowsCount = $import->getFailedRowsCount()) {
|
||||||
|
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $body;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,10 +2,13 @@
|
|||||||
|
|
||||||
namespace App\Filament\Resources;
|
namespace App\Filament\Resources;
|
||||||
|
|
||||||
|
use App\Filament\Exports\ConfigurationExporter;
|
||||||
|
use App\Filament\Imports\ConfigurationImporter;
|
||||||
use App\Filament\Resources\ConfigurationResource\Pages;
|
use App\Filament\Resources\ConfigurationResource\Pages;
|
||||||
use App\Filament\Resources\ConfigurationResource\RelationManagers;
|
use App\Filament\Resources\ConfigurationResource\RelationManagers;
|
||||||
use App\Models\Configuration;
|
use App\Models\Configuration;
|
||||||
use App\Models\Line;
|
use App\Models\Line;
|
||||||
|
use Filament\Facades\Filament;
|
||||||
use Filament\Forms;
|
use Filament\Forms;
|
||||||
use Filament\Forms\Form;
|
use Filament\Forms\Form;
|
||||||
use Filament\Forms\Get;
|
use Filament\Forms\Get;
|
||||||
@@ -14,6 +17,8 @@ use Filament\Tables;
|
|||||||
use Filament\Tables\Table;
|
use Filament\Tables\Table;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||||
|
use Filament\Tables\Actions\ImportAction;
|
||||||
|
use Filament\Tables\Actions\ExportAction;
|
||||||
|
|
||||||
class ConfigurationResource extends Resource
|
class ConfigurationResource extends Resource
|
||||||
{
|
{
|
||||||
@@ -170,6 +175,18 @@ class ConfigurationResource extends Resource
|
|||||||
Tables\Actions\ForceDeleteBulkAction::make(),
|
Tables\Actions\ForceDeleteBulkAction::make(),
|
||||||
Tables\Actions\RestoreBulkAction::make(),
|
Tables\Actions\RestoreBulkAction::make(),
|
||||||
]),
|
]),
|
||||||
|
])
|
||||||
|
->headerActions([
|
||||||
|
ImportAction::make()
|
||||||
|
->importer(ConfigurationImporter::class)
|
||||||
|
->visible(function() {
|
||||||
|
return Filament::auth()->user()->can('view import configuration');
|
||||||
|
}),
|
||||||
|
ExportAction::make()
|
||||||
|
->exporter(ConfigurationExporter::class)
|
||||||
|
->visible(function() {
|
||||||
|
return Filament::auth()->user()->can('view export configuration');
|
||||||
|
}),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user