From 6291f97a722d61092a29a092e03932409347d835 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 25 Jan 2025 12:28:59 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(content):=20separate=20st?= =?UTF-8?q?icky=20from=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bl-kernel/admin/controllers/content.php | 4 +- bl-kernel/admin/views/edit-content.php | 17 +++++-- bl-kernel/admin/views/new-content.php | 7 +-- bl-kernel/ajax/content-get-list.php | 19 +++----- bl-kernel/ajax/get-published.php | 2 +- bl-kernel/ajax/save-as-draft.php | 8 +++- bl-kernel/categories.class.php | 1 - bl-kernel/functions.php | 11 ++--- bl-kernel/helpers/updater.class.php | 9 ++++ bl-kernel/js/koblog-ajax.php | 5 +- bl-kernel/pages.class.php | 63 ++++++++++++++++--------- bl-kernel/pagex.class.php | 2 +- bl-plugins/api/plugin.php | 10 ++-- bl-plugins/navigation/plugin.php | 3 +- bl-plugins/rss/plugin.php | 8 ++-- bl-plugins/search/plugin.php | 2 +- bl-plugins/sitemap/plugin.php | 2 +- 17 files changed, 102 insertions(+), 71 deletions(-) diff --git a/bl-kernel/admin/controllers/content.php b/bl-kernel/admin/controllers/content.php index 307ab839..7e2852a0 100644 --- a/bl-kernel/admin/controllers/content.php +++ b/bl-kernel/admin/controllers/content.php @@ -35,11 +35,11 @@ function filterContentOwner($list) { // Main after POST // ============================================================================ -$published = $pages->getList($url->pageNumber(), ITEMS_PER_PAGE_ADMIN); +$published = $pages->getList($url->pageNumber(), ITEMS_PER_PAGE_ADMIN, ['article'], ['published'], false, false); $drafts = $pages->getDraftDB(['article'], true); $scheduled = $pages->getScheduledDB(['article'],true); $static = $pages->getStaticDB(['published'],true); -$sticky = $pages->getStickyDB(['published'],true); +$sticky = $pages->getStickyDB(['article'],['published'],true); $autosave = $pages->getAutosaveDB(['article'],true); // If the user is an Author filter the content he/she can edit diff --git a/bl-kernel/admin/views/edit-content.php b/bl-kernel/admin/views/edit-content.php index 54dff7eb..413607c2 100644 --- a/bl-kernel/admin/views/edit-content.php +++ b/bl-kernel/admin/views/edit-content.php @@ -156,6 +156,16 @@ echo Bootstrap::formInputHidden(array( 'rows' => 5, 'placeholder' => $L->get('this-field-can-help-describe-the-content') )); + + // Sticky + echo Bootstrap::formCheckbox(array( + 'name' => 'sticky', + 'label' => $L->g('Sticky'), + 'labelForCheckbox' => $L->g('Put this article in first position in the article list.'), + 'placeholder' => '', + 'checked' => $page->sticky(), + 'tip' => "" + )); ?> @@ -211,7 +221,6 @@ echo Bootstrap::formInputHidden(array( 'selected' => $page->type(), 'options' => array( 'article' => '- ' . $L->g('Default') . ' -', - 'sticky' => $L->g('Sticky'), 'static' => $L->g('Static') ), 'tip' => '' @@ -488,9 +497,10 @@ foreach ($customFields as $field => $options) { $("#jsbuttonPreview").on("click", function() { var uuid = $("#jsuuid").val(); var title = $("#jstitle").val(); + var type = $("#jstype").val(); var content = editorGetContent(); var ajax = new koblogAjax(); - koblogAjax.saveAsDraft(uuid, title, content).then(function(data) { + koblogAjax.saveAsDraft(uuid, title, type, content).then(function(data) { var preview = window.open("uuid() . '?preview=' . md5('autosave-' . $page->uuid()) ?>", "koblog-preview"); preview.focus(); }); @@ -525,6 +535,7 @@ foreach ($customFields as $field => $options) { var uuid = $("#jsuuid").val(); var title = $("#jstitle").val() + "[p('Autosave') ?>]"; var content = editorGetContent(); + var type = $("#jstype").val(); // Autosave when content has at least 100 characters if (content.length < 100) { return false; @@ -532,7 +543,7 @@ foreach ($customFields as $field => $options) { // Autosave only when the user change the content if (currentContent != content) { currentContent = content; - koblogAjax.saveAsDraft(uuid, title, content).then(function(data) { + koblogAjax.saveAsDraft(uuid, title, type, content).then(function(data) { if (data.status == 0) { showAlert("p('Autosave') ?>"); } diff --git a/bl-kernel/admin/views/new-content.php b/bl-kernel/admin/views/new-content.php index 62ed07d0..a78d3f82 100644 --- a/bl-kernel/admin/views/new-content.php +++ b/bl-kernel/admin/views/new-content.php @@ -192,7 +192,6 @@ echo Bootstrap::formInputHidden(array( 'selected' => '', 'options' => array( 'article' => '- ' . $L->g('Default') . ' -', - 'sticky' => $L->g('Sticky'), 'static' => $L->g('Static') ), 'tip' => '' @@ -443,8 +442,9 @@ foreach ($customFields as $field => $options) { $("#jsbuttonPreview").on("click", function() { var uuid = $("#jsuuid").val(); var title = $("#jstitle").val(); + var type = $("#jstype").val(); var content = editorGetContent(); - koblogAjax.saveAsDraft(uuid, title, content).then(function(data) { + koblogAjax.saveAsDraft(uuid, title, type, content).then(function(data) { var preview = window.open("", "koblog-preview"); preview.focus(); }); @@ -486,6 +486,7 @@ foreach ($customFields as $field => $options) { setInterval(function() { var uuid = $("#jsuuid").val(); var title = $("#jstitle").val() + "[p('Autosave') ?>]"; + var type = $("#jstype").val(); var content = editorGetContent(); // Autosave when content has at least 100 characters if (content.length < 100) { @@ -494,7 +495,7 @@ foreach ($customFields as $field => $options) { // Autosave only when the user change the content if (currentContent != content) { currentContent = content; - koblogAjax.saveAsDraft(uuid, title, content).then(function(data) { + koblogAjax.saveAsDraft(uuid, title, type, content).then(function(data) { if (data.status == 0) { showAlert("p('Autosave') ?>"); } diff --git a/bl-kernel/ajax/content-get-list.php b/bl-kernel/ajax/content-get-list.php index 25663f41..235dc705 100644 --- a/bl-kernel/ajax/content-get-list.php +++ b/bl-kernel/ajax/content-get-list.php @@ -4,23 +4,18 @@ header('Content-Type: application/json'); /* | Search for pages that have in the title the string $query and returns the array of pages | -| @_GET['published'] boolean True to search in published database -| @_GET['static'] boolean True to search in static database -| @_GET['sticky'] boolean True to search in sticky database -| @_GET['scheduled'] boolean True to search in scheduled database -| @_GET['draft'] boolean True to search in draft database -| @_GET['query'] string Text to search in the title +| @_GET['type'] the type to search in the database +| @_GET['state'] the state to search in the database +| @_GET['sticky'] boolean False to not search sticky post | | @return array */ // $_GET // ---------------------------------------------------------------------------- -$published = empty($_GET['published']) ? false:true; -$static = empty($_GET['static']) ? false:true; -$sticky = empty($_GET['sticky']) ? false:true; -$scheduled = empty($_GET['scheduled']) ? false:true; -$draft = empty($_GET['draft']) ? false:true; +$type = empty($GET_['type']) ? ['article'] : explode(",", $GET_['type']); +$state = empty($GET_['state']) ? ['published'] : explode(",", $GET_['state']); +$sticky = isset($_GET['sticky']) ? $_GET['sticky'] != false:true; // True by default $query = isset($_GET['query']) ? Text::lowercase($_GET['query']) : false; // ---------------------------------------------------------------------------- @@ -30,7 +25,7 @@ if ($query===false) { $pageNumber = 1; $numberOfItems = -1; -$pagesKey = $pages->getList($pageNumber, $numberOfItems, $published, $static, $sticky, $draft, $scheduled); +$pagesKey = $pages->getList($pageNumber, $numberOfItems, $type, $state, $sticky); $tmp = array(); foreach ($pagesKey as $pageKey) { try { diff --git a/bl-kernel/ajax/get-published.php b/bl-kernel/ajax/get-published.php index 3608ee05..61a31b65 100644 --- a/bl-kernel/ajax/get-published.php +++ b/bl-kernel/ajax/get-published.php @@ -28,7 +28,7 @@ foreach ($pagesKey as $pageKey) { $page = new Page($pageKey); if ($page->isParent() || !$checkIsParent) { // Check page status - if ($page->article() || $page->sticky() || $page->isStatic()) { + if ($page->published()) { // Check if the query contains in the title $lowerTitle = Text::lowercase($page->title()); if (Text::stringContains($lowerTitle, $query)) { diff --git a/bl-kernel/ajax/save-as-draft.php b/bl-kernel/ajax/save-as-draft.php index f6f4d988..186fe593 100644 --- a/bl-kernel/ajax/save-as-draft.php +++ b/bl-kernel/ajax/save-as-draft.php @@ -18,7 +18,9 @@ header('Content-Type: application/json'); $title = isset($_POST['title']) ? $_POST['title'] : false; $content = isset($_POST['content']) ? $_POST['content'] : false; $uuid = isset($_POST['uuid']) ? $_POST['uuid'] : false; -$type = isset($_POST['type']) ? $_POST['type'] : 'draft'; +$type = isset($_POST['type']) ? $_POST['type'] : 'article'; +$type = isset($_POST['state']) ? $_POST['state'] : 'draft'; +$type = isset($_POST['sticky']) ? $_POST['state'] : false; // ---------------------------------------------------------------------------- // Check UUID @@ -32,7 +34,9 @@ $page = array( 'slug'=>$uuid, 'title'=>$title, 'content'=>$content, - 'type'=>$type + 'type'=>$type, + 'state'=>$state, + 'sticky'=>$sticky, ); // Get the page key by the UUID diff --git a/bl-kernel/categories.class.php b/bl-kernel/categories.class.php index ff1171a7..10f040f1 100644 --- a/bl-kernel/categories.class.php +++ b/bl-kernel/categories.class.php @@ -29,7 +29,6 @@ class Categories extends dbList { if (isset($this->db[$categoryKey]['list'])) { if ( (($db[$pageKey]['type']=='article') || - ($db[$pageKey]['type']=='sticky') || ($db[$pageKey]['type']=='static')) && ($db[$pageKey]['state']=='published') ) { diff --git a/bl-kernel/functions.php b/bl-kernel/functions.php index d4825c47..c557f331 100644 --- a/bl-kernel/functions.php +++ b/bl-kernel/functions.php @@ -135,15 +135,14 @@ function buildPagesFor($for, $categoryKey = false, $tagKey = false, $authorKey = $pageNumber = $url->pageNumber(); if ($for == 'home') { - $onlyPublished = true; $numberOfItems = $site->itemsPerPage(); - $list = $pages->getList($pageNumber, $numberOfItems, $onlyPublished); + $list = $pages->getList($pageNumber, $numberOfItems, ['article'], ['published'], true, true); // Include sticky pages only in the first page - if ($pageNumber == 1) { - $sticky = $pages->getStickyDB(); - $list = array_merge($sticky, $list); - } + // if ($pageNumber == 1) { + // $sticky = $pages->getStickyDB(); + // $list = array_merge($sticky, $list); + // } } elseif ($for == 'category') { $numberOfItems = $site->itemsPerPage(); $list = $categories->getList($categoryKey, $pageNumber, $numberOfItems); diff --git a/bl-kernel/helpers/updater.class.php b/bl-kernel/helpers/updater.class.php index 2c6fc6d9..1fda64a7 100644 --- a/bl-kernel/helpers/updater.class.php +++ b/bl-kernel/helpers/updater.class.php @@ -19,12 +19,21 @@ class KoblogUpdater { } elseif ($fields['type'] === "scheduled") { $pages->db[$key]['type'] = "article"; $pages->db[$key]['state'] = "scheduled"; + } elseif ($fields['type'] === "sticky") { + $pages->db[$key]['type'] = "article"; + $pages->db[$key]['state'] = "published"; + $pages->db[$key]['sticky'] = true; } // S'il n'y a toujours rien, c'est qu'il est publiƩ if ($pages->db[$key]['state'] === null) { $pages->db[$key]['state'] = "published"; } + + // S'il n'y a toujours pas de sticky, c'est qu'il ne l'est pas + if ($pages->db[$key]['sticky'] === null) { + $pages->db[$key]['sticky'] = false; + } } return $pages->save(); diff --git a/bl-kernel/js/koblog-ajax.php b/bl-kernel/js/koblog-ajax.php index ce6df647..3d937a35 100644 --- a/bl-kernel/js/koblog-ajax.php +++ b/bl-kernel/js/koblog-ajax.php @@ -1,6 +1,6 @@ class koblogAjax { - static async saveAsDraft(uuid, title, content) { + static async saveAsDraft(uuid, title, type, content) { let url = HTML_PATH_ADMIN_ROOT+"ajax/save-as-draft" try { const response = await fetch(url, { @@ -14,7 +14,8 @@ class koblogAjax { 'uuid': "autosave-" + uuid, 'title': title, 'content': content, - 'type': 'autosave' + 'type': type, + 'state': 'autosave' }), }); const json = await response.json(); diff --git a/bl-kernel/pages.class.php b/bl-kernel/pages.class.php index 2da5ab95..9e3dbfd8 100644 --- a/bl-kernel/pages.class.php +++ b/bl-kernel/pages.class.php @@ -9,8 +9,9 @@ class Pages extends dbJSON 'description' => '', 'username' => '', 'tags' => array(), - 'type' => 'article', // article, static, sticky + 'type' => 'article', // article, static 'state' => 'published', // draft, scheduled, autosave + 'sticky' => false, 'date' => '', 'dateModified' => '', 'position' => 0, @@ -396,7 +397,7 @@ class Pages extends dbJSON return $tmp; } - public function getFilteredDB($types = [], $states = [], $onlyKeys) + public function getFilteredDB($types = [], $states = [], $sticky = false, $onlyKeys) { $tmp = $this->db; foreach ($tmp as $key => $fields) { @@ -406,6 +407,9 @@ class Pages extends dbJSON if (!empty($states) && !in_array($fields['state'], $states)) { unset($tmp[$key]); } + if (!$sticky && $fields['sticky'] == true) { + unset($tmp[$key]); + } } if ($onlyKeys) { return array_keys($tmp); @@ -418,32 +422,32 @@ class Pages extends dbJSON // $onlyKeys = false; Returns part of the database, I do not recommend use this public function getPublishedDB($types = ['article'], $onlyKeys = true) { - return $this->getFilteredDB($types, ['published'], $onlyKeys); + return $this->getFilteredDB($types, ['published'], true, $onlyKeys); } // Returns an array with a list of keys/database of draft pages public function getDraftDB($types = ['article'], $onlyKeys = true) { - return $this->getFilteredDB($types, ['draft'], $onlyKeys); + return $this->getFilteredDB($types, ['draft'], true, $onlyKeys); } // Returns an array with a list of keys/database of autosave pages public function getAutosaveDB($types = ['article'], $onlyKeys = true) { - return $this->getFilteredDB($types, ['autosave'], $onlyKeys); + return $this->getFilteredDB($types, ['autosave'], true, $onlyKeys); } // Returns an array with a list of keys/database of scheduled pages public function getScheduledDB($types = ['article'], $onlyKeys = true) { - return $this->getFilteredDB($types, ['scheduled'], $onlyKeys); + return $this->getFilteredDB($types, ['scheduled'], true, $onlyKeys); } // Returns an array with a list of keys/database of static pages // By default the static pages are sort by position public function getStaticDB($states = ['published'], $onlyKeys = true) { - $tmp = $this->getFilteredDB(['static'], $states, false); + $tmp = $this->getFilteredDB(['static'], $states, false, false); uasort($tmp, array($this, 'sortByPositionLowToHigh')); if ($onlyKeys) { return array_keys($tmp); @@ -452,9 +456,18 @@ class Pages extends dbJSON } // Returns an array with a list of keys of sticky pages - public function getStickyDB($states = ['published'], $onlyKeys = true) + public function getStickyDB($types = ['article'], $states = ['published'], $onlyKeys = true) { - return $this->getFilteredDB(['sticky'], $states, $onlyKeys); + $tmp = $this->getFilteredDB($types, $states, true, false); + foreach ($tmp as $key => $fields) { + if ($fields['sticky'] == false) { + unset($tmp[$key]); + } + } + if ($onlyKeys) { + return array_keys($tmp); + } + return $tmp; } // Returns the next number of the bigger position @@ -506,23 +519,16 @@ class Pages extends dbJSON // (int) $pageNumber, the page number // (int) $numberOfItems, amount of items to return, if -1 returns all the items // (boolean) $onlyPublished, TRUE to return only published pages - public function getList($pageNumber, $numberOfItems, $published = true, $static = false, $sticky = false, $draft = false, $scheduled = false) + public function getList($pageNumber, $numberOfItems, $types = ['article'], $states = ['published'], $sticky = true, $sortHomepage = false) { - $list = array(); - foreach ($this->db as $key => $fields) { - if ($published && $fields['type'] == 'article' && $fields['state'] == 'published') { - array_push($list, $key); - } elseif ($static && $fields['type'] == 'static') { - array_push($list, $key); - } elseif ($sticky && $fields['type'] == 'sticky') { - array_push($list, $key); - } elseif ($draft && $fields['state'] == 'draft') { - array_push($list, $key); - } elseif ($scheduled && $fields['state'] == 'scheduled') { - array_push($list, $key); - } + $tmp = $this->getFilteredDB($types, $states, $sticky, false); + + if ($sortHomepage) { + uasort($tmp, array($this, 'sortHomepage')); } + $list = array_keys($tmp); + if ($numberOfItems == -1) { return $list; } @@ -640,6 +646,17 @@ class Pages extends dbJSON return $a['date'] < $b['date']; } + private function sortHomepage($a, $b) + { + if ($a['sticky'] && !$b['sticky']) { + return false; + } elseif (!$a['sticky'] && $b['sticky']) { + return true; + } else { + return $this->sortByDateHighToLow($a, $b); + } + } + function generateUUID() { return md5(uniqid() . time()); diff --git a/bl-kernel/pagex.class.php b/bl-kernel/pagex.class.php index 200dc6a2..ec0e64a3 100644 --- a/bl-kernel/pagex.class.php +++ b/bl-kernel/pagex.class.php @@ -390,7 +390,7 @@ class Page // (boolean) Returns TRUE if the page is sticky, FALSE otherwise public function sticky() { - return ($this->getValue('type') == 'sticky'); + return ($this->getValue('sticky')); } // (boolean) Returns TRUE if the page is static, FALSE otherwise diff --git a/bl-plugins/api/plugin.php b/bl-plugins/api/plugin.php index 4d33bc46..c737db31 100644 --- a/bl-plugins/api/plugin.php +++ b/bl-plugins/api/plugin.php @@ -352,16 +352,14 @@ class pluginAPI extends Plugin global $pages; // Parameters and the default values - $published = (isset($args['article']) ? $args['article'] == 'true' : true); - $static = (isset($args['static']) ? $args['static'] == 'true' : false); - $draft = (isset($args['draft']) ? $args['draft'] == 'true' : false); - $sticky = (isset($args['sticky']) ? $args['sticky'] == 'true' : false); - $scheduled = (isset($args['scheduled']) ? $args['scheduled'] == 'true' : false); + $types = (isset($args['type']) ? $args['type'] : ['article']); + $state = (isset($args['state']) ? $args['state'] : ['published']); + $sticky = (isset($args['sticky']) ? $args['sticky'] == 'true' : true); $untagged = (isset($args['untagged']) ? $args['untagged'] == 'true' : false); $numberOfItems = (isset($args['numberOfItems']) ? $args['numberOfItems'] : 10); $pageNumber = (isset($args['pageNumber']) ? $args['pageNumber'] : 1); - $list = $pages->getList($pageNumber, $numberOfItems, $published, $static, $sticky, $draft, $scheduled); + $list = $pages->getList($pageNumber, $numberOfItems, $types, $state, $sticky); $tmp = array( 'status' => '0', diff --git a/bl-plugins/navigation/plugin.php b/bl-plugins/navigation/plugin.php index 2f5a0a1a..d53bcbc6 100644 --- a/bl-plugins/navigation/plugin.php +++ b/bl-plugins/navigation/plugin.php @@ -95,10 +95,9 @@ class pluginNavigation extends Plugin // Pages order by date else { // List of published pages - $onlyPublished = true; $pageNumber = 1; $numberOfItems = $this->getValue('numberOfItems'); - $publishedPages = $pages->getList($pageNumber, $numberOfItems, $onlyPublished); + $publishedPages = $pages->getList($pageNumber, $numberOfItems, ['article'], ['published']); foreach ($publishedPages as $pageKey) { try { diff --git a/bl-plugins/rss/plugin.php b/bl-plugins/rss/plugin.php index 0482f997..b1a766c5 100644 --- a/bl-plugins/rss/plugin.php +++ b/bl-plugins/rss/plugin.php @@ -50,11 +50,9 @@ class pluginRSS extends Plugin $list = $pages->getList( $pageNumber = 1, $numberOfItems, - $published = true, - $static = true, - $sticky = true, - $draft = false, - $scheduled = false + $type = ['article', 'static'], + $state = ['published'], + $sticky = true ); $xml = ''; diff --git a/bl-plugins/search/plugin.php b/bl-plugins/search/plugin.php index 5e3d1c9b..a263e304 100644 --- a/bl-plugins/search/plugin.php +++ b/bl-plugins/search/plugin.php @@ -187,7 +187,7 @@ EOF; { // Get all pages published global $pages; - $list = $pages->getList($pageNumber = 1, $numberOfItems = -1, $published = true, $static = true, $sticky = true, $draft = false, $scheduled = false); + $list = $pages->getList($pageNumber = 1, $numberOfItems = -1, $type = []); $cache = array(); foreach ($list as $pageKey) { diff --git a/bl-plugins/sitemap/plugin.php b/bl-plugins/sitemap/plugin.php index 753264bd..c0bd8bb0 100644 --- a/bl-plugins/sitemap/plugin.php +++ b/bl-plugins/sitemap/plugin.php @@ -53,7 +53,7 @@ class pluginSitemap extends Plugin { $xml .= ''.$site->url().''; $xml .= ''; - $list = $pages->getList($pageNumber=1, $numberOfItems=-1, $published=true, $static=true, $sticky=true, $draft=false, $scheduled=false); + $list = $pages->getList($pageNumber=1, $numberOfItems=-1, [], ['published'], true, false); foreach ($list as $pageKey) { try { // Create the page object from the page key