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

Dis 109 add templates to grapes js #155

Open
wants to merge 4 commits into
base: DIS-109-Add_Templates_To_Grapes_JS
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,19 @@
}),
contentType: "application/json",
success: function (response) {
console.log('Saved as Template');
if (response.success) {
AspenDiscovery.showMessage('Success', response.message);
} else {
AspenDiscovery.showMessage('Error', response.message || 'Failed to save template.');
}
},
error: function (xhr, status, error) {
console.error('Error saving template: ', error);
let errorMessage = 'Failed to save template. Please try again.';
if (xhr.responseJSON && xhr.responseJSON.message) {
errorMessage = xhr.responseJSON.message;
}
AspenDiscovery.showMessage('Error', errorMessage);
}
});
}
Expand Down
11 changes: 10 additions & 1 deletion code/web/interface/themes/responsive/WebBuilder/grapesjs.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,19 @@
}),
contentType: "application/json",
success: function (response) {
console.log('Saved as Grapes Page');
if (response.success) {
AspenDiscovery.showMessage('Success', response.message);
} else {
AspenDiscovery.showMessage('Error', response.message || 'Failed to save page.');
}
},
error: function (xhr, status, error) {
console.error('Error saving page: ', error);
let errorMessage = 'Failed to save page. Please try again.';
if (xhr.responseJSON && xhr.responseJSON.message){
errorMessage = xhr.responseJSON.message;
}
AspenDiscovery.showMessage('Error', errorMessage);
}
});
}
Expand Down
2 changes: 2 additions & 0 deletions code/web/release_notes/25.01.00.MD
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@

### Web Builder Updates
- When adding a form to a custom page, limit the forms displayed in the dropdown to those from the user's own library if their permissions are Administer Library Custom Forms. (*AB*)
- Add a message when saving a Grapes JS Template or Page to confirm it has been updated successfully. (*AB*)
- Add a selection of templates that load automatically when Grapes JS loads. (*AB*)

### Other Updates
- Fix linking to library websites for consortia within Web Builder and the Hours and Location dialog. (*MDN*)
Expand Down
116 changes: 80 additions & 36 deletions code/web/services/WebBuilder/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -972,43 +972,87 @@ 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']);

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,
'message' => "Template with ID $templateId not found. Unable to update."
];
}
$template->templateContent = $projectData;
$template->htmlData = $html;
$template->cssData = $css;

if(!$template->update()) {
return [
'success' => false,
'message' => 'Failed to update the template.'
];
}
return [
'success' => true,
'message' => 'Template saved successfully.'
];
} catch (Exception $e) {
return [
'success' => false,
'message' => 'an unexpected error occurred: ' . $e->getMessage()
];
}
}

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';

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,
'message' => "Page with ID $grapesPageId not found. Unable to update."
];
}
$grapesPage->grapesGenId = $grapesGenId;
$grapesPage->templateContent = $projectData;
$grapesPage->htmlData = $html;
$grapesPage->cssData = $css;

if (!$grapesPage->update()) {
return [
'success ' => false,
'message' => 'Failed to update the page.'
];
}
return [
'success' => true,
'message' => 'Page saved successfully.'
];
} catch (Exception $e) {
return [
'success' => false,
'message' => 'An unexpected error occurred: ' . $e->getMessage()
];
}
}

function loadGrapesPage() {
require_once ROOT_DIR . '/sys/WebBuilder/GrapesPage.php';
Expand Down
33 changes: 33 additions & 0 deletions code/web/sys/DBMaintenance/grapes_web_builder_updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,38 @@ function getGrapesWebBuilderUpdates() {
"INSERT INTO grapes_templates (templateName, templateContent) VALUES ('No Template', ' ')",
],
],
'add_grapes_templates_to_db' => [
'title' => 'Add Grapes Templates To DB',
'description' => 'Add grapes templates to db',
'sql' => [
'addTemplateFromJson'
],
],
];
}

function addTemplateFromJson(&$update) {
require_once ROOT_DIR . '/sys/WebBuilder/GrapesTemplate.php';
$jsonFile = './web_builder/templates.json';

if (file_exists($jsonFile)) {
$jsonData = file_get_contents($jsonFile);

$jsonDecoded = json_decode($jsonData, true);

$templates = $jsonDecoded['templates'];

foreach ($templates as $preMadeTemplate) {
$template = new GrapesTemplate();

$template->addTemplate(
$preMadeTemplate['templateName'],
$preMadeTemplate['templateContent'],
$preMadeTemplate['htmlData'] ?? '',
$preMadeTemplate['cssData'] ?? ''
);
}
$update['success'] = true;

}
}
8 changes: 8 additions & 0 deletions code/web/sys/WebBuilder/GrapesTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,13 @@ function saveAsTemplate() {

}

public function addTemplate($templateName, $templateContent, $htmlData, $cssData) {
$this->templateName = $templateName;
$this->templateContent = $templateContent;
$this->htmlData = $htmlData;
$this->cssData = $cssData;

$this->insert();
}

}
Loading
Loading