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

177 lines
3.4 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);
}
2017-12-26 17:45:02 +01:00
// Returns an array with a list of key of pages, FALSE if out of range
2017-05-31 20:17:21 +02:00
public function getList($key, $pageNumber, $amountOfItems)
2017-05-12 20:18:44 +02:00
{
2017-09-21 20:42:03 +02:00
if (empty($key)) {
return false;
}
2017-07-30 23:15:33 +02:00
if (!isset($this->db[$key])) {
2017-05-12 20:18:44 +02:00
Log::set(__METHOD__.LOG_SEP.'Error key does not exist '.$key);
return false;
2017-05-12 20:18:44 +02:00
}
$list = $this->db[$key]['list'];
2017-07-30 23:15:33 +02:00
if ($amountOfItems==-1) {
// Invert keys to values, is necesary returns as key the key pages
2017-12-26 17:45:02 +01:00
//$list = array_flip($list);
2017-07-30 23:15:33 +02:00
return $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 false;
2017-05-12 20:18:44 +02:00
}
2017-12-26 17:45:02 +01:00
//$list = array_flip($list);
2017-05-12 20:18:44 +02:00
return array_slice($list, $init, $amountOfItems, true);
}
public function generateKey($name)
{
2017-11-01 19:38:56 +01:00
$key = Text::cleanUrl($name);
if (empty($key)) {
return false;
}
return $key;
2017-05-12 20:18:44 +02:00
}
public function add($name)
{
$key = $this->generateKey($name);
2017-11-01 19:38:56 +01:00
if ($key===false) {
Log::set(__METHOD__.LOG_SEP.'Error when try to generate the key');
return false;
}
if (isset($this->db[$key])) {
2017-05-12 20:18:44 +02:00
Log::set(__METHOD__.LOG_SEP.'Error key already exist: '.$key);
return false;
}
2017-11-01 19:38:56 +01:00
$this->db[$key]['name'] = Sanitize::html($name);
2017-05-12 20:18:44 +02:00
$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);
2017-11-01 19:38:56 +01:00
$this->db[$newKey]['name'] = Sanitize::html($newName);
2017-05-12 20:18:44 +02:00
$this->db[$newKey]['list'] = $this->db[$oldKey]['list'];
2017-11-01 19:38:56 +01:00
// Remove the old key
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
}