bludit/bl-kernel/functions.php

151 lines
3.4 KiB
PHP
Raw Normal View History

2016-05-29 19:21:11 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
2017-05-09 00:24:15 +02:00
function buildPage($key)
{
global $dbPages;
global $dbUsers;
global $Parsedown;
global $Site;
2017-05-10 20:40:28 +02:00
// Page object, content from index.txt file
$page = new Page($key);
if( !$page->isValid() ) {
2017-05-09 00:24:15 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from file with key: '.$key);
return false;
}
2017-05-10 20:40:28 +02:00
// Get the database from dbPages
2017-05-09 00:24:15 +02:00
$db = $dbPages->getPageDB($key);
if( !$db ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from database with key: '.$key);
return false;
}
2017-05-10 20:40:28 +02:00
// Foreach field from database set on the object
2017-05-09 00:24:15 +02:00
foreach($db as $field=>$value) {
2017-05-10 20:40:28 +02:00
$page->setField($field, $value);
2017-05-09 00:24:15 +02:00
}
2017-05-10 20:40:28 +02:00
// Parse Markdown
$contentRaw = $page->contentRaw();
2017-05-09 00:24:15 +02:00
$content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities
2017-05-10 20:40:28 +02:00
$content = $Parsedown->text($content); // Parse Markdown
2017-05-09 00:24:15 +02:00
$content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute.
2017-05-10 20:40:28 +02:00
$page->setField('content', $content, true);
2017-05-09 00:24:15 +02:00
// Pagebrake
$explode = explode(PAGE_BREAK, $content);
2017-05-10 20:40:28 +02:00
$page->setField('contentBreak', $explode[0], true);
$page->setField('readMore', !empty($explode[1]), true);
2017-05-09 00:24:15 +02:00
// Date format
2017-05-10 20:40:28 +02:00
$pageDate = $page->date();
$page->setField('dateRaw', $pageDate, true);
2017-05-09 00:24:15 +02:00
2017-05-10 20:40:28 +02:00
$pageDateFormated = $page->dateRaw( $Site->dateFormat() );
$page->setField('date', $pageDateFormated, true);
2017-05-09 00:24:15 +02:00
2017-05-10 20:40:28 +02:00
// Generate and set the User object
$username = $page->username();
$page->setField('user', $dbUsers->getUser($username));
2017-05-09 00:24:15 +02:00
2017-05-10 20:40:28 +02:00
return $page;
2017-05-09 00:24:15 +02:00
}
2017-05-10 20:40:28 +02:00
function reindexCategories()
2016-05-29 19:21:11 +02:00
{
2017-05-10 20:40:28 +02:00
global $dbPages;
global $dbCategories;
2016-05-29 19:21:11 +02:00
2017-05-10 20:40:28 +02:00
// Get a database with published pages
$db = $dbPages->getPublishedDB();
2016-05-29 19:21:11 +02:00
2017-05-10 20:40:28 +02:00
// Regenerate the tags
$dbCategories->reindex($db);
2016-05-29 19:21:11 +02:00
return true;
}
2017-05-10 20:40:28 +02:00
function reindexTags()
2017-03-26 20:51:32 +02:00
{
2017-05-10 20:40:28 +02:00
global $dbPages;
2017-03-26 20:51:32 +02:00
global $dbCategories;
2017-05-10 20:40:28 +02:00
// Get a database with published pages
$db = $dbPages->getPublishedDB();
2017-03-26 20:51:32 +02:00
2017-05-10 20:40:28 +02:00
// Regenerate the tags
$dbTags->reindex($db);
2017-03-26 20:51:32 +02:00
return true;
}
2017-05-10 23:21:45 +02:00
function buildPagesForAdmin()
{
return buildPagesFor('admin');
}
function buildPagesForHome()
2016-05-29 19:21:11 +02:00
{
2017-05-10 23:21:45 +02:00
return buildPagesFor('home');
2017-05-10 20:40:28 +02:00
}
2017-05-12 20:18:44 +02:00
function buildPagesByCategory()
2017-05-10 20:40:28 +02:00
{
2017-05-12 20:18:44 +02:00
global $Url;
$categoryKey = $Url->slug();
2017-05-10 23:21:45 +02:00
return buildPagesFor('category', $categoryKey, false);
2017-05-10 20:40:28 +02:00
}
2017-05-12 20:18:44 +02:00
function buildPagesByTag()
2017-05-10 23:21:45 +02:00
{
2017-05-12 20:18:44 +02:00
global $Url;
$tagKey = $Url->slug();
2017-05-10 23:21:45 +02:00
return buildPagesFor('tag', false, $tagKey);
}
function buildPagesFor($for, $categoryKey=false, $tagKey=false)
2017-05-10 20:40:28 +02:00
{
global $dbPages;
2017-05-10 23:21:45 +02:00
global $dbCategories;
2016-05-29 19:21:11 +02:00
global $Site;
2017-05-10 23:21:45 +02:00
// Get the page number from URL
$pageNumber = $Url->pageNumber();
2017-05-10 20:40:28 +02:00
if($for=='admin') {
2017-05-10 23:21:45 +02:00
$onlyPublished = false;
$amountOfItems = ITEMS_PER_PAGE_ADMIN;
$list = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);
2016-05-29 19:21:11 +02:00
}
2017-05-10 20:40:28 +02:00
elseif($for=='home') {
2017-05-10 23:21:45 +02:00
$onlyPublished = true;
$amountOfItems = $Site->postsPerPage();
$list = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);
}
elseif($for=='category') {
$amountOfItems = $Site->postsPerPage();
$list = $dbCategories->getList($categoryKey, $pageNumber, $amountOfItems);
}
elseif($for=='tag') {
$amountOfItems = $Site->postsPerPage();
$list = $dbTags->getList($tagKey, $pageNumber, $amountOfItems);
2016-05-29 19:21:11 +02:00
}
2017-05-10 20:40:28 +02:00
// There are not items for the page number then set the page notfound
if( empty($list) && $pageNumber>0 ) {
$Url->setNotFound(true);
2016-05-29 19:21:11 +02:00
}
2017-05-10 20:40:28 +02:00
$pages = array();
foreach($list as $pageKey=>$fields) {
$page = buildPage($pageKey);
if($page!==false) {
array_push($pages, $page);
}
}
return $pages;
}