bludit/bl-kernel/functions.php

264 lines
5.9 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-10 23:21:45 +02:00
function buildPagesByCategory($categoryKey)
2017-05-10 20:40:28 +02:00
{
2017-05-10 23:21:45 +02:00
return buildPagesFor('category', $categoryKey, false);
2017-05-10 20:40:28 +02:00
}
2017-05-10 23:21:45 +02:00
function buildPagesByTag($tagKey)
{
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;
}
2016-05-29 19:21:11 +02:00
2017-05-10 20:40:28 +02:00
// ---- OLD
2016-05-29 19:21:11 +02:00
function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeUnpublished=true, $key=false, $type='tag')
2016-05-29 19:21:11 +02:00
{
global $dbPosts;
global $dbTags;
2017-05-04 21:32:18 +02:00
global $dbCategories;
2016-05-29 19:21:11 +02:00
global $Url;
$posts = array();
2017-05-04 21:32:18 +02:00
if( $type=='tag' && $key ) {
2016-05-29 19:21:11 +02:00
// Get the keys list from tags database, this database is optimized for this case.
2017-05-03 21:10:03 +02:00
$list = $dbTags->getList($pageNumber, $amount, $key);
}
2017-05-04 21:32:18 +02:00
elseif( $type=='category' && $key ) {
$list = $dbCategories->getListOfPosts($pageNumber, $amount, $key);
2016-05-29 19:21:11 +02:00
}
else {
// Get the keys list from posts database.
$list = $dbPosts->getList($pageNumber, $amount, $removeUnpublished);
}
// There are not posts for the page number then set the page notfound
if(empty($list) && $pageNumber>0) {
$Url->setNotFound(true);
}
// Foreach post key, build the post.
foreach($list as $postKey=>$values)
{
$Post = buildPost($postKey);
if($Post!==false) {
array_push($posts, $Post);
}
}
return $posts;
}
// PAGE FUNCTIONS
// ----------------------------------------------------------------------------
function sortPages($a, $b)
{
if ($a['position'] == $b['position']) {
return 0;
}
return ($a['position'] < $b['position']) ? -1 : 1;
}
function buildAllPages()
{
global $pagesParents;
global $pagesParentsPublished;
global $pagesPublished;
global $dbPages;
2016-09-21 04:17:45 +02:00
global $parents;
2016-05-29 19:21:11 +02:00
// Get the page list
$list = $dbPages->getDB();
// Clean pages array.
$pages = array();
// Remove the error page
unset($list['error']);
// Sorte pages
uasort($list, 'sortPages');
foreach($list as $key=>$db)
{
$Page = buildPage($key);
if($Page!==false)
{
// Filter pages, with and without parent
// If the page doesn't have a father, it's a parent page :P
if( $Page->parentKey()===false ) {
// Add the parent key in the dbPages
$dbPages->addParentKey($Page->key());
// Add the page as a parent page in the array
$pagesParents[NO_PARENT_CHAR][$Page->key()] = $Page;
// If the page is published
if($Page->published()) {
$pagesParentsPublished[NO_PARENT_CHAR][$Page->key()] = $Page;
}
}
else {
$pagesParents[$Page->parentKey()][$Page->key()] = $Page;
// If the page is published
if($Page->published()) {
$pagesParentsPublished[$Page->parentKey()][$Page->key()] = $Page;
}
}
// All pages in one array
$pages[$Page->key()] = $Page;
// If the page is published
if($Page->published()) {
$pagesPublished[$Page->parentKey()][$Page->key()] = $Page;
}
}
}
2016-10-06 15:18:38 +02:00
if( isset($pagesParentsPublished[NO_PARENT_CHAR]) ) {
$parents = $pagesParentsPublished[NO_PARENT_CHAR];
}
2016-09-21 04:17:45 +02:00
2016-05-29 19:21:11 +02:00
return $pages;
}