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

176 lines
4.3 KiB
PHP
Raw Normal View History

2015-03-08 14:02:59 -03:00
<?php defined('BLUDIT') or die('Bludit CMS.');
2015-06-26 19:12:26 -03:00
// ============================================================================
// Variables
// ============================================================================
2015-03-08 14:02:59 -03:00
$posts = array();
2015-06-26 19:12:26 -03:00
// ============================================================================
// Functions
// ============================================================================
2015-08-30 22:18:06 -03:00
function reIndexTagsPosts()
{
global $dbPosts;
global $dbTags;
2015-09-01 21:42:21 -03:00
// Remove unpublished.
$dbPosts->removeUnpublished();
2015-08-30 22:18:06 -03:00
// Regenerate the tags index for posts
$dbTags->reindexPosts( $dbPosts->db );
// Restore de db on dbPost
$dbPosts->restoreDB();
2015-08-30 22:18:06 -03:00
return true;
}
2015-05-05 01:00:01 +00:00
function buildPost($key)
2015-03-08 14:02:59 -03:00
{
global $dbPosts;
global $dbUsers;
global $Parsedown;
2015-07-24 00:28:25 -03:00
global $Site;
2015-03-08 14:02:59 -03:00
2015-09-20 18:46:50 -03:00
// Post object, content from FILE.
2015-05-05 01:00:01 +00:00
$Post = new Post($key);
if( !$Post->isValid() ) {
2015-07-05 15:00:34 -03:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the post from file with key: '.$key);
2015-03-08 14:02:59 -03:00
return false;
2015-05-05 01:00:01 +00:00
}
2015-03-08 14:02:59 -03:00
2015-09-20 18:46:50 -03:00
// Post database, content from DATABASE JSON.
2015-05-05 01:00:01 +00:00
$db = $dbPosts->getDb($key);
if( !$db ) {
2015-07-05 15:00:34 -03:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the post from database with key: '.$key);
2015-03-08 14:02:59 -03:00
return false;
2015-05-05 01:00:01 +00:00
}
2015-03-08 14:02:59 -03:00
2015-09-20 18:46:50 -03:00
// Foreach field from DATABASE.
foreach($db as $field=>$value) {
$Post->setField($field, $value);
2015-03-08 14:02:59 -03:00
}
2015-05-05 01:00:01 +00:00
// Content in raw format
2015-07-24 00:28:25 -03:00
$contentRaw = $Post->content();
$Post->setField('contentRaw', $contentRaw, true);
2015-05-05 01:00:01 +00:00
2015-03-08 14:02:59 -03:00
// Parse the content
2015-08-22 13:33:33 -03:00
$content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities
$content = $Parsedown->text($content); // Parse Markdown.
2015-07-25 21:25:13 -03:00
$content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute.
2015-03-08 14:02:59 -03:00
$Post->setField('content', $content, true);
2015-07-25 21:25:13 -03:00
// Pagebrake
$explode = explode(PAGE_BREAK, $content);
$Post->setField('breakContent', $explode[0], true);
$Post->setField('readMore', !empty($explode[1]), true);
// Date format
$postDate = $Post->date();
$Post->setField('dateRaw', $postDate, true);
$postDateFormated = $Post->dateRaw( $Site->dateFormat() );
$Post->setField('date', $postDateFormated, true);
2015-07-06 22:05:17 -03:00
// Parse username for the post.
2015-05-05 01:00:01 +00:00
if( $dbUsers->userExists( $Post->username() ) )
2015-03-08 14:02:59 -03:00
{
2015-07-14 20:57:18 -03:00
$user = $dbUsers->getDb( $Post->username() );
2015-03-08 14:02:59 -03:00
2015-07-06 22:05:17 -03:00
$Post->setField('authorFirstName', $user['firstName'], false);
$Post->setField('authorLastName', $user['lastName'], false);
2015-03-08 14:02:59 -03:00
}
return $Post;
}
2015-08-30 22:18:06 -03:00
function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeUnpublished=true, $tagKey=false)
2015-03-08 14:02:59 -03:00
{
global $dbPosts;
2015-08-29 20:26:46 -03:00
global $dbTags;
2015-03-08 14:02:59 -03:00
global $posts;
2015-05-14 22:07:45 +00:00
global $Url;
2015-03-08 14:02:59 -03:00
2015-08-29 20:26:46 -03:00
if($tagKey) {
2015-08-30 22:18:06 -03:00
// Get the keys list from tags database, this database is optimized for this case.
2015-08-29 20:26:46 -03:00
$list = $dbTags->getList($pageNumber, $amount, $tagKey);
}
else {
2015-08-30 22:18:06 -03:00
// Get the keys list from posts database.
2015-08-29 20:26:46 -03:00
$list = $dbPosts->getList($pageNumber, $amount, $removeUnpublished);
}
2015-05-14 22:07:45 +00:00
2015-08-30 22:18:06 -03:00
// There are not posts for the page number then set the page notfound
2015-09-07 21:51:48 -03:00
if(empty($list) && $pageNumber>0) {
2015-05-14 22:07:45 +00:00
$Url->setNotFound(true);
}
2015-03-08 14:02:59 -03:00
2015-08-30 22:18:06 -03:00
// Foreach post key, build the post.
2015-08-29 20:26:46 -03:00
foreach($list as $postKey=>$values)
2015-03-08 14:02:59 -03:00
{
2015-08-29 20:26:46 -03:00
$Post = buildPost($postKey);
2015-05-05 01:00:01 +00:00
if($Post!==false) {
2015-03-08 14:02:59 -03:00
array_push($posts, $Post);
}
}
}
2015-06-26 19:12:26 -03:00
// ============================================================================
// Main
// ============================================================================
2015-09-09 23:33:31 -03:00
// Search for changes on posts by the user.
if( $Site->cliMode() ) {
2015-09-20 18:46:50 -03:00
if($dbPosts->regenerateCli()) {
reIndexTagsPosts();
}
2015-09-09 23:33:31 -03:00
}
2015-09-01 21:42:21 -03:00
// Execute the scheduler.
if( $dbPosts->scheduler() ) {
// Reindex dbTags.
reIndexTagsPosts();
}
2015-08-30 22:18:06 -03:00
// Build specific post.
2015-03-08 14:02:59 -03:00
if( ($Url->whereAmI()==='post') && ($Url->notFound()===false) )
{
2015-05-05 01:00:01 +00:00
$Post = buildPost( $Url->slug() );
2015-03-08 14:02:59 -03:00
if($Post===false)
{
$Url->setNotFound(true);
unset($Post);
}
elseif( !$Post->published() )
{
$Url->setNotFound(true);
unset($Post);
}
else
{
$posts[0] = $Post;
}
2015-08-29 15:07:47 -03:00
}
2015-08-30 22:18:06 -03:00
// Build posts by specific tag.
2015-08-29 15:07:47 -03:00
elseif( ($Url->whereAmI()==='tag') && ($Url->notFound()===false) )
{
2015-08-30 22:18:06 -03:00
buildPostsForPage($Url->pageNumber(), $Site->postsPerPage(), true, $Url->slug());
2015-03-08 14:02:59 -03:00
}
2015-08-30 22:18:06 -03:00
// Build posts for homepage or admin area.
2015-03-08 14:02:59 -03:00
else
{
2015-08-30 22:18:06 -03:00
// Posts for admin area.
2015-05-05 01:00:01 +00:00
if($Url->whereAmI()==='admin') {
2015-08-30 22:18:06 -03:00
buildPostsForPage($Url->pageNumber(), POSTS_PER_PAGE_ADMIN, false);
2015-05-05 01:00:01 +00:00
}
2015-08-30 22:18:06 -03:00
// Posts for homepage
else {
buildPostsForPage($Url->pageNumber(), $Site->postsPerPage(), true);
2015-05-05 01:00:01 +00:00
}
}