bludit/kernel/boot/rules/70.pages.php

189 lines
4.3 KiB
PHP
Raw Normal View History

2015-03-08 18:02:59 +01:00
<?php defined('BLUDIT') or die('Bludit CMS.');
2015-06-27 00:12:26 +02:00
// ============================================================================
// Variables
// ============================================================================
2015-10-31 23:54:42 +01:00
// Array with all pages.
2015-03-08 18:02:59 +01:00
$pages = array();
2015-10-31 23:54:42 +01:00
// Array with all pages, order by parent.
2015-05-05 03:00:01 +02:00
$pagesParents = array(NO_PARENT_CHAR=>array());
2015-03-08 18:02:59 +01:00
2015-06-27 00:12:26 +02:00
// ============================================================================
// Functions
// ============================================================================
2015-11-21 16:11:35 +01:00
function sortPages($a, $b)
2015-05-05 03:00:01 +02:00
{
if ($a->position() == $b->position()) {
return 0;
}
return ($a->position() < $b->position()) ? -1 : 1;
}
function build_page($key)
2015-03-08 18:02:59 +01:00
{
global $dbPages;
global $dbUsers;
global $Parsedown;
2015-11-27 17:03:42 +01:00
global $Site;
2015-03-08 18:02:59 +01:00
2015-09-20 23:46:50 +02:00
// Page object, content from FILE.
2015-05-05 03:00:01 +02:00
$Page = new Page($key);
if( !$Page->isValid() ) {
2015-10-31 23:54:42 +01:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from file with key: '.$key);
2015-03-08 18:02:59 +01:00
return false;
2015-05-05 03:00:01 +02:00
}
2015-03-08 18:02:59 +01:00
2015-09-20 23:46:50 +02:00
// Page database, content from DATABASE JSON.
2015-05-05 03:00:01 +02:00
$db = $dbPages->getDb($key);
if( !$db ) {
2015-10-31 23:54:42 +01:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from database with key: '.$key);
2015-03-08 18:02:59 +01:00
return false;
2015-05-05 03:00:01 +02:00
}
2015-03-08 18:02:59 +01:00
2015-09-20 23:46:50 +02:00
// Foreach field from DATABASE.
foreach($db as $field=>$value) {
$Page->setField($field, $value);
2015-03-08 18:02:59 +01:00
}
2015-05-05 03:00:01 +02:00
// Content in raw format
2015-07-24 05:28:25 +02:00
$contentRaw = $Page->content();
2015-05-05 03:00:01 +02:00
$Page->setField('contentRaw', $Page->content(), true);
// Parse markdown content.
2015-08-22 18:33:33 +02:00
$content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities
$content = $Parsedown->text($content); // Parse Markdown.
2015-07-26 02:25:13 +02:00
$content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute.
2015-03-08 18:02:59 +01:00
$Page->setField('content', $content, true);
2015-11-27 17:03:42 +01:00
// Date format
$pageDate = $Page->date();
$Page->setField('dateRaw', $pageDate, true);
$pageDateFormated = $Page->dateRaw( $Site->dateFormat() );
$Page->setField('date', $pageDateFormated, true);
2015-05-05 03:00:01 +02:00
// Parse username for the page.
if( $dbUsers->userExists( $Page->username() ) )
2015-03-08 18:02:59 +01:00
{
2015-07-15 01:57:18 +02:00
$user = $dbUsers->getDb( $Page->username() );
2015-07-07 03:05:17 +02:00
$Page->setField('authorFirstName', $user['firstName'], false);
$Page->setField('authorLastName', $user['lastName'], false);
2015-03-08 18:02:59 +01:00
}
return $Page;
}
function build_all_pages()
{
global $pages;
2015-05-05 03:00:01 +02:00
global $pagesParents;
2015-03-08 18:02:59 +01:00
global $dbPages;
$list = $dbPages->getAll();
unset($list['error']);
2015-05-05 03:00:01 +02:00
foreach($list as $key=>$db)
2015-03-08 18:02:59 +01:00
{
2015-05-05 03:00:01 +02:00
$Page = build_page($key);
2015-03-08 18:02:59 +01:00
if($Page!==false)
{
2015-10-31 23:54:42 +01:00
// --- Order pages by parents ---
2015-05-05 03:00:01 +02:00
// Generate all posible parents.
if( $Page->parentKey()===false )
{
2015-10-31 23:54:42 +01:00
// Add the parent key in the dbPages
2015-05-05 03:00:01 +02:00
$dbPages->addParentKey($Page->key());
$pagesParents[NO_PARENT_CHAR][$Page->key()] = $Page;
}
else
{
$pagesParents[$Page->parentKey()][$Page->key()] = $Page;
}
2015-10-31 23:54:42 +01:00
// --- All pages in 1 array ---
2015-05-05 03:00:01 +02:00
$pages[$Page->key()] = $Page;
2015-03-08 18:02:59 +01:00
}
}
2015-05-05 03:00:01 +02:00
2015-11-21 16:11:35 +01:00
// --- SORT PAGES ---
2015-05-05 03:00:01 +02:00
2015-11-21 16:11:35 +01:00
// Sort parents.
$parents = $pagesParents[NO_PARENT_CHAR];
uasort($parents, 'sortPages');
2015-05-05 03:00:01 +02:00
2015-11-21 16:11:35 +01:00
// Sort children.
unset($pagesParents[NO_PARENT_CHAR]);
$children = $pagesParents;
2015-10-31 23:54:42 +01:00
$tmpPageWithParent = array();
2015-11-21 16:11:35 +01:00
foreach($children as $parentKey=>$childrenPages)
2015-05-05 03:00:01 +02:00
{
2015-11-21 16:11:35 +01:00
// If the child doesn't have a valid parent, then doesn't included them.
if(isset($pages[$parentKey]))
{
$tmpPageWithParent[$parentKey] = $childrenPages;
uasort($tmpPageWithParent[$parentKey], 'sortPages');
}
2015-05-05 03:00:01 +02:00
}
2015-11-21 16:11:35 +01:00
$pagesParents = array(NO_PARENT_CHAR=>$parents) + $tmpPageWithParent;
2015-03-08 18:02:59 +01:00
}
2015-06-27 00:12:26 +02:00
// ============================================================================
// Main
// ============================================================================
2015-09-10 04:33:31 +02:00
// Search for changes on pages by the user.
if( $Site->cliMode() ) {
$dbPages->regenerateCli();
}
2015-03-08 18:02:59 +01:00
// Filter by page, then build it
if( ($Url->whereAmI()==='page') && ($Url->notFound()===false) )
{
$Page = build_page( $Url->slug() );
if($Page===false)
{
$Url->setNotFound(true);
unset($Page);
}
elseif( !$Page->published() )
{
$Url->setNotFound(true);
unset($Page);
}
}
// Default homepage
if($Url->notFound()===false)
{
2015-08-14 03:36:03 +02:00
if( Text::isNotEmpty($Site->homepage()) && ($Url->whereAmI()==='home') )
2015-03-08 18:02:59 +01:00
{
$Url->setWhereAmI('page');
$Page = build_page( $Site->homepage() );
2015-05-05 03:00:01 +02:00
2015-08-14 03:36:03 +02:00
if($Page===false) {
2015-03-08 18:02:59 +01:00
$Url->setWhereAmI('home');
}
}
}
2015-06-03 03:17:09 +02:00
if($Url->notFound())
{
$Url->setWhereAmI('page');
$Page = new Page('error');
}
2015-03-08 18:02:59 +01:00
// Build all pages
2015-06-27 00:12:26 +02:00
build_all_pages();