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",
|
2017-04-17 12:49:03 +02:00
|
|
|
"posts": [ "first-post", "bull-terrier" ],
|
2017-03-26 20:51:32 +02:00
|
|
|
"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" ],
|
2017-04-17 12:49:03 +02:00
|
|
|
"pages": [ "cats-and-dogs" ]
|
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-04-17 12:49:03 +02:00
|
|
|
public function add($category)
|
|
|
|
{
|
|
|
|
$categoryKey = $this->generateKey($category);
|
|
|
|
if( isset($this->db[$categoryKey]) ) {
|
|
|
|
Log::set(__METHOD__.LOG_SEP.'The category already exist, key: '.$categoryKey.', name: '.$category);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db[$categoryKey]['name'] = $category;
|
|
|
|
$this->db[$categoryKey]['posts'] = array();
|
|
|
|
$this->db[$categoryKey]['pages'] = array();
|
|
|
|
|
|
|
|
$this->save();
|
|
|
|
|
|
|
|
return $categoryKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove($categoryKey)
|
|
|
|
{
|
|
|
|
if( !isset($this->db[$categoryKey]) ) {
|
|
|
|
Log::set(__METHOD__.LOG_SEP.'The category does not exist, key: '.$categoryKey);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($this->db[$categoryKey]);
|
|
|
|
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit($oldCategoryKey, $newCategory)
|
|
|
|
{
|
|
|
|
$newCategoryKey = $this->generateKey($newCategory);
|
|
|
|
if( isset($this->db[$newCategoryKey]) ) {
|
|
|
|
Log::set(__METHOD__.LOG_SEP.'The category already exist, key: '.$newCategoryKey.', name: '.$newCategory);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new category with the posts and pages from the old one
|
|
|
|
$this->db[$newCategoryKey]['name'] = $newCategory;
|
|
|
|
$this->db[$newCategoryKey]['posts'] = $this->db[$oldCategoryKey]['posts'];
|
|
|
|
$this->db[$newCategoryKey]['pages'] = $this->db[$oldCategoryKey]['posts'];
|
|
|
|
|
|
|
|
// Remove the old category
|
|
|
|
unset( $this->db[$oldCategoryKey] );
|
|
|
|
|
|
|
|
$this->save();
|
|
|
|
|
|
|
|
return $newCategoryKey;
|
|
|
|
}
|
|
|
|
|
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-04-26 18:56:10 +02:00
|
|
|
// Clean post list
|
|
|
|
foreach( $this->db as $key=>$value ) {
|
|
|
|
$this->db[$key]['posts'] = array();
|
|
|
|
}
|
2017-03-26 20:51:32 +02:00
|
|
|
|
|
|
|
// Foreach post in the database
|
|
|
|
foreach($db as $postKey=>$postData) {
|
2017-04-26 18:56:10 +02:00
|
|
|
if( !empty($postData['category']) ) {
|
2017-03-26 20:51:32 +02:00
|
|
|
$categoryKey = $postData['category'];
|
2017-04-26 18:56:10 +02:00
|
|
|
if( isset($this->db[$categoryKey]['posts']) ) {
|
|
|
|
array_push($this->db[$categoryKey]['posts'], $postKey);
|
|
|
|
}
|
2017-03-21 08:45:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 12:49:03 +02:00
|
|
|
return $this->save();
|
2017-03-26 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Re-generate pages index
|
|
|
|
// (array) $db, the $db must be sorted by date and the posts published only.
|
|
|
|
public function reIndexPages($db)
|
|
|
|
{
|
2017-04-26 18:56:10 +02:00
|
|
|
// Clean post list
|
|
|
|
foreach( $this->db as $key=>$value ) {
|
|
|
|
$this->db[$key]['pages'] = array();
|
|
|
|
}
|
2017-03-26 20:51:32 +02:00
|
|
|
|
2017-04-26 18:56:10 +02:00
|
|
|
// Foreach post in the database
|
|
|
|
foreach($db as $postKey=>$postData) {
|
|
|
|
if( !empty($postData['category']) ) {
|
|
|
|
$categoryKey = $postData['category'];
|
|
|
|
if( isset($this->db[$categoryKey]['pages']) ) {
|
|
|
|
array_push($this->db[$categoryKey]['pages'], $postKey);
|
|
|
|
}
|
2017-04-17 13:23:43 +02:00
|
|
|
}
|
2017-03-26 20:51:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-17 12:49:03 +02:00
|
|
|
return $this->save();
|
|
|
|
}
|
2017-03-21 08:45:58 +01:00
|
|
|
|
2017-04-17 12:49:03 +02:00
|
|
|
public function exists($categoryKey)
|
|
|
|
{
|
|
|
|
return isset( $this->db[$categoryKey] );
|
2017-03-21 08:45:58 +01:00
|
|
|
}
|
|
|
|
|
2017-04-17 12:49:03 +02:00
|
|
|
public function getName($categoryKey)
|
|
|
|
{
|
|
|
|
return $this->db[$categoryKey]['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generateKey($category)
|
|
|
|
{
|
|
|
|
return Text::cleanUrl($category);
|
|
|
|
}
|
2017-03-26 20:51:32 +02:00
|
|
|
|
2017-03-21 08:45:58 +01:00
|
|
|
}
|