bludit/bl-kernel/abstract/dblist.class.php

157 lines
3.0 KiB
PHP
Raw Normal View History

2017-05-12 20:18:44 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
/*
Database structure
{
"videos": {
"name": "Videos",
"list": [ "my-page", "second-page" ]
},
"pets": {
"name": "Pets",
"list": [ "cats-and-dogs" ]
}
}
*/
class dbList extends dbJSON
{
public $db = array();
function __construct($file)
{
parent::__construct($file);
}
private function getList($key, $amountOfItems, $pageNumber)
{
if( !isset($this->db[$key]) ) {
Log::set(__METHOD__.LOG_SEP.'Error key does not exist '.$key);
return array();
}
$list = $this->db[$key]['list'];
2017-05-24 00:48:29 +02:00
// The first page number is 1, so the real is 0
$realPageNumber = $pageNumber - 1;
2017-05-12 20:18:44 +02:00
$total = count($list);
2017-05-24 00:48:29 +02:00
$init = (int) $amountOfItems * $realPageNumber;
2017-05-12 20:18:44 +02:00
$end = (int) min( ($init + $amountOfItems - 1), $total );
$outrange = $init<0 ? true : $init>$end;
if($outrange) {
Log::set(__METHOD__.LOG_SEP.'Error out of range');
return array();
}
$list = array_flip($list);
return array_slice($list, $init, $amountOfItems, true);
}
public function generateKey($name)
{
return Text::cleanUrl($name);
}
public function add($name)
{
$key = $this->generateKey($name);
if( isset($this->db[$key]) ) {
Log::set(__METHOD__.LOG_SEP.'Error key already exist: '.$key);
return false;
}
$this->db[$key]['name'] = $name;
$this->db[$key]['list'] = array();
$this->sortAlphanumeric();
2017-05-12 20:18:44 +02:00
$this->save();
return $key;
}
public function remove($key)
{
if( !isset($this->db[$key]) ) {
Log::set(__METHOD__.LOG_SEP.'The key does not exist, key: '.$key);
return false;
}
unset($this->db[$key]);
return $this->save();
}
public function edit($oldKey, $newName)
{
$newKey = $this->generateKey($newName);
$this->db[$newKey]['name'] = $newName;
$this->db[$newKey]['list'] = $this->db[$oldKey]['list'];
// Remove the old category
2017-05-16 00:46:20 +02:00
if( $oldKey != $newKey ) {
unset( $this->db[$oldKey] );
}
2017-05-12 20:18:44 +02:00
$this->sortAlphanumeric();
2017-05-12 20:18:44 +02:00
$this->save();
return $newKey;
}
// Sort the categories by "Natural order"
private function sortAlphanumeric()
{
// Sort key alphanumeric strings, a01, a10, b10, c02
return ksort($this->db);
}
2017-05-12 20:18:44 +02:00
// Returns the name associated to the key, FALSE if the key doesn't exist
public function getName($key)
{
if( isset($this->db[$key]) ) {
return $this->db[$key]['name'];
}
return false;
}
// Returns an array with key=>name of the list
public function getKeyNameArray()
2017-05-12 20:18:44 +02:00
{
$tmp = array();
foreach($this->db as $key=>$fields) {
$tmp[$key] = $fields['name'];
}
return $tmp;
}
// Returns the amount of items for some key
public function countItems($key)
{
if( isset($this->db[$key]) ) {
return count($this->db[$key]['list']);
}
return 0;
}
2017-05-16 00:46:20 +02:00
public function exists($key)
{
return isset( $this->db[$key] );
}
2017-05-17 00:04:53 +02:00
// Returns an array with a portion of the database filtered by key
// Returns array( 'name'=>'', 'list'=>array() )
public function getMap($key)
{
if( isset($this->db[$key]) ) {
return $this->db[$key];
}
return false;
}
2017-05-12 20:18:44 +02:00
}