Improve function preview #1438

This commit is contained in:
Diego Najar 2022-05-08 15:03:32 +02:00
parent 22bd590cf5
commit 16bb662cce
5 changed files with 31 additions and 23 deletions

View file

@ -15,10 +15,14 @@ checkRole(array('admin', 'editor', 'author'));
// ============================================================================
$pageKey = false;
$pageUUID = false;
$pagePreviewID = false;
if (!empty($layout['parameters'])) {
try {
$pageKey = $layout['parameters'];
$page = new Page($pageKey);
$pageUUID = $page->uuid();
$pagePreviewID = $page->previewID();
} catch (Exception $e) {
Log::set(__METHOD__.LOG_SEP.'An error occurred while trying to get the page: '.$pageKey, LOG_TYPE_ERROR);
Redirect::page('content');

View file

@ -5,18 +5,23 @@
// Variables for the view
// ============================================================================
var _pageKey = <?php echo $pageKey ? '"' . $pageKey . '"' : 'null' ?>;
var _pageUUID = <?php echo $pageUUID ? '"' . $pageUUID . '"' : 'null' ?>;
var _pagePreviewID = <?php echo $pagePreviewID ? '"' . $pagePreviewID . '"' : 'null' ?>;
// ============================================================================
// Functions for the view
// ============================================================================
// Default function for the editor
// These functions work if the user does not activate any plugin
// Default function for get the content inside the textarea from the Editor
// This function is enable only when the user doesn't define a plugin as Editor
if (typeof editorGetContent != 'function') {
window.editorGetContent = function() {
return $('#editor').val();
};
}
}
// Default function for insert content inside the textarea from the Editor
// This function is enable only when the user doesn't define a plugin as Editor
if (typeof editorInsertContent != 'function') {
window.editorInsertContent = function(content, type = '') {
if (type == 'image') {
@ -28,8 +33,10 @@
};
}
// Create the a page
// Create a new page
// This function set the global variable "_pageKey"
// This function set the global variable "_pageUUID"
// This function set the global variable "_pagePreviewID"
function createPage() {
logs('Creating page.');
api.createPage().then(function(response) {
@ -37,8 +44,8 @@
logs('Page created. Key: ' + response.data.key);
// Set the global variable with the page key
_pageKey = response.data.key;
// Set Friendly URL
//$('#friendlyURL').val(response.data.key);
_pageUUID = response.data.uuid;
_pagePreviewID = response.data.previewID;
// Get current files
fmGetFiles();
} else {
@ -51,7 +58,7 @@
// Set the page in the editor
function setPage() {
logs('Setting up the page');
logs('Setting up the page.');
// Get current files
fmGetFiles();
return true;
@ -60,7 +67,7 @@
// Save the current page
// This function set the global variable "_pageKey"
function savePage(args = []) {
console.log(_pageKey);
console.log('Saving page. Key: ' + _pageKey);
if (_pageKey == null) {
logs('Error, page not created.');
showAlertError("Error, page not created.");
@ -75,12 +82,6 @@
// Set the global variable with the page key
// The page key can change after save the page so you need to set again the variable
_pageKey = response.data.key;
// Set friendly URL with the key
//$('#friendlyURL').val(response.data.key);
$('#btnPreview').attr('data-key', response.data.key);
if (response.data.preview) {
$('#btnPreview').attr('data-preview', response.data.preview);
}
} else {
logs('An error occurred while trying to save the current page.');
showAlertError(response.message);
@ -233,9 +234,7 @@
});
$('#btnPreview').click(function() {
if ($(this).attr('data-key') && $(this).attr('data-preview')) {
window.open('<?php echo DOMAIN_PAGES; ?>'+$(this).attr('data-key')+'?preview='+$(this).attr('data-preview'));
}
window.open(DOMAIN_PAGES + _pageKey + '?preview=' + _pagePreviewID);
});
$('#category').on("change", function() {

View file

@ -762,7 +762,7 @@ function buildThePage() {
}
if ($page->draft() || $page->scheduled() || $page->autosave()) {
if ($url->parameter('preview')!==md5($page->uuid())) {
if ($url->parameter('preview')!==$page->previewID()) {
$url->setNotFound();
return false;
}

View file

@ -370,6 +370,11 @@ class Page {
return $this->getValue('uuid');
}
public function previewID()
{
return md5($this->getValue('uuid'));
}
// Returns the field key
public function key()
{

View file

@ -526,10 +526,13 @@ class pluginAPI extends Plugin {
);
}
// Get the current page to get the uuid and the previewID
$page = new Page($key);
return array(
'status'=>'0',
'message'=>'Page created.',
'data'=>array('key'=>$key)
'data'=>array('key'=>$key, 'uuid'=>$page->uuid(), 'previewID'=>$page->previewID())
);
}
@ -548,14 +551,11 @@ class pluginAPI extends Plugin {
'message'=>'Error trying to edit the page.'
);
}
$page = new Page($newKey);
$preview = md5($page->uuid());
return array(
'status'=>'0',
'message'=>'Page edited.',
'data'=>array('key'=>$newKey, 'preview'=>$preview)
'data'=>array('key'=>$newKey)
);
}