Browse Source

Add page navigation for static pages

pull/663/merge
danpros 1 year ago
parent
commit
4b70fab511
4 changed files with 299 additions and 49 deletions
  1. +4
    -4
      system/admin/views/clear-cache.html.php
  2. +1
    -3
      system/admin/views/static-pages.html.php
  3. +61
    -13
      system/htmly.php
  4. +233
    -29
      system/includes/functions.php

+ 4
- 4
system/admin/views/clear-cache.html.php View File

@ -1,16 +1,16 @@
<?php if (!defined('HTMLY')) die('HTMLy'); ?>
<?php
foreach (glob('cache/page/*.cache', GLOB_NOSORT) as $file) {
unlink($file);
}
foreach (glob('cache/index/*.txt', GLOB_NOSORT) as $file) {
unlink($file);
}
rebuilt_cache('all');
foreach (glob('cache/page/*.cache', GLOB_NOSORT) as $file) {
unlink($file);
}
echo i18n('All_cache_has_been_deleted');
?>

+ 1
- 3
system/admin/views/static-pages.html.php View File

@ -8,7 +8,6 @@
if (isset($_SESSION[config("site.url")]['user'])) {
$posts = get_static_post(null);
if (!empty($posts)) {
krsort($posts);
echo '<table class="table post-list">';
echo '<tr class="head"><th>' . i18n('Title') . '</th>';
if (config("views.counter") == "true")
@ -33,8 +32,7 @@ if (isset($_SESSION[config("site.url")]['user'])) {
echo '<td><a class="btn btn-primary btn-xs" href="' . $p->url . '/add?destination=admin/pages">' . i18n('Add_sub') . '</a> <a class="btn btn-primary btn-xs" href="' . $p->url . '/edit?destination=admin/pages">' . i18n('Edit') . '</a> <a class="btn btn-danger btn-xs" href="' . $p->url . '/delete?destination=admin/pages">' . i18n('Delete') . '</a></td>';
echo '</tr>';
$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 '<tr class="' . $class . '">';


+ 61
- 13
system/htmly.php View File

@ -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' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; ' . 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' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; ' . 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' => '<style>.breadcrumb-list {margin:0; padding:0;} .breadcrumb-list li {display: inline-block; list-style: none;}</style><ol class="breadcrumb-list" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="breadcrumb-item" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a itemprop="item" href="' . site_url() . '"><span itemprop="name">' . config('breadcrumb.home') . '</span></a><meta itemprop="position" content="1" /></li> &#187; '. $blog . '<li class="breadcrumb-item" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">' . $current->categoryb . '<meta itemprop="position" content="3" /></li>' . ' &#187; ' . $current->title . '</ol>',
@ -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' => '<span><a href="' . site_url() . '">' . config('breadcrumb.home') . '</a></span> &#187; ' . $current->categoryb . ' &#187; ' . $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' => '<span><a href="' . site_url() . '">' . config('breadcrumb.home') . '</a></span> &#187; ' . $current->categoryb . ' &#187; ' . $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' => '<span><a href="' . site_url() . '">' . config('breadcrumb.home') . '</a></span> &#187; ' . $current->categoryb . ' &#187; ' . $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' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; ' . $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' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; ' . $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' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; ' . 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' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; <a href="' . $parent_post[0]->url . '">' . $parent_post[0]->title . '</a> &#187; ' . $post->title,
'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; <a href="' . $parent_post['current']->url . '">' . $parent_post['current']->title . '</a> &#187; ' . $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' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; <a href="' . $post->url . '">' . $post->title . '</a> &#187; ' . $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' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; <a href="' . $post->url . '">' . $post->title . '</a> &#187; ' . $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' => '<style>.breadcrumb-list {margin:0; padding:0;} .breadcrumb-list li {display: inline-block; list-style: none;}</style><ol class="breadcrumb-list" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="breadcrumb-item" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a itemprop="item" href="' . site_url() . '"><span itemprop="name">' . config('breadcrumb.home') . '</span></a><meta itemprop="position" content="1" /></li> &#187; '. $blog . '<li class="breadcrumb-item" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">' . $current->categoryb . '<meta itemprop="position" content="3" /></li>' . ' &#187; ' . $current->title . '</ol>',
@ -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' => '<span><a href="' . site_url() . '">' . config('breadcrumb.home') . '</a></span> &#187; ' . $current->categoryb . ' &#187; ' . $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' => '<span><a rel="v:url" href="' . site_url() . '">' . config('breadcrumb.home') . '</a></span> &#187; ' . $current->categoryb . ' &#187; ' . $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' => '<span><a href="' . site_url() . '">' . config('breadcrumb.home') . '</a></span> &#187; ' . $current->categoryb . ' &#187; ' . $current->title


+ 233
- 29
system/includes/functions.php View File

@ -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 '<li class="' . $class . $active .' dropdown">';


Loading…
Cancel
Save