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 @@ +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);