Skip to content

Commit

Permalink
Custom CSV delimiter in import
Browse files Browse the repository at this point in the history
  • Loading branch information
sabas committed Mar 25, 2023
1 parent d62a1c8 commit abaadf5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions resources/views/subscribers/import.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

<x-sendportal.select-field name="tags[]" :label="__('Tags')" :options="$tags" multiple />

<x-sendportal.text-field name="delimiter" :label="__('Custom CSV delimiter')" :value="" />

<div class="form-group row">
<div class="offset-sm-3 col-sm-9">
<a href="{{ route('sendportal.subscribers.index') }}" class="btn btn-light">{{ __('Back') }}</a>
Expand Down
10 changes: 6 additions & 4 deletions src/Http/Controllers/Subscribers/SubscribersImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public function store(SubscribersImportRequest $request): RedirectResponse

$path = $request->file('file')->storeAs('imports', $filename, 'local');

$errors = $this->validateCsvContents(Storage::disk('local')->path($path));
$delimiter = $request->query('delimiter', ',');

$errors = $this->validateCsvContents(Storage::disk('local')->path($path), $delimiter);

if (count($errors->getBags())) {
Storage::disk('local')->delete($path);
Expand All @@ -69,7 +71,7 @@ public function store(SubscribersImportRequest $request): RedirectResponse
'updated' => 0
];

(new FastExcel)->import(Storage::disk('local')->path($path), function (array $line) use ($request, &$counter) {
(new FastExcel)->configureCsv($delimiter)->import(Storage::disk('local')->path($path), function (array $line) use ($request, &$counter) {
$data = Arr::only($line, ['id', 'email', 'first_name', 'last_name']);

$data['tags'] = $request->get('tags') ?? [];
Expand Down Expand Up @@ -103,13 +105,13 @@ public function store(SubscribersImportRequest $request): RedirectResponse
* @throws ReaderNotOpenedException
* @throws UnsupportedTypeException
*/
protected function validateCsvContents(string $path): ViewErrorBag
protected function validateCsvContents(string $path, $delimiter): ViewErrorBag
{
$errors = new ViewErrorBag();

$row = 1;

(new FastExcel)->import($path, function (array $line) use ($errors, &$row) {
(new FastExcel)->configureCsv($delimiter)->import($path, function (array $line) use ($errors, &$row) {
$data = Arr::only($line, ['id', 'email', 'first_name', 'last_name']);

try {
Expand Down

0 comments on commit abaadf5

Please sign in to comment.