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

142 lines
3.5 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-03-08 18:02:59 +01:00
$posts = array();
2015-06-27 00:12:26 +02:00
// ============================================================================
// Functions
// ============================================================================
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-07-24 05:28:25 +02:00
global $Site;
2015-03-08 18:02:59 +01:00
2015-08-26 05:42:32 +02:00
// Post object, this get the content from the file.
2015-05-05 03:00:01 +02:00
$Post = new Post($key);
if( !$Post->isValid() ) {
2015-07-05 20:00:34 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the post 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-08-26 05:42:32 +02:00
// Page database, this get the contente from the database json.
2015-05-05 03:00:01 +02:00
$db = $dbPosts->getDb($key);
if( !$db ) {
2015-07-05 20:00:34 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the post 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-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-08-26 05:42:32 +02:00
// Not overwrite the value from file.
$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
2015-07-24 05:28:25 +02:00
$contentRaw = $Post->content();
$Post->setField('contentRaw', $contentRaw, true);
2015-05-05 03:00:01 +02:00
2015-03-08 18:02:59 +01:00
// Parse the 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
$Post->setField('content', $content, true);
2015-07-26 02:25:13 +02:00
// Pagebrake
$explode = explode(PAGE_BREAK, $content);
$Post->setField('breakContent', $explode[0], true);
$Post->setField('readMore', !empty($explode[1]), true);
2015-07-07 03:05:17 +02:00
// Parse username for the post.
2015-05-05 03:00:01 +02:00
if( $dbUsers->userExists( $Post->username() ) )
2015-03-08 18:02:59 +01:00
{
2015-07-15 01:57:18 +02:00
$user = $dbUsers->getDb( $Post->username() );
2015-03-08 18:02:59 +01:00
2015-07-07 03:05:17 +02:00
$Post->setField('authorFirstName', $user['firstName'], false);
2015-08-22 18:33:33 +02:00
2015-07-07 03:05:17 +02:00
$Post->setField('authorLastName', $user['lastName'], false);
2015-03-08 18:02:59 +01:00
}
return $Post;
}
2015-08-30 01:26:46 +02:00
function buildPostForPage($pageNumber=0, $amount=5, $removeUnpublished=false, $tagKey=false)
2015-03-08 18:02:59 +01:00
{
global $dbPosts;
2015-08-30 01:26:46 +02:00
global $dbTags;
2015-03-08 18:02:59 +01:00
global $posts;
2015-05-15 00:07:45 +02:00
global $Url;
2015-03-08 18:02:59 +01:00
2015-08-30 01:26:46 +02:00
if($tagKey) {
$list = $dbTags->getList($pageNumber, $amount, $tagKey);
}
else {
$list = $dbPosts->getList($pageNumber, $amount, $removeUnpublished);
}
2015-05-15 00:07:45 +02:00
2015-08-30 01:26:46 +02:00
// There are not post for the pageNumber then set the page notfound
2015-05-19 01:22:05 +02:00
if(empty($list) && $pageNumber>0) {
2015-05-15 00:07:45 +02:00
$Url->setNotFound(true);
}
2015-03-08 18:02:59 +01:00
2015-08-30 01:26:46 +02:00
foreach($list as $postKey=>$values)
2015-03-08 18:02:59 +01:00
{
2015-08-30 01:26:46 +02:00
$Post = buildPost($postKey);
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);
}
}
}
2015-08-26 05:42:32 +02:00
2015-06-27 00:12:26 +02:00
// ============================================================================
// Main
// ============================================================================
2015-03-08 18:02:59 +01:00
// 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;
}
2015-08-29 20:07:47 +02:00
}
elseif( ($Url->whereAmI()==='tag') && ($Url->notFound()===false) )
{
2015-08-30 01:26:46 +02:00
buildPostForPage($Url->pageNumber(), $Site->postsPerPage(), true, $Url->slug());
2015-03-08 18:02:59 +01:00
}
// Build post per page
else
{
2015-05-05 03:00:01 +02:00
if($Url->whereAmI()==='admin') {
2015-08-30 01:26:46 +02:00
// Build post for admin area with drafts+schedulers
buildPostForPage($Url->pageNumber(), POSTS_PER_PAGE_ADMIN, true);
2015-05-05 03:00:01 +02:00
}
else
{
2015-08-30 01:26:46 +02:00
// Build post for the site, without the drafts and scheduleres posts
buildPostForPage($Url->pageNumber(), $Site->postsPerPage(), false);
2015-05-05 03:00:01 +02:00
}
2015-03-08 18:02:59 +01:00
}