Skip to content
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

Fix 500 Error When Applying Templates to Unsaved Screen Pages #7501

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions ProcessMaker/Exception/MissingScreenPageException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace ProcessMaker\Exception;

use Exception;

class MissingScreenPageException extends Exception
{
protected $message = 'The specified screen page does not exist in the configuration. Please try saving the screen or check the configuration.';
}
32 changes: 28 additions & 4 deletions ProcessMaker/Templates/ScreenTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading