diff --git a/kernel/abstract/dbjson.class.php b/kernel/abstract/dbjson.class.php index 3bc0252b..2b7aec1d 100644 --- a/kernel/abstract/dbjson.class.php +++ b/kernel/abstract/dbjson.class.php @@ -61,17 +61,20 @@ class dbJSON // Save the JSON file. public function save() { + $data = ''; + if($this->firstLine) { $data = "".PHP_EOL; } - else { - $data = ''; - } + // Serialize database $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. - file_put_contents($this->file, $data, LOCK_EX); + return file_put_contents($this->file, $data, LOCK_EX); } private function serialize($data) diff --git a/kernel/boot/rules/70.posts.php b/kernel/boot/rules/70.posts.php index eb5af84b..0b08e20f 100644 --- a/kernel/boot/rules/70.posts.php +++ b/kernel/boot/rules/70.posts.php @@ -15,11 +15,8 @@ function reIndexTagsPosts() global $dbPosts; global $dbTags; - // Remove unpublished, only drafts. - $dbPosts->removeUnpublished(false); - - // Sort posts - $dbPosts->sortByDate(); + // Remove unpublished. + $dbPosts->removeUnpublished(); // Regenerate the tags index for posts $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 - if(empty($list) && $pageNumber>0) { + //if(empty($list) && $pageNumber>0) { + if(empty($list)) { $Url->setNotFound(true); } @@ -120,6 +118,12 @@ function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeU // Main // ============================================================================ +// Execute the scheduler. +if( $dbPosts->scheduler() ) { + // Reindex dbTags. + reIndexTagsPosts(); +} + // Build specific post. if( ($Url->whereAmI()==='post') && ($Url->notFound()===false) ) { diff --git a/kernel/dbposts.class.php b/kernel/dbposts.class.php index f4314697..5cb0ba7d 100644 --- a/kernel/dbposts.class.php +++ b/kernel/dbposts.class.php @@ -7,7 +7,7 @@ class dbPosts extends dbJSON 'content'=> array('inFile'=>true, 'value'=>''), 'description'=> 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'=>''), 'allowComments'=> array('inFile'=>false, 'value'=>false), 'date'=> array('inFile'=>false, 'value'=>'') @@ -91,6 +91,7 @@ class dbPosts extends dbJSON { $dataForDb = array(); // This data will be saved in the database $dataForFile = array(); // This data will be saved in the file + $currentDate = Date::current(DB_DATE_FORMAT); // Generate the database key. $key = $this->generateKey($args['slug']); @@ -101,8 +102,14 @@ class dbPosts extends dbJSON return false; } + // Date 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. @@ -153,6 +160,10 @@ class dbPosts extends dbJSON // Save the database $this->db[$key] = $dataForDb; + + // Sort posts before save. + $this->sortByDate(); + if( $this->save() === false ) { Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.'); return false; @@ -214,11 +225,7 @@ class dbPosts extends dbJSON $end = (int) min( ($init + $postPerPage - 1), $totalPosts - 1 ); $outrange = $init<0 ? true : $init>$end; - if(!$outrange) - { - // Sort posts - $this->sortByDate(); - + if(!$outrange) { 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. if( $this->save() === false ) { Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.'); @@ -263,14 +273,12 @@ class dbPosts extends dbJSON return true; } - // Remove the posts not published, status != published or date grater than current date. - public function removeUnpublished($scheduled=true) + // Remove unpublished posts, status != published. + public function removeUnpublished() { - $currentDate = Date::current(DB_DATE_FORMAT); - foreach($this->db as $key=>$values) { - if( ($values['status']!='published') || ( ($values['date']>$currentDate) && $scheduled ) ) { + if($values['status']!='published') { unset($this->db[$key]); } } @@ -280,6 +288,44 @@ class dbPosts extends dbJSON 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. public function sortByDate($HighToLow=true) { @@ -290,6 +336,9 @@ class dbPosts extends dbJSON uasort($this->db, array($this, 'sortLowToHigh')); } + + Log::set(__METHOD__.LOG_SEP.'Sorted.'.$HighToLow); + return true; } diff --git a/kernel/dbtags.class.php b/kernel/dbtags.class.php index 4b02f27e..0faacddc 100644 --- a/kernel/dbtags.class.php +++ b/kernel/dbtags.class.php @@ -78,6 +78,7 @@ class dbTags extends dbJSON } $this->db['postsIndex'] = $tagsIndex; + if( $this->save() === false ) { Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.'); return false; diff --git a/kernel/post.class.php b/kernel/post.class.php index 6a8d2aea..ef5fb616 100644 --- a/kernel/post.class.php +++ b/kernel/post.class.php @@ -63,15 +63,13 @@ class Post extends fileContent // Returns TRUE if the post is published, FALSE otherwise. public function published() { - $currentDate = Date::current(DB_DATE_FORMAT); - return ( ($this->getField('status')==='published') && ($this->getField('date')<=$currentDate) ); + return ($this->getField('status')==='published'); } // Returns TRUE if the post is scheduled, FALSE otherwise. public function scheduled() { - $currentDate = Date::current(DB_DATE_FORMAT); - return ( ($this->getField('status')==='published') && ($this->getField('date')>$currentDate) ); + return ($this->getField('status')==='scheduled'); } // Returns TRUE if the post is draft, FALSE otherwise.