Skip to content

Commit 78b5198

Browse files
authored
Merge pull request simonhamp#28 from monaye/master
Added importable resouce, importable attributes and custom importer
2 parents 127f94f + 458fbda commit 78b5198

File tree

3 files changed

+102
-32
lines changed

3 files changed

+102
-32
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,54 @@ class NovaServiceProvider extends NovaApplicationServiceProvider
3434
}
3535
```
3636

37+
## Options
38+
By default, all of your Nova Resources will be available for import. However, there are a number of ways that you can explicitly limit what's available for importing.
39+
40+
`public static $canImportResource = false;`
41+
*Default:* `true`
42+
Add this static property to your Resource to prevent it from showing up in the Nova CSV Import tool interface.
43+
44+
`public static function canImportResource($request): bool`
45+
Define a `canImportResource` method to use more complex logic to decide if this Resource can be shown during import. If defined, this takes precedence over the `$canImportResource` property.
46+
47+
`public static function excludeAttributesFromImport(): array`
48+
*Default:* `[]`
49+
Define a `excludeAttributesFromImport` method that returns an array of attribute names that you want to _exclude_ from being visible in the import tool for this Resource.
50+
51+
52+
### Example
53+
54+
```php
55+
// App\Nova\User
56+
public static function canImportResource(Request $request)
57+
{
58+
return $request->user()->can("create", self::$model);
59+
}
60+
61+
public static function excludeAttributesFromImport()
62+
{
63+
return ['password'];
64+
}
65+
```
66+
67+
## Importer Class
68+
This package uses [maatwebsite/excel](https://github.com/Maatwebsite/Laravel-Excel) behind the scenes to handle the actual import. You can find more information about how importing [works here](https://docs.laravel-excel.com/3.1/imports/basics.html#importing-basics).
69+
70+
You can define your own importer class by providing the relevant class name in your published copy of this package's config file.
71+
72+
First, publish the config file:
73+
```
74+
php artisan vendor:publish --tag=nova-csv-import
75+
```
76+
77+
Then, define and register your own importer class:
78+
```
79+
<?php
80+
81+
return [
82+
'importer' => App\Utilities\Importer::class,
83+
];
84+
```
3785
## Testing
3886

3987
We need tests! Can you help? Please consider contributing.

src/Http/Controllers/ImportController.php

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class ImportController
2020

2121
public function __construct()
2222
{
23-
$class = config('laravel-nova-csv-import.importer');
24-
23+
$class = config('nova-csv-importer.importer');
2524
$this->importer = new $class;
2625
}
2726

@@ -37,46 +36,67 @@ public function preview(NovaRequest $request, $file)
3736

3837
$sample = $import->take(10)->all();
3938

40-
$resources = collect(Nova::$resources);
41-
42-
$resources = $resources->filter(function ($resource) {
43-
if ($resource === ActionResource::class) {
44-
return false;
45-
}
39+
$resources = $this->getAvailableResourcesForImport($request);
4640

47-
if (!isset($resource::$model)) {
48-
return false;
49-
}
41+
$fields = $resources->mapWithKeys(function ($resource) use ($request) {
42+
return $this->getAvailableFieldsForImport($resource, $request);
43+
});
5044

51-
$static_vars = (new \ReflectionClass((string) $resource))->getStaticProperties();
45+
$resources = $resources->mapWithKeys(function ($resource) {
46+
return [$resource::uriKey() => $resource::label()];
47+
});
5248

53-
if(!isset($static_vars['canImportResource'])) {
54-
return true;
55-
}
49+
return response()->json(compact('sample', 'resources', 'fields', 'total_rows', 'headings'));
50+
}
5651

57-
return isset($static_vars['canImportResource']) && $static_vars['canImportResource'];
58-
});
52+
public function getAvailableFieldsForImport(String $resource, $request)
53+
{
54+
$novaResource = new $resource(new $resource::$model);
55+
$fieldsCollection = collect($novaResource->creationFields($request));
5956

60-
$fields = $resources->map(function ($resource) {
61-
$model = $resource::$model;
57+
if (method_exists($novaResource, 'excludeAttributesFromImport')) {
58+
$fieldsCollection = $fieldsCollection->filter(function(Field $field) use ($novaResource, $request) {
59+
return !in_array($field->attribute, $novaResource::excludeAttributesFromImport($request));
60+
});
61+
}
6262

63-
return new $resource(new $model);
64-
})->mapWithKeys(function (Resource $resource) use ($request) {
65-
$fields = collect($resource->creationFields($request))
66-
->map(function (Field $field) {
63+
$fields = $fieldsCollection->map(function (Field $field) {
6764
return [
6865
'name' => $field->name,
6966
'attribute' => $field->attribute
7067
];
7168
});
72-
return [$resource->uriKey() => $fields];
73-
});
69+
70+
return [$novaResource->uriKey() => $fields];
71+
}
7472

75-
$resources = $resources->mapWithKeys(function ($resource) {
76-
return [$resource::uriKey() => $resource::label()];
77-
});
73+
public function getAvailableResourcesForImport(NovaRequest $request) {
7874

79-
return response()->json(compact('sample', 'resources', 'fields', 'total_rows', 'headings'));
75+
$novaResources = collect(Nova::authorizedResources($request));
76+
77+
return $novaResources->filter(function ($resource) use ($request) {
78+
if ($resource === ActionResource::class) {
79+
return false;
80+
}
81+
82+
if (!isset($resource::$model)) {
83+
return false;
84+
}
85+
86+
$resourceReflection = (new \ReflectionClass((string) $resource));
87+
88+
if ($resourceReflection->hasMethod('canImportResource')) {
89+
return $resource::canImportResource($request);
90+
}
91+
92+
$static_vars = $resourceReflection->getStaticProperties();
93+
94+
if (!isset($static_vars['canImportResource'])) {
95+
return true;
96+
}
97+
98+
return isset($static_vars['canImportResource']) && $static_vars['canImportResource'];
99+
});
80100
}
81101

82102
public function import(NovaRequest $request, $file)

src/ToolServiceProvider.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ public function boot()
1919
{
2020
$this->loadViewsFrom(__DIR__.'/../resources/views', 'laravel-nova-csv-import');
2121

22-
$this->mergeConfigFrom(__DIR__.'/config.php', 'laravel-nova-csv-import');
23-
2422
$this->app->booted(function () {
2523
$this->routes();
2624
});
2725

2826
Nova::serving(function (ServingNova $event) {
2927

3028
});
29+
30+
$this->publishes([
31+
__DIR__.'/config.php' => config_path('nova-csv-importer.php')
32+
], 'nova-csv-import');
3133
}
3234

3335
/**
@@ -54,6 +56,6 @@ protected function routes()
5456
*/
5557
public function register()
5658
{
57-
//
59+
$this->mergeConfigFrom(__DIR__.'/config.php', 'nova-csv-importer');
5860
}
5961
}

0 commit comments

Comments
 (0)