2015-03-08 18:02:59 +01:00
|
|
|
<?php defined('BLUDIT') or die('Bludit CMS.');
|
|
|
|
|
|
|
|
$posts = array();
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
function buildPost($key)
|
2015-03-08 18:02:59 +01:00
|
|
|
{
|
|
|
|
global $dbPosts;
|
|
|
|
global $dbUsers;
|
|
|
|
global $Parsedown;
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
// Post object.
|
|
|
|
$Post = new Post($key);
|
|
|
|
if( !$Post->isValid() ) {
|
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-05-05 03:00:01 +02:00
|
|
|
// Page database.
|
|
|
|
$db = $dbPosts->getDb($key);
|
|
|
|
if( !$db ) {
|
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-05-05 03:00:01 +02:00
|
|
|
// Foreach field from database.
|
|
|
|
foreach($db as $field=>$value)
|
2015-03-08 18:02:59 +01:00
|
|
|
{
|
2015-05-05 03:00:01 +02:00
|
|
|
if($field=='unixTimeCreated')
|
2015-03-08 18:02:59 +01:00
|
|
|
{
|
2015-05-05 03:00:01 +02:00
|
|
|
// 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);
|
2015-03-08 18:02:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-05 03:00:01 +02:00
|
|
|
// Other fields, not overwrite from file fields.
|
|
|
|
$Post->setField($field, $value, false);
|
2015-03-08 18:02:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
// Content in raw format
|
|
|
|
$Post->setField('contentRaw', $Post->content(), true);
|
|
|
|
|
2015-03-08 18:02:59 +01:00
|
|
|
// Parse the content
|
|
|
|
$content = $Parsedown->text( $Post->content() );
|
|
|
|
$Post->setField('content', $content, true);
|
|
|
|
|
|
|
|
// User / Author
|
2015-05-05 03:00:01 +02:00
|
|
|
if( $dbUsers->userExists( $Post->username() ) )
|
2015-03-08 18:02:59 +01:00
|
|
|
{
|
|
|
|
$user = $dbUsers->get( $Post->username() );
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
$Post->setField('author', $user['firstName'].', '.$user['lastName'], false);
|
2015-03-08 18:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $Post;
|
|
|
|
}
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
function build_posts_per_page($draftPosts=false)
|
2015-03-08 18:02:59 +01:00
|
|
|
{
|
|
|
|
global $dbPosts;
|
|
|
|
global $posts;
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
$list = $dbPosts->getPage(0, 5, $draftPosts);
|
2015-03-08 18:02:59 +01:00
|
|
|
|
|
|
|
foreach($list as $slug=>$db)
|
|
|
|
{
|
2015-05-05 03:00:01 +02:00
|
|
|
$Post = buildPost($slug);
|
2015-03-08 18:02:59 +01:00
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
if($Post!==false) {
|
2015-03-08 18:02:59 +01:00
|
|
|
array_push($posts, $Post);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by post, then build it
|
|
|
|
if( ($Url->whereAmI()==='post') && ($Url->notFound()===false) )
|
|
|
|
{
|
2015-05-05 03:00:01 +02:00
|
|
|
$Post = buildPost( $Url->slug() );
|
2015-03-08 18:02:59 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2015-05-05 03:00:01 +02:00
|
|
|
if($Url->whereAmI()==='admin') {
|
|
|
|
// Build post for admin area with drafts
|
|
|
|
build_posts_per_page(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
build_posts_per_page();
|
|
|
|
}
|
2015-03-08 18:02:59 +01:00
|
|
|
}
|