bludit/bl-kernel/dbcategories.class.php

143 lines
3.2 KiB
PHP
Raw Normal View History

2017-03-21 08:45:58 +01:00
<?php defined('BLUDIT') or die('Bludit CMS.');
/*
Database structure
- To re index the list of posts and pages need to be sorted
{
2017-03-26 20:51:32 +02:00
"videos": {
"name": "Videos",
"posts": [ "first-post", "second-post" ],
"pages": [ "my-page", "second-page" ]
2017-03-21 08:45:58 +01:00
},
2017-03-26 20:51:32 +02:00
"pets": {
"name": "Pets",
"posts": [ "second-post", "bull-terrier" ],
"pages": [ ]
2017-03-21 08:45:58 +01:00
}
}
*/
class dbCategories extends dbJSON
{
2017-03-26 20:51:32 +02:00
public $dbFields = array();
2017-03-21 08:45:58 +01:00
function __construct()
{
parent::__construct(PATH_DATABASES.'categories.php');
}
2017-03-26 20:51:32 +02:00
private function getByCategory($type='posts', $categoryKey, $amountPerPage, $pageNumber)
2017-03-21 08:45:58 +01:00
{
// Check if the category exists
2017-03-26 20:51:32 +02:00
if( !isset($this->db[$categoryKey]) ) {
Log::set(__METHOD__.LOG_SEP.'Error getting '.$type.' by the category: '.$categoryKey);
2017-03-21 08:45:58 +01:00
return array();
}
2017-03-26 20:51:32 +02:00
$list = $this->db[$categoryKey][$type];
2017-03-21 08:45:58 +01:00
$init = (int) $amountPerPage * $pageNumber;
$end = (int) min( ($init + $amountPerPage - 1), count($list) - 1 );
$outrange = $init<0 ? true : $init > $end;
if($outrange) {
Log::set(__METHOD__.LOG_SEP.'Error getting '.$type.' by the category, out of range, pageNumber: '.$pageNumber);
return array();
}
$tmp = array_flip($list);
return array_slice($tmp, $init, $amountPerPage, true);
}
2017-03-26 20:51:32 +02:00
public function getPagesByCategory($categoryKey, $amountPerPage, $pageNumber)
2017-03-21 08:45:58 +01:00
{
2017-03-26 20:51:32 +02:00
return $this->getByCategory('pages', $categoryKey, $amountPerPage, $pageNumber);
2017-03-21 08:45:58 +01:00
}
2017-03-26 20:51:32 +02:00
public function getPostsByCategory($categoryKey, $amountPerPage, $pageNumber)
2017-03-21 08:45:58 +01:00
{
2017-03-26 20:51:32 +02:00
return $this->getByCategory('posts', $categoryKey, $amountPerPage, $pageNumber);
2017-03-21 08:45:58 +01:00
}
2017-03-26 20:51:32 +02:00
private function countByCategory($type='posts', $categoryKey)
2017-03-21 08:45:58 +01:00
{
2017-03-26 20:51:32 +02:00
if( isset($this->db[$categoryKey][$type]) ) {
return count($this->db[$categoryKey][$type]);
2017-03-21 08:45:58 +01:00
}
return 0;
}
2017-03-26 20:51:32 +02:00
public function countPostsByCategory($categoryKey)
{
return $this->countByCategory('posts', $categoryKey);
}
public function countPagesByCategory($categoryKey)
2017-03-21 08:45:58 +01:00
{
2017-03-26 20:51:32 +02:00
return $this->countByCategory('pages', $categoryKey);
2017-03-21 08:45:58 +01:00
}
2017-03-26 20:51:32 +02:00
public function getAll()
2017-03-21 08:45:58 +01:00
{
2017-03-26 20:51:32 +02:00
$tmp = array();
foreach($this->db as $key=>$data) {
$tmp[$key] = $data['name'];
}
// Sort low to high, by value.
natcasesort($tmp);
return $tmp;
2017-03-21 08:45:58 +01:00
}
2017-03-26 20:51:32 +02:00
// Re-generate posts index
2017-03-21 08:45:58 +01:00
// (array) $db, the $db must be sorted by date and the posts published only.
2017-03-26 20:51:32 +02:00
public function reIndexPosts($db)
2017-03-21 08:45:58 +01:00
{
2017-03-26 20:51:32 +02:00
$index = array();
// Foreach post in the database
foreach($db as $postKey=>$postData) {
if(!empty($postData['category'])) {
$categoryKey = $postData['category'];
array_push($index, $postKey);
2017-03-21 08:45:58 +01:00
}
}
2017-03-26 20:51:32 +02:00
$this->db[$categoryKey]['posts'] = $index;
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return true;
}
// Re-generate pages index
// (array) $db, the $db must be sorted by date and the posts published only.
public function reIndexPages($db)
{
$index = array();
// Foreach post in the database
foreach($db as $pageKey=>$pageData) {
$categoryKey = $pageData['category'];
array_push($index, $pageKey);
}
$this->db[$categoryKey]['pages'] = $index;
2017-03-21 08:45:58 +01:00
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return true;
}
2017-03-26 20:51:32 +02:00
2017-03-21 08:45:58 +01:00
}