From dc60cc5dbeeaf6164f9d69b714492c8998b45f5d Mon Sep 17 00:00:00 2001 From: Alex Runyan Date: Tue, 6 Feb 2024 13:40:48 -0500 Subject: [PATCH] Create the Uncategorized category if it is not found for an imported process --- .../ImportExport/Exporters/ExporterBase.php | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/ProcessMaker/ImportExport/Exporters/ExporterBase.php b/ProcessMaker/ImportExport/Exporters/ExporterBase.php index 9fbe9cc196..a801f1d8e1 100644 --- a/ProcessMaker/ImportExport/Exporters/ExporterBase.php +++ b/ProcessMaker/ImportExport/Exporters/ExporterBase.php @@ -509,21 +509,32 @@ protected function associateCategories($categoryClass, $property) }) ->first(); + // If the process has a set category, we need to verify the "Uncategorized" + // category exists for this model and if it doesn't, recreate it + $uncategorizedCategory = function () use ($categoryClass) { + return $categoryClass::where('name', 'Uncategorized')->firstOrCreate([ + 'name' => __('Uncategorized'), + 'status' => 'ACTIVE', + 'is_system' => false, + ]); + }; + // If a template is being used and an associated category is present, add that category to the collection. // Otherwise, if the collection is empty and there's an uncategorized reference, add the uncategorized category. if ($isTemplate && isset($this->model->process_category_id)) { if ($this->getReference('uncategorized-category')) { - $categories->push($categoryClass::where('name', 'Uncategorized')->firstOrFail()); + $categories->push($uncategorizedCategory()); } else { - $categorFind = $categoryClass::find($this->model->process_category_id); - if (!$categorFind) { - $categorFind = $categoryClass::where('name', 'Uncategorized')->firstOrFail(); - \Log::debug($categoryClass . ' ID: ' . $this->model->process_category_id . ' not found. Changing category toUncategorize.'); + $foundCategory = $categoryClass::find($this->model->process_category_id); + + if (!$foundCategory instanceof $categoryClass) { + Log::warning("Import/Export: Unable to find ".$categoryClass::class." with id {$this->model->process_category_id}, updated to 'Uncategorized'."); } - $categories->push($categorFind); + + $categories->push($foundCategory ?? $uncategorizedCategory()); } } elseif ($categories->empty() && $this->getReference('uncategorized-category')) { - $categories->push($categoryClass::where('name', 'Uncategorized')->firstOrFail()); + $categories->push($uncategorizedCategory()); } $categoriesString = $categories->map(fn ($c) => $c->id)->unique()->join(',');