Skip to content

Commit aa655fd

Browse files
authored
Merge pull request #6165 from ProcessMaker/bugfix/FOUR-13983
FOUR-13983: Ensure "Uncategorized" process category exists during import
2 parents ae5ad2d + dc60cc5 commit aa655fd

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

ProcessMaker/ImportExport/Exporters/ExporterBase.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -509,21 +509,32 @@ protected function associateCategories($categoryClass, $property)
509509
})
510510
->first();
511511

512+
// If the process has a set category, we need to verify the "Uncategorized"
513+
// category exists for this model and if it doesn't, recreate it
514+
$uncategorizedCategory = function () use ($categoryClass) {
515+
return $categoryClass::where('name', 'Uncategorized')->firstOrCreate([
516+
'name' => __('Uncategorized'),
517+
'status' => 'ACTIVE',
518+
'is_system' => false,
519+
]);
520+
};
521+
512522
// If a template is being used and an associated category is present, add that category to the collection.
513523
// Otherwise, if the collection is empty and there's an uncategorized reference, add the uncategorized category.
514524
if ($isTemplate && isset($this->model->process_category_id)) {
515525
if ($this->getReference('uncategorized-category')) {
516-
$categories->push($categoryClass::where('name', 'Uncategorized')->firstOrFail());
526+
$categories->push($uncategorizedCategory());
517527
} else {
518-
$categorFind = $categoryClass::find($this->model->process_category_id);
519-
if (!$categorFind) {
520-
$categorFind = $categoryClass::where('name', 'Uncategorized')->firstOrFail();
521-
\Log::debug($categoryClass . ' ID: ' . $this->model->process_category_id . ' not found. Changing category toUncategorize.');
528+
$foundCategory = $categoryClass::find($this->model->process_category_id);
529+
530+
if (!$foundCategory instanceof $categoryClass) {
531+
Log::warning("Import/Export: Unable to find ".$categoryClass::class." with id {$this->model->process_category_id}, updated to 'Uncategorized'.");
522532
}
523-
$categories->push($categorFind);
533+
534+
$categories->push($foundCategory ?? $uncategorizedCategory());
524535
}
525536
} elseif ($categories->empty() && $this->getReference('uncategorized-category')) {
526-
$categories->push($categoryClass::where('name', 'Uncategorized')->firstOrFail());
537+
$categories->push($uncategorizedCategory());
527538
}
528539

529540
$categoriesString = $categories->map(fn ($c) => $c->id)->unique()->join(',');

0 commit comments

Comments
 (0)