¨4.0.1¨
This commit is contained in:
@@ -19,4 +19,8 @@ class DownloadCsvController
|
||||
return response()->download($path);
|
||||
}
|
||||
}
|
||||
|
||||
public function generate(){
|
||||
echo 'Hi';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Import\Http\Controllers\Admin;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class DownloadCsvTemplateController
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function download()
|
||||
{
|
||||
$template_types = ['product' => 'products.csv'];
|
||||
|
||||
if (array_key_exists(request('template_type'), $template_types)) {
|
||||
$path = storage_path('app/csv_templates/' . $template_types[request('template_type')]);
|
||||
|
||||
return response()->download($path);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Modules\Import\Http\Controllers\Admin;
|
||||
|
||||
use Maatwebsite\Excel\Excel;
|
||||
use Illuminate\Http\Response;
|
||||
use Modules\Import\Imports\ProductImport;
|
||||
use Maatwebsite\Excel\Facades\Excel as ExcelFacade;
|
||||
use Modules\Import\Http\Requests\StoreImporterRequest;
|
||||
@@ -12,18 +13,20 @@ class ImporterController
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('import::admin.importer.index');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Modules\Import\Http\Requests\StoreImporterRequest $request
|
||||
* @return \Illuminate\Http\Response
|
||||
* @param StoreImporterRequest $request
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function store(StoreImporterRequest $request)
|
||||
{
|
||||
|
||||
@@ -14,6 +14,7 @@ class StoreImporterRequest extends Request
|
||||
*/
|
||||
protected $availableAttributes = 'import::attributes';
|
||||
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
|
||||
@@ -18,6 +18,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
return 200;
|
||||
}
|
||||
|
||||
|
||||
public function onRow(Row $row)
|
||||
{
|
||||
$data = $this->normalize($row->toArray());
|
||||
@@ -26,11 +27,12 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
|
||||
try {
|
||||
Product::create($data);
|
||||
} catch (QueryException | ValidationException $e) {
|
||||
} catch (QueryException|ValidationException $e) {
|
||||
session()->push('importer_errors', $row->getIndex());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function normalize(array $data)
|
||||
{
|
||||
return array_filter([
|
||||
@@ -65,6 +67,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private function explode($values)
|
||||
{
|
||||
if (trim($values) == '') {
|
||||
@@ -74,6 +77,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
return array_map('trim', explode(',', $values));
|
||||
}
|
||||
|
||||
|
||||
private function normalizeFiles(array $data)
|
||||
{
|
||||
return [
|
||||
@@ -82,6 +86,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
private function normalizeMetaData($data)
|
||||
{
|
||||
return [
|
||||
@@ -90,6 +95,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
private function normalizeAttributes(array $data)
|
||||
{
|
||||
$attributes = [];
|
||||
@@ -104,15 +110,17 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
|
||||
private function findAttributes(array $data)
|
||||
{
|
||||
return collect($data)->filter(function ($value, $column) {
|
||||
preg_match('/^attribute_\d$/', $column, $matches);
|
||||
|
||||
return ! empty($matches);
|
||||
return !empty($matches);
|
||||
})->filter();
|
||||
}
|
||||
|
||||
|
||||
private function findAttributeValues(array $data, $attributeNumber)
|
||||
{
|
||||
return collect($data)->filter(function ($value, $column) use ($attributeNumber) {
|
||||
@@ -122,6 +130,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
})->flatten()->toArray();
|
||||
}
|
||||
|
||||
|
||||
private function normalizeOptions(array $data)
|
||||
{
|
||||
$options = [];
|
||||
@@ -144,23 +153,25 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
return $options;
|
||||
}
|
||||
|
||||
|
||||
private function findOptionPrefixes(array $data)
|
||||
{
|
||||
return collect($data)->filter(function ($value, $column) {
|
||||
preg_match('/^option_\d_name$/', $column, $matches);
|
||||
|
||||
return ! empty($matches);
|
||||
return !empty($matches);
|
||||
})->keys()->map(function ($column) {
|
||||
return str_replace('_name', '', $column);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private function findOptionAttributes(array $data, $optionPrefix)
|
||||
{
|
||||
return collect($data)->filter(function ($value, $column) use ($optionPrefix) {
|
||||
preg_match("/{$optionPrefix}_.*/", $column, $matches);
|
||||
|
||||
return ! empty($matches);
|
||||
return !empty($matches);
|
||||
})->mapWithKeys(function ($value, $column) use ($optionPrefix) {
|
||||
$column = str_replace("{$optionPrefix}_", '', $column);
|
||||
|
||||
@@ -168,6 +179,7 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private function findOptionValues(Collection $option)
|
||||
{
|
||||
$values = [];
|
||||
@@ -189,12 +201,13 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
return $values;
|
||||
}
|
||||
|
||||
|
||||
private function findOptionValuePrefixes(Collection $option)
|
||||
{
|
||||
return $option->filter(function ($value, $column) {
|
||||
preg_match('/value_\d_.+/', $column, $matches);
|
||||
|
||||
return ! empty($matches);
|
||||
return !empty($matches);
|
||||
})->keys()->map(function ($column) {
|
||||
preg_match('/value_\d/', $column, $matches);
|
||||
|
||||
@@ -202,12 +215,13 @@ class ProductImport implements OnEachRow, WithChunkReading, WithHeadingRow
|
||||
})->unique();
|
||||
}
|
||||
|
||||
|
||||
private function findOptionValueAttributes(Collection $option, $valuePrefix)
|
||||
{
|
||||
return $option->filter(function ($value, $column) use ($valuePrefix) {
|
||||
preg_match("/{$valuePrefix}_.*/", $column, $matches);
|
||||
|
||||
return ! empty($matches);
|
||||
return !empty($matches);
|
||||
})->mapWithKeys(function ($value, $column) use ($valuePrefix) {
|
||||
$column = str_replace("{$valuePrefix}_", '', $column);
|
||||
|
||||
|
||||
@@ -2,13 +2,10 @@
|
||||
|
||||
namespace Modules\Import\Providers;
|
||||
|
||||
use Modules\Support\Traits\AddsAsset;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class ImportServiceProvider extends ServiceProvider
|
||||
{
|
||||
use AddsAsset;
|
||||
|
||||
/**
|
||||
* Bootstrap the application services.
|
||||
*
|
||||
@@ -16,6 +13,5 @@ class ImportServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->addAdminAssets('admin.importer.index', ['admin.import.js']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
return [
|
||||
'csv_file' => 'CSV File',
|
||||
'import_type' => 'Import Type',
|
||||
'template_type' => 'Template Type',
|
||||
];
|
||||
|
||||
@@ -2,10 +2,14 @@
|
||||
|
||||
return [
|
||||
'importer' => 'Importer',
|
||||
'download_csv' => 'Download CSV',
|
||||
'import' => 'Import',
|
||||
'import_types' => [
|
||||
'product' => 'Product',
|
||||
],
|
||||
'template_types' => [
|
||||
'product' => 'Product',
|
||||
],
|
||||
'run' => 'Run',
|
||||
'download' => 'Download',
|
||||
'download_csv_template' => 'Download CSV Template',
|
||||
];
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'the_importer_has_been_run_successfully' => 'The importer has been run successfully.',
|
||||
'there_was_an_error_on_rows' => 'There was an error on rows (:rows).',
|
||||
'the_importer_has_been_run_successfully' => 'The importer has been run successfully',
|
||||
'there_was_an_error_on_rows' => 'There was an error on rows (:rows)',
|
||||
];
|
||||
|
||||
@@ -7,46 +7,73 @@
|
||||
@endcomponent
|
||||
|
||||
@section('content')
|
||||
<div class="row">
|
||||
<div class="btn-group pull-right">
|
||||
<a href="#" class="btn btn-primary btn-actions">
|
||||
{{ trans('import::importer.download_csv') }}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form method="POST" action="{{ route('admin.importer.store') }}" enctype="multipart/form-data" class="form-horizontal">
|
||||
<form method="POST" action="{{ route('admin.download_csv_template.download') }}" enctype="multipart/form-data"
|
||||
class="form-horizontal">
|
||||
@csrf
|
||||
|
||||
<div class="accordion-content">
|
||||
<div class="accordion-box-content clearfix">
|
||||
<div class="col-md-12">
|
||||
<div class="accordion-box-content">
|
||||
<div class="tab-content clearfix">
|
||||
<div class="tab-pane fade in active">
|
||||
<h3 class="tab-content-title">
|
||||
{{ trans('import::importer.import') }}
|
||||
</h3>
|
||||
<div class="accordion-content">
|
||||
<div class="accordion-box-content clearfix">
|
||||
<div class="col-md-12">
|
||||
<div class="accordion-box-content">
|
||||
<div class="tab-content clearfix">
|
||||
<div class="tab-pane fade in active">
|
||||
<h4 class="tab-content-title">
|
||||
{{ trans('import::importer.download_csv_template') }}
|
||||
</h4>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-lg-6 col-md-12">
|
||||
<div class="row">
|
||||
<div class="col-lg-6 col-md-12">
|
||||
{{ Form::select('template_type', trans('import::attributes.template_type'), $errors, trans('import::importer.template_types'), null, ['required' => true]) }}
|
||||
|
||||
<div class="form-group">
|
||||
<div class="col-md-offset-3 col-md-10">
|
||||
<button type="submit" class="btn btn-primary" data-loading>
|
||||
{{ trans('import::importer.download') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<form method="POST" action="{{ route('admin.importer.store') }}" enctype="multipart/form-data"
|
||||
class="form-horizontal m-t-10">
|
||||
@csrf
|
||||
|
||||
<div class="accordion-content">
|
||||
<div class="accordion-box-content clearfix">
|
||||
<div class="col-md-12">
|
||||
<div class="accordion-box-content">
|
||||
<div class="tab-content clearfix">
|
||||
<div class="tab-pane fade in active">
|
||||
<h4 class="tab-content-title">
|
||||
{{ trans('import::importer.import') }}
|
||||
</h4>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-lg-6 col-md-12">
|
||||
{{ Form::file('csv_file', trans('import::attributes.csv_file'), $errors, null, ['required' => true]) }}
|
||||
{{ Form::select('import_type', trans('import::attributes.import_type'), $errors, trans('import::importer.import_types'), null, ['required' => true]) }}
|
||||
|
||||
<div class="form-group">
|
||||
<div class="col-md-offset-3 col-md-10">
|
||||
<button type="submit" class="btn btn-primary" data-loading>
|
||||
{{ trans('import::importer.run') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-md-offset-3 col-md-10">
|
||||
<button type="submit" class="btn btn-primary" data-loading>
|
||||
{{ trans('import::importer.run') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
@endsection
|
||||
|
||||
@@ -14,8 +14,8 @@ Route::post('importer', [
|
||||
'middleware' => 'can:admin.importer.create',
|
||||
]);
|
||||
|
||||
Route::get('download-csv', [
|
||||
'as' => 'admin.download_csv.index',
|
||||
'uses' => 'DownloadCsvController@index',
|
||||
Route::post('download-csv-template', [
|
||||
'as' => 'admin.download_csv_template.download',
|
||||
'uses' => 'DownloadCsvTemplateController@download',
|
||||
'middleware' => 'can:admin.importer.index',
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user