Skip to content
This repository has been archived by the owner on Apr 18, 2020. It is now read-only.

Commit

Permalink
add configuration file
Browse files Browse the repository at this point in the history
ignore non creation fields
fix issue with belongsTo fields
  • Loading branch information
Sparclex committed Sep 25, 2018
1 parent 3c972c1 commit 7be66ef
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 47 deletions.
8 changes: 7 additions & 1 deletion src/CardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public function boot()
$this->routes();
});

$this->publishes([
__DIR__ . '/config.php' => config_path('sparclex-nova-import-card.php'),
]);

Nova::serving(function (ServingNova $event) {
Nova::script('nova-import-card', __DIR__.'/../dist/js/card.js');
Nova::style('nova-import-card', __DIR__.'/../dist/css/card.css');
Expand Down Expand Up @@ -49,6 +53,8 @@ protected function routes()
*/
public function register()
{
//
$this->mergeConfigFrom(
__DIR__ . '/config.php', 'sparclex-nova-import-card'
);
}
}
40 changes: 24 additions & 16 deletions src/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Http\Requests\NovaRequest;

Expand All @@ -14,29 +15,36 @@ class ImportController extends Controller
public function handle(NovaRequest $request)
{
$resource = $request->newResource();
$rules = $resource->availableFields($request)->mapWithKeys(function($field) use($request) {
return $field->getCreationRules($request);
})->mapWithKeys(function($rule, $key) {
return ["*.".$key => $rule];
});
$fileReader = $resource::$importFileReader ?? CsvFileReader::class;
$importHandler = $resource::$importHandler ?? ImportHandler::class;
$fileReader = $resource::$importFileReader ?? config('sparclex-nova-import-card.file_reader');


$data = $this->validate($request, [
'file' => 'required|file|mimes:' . $fileReader::mimes()
]);

$fileReader = new $fileReader($data['file']);
$data = $fileReader->read();
$fileReader->afterRead();

$importHandler = new $importHandler($data);
$importHandler->validate($rules->toArray());
$this->validateFields($data, $request, $resource);

DB::transaction(function() use($resource, $data) {
$importHandler = $resource::$importHandler ?? config('sparclex-nova-import-card.import_handler');
(new $importHandler($data))->handle($resource);
});

if($error = $importHandler->handle($resource->resource)) {
return Action::danger($error);
} else {
return Action::message(__('Import successful'));
}
return Action::message(__('Import successful'));
}

/**
* @param $data
* @param NovaRequest $request
* @param $resource
*/
protected function validateFields($data, $request, $resource): void
{
$rules = collect($resource::rulesForCreation($request))->mapWithKeys(function($rule, $key) {
return ["*.".$key => $rule];
});
$this->getValidationFactory()->make($data, $rules->toArray())->validate();
}
}
}
39 changes: 9 additions & 30 deletions src/ImportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Sparclex\NovaImportCard;

use Illuminate\Contracts\Validation\Factory;
use Illuminate\Support\Facades\DB;
use Laravel\Nova\Http\Requests\NovaRequest;

/**
* Imports the uploaded data which was extracted by the
Expand Down Expand Up @@ -37,41 +37,20 @@ public function __construct(array $data)
* @param $model
* @return string|null error message
*/
public function handle($model)
public function handle($resource)
{
$data = $this->data;
try {
DB::beginTransaction();
foreach ($data as $entry) {
foreach ($entry as $attribute => $value) {
$model->{$attribute} = $value;
}
$model->save();
$model = $model->newInstance();
}
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
return $e->getMessage();
foreach ($data as $entry) {
[$model, $callbacks] = $resource::fill(
new ImportNovaRequest($entry), $resource::newModel()
);
$model->save();
collect($callbacks)->each->__invoke();
}
return null;
}

/**
* Validates the uploaded data
*
* @param $rules resource field creation rules
*/
public function validate($rules)
{

$this->getValidationFactory()->make(
$this->data, $rules
)->validate();
}

public function getValidationFactory()
{
return app(Factory::class);
}
}
}
34 changes: 34 additions & 0 deletions src/ImportNovaRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Sparclex\NovaImportCard;

use Laravel\Nova\Http\Requests\NovaRequest;

class ImportNovaRequest extends NovaRequest
{
protected $data;

public function __construct($data)
{
parent::__construct();
$this->data = $data;
}

/**
* Retrieve an input item from the request.
*
* @param string|null $key
* @param string|array|null $default
* @return string|array|null
*/
public function input($key = null, $default = null)
{
if (!$key) {
return $this->data;
}
if (!isset($this->data[$key])) {
return $default;
}
return $this->data[$key];
}
}
19 changes: 19 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

return [
/**
* A file reader converts a type of file (xml, csv, xlsx..) to an array
*
* This is the default file reader used, when there is no static variable $importFileReader
* present in the resource class
*/
'file_reader' => Sparclex\NovaImportCard\CsvFileReader::class,

/**
* An import handler stores the output of the file reader into the database
*
* This is the default import handler used, when there is no static variable $importHandler
* present in the resource class
*/
'import_handler' => Sparclex\NovaImportCard\ImportHandler::class
];

0 comments on commit 7be66ef

Please sign in to comment.