diff --git a/code/web/interface/themes/responsive/WebBuilder/createTemplatejs.tpl b/code/web/interface/themes/responsive/WebBuilder/createTemplatejs.tpl index c446a9824a..02bf4d8543 100644 --- a/code/web/interface/themes/responsive/WebBuilder/createTemplatejs.tpl +++ b/code/web/interface/themes/responsive/WebBuilder/createTemplatejs.tpl @@ -108,10 +108,24 @@ }), contentType: "application/json", success: function (response) { - console.log('Saved as Template'); + var title = response.title; + var message; + + if (response.success) { + message = response.message || 'Template updated successfully.'; + AspenDiscovery.showMessage(title, message); + } else { + message = response.message || 'Failed to save template.' + AspenDiscovery.showMessage(title, message); + } }, error: function (xhr, status, error) { console.error('Error saving template: ', error); + var errorMessage = 'Failed to save template. Please try again.'; + if (xhr.responseJSON && xhr.responseJSON.message) { + errorMessage = xhr.responseJSON.message; + } + AspenDiscovery.showMessage('Error', errorMessage); } }); } diff --git a/code/web/interface/themes/responsive/WebBuilder/grapesjs.tpl b/code/web/interface/themes/responsive/WebBuilder/grapesjs.tpl index c12643102f..a7798532a8 100644 --- a/code/web/interface/themes/responsive/WebBuilder/grapesjs.tpl +++ b/code/web/interface/themes/responsive/WebBuilder/grapesjs.tpl @@ -106,10 +106,23 @@ }), contentType: "application/json", success: function (response) { - console.log('Saved as Grapes Page'); + var title = response.title; + var message; + if (response.success) { + message = response.message || 'Page Saved.'; + AspenDiscovery.showMessage(title, message); + } else { + message = response.message || 'Failed to save page.'; + AspenDiscovery.showMessage(title, message); + } }, error: function (xhr, status, error) { console.error('Error saving page: ', error); + var errorMessage = 'Failed to save page. Please try again.'; + if (xhr.responseJSON && xhr.responseJSON.message){ + errorMessage = xhr.responseJSON.message; + } + AspenDiscovery.showMessage('Error', errorMessage); } }); } diff --git a/code/web/release_notes/25.02.00.MD b/code/web/release_notes/25.02.00.MD index 96ae64d902..2e1106d050 100644 --- a/code/web/release_notes/25.02.00.MD +++ b/code/web/release_notes/25.02.00.MD @@ -57,6 +57,8 @@ //james //alexander +### Web Builder Updates +- Add a message when saving a Grapes JS Template or Page to confirm it has been updated successfully. (DIS-92) (*AB*) //chloe diff --git a/code/web/services/WebBuilder/AJAX.php b/code/web/services/WebBuilder/AJAX.php index 271ed928ad..ebbed210a1 100644 --- a/code/web/services/WebBuilder/AJAX.php +++ b/code/web/services/WebBuilder/AJAX.php @@ -972,43 +972,206 @@ function trackWebResourceUsage() { } function saveAsTemplate(){ require_once ROOT_DIR . '/sys/WebBuilder/GrapesTemplate.php'; - $newGrapesPageContent = json_decode(file_get_contents("php://input"), true); - $templateId = $newGrapesPageContent['templateId']; - $html = $newGrapesPageContent['html']; - $css = $newGrapesPageContent['css']; - $projectData = json_encode($newGrapesPageContent['projectData']); - - $template = new GrapesTemplate(); - $template->id = $templateId; - - if ($template->find(true)) { - $template->templateContent = $projectData; - $template->htmlData = $html; - $template->cssData = $css; - } - $template->update(); - } - - function saveAsPage() { - require_once ROOT_DIR . '/sys/WebBuilder/GrapesPage.php'; - $newGrapesPageContent = json_decode(file_get_contents("php://input"), true); - $grapesPageId = $newGrapesPageContent['grapesPageId']; - $grapesGenId = $newGrapesPageContent['grapesGenId']; - $templateId = $newGrapesPageContent['templateId']; - $html = $newGrapesPageContent['html']; - $css = $newGrapesPageContent['css']; - $grapesPage = new GrapesPage(); - $grapesPage->id = $grapesPageId; - $projectData = json_encode($newGrapesPageContent['projectData']); + + $activeUser = UserAccount::getActiveUserObj(); + + if (!$activeUser) { + return [ + 'success' => false, + 'title' => translate([ + 'text' =>'Error', + 'isPublicFacing' => true, + ]), + 'message' => translate([ + 'text' => 'You must be logged in to save grapes templates.', + 'isPublicFacing' => true + ]) + ]; + } + if (!UserAccount::userHasPermission([ + 'Administer All Grapes Pages', + 'Administer Library Custom Pages', + ])) { + return [ + 'success' => false, + 'title' => translate([ + 'text' =>'Error', + 'isPublicFacing' => true, + ]), + 'message' => translate([ + 'text' => 'You do not have the correct permissions to save grapes templates.', + 'isPublicFacing' => true + ]) + ]; + } + try { + $newGrapesPageContent = json_decode(file_get_contents("php://input"), true); + $templateId = $newGrapesPageContent['templateId']; + $html = $newGrapesPageContent['html']; + $css = $newGrapesPageContent['css']; + $projectData = json_encode($newGrapesPageContent['projectData']); + + $template = new GrapesTemplate(); + $template->id = $templateId; + + if (!$template->find(true)) { + return [ + 'success' => false, + 'title' => translate([ + 'text' => 'Error', + 'isPublicFacing' => true + ]), + 'message' => translate([ + 'text' => "Template with ID $templateId not found. Unable to update.", + 'isPublicFacing' => true + ]) + ]; + } + $template->templateContent = $projectData; + $template->htmlData = $html; + $template->cssData = $css; + + if(!$template->update()) { + return [ + 'success' => false, + 'title' => translate([ + 'text' => 'Error', + 'isPublicFacing' => true + ]), + 'message' => translate([ + 'text' => 'Failed to update the template.', + 'isPublicFacing' => true + ]) + ]; + } + return [ + 'success' => true, + 'title' => translate([ + 'text' => 'Success', + 'isPublicFacing' => true + ]), + 'message' => translate([ + 'text' => 'Template saved successfully.', + 'isPublicFacing' => true + ]) + ]; + } catch (Exception $e) { + return [ + 'success' => false, + 'title' => translate([ + 'text' => 'Error', + 'isPublicFacing' => true + ]), + 'message' => translate([ + 'text' => 'An unexpected error occurred: ' . $e->getMessage(), + 'isPublicFacing' => true + ]) + ]; + } + } - if ($grapesPage->find(true)) { - $grapesPage->grapesGenId = $grapesGenId; - $grapesPage->templateContent = $projectData; - $grapesPage->htmlData = $html; - $grapesPage->cssData = $css; - } - $grapesPage->update(); - } + function saveAsPage() { + require_once ROOT_DIR . '/sys/WebBuilder/GrapesPage.php'; + + $activeUser = UserAccount::getActiveUserObj(); + + if (!$activeUser) { + return [ + 'success' => false, + 'title' => translate([ + 'text' =>'Error', + 'isPublicFacing' => true, + ]), + 'message' => translate([ + 'text' => 'You must be logged in to save grapes pages.', + 'isPublicFacing' => true + ]) + ]; + } + if (!UserAccount::userHasPermission([ + 'Administer All Grapes Pages', + 'Administer Library Custom Pages', + ])) { + return [ + 'success' => false, + 'title' => translate([ + 'text' =>'Error', + 'isPublicFacing' => true, + ]), + 'message' => translate([ + 'text' => 'You do not have the correct permissions to save grapes pages.', + 'isPublicFacing' => true + ]) + ]; + } + + try { + $newGrapesPageContent = json_decode(file_get_contents("php://input"), true); + $grapesPageId = $newGrapesPageContent['grapesPageId']; + $grapesGenId = $newGrapesPageContent['grapesGenId']; + $templateId = $newGrapesPageContent['templateId']; + $html = $newGrapesPageContent['html']; + $css = $newGrapesPageContent['css']; + $grapesPage = new GrapesPage(); + $grapesPage->id = $grapesPageId; + $projectData = json_encode($newGrapesPageContent['projectData']); + + if (!$grapesPage->find(true)) { + return [ + 'success' =>false, + 'title' => translate([ + 'text' => 'Error', + 'isPublicFacing' => true + ]), + 'message' => translate([ + 'text' => "Page with ID $grapesPageId not found. Unable to update.", + 'isPublicFacing' => true + ]) + ]; + } + $grapesPage->grapesGenId = $grapesGenId; + $grapesPage->templateContent = $projectData; + $grapesPage->htmlData = $html; + $grapesPage->cssData = $css; + + if (!$grapesPage->update()) { + return [ + 'success' => false, + 'title' => translate([ + 'text' => 'Error', + 'isPublicFacing' => true + ]), + 'message' => translate([ + 'text' => 'Failed to update the page.', + 'isPublicFacing' => true + ]) + ]; + } + return [ + 'success' => true, + 'title' => translate([ + 'text' => 'Success', + 'isPublicFacing' => true + ]), + 'message' => translate([ + 'text' => 'Page saved successfully.', + 'isPublicFacing' => true + ]) + ]; + } catch (Exception $e) { + return [ + 'success' => false, + 'title' => translate([ + 'text' => 'Error', + 'isPublicFacing' => true + ]), + 'message' => translate([ + 'text' => 'An unexpected error occurred: ' . $e->getMessage(), + 'isPublicFacing' => true + ]) + ]; + } + } function loadGrapesPage() { require_once ROOT_DIR . '/sys/WebBuilder/GrapesPage.php'; diff --git a/code/web/services/WebBuilder/GrapesPages.php b/code/web/services/WebBuilder/GrapesPages.php index d27071a39a..7d3d75058f 100644 --- a/code/web/services/WebBuilder/GrapesPages.php +++ b/code/web/services/WebBuilder/GrapesPages.php @@ -83,8 +83,8 @@ function getBreadcrumbs(): array { function canView(): bool { return UserAccount::userHasPermission([ - 'Administer All Basic Pages', - 'Administer Library Basic Pages', + 'Administer All Grapes Pages', + 'Administer Library Grapes Pages', ]); }