From 3e6ecaef49787559feba7e70eb221e3e08c94b1a Mon Sep 17 00:00:00 2001 From: bernardhanna Date: Thu, 5 Feb 2026 10:18:58 +0000 Subject: [PATCH] tweaks --- .../Controllers/BulkEventUploadController.php | 12 ++++++++++++ app/Imports/GenericEventsImport.php | 19 +++++++++++++++++++ app/Services/BulkEventUploadValidator.php | 18 ++++++++++++++++++ .../views/admin/bulk-upload/preview.blade.php | 10 +++++++--- 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/BulkEventUploadController.php b/app/Http/Controllers/BulkEventUploadController.php index b8625442a..3e4539d33 100644 --- a/app/Http/Controllers/BulkEventUploadController.php +++ b/app/Http/Controllers/BulkEventUploadController.php @@ -147,6 +147,18 @@ function ($attribute, $value, $fail) { foreach ($result->failures as $row => $reason) { $rowStatuses[$row] = ['row' => $row, 'valid' => false, 'message' => $reason]; } + + $dataRowCount = BulkEventUploadValidator::getDataRowCount($path, $tempDisk); + $firstDataRow = 2; + for ($r = $firstDataRow; $r < $firstDataRow + $dataRowCount; $r++) { + if (! isset($rowStatuses[$r])) { + $rowStatuses[$r] = [ + 'row' => $r, + 'valid' => false, + 'message' => 'Row was not validated (empty row or reader skipped).', + ]; + } + } ksort($rowStatuses); return redirect()->route('admin.bulk-upload.preview') diff --git a/app/Imports/GenericEventsImport.php b/app/Imports/GenericEventsImport.php index d81e1975c..95b8914d6 100644 --- a/app/Imports/GenericEventsImport.php +++ b/app/Imports/GenericEventsImport.php @@ -174,6 +174,24 @@ private function recordCreated(Event $event): void public function model(array $row): ?Model { $rowIndex = $this->currentRow++; + + try { + return $this->processRow($rowIndex, $row); + } catch (\Throwable $e) { + if ($this->result) { + $this->result->addFailure($rowIndex, 'Row ' . $rowIndex . ' — ' . $e->getMessage()); + } + Log::warning('Import row ' . $rowIndex . ' failed: ' . $e->getMessage(), ['row' => $row]); + + return null; + } + } + + /** + * Process a single row (validation + optional persist). Called from model() inside try-catch. + */ + protected function processRow(int $rowIndex, array $row): ?Model + { $row = $this->normalizeRow($row); Log::info('Importing row:', $row); @@ -378,3 +396,4 @@ public function model(array $row): ?Model } } } + diff --git a/app/Services/BulkEventUploadValidator.php b/app/Services/BulkEventUploadValidator.php index e390d5393..2f1ccfa93 100644 --- a/app/Services/BulkEventUploadValidator.php +++ b/app/Services/BulkEventUploadValidator.php @@ -63,4 +63,22 @@ public static function validateRequiredColumns(string $path, string $disk = 'loc 'missing' => $missing, ]; } + + /** + * Return the number of data rows (excluding header) in the file. + * + * @return int 0 if unreadable or empty + */ + public static function getDataRowCount(string $path, string $disk = 'local'): int + { + try { + $import = new HeadingRowImport(1); + $data = Excel::toArray($import, $path, $disk); + $rows = $data[0] ?? []; + + return is_array($rows) ? count($rows) : 0; + } catch (\Throwable $e) { + return 0; + } + } } diff --git a/resources/views/admin/bulk-upload/preview.blade.php b/resources/views/admin/bulk-upload/preview.blade.php index 190870635..9164731c6 100644 --- a/resources/views/admin/bulk-upload/preview.blade.php +++ b/resources/views/admin/bulk-upload/preview.blade.php @@ -8,7 +8,11 @@
-

Rows below show validation results: green = valid, red = problem. You can still run the import; invalid rows will be skipped. Click Import to run the import.

+ @php + $validCount = collect($row_statuses)->where('valid', true)->count(); + $totalCount = count($row_statuses); + @endphp +

{{ $validCount }} of {{ $totalCount }} rows passed validation. Rows below: green = valid, red = problem (see Details). You can still run the import; invalid rows will be skipped. Click Import to run the import.

@if ($errors->any())
@@ -26,7 +30,7 @@ Row Status - Details + Details (why invalid) @@ -40,7 +44,7 @@ Problem @endif - + @if ($item['valid']) — @else