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

182 lines
4.1 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",
"template: "",
2018-08-02 17:06:53 +02:00
"description: "",
2017-05-12 20:18:44 +02:00
"list": [ "my-page", "second-page" ]
},
"pets": {
"name": "Pets",
"template: "",
2018-08-02 17:06:53 +02:00
"description: "",
2017-05-12 20:18:44 +02:00
"list": [ "cats-and-dogs" ]
}
}
*/
class dbList extends dbJSON
{
public $db = array();
function __construct($file)
{
parent::__construct($file);
}
// Returns the list of keys filter by pageNumber
2017-05-31 20:17:21 +02:00
public function getList($key, $pageNumber, $amountOfItems)
2017-05-12 20:18:44 +02:00
{
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 of keys
2017-05-12 20:18:44 +02:00
$list = $this->db[$key]['list'];
// Returns all the items from the list
2017-07-30 23:15:33 +02:00
if ($amountOfItems==-1) {
// Invert keys to values, is necesary returns as key the key pages
$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;
$chunks = array_chunk($list, $amountOfItems);
if (isset($chunks[$realPageNumber])) {
return $chunks[$realPageNumber];
2017-05-12 20:18:44 +02:00
}
// Out of index,returns FALSE
return false;
2017-05-12 20:18:44 +02:00
}
public function generateKey($name)
{
2017-11-01 19:38:56 +01:00
$key = Text::cleanUrl($name);
while (isset($this->db[$key])) {
$key++;
2017-11-01 19:38:56 +01:00
}
return $key;
2017-05-12 20:18:44 +02:00
}
// Add a new item to the dblist
2018-08-02 17:06:53 +02:00
// $args => 'name', 'template', 'description', list'
public function add($args)
2017-05-12 20:18:44 +02:00
{
$key = $this->generateKey($args['name']);
2017-11-01 19:38:56 +01:00
$this->db[$key]['name'] = $args['name'];
$this->db[$key]['template'] = isset($args['template'])?$args['template']:'';
2018-08-02 17:06:53 +02:00
$this->db[$key]['description'] = isset($args['description'])?$args['description']:'';
$this->db[$key]['list'] = isset($args['list'])?$args['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])) {
2017-05-12 20:18:44 +02:00
Log::set(__METHOD__.LOG_SEP.'The key does not exist, key: '.$key);
return false;
}
unset($this->db[$key]);
return $this->save();
}
// Edit an item to the dblist
2018-08-02 17:06:53 +02:00
// $args => 'name', 'oldkey', 'newKey', 'template', 'description'
public function edit($args)
{
if ( isset($this->db[$args['newKey']]) && ($args['newKey']!==$args['oldKey']) ) {
Log::set(__METHOD__.LOG_SEP.'The new key already exists. Key: '.$args['newKey'], LOG_TYPE_WARN);
return false;
}
$this->db[$args['newKey']]['name'] = $args['name'];
$this->db[$args['newKey']]['template'] = isset($args['template'])?$args['template']:'';
2018-08-02 17:06:53 +02:00
$this->db[$args['newKey']]['description'] = isset($args['description'])?$args['description']:'';
$this->db[$args['newKey']]['list'] = $this->db[$args['oldKey']]['list'];
// Remove the old category
if ($args['oldKey'] !== $args['newKey']) {
unset( $this->db[$args['oldKey']] );
2017-05-16 00:46:20 +02:00
}
2017-05-12 20:18:44 +02:00
$this->sortAlphanumeric();
2017-05-12 20:18:44 +02:00
$this->save();
return $args['newKey'];
2017-05-12 20:18:44 +02:00
}
// 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])) {
2017-05-12 20:18:44 +02:00
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] );
}
public function existsName($name)
{
foreach ($this->db as $key=>$fields) {
if ($name==$fields['name']) {
return true;
}
}
return false;
}
2017-05-17 00:04:53 +02:00
// Returns an array with a portion of the database filtered by key
2018-08-02 17:06:53 +02:00
// Returns array( 'key'=>'', 'name'=>'', 'template'=>'', 'description'=>'', list'=>array() )
2017-05-17 00:04:53 +02:00
public function getMap($key)
{
if (isset($this->db[$key])) {
$tmp = $this->db[$key];
$tmp['key'] = $key;
return $tmp;
2017-05-17 00:04:53 +02:00
}
return false;
}
2017-05-12 20:18:44 +02:00
}