From 9f15b0ebbe54b61107c4f59e87c24032014edd62 Mon Sep 17 00:00:00 2001 From: Sanja Date: Tue, 8 Oct 2024 13:29:51 -0700 Subject: [PATCH 1/2] Use the 'screen_versions' table when applying the template; Handle fallback if screen_versions does not exist --- ProcessMaker/Templates/ScreenTemplate.php | 32 ++++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/ProcessMaker/Templates/ScreenTemplate.php b/ProcessMaker/Templates/ScreenTemplate.php index a1dd1d6533..28455ecb80 100644 --- a/ProcessMaker/Templates/ScreenTemplate.php +++ b/ProcessMaker/Templates/ScreenTemplate.php @@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; use ProcessMaker\Events\TemplateCreated; +use ProcessMaker\Exception\MissingScreenPageException; use ProcessMaker\Helpers\ScreenTemplateHelper; use ProcessMaker\ImportExport\Importer; use ProcessMaker\ImportExport\Options; @@ -700,10 +701,19 @@ public function applyTemplate(Request $request) // Import the template to get the screen config $newScreenId = $this->handleTemplateImport($template); $newTemplateScreen = Screen::select('config')->where('id', $newScreenId)->firstOrFail(); - // Get the current screen to apply the template + + // Get the current screen and screen page $screenId = $request->get('screenId'); - $screen = Screen::where('id', $screenId)->firstOrFail(); $currentScreenPage = $request->get('currentScreenPage'); + + if (hasPackage('package-versions')) { + // Fetch the latest screen version + $screen = \ProcessMaker\Models\ScreenVersion::where('screen_id', $screenId)->latest()->firstOrFail(); + } else { + // Fallback: Use the screen model instead + $screen = Screen::where('id', $screenId)->firstOrFail(); + } + // Get the selected template options $templateOptions = $request->get('templateOptions', []); $supportedOptionComponents = ScreenComponents::getComponents(); @@ -718,10 +728,16 @@ public function applyTemplate(Request $request) Screen::where('id', $newScreenId)->delete(); // Clean up the temporary imported template screen } catch (ModelNotFoundException $e) { Log::error('Template or screen not found: ' . $e->getMessage()); - throw new ModelNotFoundException('Template or screen not found.'); + + return response()->json(['error' => 'Template or screen not found.'], 400); + } catch (MissingScreenPageException $e) { + Log::error('Error applying template to specific screen page: ' . $e->getMessage()); + + return response()->json(['error' => $e->getMessage()], 400); } catch (Exception $e) { Log::error('Error applying template: ' . $e->getMessage()); - throw new Exception('Failed to apply template.'); + + return response()->json(['error' => 'Failed to apply template.'], 400); } } @@ -799,6 +815,10 @@ private function mergeLayout($screen, $currentScreenPage, $newTemplateScreen, $t } $screenConfig = $screen->config; + // Check if the currentScreenPage exists in the screenConfig array + if (!isset($screenConfig[$currentScreenPage])) { + throw new MissingScreenPageException(); + } $screenConfig[$currentScreenPage]['items'] = array_merge($screenConfig[$currentScreenPage]['items'], $templateComponents); @@ -817,6 +837,10 @@ private function mergeFields($screen, $currentScreenPage, $newTemplateScreen, $t } $screenConfig = $screen->config; + // Check if the currentScreenPage exists in the screenConfig array + if (!isset($screenConfig[$currentScreenPage])) { + throw new MissingScreenPageException(); + } $screenConfig[$currentScreenPage]['items'] = array_merge($screenConfig[$currentScreenPage]['items'], $templateComponents); From 487a51d92fc1d562e66ce52628445a312948abe5 Mon Sep 17 00:00:00 2001 From: Sanja Date: Tue, 8 Oct 2024 13:30:03 -0700 Subject: [PATCH 2/2] Create MissingScreenPageException.php --- ProcessMaker/Exception/MissingScreenPageException.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 ProcessMaker/Exception/MissingScreenPageException.php diff --git a/ProcessMaker/Exception/MissingScreenPageException.php b/ProcessMaker/Exception/MissingScreenPageException.php new file mode 100644 index 0000000000..a10c22b458 --- /dev/null +++ b/ProcessMaker/Exception/MissingScreenPageException.php @@ -0,0 +1,10 @@ +