diff --git a/ProcessMaker/Http/Controllers/Api/DeployProcess.php b/ProcessMaker/Http/Controllers/Api/DeployProcess.php index 1070693b7f..e9eed4beab 100644 --- a/ProcessMaker/Http/Controllers/Api/DeployProcess.php +++ b/ProcessMaker/Http/Controllers/Api/DeployProcess.php @@ -13,6 +13,7 @@ use ProcessMaker\Http\Controllers\Api\Concerns\AppliesDeployVersionName; use ProcessMaker\Http\Controllers\Controller; use ProcessMaker\Models\Process; +use ProcessMaker\Services\DeployCategoryResolver; use ProcessMaker\Services\ProcessBpmnReferenceService; use Throwable; @@ -31,7 +32,8 @@ class DeployProcess extends Controller ]; public function __construct( - private readonly ProcessBpmnReferenceService $references + private readonly ProcessBpmnReferenceService $references, + private readonly DeployCategoryResolver $categories, ) { } @@ -50,6 +52,7 @@ public function deployProcess(Request $request): JsonResponse 'bpmn' => 'required|string', 'name' => 'required|string|max:255', 'description' => 'nullable|string|max:65535', + 'process_category_name' => 'nullable|string|max:255', ], $isUpdate), ); @@ -101,11 +104,32 @@ private function upsertProcessByName(Request $request, string $bpmn): array if ($created) { $process = new Process(); $process->name = $request->input('name'); - $process->description = (string) $request->input('description', ''); + $process->description = $this->nonEmptyDescription( + $request->input('description'), + (string) $request->input('name'), + ); $process->status = 'ACTIVE'; $process->user_id = $request->user()->id; + $categoryId = $this->categories->resolveProcessCategoryId( + $request->input('process_category_name') + ); + if ($categoryId !== null) { + $process->process_category_id = $categoryId; + } } elseif ($request->has('description')) { - $process->description = (string) $request->input('description'); + $process->description = $this->nonEmptyDescription( + $request->input('description'), + (string) $process->name, + ); + } + + if (!$created && $request->has('process_category_name')) { + $categoryId = $this->categories->resolveProcessCategoryId( + $request->input('process_category_name') + ); + if ($categoryId !== null) { + $process->process_category_id = $categoryId; + } } $process->bpmn = $bpmn; @@ -186,4 +210,13 @@ private function validateXml(string $bpmn): array return $valid ? [] : $errors; } + + private function nonEmptyDescription(mixed $description, string $fallbackName): string + { + if (is_string($description) && trim($description) !== '') { + return trim($description); + } + + return 'Process: ' . trim($fallbackName); + } } diff --git a/ProcessMaker/Http/Controllers/Api/DeployScreen.php b/ProcessMaker/Http/Controllers/Api/DeployScreen.php index 7c7a1bee00..70af34a570 100644 --- a/ProcessMaker/Http/Controllers/Api/DeployScreen.php +++ b/ProcessMaker/Http/Controllers/Api/DeployScreen.php @@ -10,6 +10,7 @@ use ProcessMaker\Http\Controllers\Api\Concerns\AppliesDeployVersionName; use ProcessMaker\Http\Controllers\Controller; use ProcessMaker\Models\Screen; +use ProcessMaker\Services\DeployCategoryResolver; use ProcessMaker\Traits\TaskScreenResourceTrait; use Symfony\Component\Yaml\Yaml; @@ -20,6 +21,10 @@ class DeployScreen extends Controller private const EMPTY_PRESET_DATA = '{}'; + public function __construct(private readonly DeployCategoryResolver $categories) + { + } + /** * A whitelist of attributes that should not be * sanitized by our SanitizeInput middleware. @@ -71,8 +76,20 @@ private function persistDeployedScreen(Request $request, string $name): JsonResp if ($created) { $screen = new Screen(); $screen->title = $name; - $screen->description = $parsedData['description'] ?? ''; + $screen->description = $this->screenDescription($parsedData, $name); $screen->type = $parsedData['type'] ?? 'FORM'; + } elseif (array_key_exists('description', $parsedData)) { + $screen->description = $this->screenDescription($parsedData, $name); + } + + $categoryName = $parsedData['category'] ?? $parsedData['screen_category_name'] ?? null; + if ($created || $categoryName !== null) { + $categoryId = $this->categories->resolveScreenCategoryId( + is_string($categoryName) ? $categoryName : null + ); + if ($categoryId !== null) { + $screen->screen_category_id = (string) $categoryId; + } } $this->applyYamlFields($screen, $parsedData, $created); @@ -281,4 +298,17 @@ private function isJsonString(string $data): bool return true; } + + /** + * @param array $parsedData + */ + private function screenDescription(array $parsedData, string $title): string + { + $description = $parsedData['description'] ?? ''; + if (is_string($description) && trim($description) !== '') { + return trim($description); + } + + return 'Screen: ' . trim($title); + } } diff --git a/ProcessMaker/Http/Controllers/Api/DeployScript.php b/ProcessMaker/Http/Controllers/Api/DeployScript.php index 9f83f8a4e7..ae64122157 100644 --- a/ProcessMaker/Http/Controllers/Api/DeployScript.php +++ b/ProcessMaker/Http/Controllers/Api/DeployScript.php @@ -10,6 +10,7 @@ use ProcessMaker\Http\Controllers\Api\Concerns\AppliesDeployVersionName; use ProcessMaker\Http\Controllers\Controller; use ProcessMaker\Models\Script; +use ProcessMaker\Services\DeployCategoryResolver; use ProcessMaker\Services\ScriptExecutorResolver; class DeployScript extends Controller @@ -32,8 +33,10 @@ class DeployScript extends Controller private const DEFAULT_RETRY_WAIT_TIME = 5; - public function __construct(private readonly ScriptExecutorResolver $executors) - { + public function __construct( + private readonly ScriptExecutorResolver $executors, + private readonly DeployCategoryResolver $categories, + ) { } /** @@ -53,6 +56,8 @@ public function deployScript(Request $request): JsonResponse $this->deployRulesForUpdate([ 'code' => 'required|string', 'name' => 'required|string', + 'description' => 'nullable|string|max:65535', + 'script_category_name' => 'nullable|string|max:255', ], $isUpdate), ); @@ -83,7 +88,7 @@ private function persistDeployedScript(Request $request, string $name): JsonResp $script = new Script(); $script->title = $name; - $script->description = ''; + $script->description = $this->deployScriptDescription($request, $name); $script->language = $executor->language; $script->script_executor_id = $executor->id; $script->run_as_user_id = $request->user()->id; @@ -92,6 +97,19 @@ private function persistDeployedScript(Request $request, string $name): JsonResp $script->retry_wait_time = self::DEFAULT_RETRY_WAIT_TIME; } + if (!$created && $request->has('description')) { + $script->description = $this->deployScriptDescription($request, $name); + } + + if ($created || $request->has('script_category_name')) { + $categoryId = $this->categories->resolveScriptCategoryId( + $request->input('script_category_name') + ); + if ($categoryId !== null) { + $script->script_category_id = (string) $categoryId; + } + } + $script->code = $code; $publishedVersionIdBefore = $this->latestPublishedVersionId($script); $script->save(); @@ -150,4 +168,14 @@ public function getScriptPhp(Request $request) ->header('X-Script-Id', (string) $script->id) ->header('X-Script-Name', $script->title); } + + private function deployScriptDescription(Request $request, string $title): string + { + $description = $request->input('description'); + if (is_string($description) && trim($description) !== '') { + return trim($description); + } + + return 'Script: ' . trim($title); + } } diff --git a/ProcessMaker/Services/DeployCategoryResolver.php b/ProcessMaker/Services/DeployCategoryResolver.php new file mode 100644 index 0000000000..85e0f100b2 --- /dev/null +++ b/ProcessMaker/Services/DeployCategoryResolver.php @@ -0,0 +1,56 @@ +resolveActiveCategoryId(ProcessCategory::class, $name); + } + + public function resolveScreenCategoryId(?string $name): ?int + { + return $this->resolveActiveCategoryId(ScreenCategory::class, $name); + } + + public function resolveScriptCategoryId(?string $name): ?int + { + return $this->resolveActiveCategoryId(ScriptCategory::class, $name); + } + + /** + * @param class-string $modelClass + */ + private function resolveActiveCategoryId(string $modelClass, ?string $name): ?int + { + $trimmed = $name !== null ? trim($name) : ''; + if ($trimmed === '') { + $trimmed = self::UNCATEGORIZED; + } + + $category = $modelClass::query() + ->where('name', $trimmed) + ->where('status', 'ACTIVE') + ->first(); + + if ($category !== null) { + return (int) $category->getKey(); + } + + if ($trimmed !== self::UNCATEGORIZED) { + return $this->resolveActiveCategoryId($modelClass, self::UNCATEGORIZED); + } + + return null; + } +} diff --git a/ProcessMaker/Services/ProcessBpmnReferenceService.php b/ProcessMaker/Services/ProcessBpmnReferenceService.php index 142c007dfd..db21055f21 100644 --- a/ProcessMaker/Services/ProcessBpmnReferenceService.php +++ b/ProcessMaker/Services/ProcessBpmnReferenceService.php @@ -198,8 +198,13 @@ private function replaceNamedReferences( return $bpmn; } - $idsByName = $loadIds($names); - $missing = array_values(array_diff($names, array_keys($idsByName))); + $resolvedNames = array_map( + static fn (string $name): string => html_entity_decode($name, ENT_QUOTES | ENT_XML1, 'UTF-8'), + $names + ); + + $idsByName = $loadIds($resolvedNames); + $missing = array_values(array_diff($resolvedNames, array_keys($idsByName))); if ($missing !== []) { throw new MissingBpmnReferenceException( 'Missing ' . $type . ' reference(s): ' . implode(', ', $missing) @@ -208,8 +213,11 @@ private function replaceNamedReferences( return preg_replace_callback( $pattern, - static fn (array $match): string => $match[1] . $match[2] - . $idsByName[$match[3]] . $match[2], + static function (array $match) use ($idsByName): string { + $name = html_entity_decode($match[3], ENT_QUOTES | ENT_XML1, 'UTF-8'); + + return $match[1] . $match[2] . $idsByName[$name] . $match[2]; + }, $bpmn ); } diff --git a/ProcessMaker/Services/ScriptExecutorResolver.php b/ProcessMaker/Services/ScriptExecutorResolver.php index c50f3fbcc7..3ab4b1e2af 100644 --- a/ProcessMaker/Services/ScriptExecutorResolver.php +++ b/ProcessMaker/Services/ScriptExecutorResolver.php @@ -10,14 +10,6 @@ class ScriptExecutorResolver { public function resolveForNewScript(): ?ScriptExecutor { - $nayra = ScriptExecutor::query() - ->where('language', 'php-nayra') - ->orderBy('id') - ->first(); - if ($nayra !== null) { - return $nayra; - } - $php = ScriptExecutor::query() ->where('language', 'php') ->orderBy('id') @@ -26,6 +18,10 @@ public function resolveForNewScript(): ?ScriptExecutor return $php; } - return ScriptExecutor::query()->orderBy('id')->first(); + return ScriptExecutor::query() + ->where('language', '!=', 'php-nayra') + ->orderBy('id') + ->first() + ?? ScriptExecutor::query()->orderBy('id')->first(); } } diff --git a/tests/Feature/Api/DeployScriptControllerTest.php b/tests/Feature/Api/DeployScriptControllerTest.php index 11e23338e8..337c87c2fd 100644 --- a/tests/Feature/Api/DeployScriptControllerTest.php +++ b/tests/Feature/Api/DeployScriptControllerTest.php @@ -218,25 +218,27 @@ public function testGetScriptPhpByNameReturnsPhpCode(): void $this->assertSame($code, $response->getContent()); } - public function testDeployScriptPrefersPhpNayraExecutorOnCreate(): void + public function testDeployScriptPrefersPhpExecutorOnCreate(): void { - if (!ScriptExecutor::query()->where('language', 'php-nayra')->exists()) { - ScriptExecutor::factory()->create(['language' => 'php-nayra']); + if (!ScriptExecutor::query()->where('language', 'php')->exists()) { + ScriptExecutor::factory()->create(['language' => 'php', 'title' => 'PHP Executor']); } $executor = app(ScriptExecutorResolver::class)->resolveForNewScript(); $this->assertNotNull($executor); - $this->assertSame('php-nayra', $executor->language); + $this->assertSame('php', $executor->language); $this->apiCall('POST', self::API_ENDPOINT, [ 'code' => ' 'new-nayra-script', + 'name' => 'new-php-script', + 'description' => 'Merged migration script.', ]) ->assertStatus(200) ->assertJson(['action' => 'created']); - $script = Script::where('title', 'new-nayra-script')->first(); + $script = Script::where('title', 'new-php-script')->first(); $this->assertSame($executor->id, $script->script_executor_id); + $this->assertSame('Merged migration script.', $script->description); } private function ensureExecutor(): ScriptExecutor