Skip to content

Commit

Permalink
feature: Import action
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Oct 29, 2023
1 parent 1801f08 commit 685bfad
Show file tree
Hide file tree
Showing 20 changed files with 2,042 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"kirschbaum-development/eloquent-power-joins": "^3.0",
"laravel/cashier": "^14.2",
"laravel/pint": "^1.0",
"league/csv": "^9.11",
"league/flysystem-aws-s3-v3": "^3.0",
"nunomaduro/larastan": "^2.2",
"nunomaduro/termwind": "^1.0",
Expand Down
90 changes: 89 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/actions/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"illuminate/contracts": "^10.0",
"illuminate/database": "^10.0",
"illuminate/support": "^10.0",
"league/csv": "^9.11",
"spatie/laravel-package-tools": "^1.9"
},
"autoload": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('failed_import_rows', function (Blueprint $table) {
$table->id();
$table->json('data');
$table->foreignId('import_id')->constrained()->cascadeOnDelete();
$table->text('validation_error')->nullable();
$table->timestamps();
});
}
};
27 changes: 27 additions & 0 deletions packages/actions/database/migrations/create_imports_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('imports', function (Blueprint $table) {
$table->id();
$table->timestamp('completed_at')->nullable();
$table->string('file_name');
$table->string('file_path');
$table->string('importer');
$table->unsignedInteger('processed_rows')->default(0);
$table->unsignedInteger('total_rows');
$table->unsignedInteger('successful_rows')->default(0);
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
};
77 changes: 77 additions & 0 deletions packages/actions/resources/lang/en/import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

return [

'label' => 'Import :label',

'modal' => [

'heading' => 'Import :label',

'form' => [

'file' => [
'label' => 'File',
'placeholder' => 'Upload a .csv file',
],

'columns' => [
'label' => 'Columns',
'placeholder' => 'Select a column',
],

],

'actions' => [

'download_example' => [
'label' => 'Download an example .csv file.',
],

'import' => [
'label' => 'Start import',
],

],

],

'notifications' => [

'completed' => [

'title' => 'Import completed',

'actions' => [

'download_failed_rows_csv' => [
'label' => 'Download information about the failed row|Download information about the failed rows',
],

],

],

'max_rows' => [
'title' => 'That file is too large to import',
'body' => 'You may not import more than :count row at once.',
],

'started' => [
'title' => 'Import started',
'body' => 'Your import has begun and 1 row will be processed in the background.|Your import has begun and :count rows will be processed in the background.',
],

],

'example_csv' => [
'file_name' => ':importer-example',
],

'failure_csv' => [
'file_name' => 'import-:import_id-:csv_name-failed-rows',
'error_header' => 'error',
'system_error' => 'System error, please contact support.',
],

];
8 changes: 8 additions & 0 deletions packages/actions/routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Filament\Actions\Imports\Http\Controllers\DownloadImportFailureCsv;
use Illuminate\Support\Facades\Route;

Route::get('/filament/imports/{import}/failed-rows/download', DownloadImportFailureCsv::class)
->name('filament.imports.failed-rows.download')
->middleware(['web', 'auth']);
42 changes: 42 additions & 0 deletions packages/actions/src/ActionsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Actions;

use Filament\Actions\Testing\TestsActions;
use Illuminate\Filesystem\Filesystem;
use Livewire\Features\SupportTesting\Testable;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
Expand All @@ -13,12 +14,53 @@ public function configurePackage(Package $package): void
{
$package
->name('filament-actions')
->hasCommands($this->getCommands())
->hasMigrations([
'create_imports_table',
'create_failed_import_rows_table',
])
->hasRoute('web')
->hasTranslations()
->hasViews();
}

public function packageBooted(): void
{
if ($this->app->runningInConsole()) {
foreach (app(Filesystem::class)->files(__DIR__ . '/../stubs/') as $file) {
$this->publishes([
$file->getRealPath() => base_path("stubs/filament/{$file->getFilename()}"),
], 'filament-stubs');
}
}

Testable::mixin(new TestsActions());
}

/**
* @return array<class-string>
*/
protected function getCommands(): array
{
$commands = [
Commands\MakeImporterCommand::class,
];

$aliases = [];

foreach ($commands as $command) {
$class = 'Filament\\Actions\\Commands\\Aliases\\' . class_basename($command);

if (! class_exists($class)) {
continue;
}

$aliases[] = $class;
}

return [
...$commands,
...$aliases,
];
}
}
12 changes: 12 additions & 0 deletions packages/actions/src/Commands/Aliases/MakeImporterCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Filament\Actions\Commands\Aliases;

use Filament\Actions\Commands;

class MakeImporterCommand extends Commands\MakeImporterCommand
{
protected $hidden = true;

protected $signature = 'filament:importer {name?} {--G|generate} {--F|force}';
}
Loading

0 comments on commit 685bfad

Please sign in to comment.