Skip to content
Merged

tweaks #3274

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/Http/Controllers/BulkEventUploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
19 changes: 19 additions & 0 deletions app/Imports/GenericEventsImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -378,3 +396,4 @@ public function model(array $row): ?Model
}
}
}

18 changes: 18 additions & 0 deletions app/Services/BulkEventUploadValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
10 changes: 7 additions & 3 deletions resources/views/admin/bulk-upload/preview.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
</section>

<section class="codeweek-content-wrapper">
<p class="mb-4">Rows below show validation results: <span class="bg-trans-success text-green-800 px-1 rounded">green</span> = valid, <span class="bg-trans-danger text-red-800 px-1 rounded">red</span> = problem. You can still run the import; invalid rows will be skipped. Click <strong>Import</strong> to run the import.</p>
@php
$validCount = collect($row_statuses)->where('valid', true)->count();
$totalCount = count($row_statuses);
@endphp
<p class="mb-2"><strong>{{ $validCount }} of {{ $totalCount }}</strong> rows passed validation. Rows below: <span class="bg-trans-success text-green-800 px-1 rounded">green</span> = valid, <span class="bg-trans-danger text-red-800 px-1 rounded">red</span> = problem (see Details). You can still run the import; invalid rows will be skipped. Click <strong>Import</strong> to run the import.</p>

@if ($errors->any())
<div class="mb-4 p-4 rounded bg-red-50 border border-red-200">
Expand All @@ -26,7 +30,7 @@
<tr class="bg-gray-100">
<th class="border border-gray-300 px-3 py-2 text-left">Row</th>
<th class="border border-gray-300 px-3 py-2 text-left">Status</th>
<th class="border border-gray-300 px-3 py-2 text-left">Details</th>
<th class="border border-gray-300 px-3 py-2 text-left">Details (why invalid)</th>
</tr>
</thead>
<tbody>
Expand All @@ -40,7 +44,7 @@
<span class="text-red-800 font-medium">Problem</span>
@endif
</td>
<td class="border border-gray-300 px-3 py-2 {{ $item['valid'] ? 'text-green-700' : 'text-red-700' }}">
<td class="border border-gray-300 px-3 py-2 {{ $item['valid'] ? 'text-green-700' : 'text-red-700' }} max-w-xl">
@if ($item['valid'])
@else
Expand Down