Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ coverage
phpunit.xml
psalm.xml
testbench.yaml
vendor
./vendor
106 changes: 89 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![PHPCS check](https://github.com/thomascombe/backpack-async-export/actions/workflows/phpcs.yml/badge.svg)](https://github.com/thomascombe/backpack-async-export/actions/workflows/phpcs.yml)
[![Total Downloads](https://img.shields.io/packagist/dt/thomascombe/backpack-async-export.svg?style=flat-square)](https://packagist.org/packages/thomascombe/backpack-async-export)

This is a package to manage async export in [Backpack](https://backpackforlaravel.com/) for Laravel
This is a package to manage async export and import in [Backpack](https://backpackforlaravel.com/) for Laravel

<p align="center"><img src="/docs/images/demo.png" alt="Demo of Laravel Backpack Async Export"></p>

Expand Down Expand Up @@ -34,15 +34,20 @@ This is the contents of the published config file:

```php
return [
'feature_enabled' => [
'export' => true,
'import' => true,
],
'user_model' => 'App\Models\User',
'export_model' => \Thomascombe\BackpackAsyncExport\Models\Export::class,
'admin_route' => 'export',
'import_export_model' => \Thomascombe\BackpackAsyncExport\Models\ImportExport::class,
'admin_export_route' => 'export',
'admin_import_route' => 'import',
'export_memory_limit' => '2048M',
'disk' => 'local',
];
```

## Usage
## Usage for export

### Add export item in menu
```bash
Expand Down Expand Up @@ -82,17 +87,18 @@ public function setup()
```

### Add method to your CRUD controller

```php
use Thomascombe\BackpackAsyncExport\Enums\ExportStatus;
use Thomascombe\BackpackAsyncExport\Models\Export;
use Thomascombe\BackpackAsyncExport\Models\ImportExport;

public function getExport(): Export
public function getExport(): ImportExport
{
return Export::create([
Export::COLUMN_USER_ID => backpack_user()->id,
Export::COLUMN_STATUS => ExportStatus::Created,
Export::COLUMN_FILENAME => sprintf('export/users_%s.xlsx', now()->toIso8601String()),
Export::COLUMN_EXPORT_TYPE => UserExport::class,
return ImportExport::create([
ImportExport::COLUMN_USER_ID => backpack_user()->id,
ImportExport::COLUMN_STATUS => ExportStatus::Created,
ImportExport::COLUMN_FILENAME => sprintf('export/users_%s.xlsx', now()->toIso8601String()),
ImportExport::COLUMN_EXPORT_TYPE => UserExport::class,
]);
}

Expand All @@ -102,14 +108,80 @@ public function getExportParameters(): array
}
```

## Usage for import

### Add import item in menu
```bash
php artisan backpack:add-sidebar-content "<li class='nav-item'><a class='nav-link' href='{{ backpack_url('import') }}'><i class='nav-icon la la-file-import'></i> <span>Import</span></a></li>"
```

### Create you import class
```bash
php artisan make:import UserImport --model=App\Models\User
```
For all details, have a look at [Laravel Excel Package](https://laravel-excel.com/)

### Create your controller
```bash
php artisan backpack:crud {Name}CrudController
```

### Your controller need to implement interface
```php
use Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Interfaces\ImportableCrud;

class {Name}CrudController extends CrudController implements ImportableCrud {}
```

### Use awesome trait
```php
use Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Traits\HasImportButton;
```

### Call method to add buttons
```php
public function setup()
{
// ...
$this->addImportButtons();
}
```

### Add method to your CRUD controller

```php
use Thomascombe\BackpackAsyncExport\Enums\ExportStatus;
use Thomascombe\BackpackAsyncExport\Models\ImportExport;

public function getImport(): ImportExport
{
return ImportExport::create([
ImportExport::COLUMN_USER_ID => backpack_user()->id,
ImportExport::COLUMN_STATUS => ExportStatus::Created,
ImportExport::COLUMN_FILENAME => sprintf('export/users_%s.xlsx', now()->toIso8601String()),
ImportExport::COLUMN_EXPORT_TYPE => UserExport::class,
]);
}

public function getImportParameters(): array
{
return [
'private' => [
'hint' => 'CSV file required',
'mimetypes' => ['text/csv', 'application/csv'],
],
];
}
```

## Need more?

### Override Export model
You can override `Export` model using config : `export_model`.
Your model class **need** to implement `\Thomascombe\BackpackAsyncExport\Models\Export`.
### Override ImportExport model
You can override `ImportExport` model using config : `import_export_model`.
Your model class **need** to implement `\Thomascombe\BackpackAsyncExport\Models\ImportExport`.

```php
class Export extends \Thomascombe\BackpackAsyncExport\Models\Export
class ImportExport extends \Thomascombe\BackpackAsyncExport\Models\ImportExport
{
}
```
Expand Down Expand Up @@ -153,9 +225,9 @@ public function getAvailableExports(): array

For each new export you have to add news methods:
```php
public function getExport*All*(): Export
public function getExport*All*(): ImportExport
{
return Export::create(...);
return ImportExport::create(...);
}

public function getExport*All*Parameters(): array
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
],
"require": {
"php": "^7.4|^8.0",
"backpack/crud": "4.1.*",
"illuminate/contracts": "^6.0|^7.0|^8.0",
"backpack/crud": "4.1.*|~5.0",
"illuminate/contracts": "^6.0|^7.0|^8.0|^9.0",
"maatwebsite/excel": "^3.1",
"thomascombe/laravel-package-tools": "^1.7"
},
Expand Down Expand Up @@ -49,7 +49,10 @@
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
}
},
"extra": {
"laravel": {
Expand Down
21 changes: 0 additions & 21 deletions config/backpack-async-export.php

This file was deleted.

30 changes: 30 additions & 0 deletions config/backpack-async-import-export.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

return [
/**************
* GLOBAL
***************
*/
'feature_enabled' => [
'export' => true,
'import' => true,
],
/**************
* MODEL
***************
*/
'user_model' => 'App\Models\User',
'import_export_model' => \Thomascombe\BackpackAsyncExport\Models\ImportExport::class,
/**************
* Routing
***************
*/
'admin_export_route' => 'export',
'admin_import_route' => 'import',
/**************
* Filesystem
***************
*/
'export_memory_limit' => '2048M',
'disk' => 'local',
];
19 changes: 0 additions & 19 deletions database/factories/ModelFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBackpackAsyncExportTable extends Migration
class CreateBackpackAsyncImportExportsTable extends Migration
{
public function up()
{
Schema::create('exports', function (Blueprint $table) {
Schema::create('import_exports', function (Blueprint $table) {
$table->bigIncrements('id');

$table->unsignedBigInteger('user_id');
$table->string('action_type');
$table->string('export_type');
$table->string('filename');
$table->string('disk');
Expand All @@ -22,7 +23,7 @@ class CreateBackpackAsyncExportTable extends Migration
$table->timestamps();
$table->softDeletes();

$table->foreign('user_id', 'exports_ibfk_1')
$table->foreign('user_id', 'import_exports_ibfk_1')
->references('id')
->on('users');
});
Expand Down
13 changes: 13 additions & 0 deletions resources/lang/en/admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'column' => [
'file' => 'File',
],
'button' => [
'import' => 'Import',
],
'operation' => [
'import' => 'Import',
],
];
25 changes: 25 additions & 0 deletions resources/lang/en/import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

return [
'name' => [
'singular' => 'import',
'plurial' => 'imports',
],
'columns' => [
'user_id' => 'User',
'export_type' => 'Type',
'filename' => 'Filename',
'status' => 'Status',
'error' => 'Error',
'completed_at' => 'Completed at',
],
'buttons' => [
'import' => 'Import',
],
'notifications' => [
'queued' => 'Import launched! Results will be available when ready on "Import" tab',
],
'errors' => [
'global-import' => 'Error during import',
],
];
2 changes: 1 addition & 1 deletion resources/views/buttons/export.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@if ($crud->hasAccess('list'))
@php($exports = $crud->get('exports', ['default' => null]))
@foreach($exports as $export => $exportsName)
<a href="{{ url($crud->route.'/'.config('backpack-async-export.admin_route')) }}?{{ \Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Interfaces\MultiExportableCrud::QUERY_PARAM }}={{ $export }}" class="btn btn-xs btn-default"><i class="fa fa-ban"></i> {{ __('backpack-async-export::export.buttons.exports') }}@if (!empty($exportsName)) ({{ $exportsName }})@endif</a>
<a href="{{ url($crud->route.'/'.config('backpack-async-import-export.admin_export_route')) }}?{{ \Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Interfaces\MultiExportableCrud::QUERY_PARAM }}={{ $export }}" class="btn btn-xs btn-default"><i class="fa fa-ban"></i> {{ __('backpack-async-export::export.buttons.exports') }}@if (!empty($exportsName)) ({{ $exportsName }})@endif</a>
@endforeach
@endif
5 changes: 5 additions & 0 deletions resources/views/buttons/import.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@if ($crud->hasAccess('list'))
@php($route = $crud->get('import_route'))

<a href="{{ $route }}" class="btn btn-xs btn-default"><i class="fa fa-ban"></i> {{ __('backpack-async-export::import.buttons.import') }}</a>
@endif
Loading