From 16bb662cceaf9350302c03d715ec67e22721421e Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Sun, 8 May 2022 15:03:32 +0200 Subject: [PATCH] Improve function preview #1438 --- bl-kernel/admin/controllers/editor.php | 4 ++++ bl-kernel/admin/views/editor.php | 33 +++++++++++++------------- bl-kernel/functions.php | 2 +- bl-kernel/page.class.php | 5 ++++ bl-plugins/api/plugin.php | 10 ++++---- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/bl-kernel/admin/controllers/editor.php b/bl-kernel/admin/controllers/editor.php index 06c4dd8c..0832eaa2 100644 --- a/bl-kernel/admin/controllers/editor.php +++ b/bl-kernel/admin/controllers/editor.php @@ -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'); diff --git a/bl-kernel/admin/views/editor.php b/bl-kernel/admin/views/editor.php index ed4f7824..48177b20 100644 --- a/bl-kernel/admin/views/editor.php +++ b/bl-kernel/admin/views/editor.php @@ -5,18 +5,23 @@ // Variables for the view // ============================================================================ var _pageKey = ; + var _pageUUID = ; + var _pagePreviewID = ; // ============================================================================ // 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(''+$(this).attr('data-key')+'?preview='+$(this).attr('data-preview')); - } + window.open(DOMAIN_PAGES + _pageKey + '?preview=' + _pagePreviewID); }); $('#category').on("change", function() { diff --git a/bl-kernel/functions.php b/bl-kernel/functions.php index 6e2b2610..e91db9b0 100644 --- a/bl-kernel/functions.php +++ b/bl-kernel/functions.php @@ -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; } diff --git a/bl-kernel/page.class.php b/bl-kernel/page.class.php index 20cc26c6..65041b5b 100644 --- a/bl-kernel/page.class.php +++ b/bl-kernel/page.class.php @@ -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() { diff --git a/bl-plugins/api/plugin.php b/bl-plugins/api/plugin.php index 39b062e1..205e71fe 100644 --- a/bl-plugins/api/plugin.php +++ b/bl-plugins/api/plugin.php @@ -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) ); }