bludit/kernel/boot/rules/70.build_posts.php
2015-05-05 01:00:01 +00:00

108 lines
1.9 KiB
PHP

<?php defined('BLUDIT') or die('Bludit CMS.');
$posts = array();
function buildPost($key)
{
global $dbPosts;
global $dbUsers;
global $Parsedown;
// Post object.
$Post = new Post($key);
if( !$Post->isValid() ) {
return false;
}
// Page database.
$db = $dbPosts->getDb($key);
if( !$db ) {
return false;
}
// Foreach field from database.
foreach($db as $field=>$value)
{
if($field=='unixTimeCreated')
{
// Format dates, not overwrite from file fields.
$Post->setField('unixTimeCreated', $value, false);
$Post->setField('date', Date::format($value, '%d %B'), false);
$Post->setField('timeago', Date::timeago($value), false);
}
else
{
// Other fields, not overwrite from file fields.
$Post->setField($field, $value, false);
}
}
// Content in raw format
$Post->setField('contentRaw', $Post->content(), true);
// Parse the content
$content = $Parsedown->text( $Post->content() );
$Post->setField('content', $content, true);
// User / Author
if( $dbUsers->userExists( $Post->username() ) )
{
$user = $dbUsers->get( $Post->username() );
$Post->setField('author', $user['firstName'].', '.$user['lastName'], false);
}
return $Post;
}
function build_posts_per_page($draftPosts=false)
{
global $dbPosts;
global $posts;
$list = $dbPosts->getPage(0, 5, $draftPosts);
foreach($list as $slug=>$db)
{
$Post = buildPost($slug);
if($Post!==false) {
array_push($posts, $Post);
}
}
}
// Filter by post, then build it
if( ($Url->whereAmI()==='post') && ($Url->notFound()===false) )
{
$Post = buildPost( $Url->slug() );
if($Post===false)
{
$Url->setNotFound(true);
unset($Post);
}
elseif( !$Post->published() )
{
$Url->setNotFound(true);
unset($Post);
}
else
{
$posts[0] = $Post;
}
}
// Build post per page
else
{
if($Url->whereAmI()==='admin') {
// Build post for admin area with drafts
build_posts_per_page(true);
}
else
{
build_posts_per_page();
}
}