From 4b70fab5111e9c43869e505bb7e9f163c7801dc7 Mon Sep 17 00:00:00 2001 From: danpros Date: Sun, 31 Dec 2023 09:11:14 +0700 Subject: [PATCH] Add page navigation for static pages --- system/admin/views/clear-cache.html.php | 8 +- system/admin/views/static-pages.html.php | 4 +- system/htmly.php | 74 +++++++-- system/includes/functions.php | 262 +++++++++++++++++++++++++++---- 4 files changed, 299 insertions(+), 49 deletions(-) diff --git a/system/admin/views/clear-cache.html.php b/system/admin/views/clear-cache.html.php index 99a4cf1..db722fa 100644 --- a/system/admin/views/clear-cache.html.php +++ b/system/admin/views/clear-cache.html.php @@ -1,16 +1,16 @@ \ No newline at end of file diff --git a/system/admin/views/static-pages.html.php b/system/admin/views/static-pages.html.php index 12aabee..8e0a408 100644 --- a/system/admin/views/static-pages.html.php +++ b/system/admin/views/static-pages.html.php @@ -8,7 +8,6 @@ if (isset($_SESSION[config("site.url")]['user'])) { $posts = get_static_post(null); if (!empty($posts)) { - krsort($posts); echo ''; echo ''; if (config("views.counter") == "true") @@ -33,8 +32,7 @@ if (isset($_SESSION[config("site.url")]['user'])) { echo ''; echo ''; - $shortUrl = substr($p->url, strrpos($p->url, "/") + 1); - $subPages = get_static_sub_post($shortUrl, null); + $subPages = get_static_sub_post($p->md); foreach ($subPages as $sp) { echo ''; diff --git a/system/htmly.php b/system/htmly.php index 82bac2c..0ab6f83 100644 --- a/system/htmly.php +++ b/system/htmly.php @@ -65,6 +65,7 @@ get('/index', function () { 'bodyclass' => 'in-front', 'breadcrumb' => '', 'p' => $front, + 'static' => $front, 'type' => 'is_frontpage', 'is_front' => true, ), $layout); @@ -1748,9 +1749,9 @@ get('/admin/categories/:category', function ($category) { if(strtolower($category) !== 'uncategorized') { $desc = $desc[0]; } - + $total = $desc->count; - + if (empty($posts) || $page < 1) { // a non-existing page not_found(); @@ -1880,7 +1881,8 @@ get('/category/:category/edit', function ($category) { 'is_admin' => true, 'bodyclass' => 'edit-category', 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . i18n('Category') . ': ' . $post->title, - 'p' => $post + 'p' => $post, + 'static' => $post, )); } else { $login = site_url() . 'login'; @@ -1964,6 +1966,7 @@ get('/category/:category/delete', function ($category) { 'bodyclass' => 'delete-category', 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . i18n('Category') . ': ' . $post->title, 'p' => $post, + 'static' => $post, 'type' => 'categoryPage', )); } else { @@ -2409,6 +2412,7 @@ get('/post/:name', function ($name) { 'description' => $current->description, 'canonical' => $current->url, 'p' => $current, + 'post' => $current, 'author' => $author, 'bodyclass' => 'in-post category-' . $current->ct . ' type-' . $current->type, 'breadcrumb' => '', @@ -2464,6 +2468,7 @@ get('/post/:name/edit', function ($name) { 'description' => strip_tags(blog_description()), 'canonical' => site_url(), 'p' => $current, + 'post' => $current, 'type' => $type, 'is_admin' => true, 'bodyclass' => 'edit-post', @@ -2475,6 +2480,7 @@ get('/post/:name/edit', function ($name) { 'description' => strip_tags(blog_description()), 'canonical' => site_url(), 'p' => $current, + 'post' => $current, 'bodyclass' => 'denied', 'is_admin' => true, 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $current->categoryb . ' » ' . $current->title @@ -2667,6 +2673,7 @@ get('/post/:name/delete', function ($name) { 'description' => strip_tags(blog_description()), 'canonical' => site_url(), 'p' => $current, + 'post' => $current, 'is_admin' => true, 'bodyclass' => 'delete-post', 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $current->categoryb . ' » ' . $current->title @@ -2677,6 +2684,7 @@ get('/post/:name/delete', function ($name) { 'description' => strip_tags(blog_description()), 'canonical' => site_url(), 'p' => $current, + 'post' => $current, 'is_admin' => true, 'bodyclass' => 'delete-post', 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $current->categoryb . ' » ' . $current->title @@ -2853,12 +2861,24 @@ get('/:static', function ($static) { } $post = get_static_post($static); + + if (array_key_exists('prev', $post)) { + $prev = $post['prev']; + } else { + $prev = array(); + } + + if (array_key_exists('next', $post)) { + $next = $post['next']; + } else { + $next = array(); + } if (!$post) { not_found(); } - $post = $post[0]; + $post = $post['current']; if (config("views.counter") == "true") { add_view($post->file); @@ -2893,7 +2913,10 @@ get('/:static', function ($static) { 'bodyclass' => 'in-page ' . strtolower($static), 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $post->title, 'p' => $post, + 'static' => $post, 'type' => 'staticPage', + 'prev' => static_prev($prev), + 'next' => static_next($next), 'is_page' => true, ), $layout); } @@ -2912,7 +2935,7 @@ get('/:static/add', function ($static) { not_found(); } - $post = $post[0]; + $post = $post['current']; render('add-page', array( 'title' => i18n('Add_new_page') . ' - ' . blog_title(), @@ -2985,7 +3008,7 @@ get('/:static/edit', function ($static) { not_found(); } - $post = $post[0]; + $post = $post['current']; render('edit-page', array( 'title' => i18n('Edit') . ': ' . $post->title . ' - ' . blog_title(), @@ -2995,6 +3018,7 @@ get('/:static/edit', function ($static) { 'is_admin' => true, 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $post->title, 'p' => $post, + 'static' => $post, 'type' => 'staticPage', )); } else { @@ -3066,7 +3090,7 @@ get('/:static/delete', function ($static) { not_found(); } - $post = $post[0]; + $post = $post['current']; render('delete-page', array( 'title' => i18n('Delete') . ': ' . $post->title . ' - ' . blog_title(), @@ -3076,6 +3100,7 @@ get('/:static/delete', function ($static) { 'is_admin' => true, 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . i18n('Delete') . ': ' . $post->title, 'p' => $post, + 'static' => $post, 'type' => 'staticPage', )); } else { @@ -3114,10 +3139,23 @@ get('/:static/:sub', function ($static, $sub) { not_found(); } $post = get_static_sub_post($static, $sub); + + if (array_key_exists('prev', $post)) { + $prev = $post['prev']; + } else { + $prev = array(); + } + + if (array_key_exists('next', $post)) { + $next = $post['next']; + } else { + $next = array(); + } + if (!$post) { not_found(); } - $post = $post[0]; + $post = $post['current']; if (config("views.counter") == "true") { add_view($post->file); @@ -3157,8 +3195,11 @@ get('/:static/:sub', function ($static, $sub) { 'description' => $post->description, 'canonical' => $post->url, 'bodyclass' => 'in-page ' . strtolower($static) . ' ' . strtolower($sub) , - 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $parent_post[0]->title . ' » ' . $post->title, + 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $parent_post['current']->title . ' » ' . $post->title, 'p' => $post, + 'static' => $post, + 'prev' => static_prev($prev), + 'next' => static_next($next), 'type' => 'subPage', 'is_subpage' => true, ), $layout); @@ -3176,7 +3217,7 @@ get('/:static/:sub/edit', function ($static, $sub) { not_found(); } - $post = $post[0]; + $post = $post['current']; $page = get_static_sub_post($static, $sub); @@ -3184,7 +3225,7 @@ get('/:static/:sub/edit', function ($static, $sub) { not_found(); } - $page = $page[0]; + $page = $page['current']; render('edit-page', array( 'title' => i18n('Edit') . ': ' . $page->title . ' - ' . blog_title(), @@ -3194,6 +3235,7 @@ get('/:static/:sub/edit', function ($static, $sub) { 'is_admin' => true, 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $post->title . ' » ' . $page->title, 'p' => $page, + 'static' => $page, 'type' => 'subPage', )); } else { @@ -3270,7 +3312,7 @@ get('/:static/:sub/delete', function ($static, $sub) { not_found(); } - $post = $post[0]; + $post = $post['current']; $page = get_static_sub_post($static, $sub); @@ -3278,7 +3320,7 @@ get('/:static/:sub/delete', function ($static, $sub) { not_found(); } - $page = $page[0]; + $page = $page['current']; render('delete-page', array( 'title' => i18n('Delete') . ': ' . $page->title . ' - ' . blog_title(), @@ -3288,6 +3330,7 @@ get('/:static/:sub/delete', function ($static, $sub) { 'is_admin' => true, 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $post->title . ' » ' . $page->title, 'p' => $page, + 'static' => $page, 'type' => 'subPage', )); } else { @@ -3410,6 +3453,7 @@ get('/:year/:month/:name', function ($year, $month, $name) { 'description' => $current->description, 'canonical' => $current->url, 'p' => $current, + 'post' => $current, 'author' => $author, 'bodyclass' => 'in-post category-' . $current->ct . ' type-' . $current->type, 'breadcrumb' => '', @@ -3465,6 +3509,7 @@ get('/:year/:month/:name/edit', function ($year, $month, $name) { 'description' => strip_tags(blog_description()), 'canonical' => site_url(), 'p' => $current, + 'post' => $current, 'type' => $type, 'bodyclass' => 'edit-post', 'is_admin' => true, @@ -3476,6 +3521,7 @@ get('/:year/:month/:name/edit', function ($year, $month, $name) { 'description' => strip_tags(blog_description()), 'canonical' => site_url(), 'p' => $current, + 'post' => $current, 'bodyclass' => 'denied', 'is_admin' => true, 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $current->categoryb . ' » ' . $current->title @@ -3662,6 +3708,7 @@ get('/:year/:month/:name/delete', function ($year, $month, $name) { 'description' => strip_tags(blog_description()), 'canonical' => site_url(), 'p' => $current, + 'post' => $current, 'bodyclass' => 'delete-post', 'is_admin' => true, 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $current->categoryb . ' » ' . $current->title @@ -3672,6 +3719,7 @@ get('/:year/:month/:name/delete', function ($year, $month, $name) { 'description' => strip_tags(blog_description()), 'canonical' => site_url(), 'p' => $current, + 'post' => $current, 'bodyclass' => 'delete-post', 'is_admin' => true, 'breadcrumb' => '' . config('breadcrumb.home') . ' » ' . $current->categoryb . ' » ' . $current->title diff --git a/system/includes/functions.php b/system/includes/functions.php index 329f383..bc606f4 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -37,7 +37,7 @@ function get_static_pages() } // Get static page path. Unsorted. -function get_static_sub_pages($static = null) +function get_static_subpages($static = null) { static $_sub_page = array(); @@ -105,7 +105,7 @@ function get_draft_posts() $_draft[] = pathinfo($file); } } - usort($_draft, "sortfile"); + usort($_draft, "sortfile_a"); } return $_draft; } @@ -165,13 +165,19 @@ function get_gallery() { $_gallery[] = pathinfo($file); } } - usort($_gallery, "sortfile"); + usort($_gallery, "sortfile_d"); } return $_gallery; } // usort function. Sort by filename. -function sortfile($a, $b) +function sortfile_a($a, $b) +{ + return $a['basename'] == $b['basename'] ? 0 : (($a['basename'] > $b['basename']) ? 1 : -1); +} + +// usort function. +function sortfile_d($a, $b) { return $a['basename'] == $b['basename'] ? 0 : (($a['basename'] < $b['basename']) ? 1 : -1); } @@ -196,7 +202,7 @@ function rebuilt_cache($type = null) mkdir($dir, 0775, true); } - // Rebuilt posts index sorted/unsorted + // Rebuilt posts index $tmp = array(); $tmp = glob('content/*/blog/*/*/*.md', GLOB_NOSORT); if (is_array($tmp)) { @@ -206,7 +212,7 @@ function rebuilt_cache($type = null) } } } - usort($posts_cache, "sortfile"); + usort($posts_cache, "sortfile_d"); $string_posts = serialize($posts_cache); file_put_contents('cache/index/index-posts.txt', print_r($string_posts, true)); @@ -218,7 +224,7 @@ function rebuilt_cache($type = null) $page_cache[] = pathinfo($file); } } - usort($page_cache, "sortfile"); + usort($page_cache, "sortfile_a"); $page_string = serialize($page_cache); file_put_contents('cache/index/index-pages.txt', print_r($page_string, true)); @@ -230,7 +236,7 @@ function rebuilt_cache($type = null) $subpage_cache[] = pathinfo($file); } } - usort($subpage_cache, "sortfile"); + usort($subpage_cache, "sortfile_a"); $subpage_string = serialize($subpage_cache); file_put_contents('cache/index/index-sub-pages.txt', print_r($subpage_string, true)); @@ -242,7 +248,7 @@ function rebuilt_cache($type = null) $author_cache[] = pathinfo($file); } } - usort($author_cache, "sortfile"); + usort($author_cache, "sortfile_a"); $author_string = serialize($author_cache); file_put_contents('cache/index/index-author.txt', print_r($author_string, true)); @@ -254,7 +260,7 @@ function rebuilt_cache($type = null) $category_cache[] = pathinfo($file); } } - usort($category_cache, "sortfile"); + usort($category_cache, "sortfile_a"); $category_string = serialize($category_cache); file_put_contents('cache/index/index-category.txt', print_r($category_string, true)); @@ -266,7 +272,7 @@ function rebuilt_cache($type = null) $scheduled[] = pathinfo($file); } } - usort($scheduled, "sortfile"); + usort($scheduled, "sortfile_d"); $scheduled_string = serialize($scheduled); file_put_contents('cache/index/index-scheduled.txt', print_r($scheduled_string, true)); @@ -435,6 +441,106 @@ function get_posts($posts, $page = 1, $perpage = 0) return $tmp; } +function get_pages($pages, $page = 1, $perpage = 0) +{ + if (empty($pages)) { + $pages = get_static_page(); + } + + $tmp = array(); + + // Extract a specific page with results + $pages = array_slice($pages, ($page - 1) * $perpage, $perpage); + + foreach ($pages as $index => $v) { + $post = new stdClass; + + // The static page URL + $url= $v['filename']; + + $post->url = site_url() . $url; + + $post->file = $v['dirname'] . '/' . $v['basename']; + $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($post->file))); + + $post->md = $url; + + // Get the contents and convert it to HTML + $content = file_get_contents($post->file); + + // Extract the title and body + $post->title = get_content_tag('t', $content, 'Untitled static page: ' . format_date($post->lastMod, 'l, j F Y, H:i')); + + // Get the contents and convert it to HTML + $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); + + if (config('views.counter') == 'true') { + $post->views = get_views($post->file); + } + + $post->description = get_content_tag("d", $content, get_description($post->body)); + + $word_count = str_word_count(strip_tags($post->body)); + $post->readTime = ceil($word_count / 200); + + $tmp[] = $post; + } + + return $tmp; + +} + +function get_subpages($sub_pages, $page = 1, $perpage = 0) +{ + if (empty($sub_pages)) { + $sub_pages = get_static_subpages(); + } + + $tmp = array(); + + // Extract a specific page with results + $sub_pages = array_slice($sub_pages, ($page - 1) * $perpage, $perpage); + + foreach ($sub_pages as $index => $v) { + + $post = new stdClass; + + $static = str_replace(dirname($v['dirname']) . '/', '', $v['dirname']); + + // The static page URL + $url= $v['filename']; + $post->url = site_url() . $static . "/" . $url; + + $post->file = $v['dirname'] . '/' . $v['basename']; + $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($post->file))); + + $post->md = $url; + + $post->parent = $static; + + // Get the contents and convert it to HTML + $content = file_get_contents($post->file); + + // Extract the title and body + $post->title = get_content_tag('t', $content, 'Untitled static subpage: ' . format_date($post->lastMod, 'l, j F Y, H:i')); + + // Get the contents and convert it to HTML + $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); + + $post->views = get_views($post->file); + + $post->description = get_content_tag("d", $content, get_description($post->body)); + + $word_count = str_word_count(strip_tags($post->body)); + $post->readTime = ceil($word_count / 200); + + $tmp[] = $post; + } + + return $tmp; + +} + // Find post by year, month and name, previous, and next. function find_post($year, $month, $name) { @@ -653,7 +759,7 @@ function get_category_info($category) } } } - + return $tmp; } @@ -939,17 +1045,14 @@ function default_profile($name) } // Return static page. -function get_static_post($static) +function get_static_post($static = null) { - $posts = get_static_pages(); + $pages = get_static_pages(); - $tmp = array(); - - if (!empty($posts)) { - - foreach ($posts as $index => $v) { - if (stripos($v['basename'], $static . '.md') !== false) { + if (!empty($pages)) { + foreach ($pages as $index => $v) { + if (is_null($static)) { $post = new stdClass; // The static page URL @@ -980,26 +1083,61 @@ function get_static_post($static) $word_count = str_word_count(strip_tags($post->body)); $post->readTime = ceil($word_count / 200); - $tmp[] = $post; + $tmp[] = $post; + + } elseif (stripos($v['basename'], $static . '.md') !== false) { + + // Use the get_posts method to return + // a properly parsed object + + $ar = get_pages($pages, $index + 1, 1); + $nx = get_pages($pages, $index, 1); + $pr = get_pages($pages, $index + 2, 1); + + if ($index == 0) { + if (isset($pr[0])) { + return array( + 'current' => $ar[0], + 'prev' => $pr[0] + ); + } else { + return array( + 'current' => $ar[0], + 'prev' => null + ); + } + } elseif (count($pages) == $index + 1) { + return array( + 'current' => $ar[0], + 'next' => $nx[0] + ); + } else { + return array( + 'current' => $ar[0], + 'next' => $nx[0], + 'prev' => $pr[0] + ); + } } } } - + return $tmp; } // Return static page. -function get_static_sub_post($static, $sub_static) +function get_static_sub_post($static, $sub_static = null) { - $posts = get_static_sub_pages($static); + $sub_pages = get_static_subpages($static); $tmp = array(); - if (!empty($posts)) { - - foreach ($posts as $index => $v) { - if (stripos($v['basename'], $sub_static . '.md') !== false) { + if (!empty($sub_pages)) { + foreach ($sub_pages as $index => $v) { + + if (is_null($sub_static)) { + $post = new stdClass; // The static page URL @@ -1030,6 +1168,40 @@ function get_static_sub_post($static, $sub_static) $post->readTime = ceil($word_count / 200); $tmp[] = $post; + + } elseif (stripos($v['basename'], $sub_static . '.md') !== false) { + + // Use the get_posts method to return + // a properly parsed object + + $ar = get_subpages($sub_pages, $index + 1, 1); + $nx = get_subpages($sub_pages, $index, 1); + $pr = get_subpages($sub_pages, $index + 2, 1); + + if ($index == 0) { + if (isset($pr[0])) { + return array( + 'current' => $ar[0], + 'prev' => $pr[0] + ); + } else { + return array( + 'current' => $ar[0], + 'prev' => null + ); + } + } elseif (count($sub_pages) == $index + 1) { + return array( + 'current' => $ar[0], + 'next' => $nx[0] + ); + } else { + return array( + 'current' => $ar[0], + 'next' => $nx[0], + 'prev' => $pr[0] + ); + } } } } @@ -1704,6 +1876,38 @@ function has_next($next) } } +function static_prev($prev) +{ + if (!empty($prev)) { + return array( + 'url' => $prev->url, + 'title' => $prev->title, + 'body' => $prev->body, + 'description' => $prev->description, + 'views' => $prev->views, + 'file' => $prev->file, + 'readTime' => $prev->readTime, + 'lastMod' => $prev->lastMod + ); + } +} + +function static_next($next) +{ + if (!empty($next)) { + return array( + 'url' => $next->url, + 'title' => $next->title, + 'body' => $next->body, + 'description' => $next->description, + 'views' => $next->views, + 'file' => $next->file, + 'readTime' => $next->readTime, + 'lastMod' => $next->lastMod + ); + } +} + // Helper function to determine whether // to show the pagination buttons function has_pagination($total, $perpage, $page = 1) @@ -2331,7 +2535,7 @@ function get_menu($custom) $active = ''; } - $subPages = get_static_sub_pages($filename); + $subPages = get_static_subpages($filename); if (!empty($subPages)) { asort($subPages); echo '
' . i18n('Title') . '' . i18n('Add_sub') . ' ' . i18n('Edit') . ' ' . i18n('Delete') . '