Skip to content

FOUR-13983: Ensure "Uncategorized" process category exists during import #6165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2024
Merged
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
25 changes: 18 additions & 7 deletions ProcessMaker/ImportExport/Exporters/ExporterBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(',');
Expand Down