Improvements on tags

This commit is contained in:
dignajar 2015-09-01 21:42:21 -03:00
parent 0c70da532b
commit 83ec140297
5 changed files with 81 additions and 26 deletions

View File

@ -61,17 +61,20 @@ class dbJSON
// Save the JSON file. // Save the JSON file.
public function save() public function save()
{ {
$data = '';
if($this->firstLine) { if($this->firstLine) {
$data = "<?php defined('BLUDIT') or die('Bludit CMS.'); ?>".PHP_EOL; $data = "<?php defined('BLUDIT') or die('Bludit CMS.'); ?>".PHP_EOL;
} }
else {
$data = '';
}
// Serialize database
$data .= $this->serialize($this->db); $data .= $this->serialize($this->db);
// Backup the new database.
$this->dbBackup = $this->db;
// LOCK_EX flag to prevent anyone else writing to the file at the same time. // LOCK_EX flag to prevent anyone else writing to the file at the same time.
file_put_contents($this->file, $data, LOCK_EX); return file_put_contents($this->file, $data, LOCK_EX);
} }
private function serialize($data) private function serialize($data)

View File

@ -15,11 +15,8 @@ function reIndexTagsPosts()
global $dbPosts; global $dbPosts;
global $dbTags; global $dbTags;
// Remove unpublished, only drafts. // Remove unpublished.
$dbPosts->removeUnpublished(false); $dbPosts->removeUnpublished();
// Sort posts
$dbPosts->sortByDate();
// Regenerate the tags index for posts // Regenerate the tags index for posts
$dbTags->reindexPosts( $dbPosts->db ); $dbTags->reindexPosts( $dbPosts->db );
@ -102,7 +99,8 @@ function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeU
} }
// There are not posts for the page number then set the page notfound // There are not posts for the page number then set the page notfound
if(empty($list) && $pageNumber>0) { //if(empty($list) && $pageNumber>0) {
if(empty($list)) {
$Url->setNotFound(true); $Url->setNotFound(true);
} }
@ -120,6 +118,12 @@ function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeU
// Main // Main
// ============================================================================ // ============================================================================
// Execute the scheduler.
if( $dbPosts->scheduler() ) {
// Reindex dbTags.
reIndexTagsPosts();
}
// Build specific post. // Build specific post.
if( ($Url->whereAmI()==='post') && ($Url->notFound()===false) ) if( ($Url->whereAmI()==='post') && ($Url->notFound()===false) )
{ {

View File

@ -7,7 +7,7 @@ class dbPosts extends dbJSON
'content'=> array('inFile'=>true, 'value'=>''), 'content'=> array('inFile'=>true, 'value'=>''),
'description'=> array('inFile'=>false, 'value'=>''), 'description'=> array('inFile'=>false, 'value'=>''),
'username'=> array('inFile'=>false, 'value'=>''), 'username'=> array('inFile'=>false, 'value'=>''),
'status'=> array('inFile'=>false, 'value'=>'draft'), 'status'=> array('inFile'=>false, 'value'=>'draft'), // published, draft, scheduled
'tags'=> array('inFile'=>false, 'value'=>''), 'tags'=> array('inFile'=>false, 'value'=>''),
'allowComments'=> array('inFile'=>false, 'value'=>false), 'allowComments'=> array('inFile'=>false, 'value'=>false),
'date'=> array('inFile'=>false, 'value'=>'') 'date'=> array('inFile'=>false, 'value'=>'')
@ -91,6 +91,7 @@ class dbPosts extends dbJSON
{ {
$dataForDb = array(); // This data will be saved in the database $dataForDb = array(); // This data will be saved in the database
$dataForFile = array(); // This data will be saved in the file $dataForFile = array(); // This data will be saved in the file
$currentDate = Date::current(DB_DATE_FORMAT);
// Generate the database key. // Generate the database key.
$key = $this->generateKey($args['slug']); $key = $this->generateKey($args['slug']);
@ -101,8 +102,14 @@ class dbPosts extends dbJSON
return false; return false;
} }
// Date
if(!Valid::date($args['date'], DB_DATE_FORMAT)) { if(!Valid::date($args['date'], DB_DATE_FORMAT)) {
$args['date'] = Date::current(DB_DATE_FORMAT); $args['date'] = $currentDate;
}
// Schedule post?
if( ($args['date']>$currentDate) && ($args['status']=='published') ) {
$args['status'] = 'scheduled';
} }
// Verify arguments with the database fields. // Verify arguments with the database fields.
@ -153,6 +160,10 @@ class dbPosts extends dbJSON
// Save the database // Save the database
$this->db[$key] = $dataForDb; $this->db[$key] = $dataForDb;
// Sort posts before save.
$this->sortByDate();
if( $this->save() === false ) { if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.'); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false; return false;
@ -214,11 +225,7 @@ class dbPosts extends dbJSON
$end = (int) min( ($init + $postPerPage - 1), $totalPosts - 1 ); $end = (int) min( ($init + $postPerPage - 1), $totalPosts - 1 );
$outrange = $init<0 ? true : $init>$end; $outrange = $init<0 ? true : $init>$end;
if(!$outrange) if(!$outrange) {
{
// Sort posts
$this->sortByDate();
return array_slice($this->db, $init, $postPerPage, true); return array_slice($this->db, $init, $postPerPage, true);
} }
@ -254,6 +261,9 @@ class dbPosts extends dbJSON
} }
} }
// Sort posts before save.
$this->sortByDate();
// Save the database. // Save the database.
if( $this->save() === false ) { if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.'); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
@ -263,14 +273,12 @@ class dbPosts extends dbJSON
return true; return true;
} }
// Remove the posts not published, status != published or date grater than current date. // Remove unpublished posts, status != published.
public function removeUnpublished($scheduled=true) public function removeUnpublished()
{ {
$currentDate = Date::current(DB_DATE_FORMAT);
foreach($this->db as $key=>$values) foreach($this->db as $key=>$values)
{ {
if( ($values['status']!='published') || ( ($values['date']>$currentDate) && $scheduled ) ) { if($values['status']!='published') {
unset($this->db[$key]); unset($this->db[$key]);
} }
} }
@ -280,6 +288,44 @@ class dbPosts extends dbJSON
return true; return true;
} }
// Return TRUE if there are new posts published, FALSE otherwise.
public function scheduler()
{
// Get current date.
$currentDate = Date::current(DB_DATE_FORMAT);
$saveDatabase = false;
// Check scheduled posts and publish.
foreach($this->db as $postKey=>$values)
{
if($values['status']=='scheduled')
{
// Publish post.
if($values['date']<=$currentDate) {
$this->db[$postKey]['status'] = 'published';
$saveDatabase = true;
}
}
elseif($values['status']=='published') {
break;
}
}
// Save the database.
if($saveDatabase)
{
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return true;
}
return false;
}
// Sort posts by date. // Sort posts by date.
public function sortByDate($HighToLow=true) public function sortByDate($HighToLow=true)
{ {
@ -290,6 +336,9 @@ class dbPosts extends dbJSON
uasort($this->db, array($this, 'sortLowToHigh')); uasort($this->db, array($this, 'sortLowToHigh'));
} }
Log::set(__METHOD__.LOG_SEP.'Sorted.'.$HighToLow);
return true; return true;
} }

View File

@ -78,6 +78,7 @@ class dbTags extends dbJSON
} }
$this->db['postsIndex'] = $tagsIndex; $this->db['postsIndex'] = $tagsIndex;
if( $this->save() === false ) { if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.'); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false; return false;

View File

@ -63,15 +63,13 @@ class Post extends fileContent
// Returns TRUE if the post is published, FALSE otherwise. // Returns TRUE if the post is published, FALSE otherwise.
public function published() public function published()
{ {
$currentDate = Date::current(DB_DATE_FORMAT); return ($this->getField('status')==='published');
return ( ($this->getField('status')==='published') && ($this->getField('date')<=$currentDate) );
} }
// Returns TRUE if the post is scheduled, FALSE otherwise. // Returns TRUE if the post is scheduled, FALSE otherwise.
public function scheduled() public function scheduled()
{ {
$currentDate = Date::current(DB_DATE_FORMAT); return ($this->getField('status')==='scheduled');
return ( ($this->getField('status')==='published') && ($this->getField('date')>$currentDate) );
} }
// Returns TRUE if the post is draft, FALSE otherwise. // Returns TRUE if the post is draft, FALSE otherwise.