Skip to content

Commit c44199b

Browse files
committed
bugfix: failed to importing navigation
1 parent 348a674 commit c44199b

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

src/Models/Navigation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Navigation extends BaseModel implements NavigationContract
1818
use Concerns\HasTranslations;
1919
use NodeTrait;
2020

21-
protected $guarded = ['id'];
21+
protected $guarded = [];
2222

2323
protected $table = 'navigation';
2424

src/Services/ImportDataService.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,30 +474,53 @@ protected function processForNavigation()
474474

475475
$this->guardAgaintsTableExist($model);
476476

477-
$navData = collect($this->pendingData['navigation'] ?? [])->groupBy(fn ($d) => $d->category)->toArray();
477+
$flatNavigation = collect($this->pendingData['navigation'] ?? [])
478+
->map(fn (Entities\Navigation $item) => array_merge([$item], $this->getFlatNavigationFor($item)))
479+
->flatten(1)
480+
->all();
478481

479-
foreach ($navData as $category => $items) {
482+
// Create or update navigation records
483+
foreach ($flatNavigation as $item) {
484+
try {
485+
$item->validate();
486+
487+
$navigationData = $this->mutateNavigationData($item);
488+
$this->finished['navigation'][] = $model::updateOrCreate(
489+
Arr::only($navigationData, ['id']),
490+
Arr::except($navigationData, ['id', 'children'])
491+
);
492+
493+
} catch (\Throwable $th) {
494+
$errorMsg = 'Error while create/update navigation record: ' . json_encode($item->toArray());
495+
if (filled($th->getMessage())) {
496+
$errorMsg .= ' - ' . $th->getMessage();
497+
} else {
498+
$errorMsg .= ' - ' . get_class($th);
499+
}
500+
$this->processErrors['navigation']['s1'][] = $errorMsg;
501+
}
502+
}
503+
504+
// Create tree structure for navigation items
505+
foreach (collect($this->pendingData['navigation'] ?? [])->groupBy('category') as $category => $items) {
506+
507+
// Create a tree data
480508
$treeData = [];
481509
foreach ($items as $item) {
482510
try {
483511
$item->validate();
484512

485513
$navigationData = $this->mutateNavigationData($item);
486514
$treeData[] = $navigationData;
487-
$navigation = $model::updateOrCreate(
488-
Arr::only($navigationData, ['id']),
489-
Arr::except($navigationData, ['id', 'children'])
490-
);
491-
$this->finished['navigation'][] = $navigation ?? null;
492515

493516
} catch (\Throwable $th) {
494-
$errorMsg = 'Error while processing navigation item: ' . $item->title;
517+
$errorMsg = 'Error while making navigation tree data: ' . json_encode($item->toArray());
495518
if (filled($th->getMessage())) {
496519
$errorMsg .= ' - ' . $th->getMessage();
497520
} else {
498521
$errorMsg .= ' - ' . get_class($th);
499522
}
500-
$this->processErrors['navigation'][$category][] = $errorMsg;
523+
$this->processErrors['navigation']['s2'][] = $errorMsg;
501524
}
502525
}
503526

@@ -840,4 +863,11 @@ protected function mutateNavigationData(Entities\Navigation $item): array
840863

841864
return $data;
842865
}
866+
867+
private function getFlatNavigationFor(Entities\Navigation $item): array
868+
{
869+
return collect($item->children)->flatMap(function (Entities\Navigation $child) {
870+
return array_merge([$child], $this->getFlatNavigationFor($child));
871+
})->toArray();
872+
}
843873
}

0 commit comments

Comments
 (0)