2017-05-12 20:18:44 +02:00
|
|
|
<?php defined('BLUDIT') or die('Bludit CMS.');
|
|
|
|
|
|
|
|
/*
|
|
|
|
Database structure
|
|
|
|
|
|
|
|
{
|
|
|
|
"videos": {
|
|
|
|
"name": "Videos",
|
2018-04-27 20:36:43 +02:00
|
|
|
"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",
|
2018-04-27 20:36:43 +02:00
|
|
|
"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);
|
|
|
|
}
|
|
|
|
|
2018-08-02 22:33:53 +02:00
|
|
|
public function keys()
|
|
|
|
{
|
|
|
|
return array_keys($this->db);
|
|
|
|
}
|
|
|
|
|
2018-04-27 20:36:43 +02:00
|
|
|
// Returns the list of keys filter by pageNumber
|
2018-08-06 21:46:58 +02:00
|
|
|
public function getList($key, $pageNumber, $numberOfItems)
|
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);
|
2017-07-26 01:17:13 +02:00
|
|
|
return false;
|
2017-05-12 20:18:44 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 20:36:43 +02:00
|
|
|
// List of keys
|
2017-05-12 20:18:44 +02:00
|
|
|
$list = $this->db[$key]['list'];
|
|
|
|
|
2018-04-27 20:36:43 +02:00
|
|
|
// Returns all the items from the list
|
2018-08-06 21:46:58 +02:00
|
|
|
if ($numberOfItems==-1) {
|
2017-07-30 23:15:33 +02:00
|
|
|
// Invert keys to values, is necesary returns as key the key pages
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2017-05-24 00:48:29 +02:00
|
|
|
// The first page number is 1, so the real is 0
|
|
|
|
$realPageNumber = $pageNumber - 1;
|
2018-08-06 21:46:58 +02:00
|
|
|
$chunks = array_chunk($list, $numberOfItems);
|
2018-04-27 20:36:43 +02:00
|
|
|
if (isset($chunks[$realPageNumber])) {
|
|
|
|
return $chunks[$realPageNumber];
|
2017-05-12 20:18:44 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 20:36:43 +02:00
|
|
|
// Out of index,returns FALSE
|
|
|
|
return false;
|
2017-05-12 20:18:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function generateKey($name)
|
|
|
|
{
|
2018-08-20 22:32:14 +02:00
|
|
|
global $L;
|
|
|
|
|
2017-11-01 19:38:56 +01:00
|
|
|
$key = Text::cleanUrl($name);
|
2018-08-20 22:32:14 +02:00
|
|
|
if (Text::isEmpty($key)) {
|
|
|
|
$key = $L->g('empty');
|
|
|
|
}
|
2018-04-27 20:36:43 +02:00
|
|
|
while (isset($this->db[$key])) {
|
|
|
|
$key++;
|
2017-11-01 19:38:56 +01:00
|
|
|
}
|
|
|
|
return $key;
|
2017-05-12 20:18:44 +02:00
|
|
|
}
|
|
|
|
|
2018-04-27 20:36:43 +02:00
|
|
|
// Add a new item to the dblist
|
2018-08-02 17:06:53 +02:00
|
|
|
// $args => 'name', 'template', 'description', list'
|
2018-04-27 20:36:43 +02:00
|
|
|
public function add($args)
|
2017-05-12 20:18:44 +02:00
|
|
|
{
|
2018-04-27 20:36:43 +02:00
|
|
|
$key = $this->generateKey($args['name']);
|
2017-11-01 19:38:56 +01:00
|
|
|
|
2018-04-27 20:36:43 +02: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']:'';
|
2018-04-27 20:36:43 +02:00
|
|
|
$this->db[$key]['list'] = isset($args['list'])?$args['list']:array();
|
2017-05-27 19:46:46 +02:00
|
|
|
|
|
|
|
$this->sortAlphanumeric();
|
2017-05-12 20:18:44 +02:00
|
|
|
$this->save();
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove($key)
|
|
|
|
{
|
2018-04-27 20:36:43 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-04-27 20:36:43 +02:00
|
|
|
// Edit an item to the dblist
|
2018-08-02 17:06:53 +02:00
|
|
|
// $args => 'name', 'oldkey', 'newKey', 'template', 'description'
|
2018-04-27 20:36:43 +02:00
|
|
|
public function edit($args)
|
2018-04-22 17:45:31 +02:00
|
|
|
{
|
2018-07-10 19:53:53 +02:00
|
|
|
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);
|
2018-04-22 17:45:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-27 20:36:43 +02:00
|
|
|
$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']:'';
|
2018-04-27 20:36:43 +02:00
|
|
|
$this->db[$args['newKey']]['list'] = $this->db[$args['oldKey']]['list'];
|
2018-04-22 17:45:31 +02:00
|
|
|
|
2018-04-27 20:36:43 +02:00
|
|
|
// 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
|
|
|
|
2017-05-27 19:46:46 +02:00
|
|
|
$this->sortAlphanumeric();
|
2017-05-12 20:18:44 +02:00
|
|
|
$this->save();
|
2018-04-27 20:36:43 +02:00
|
|
|
return $args['newKey'];
|
2017-05-12 20:18:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-27 19:46:46 +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)
|
|
|
|
{
|
2018-04-27 20:36:43 +02:00
|
|
|
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
|
2017-05-27 19:46:46 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-04 12:44:37 +02:00
|
|
|
// Returns the number of items in the list
|
2017-05-12 20:18:44 +02:00
|
|
|
public function countItems($key)
|
|
|
|
{
|
2018-08-04 12:44:37 +02:00
|
|
|
if (isset($this->db[$key])) {
|
2017-05-12 20:18:44 +02:00
|
|
|
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] );
|
|
|
|
}
|
|
|
|
|
2018-04-27 20:36:43 +02:00
|
|
|
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)
|
|
|
|
{
|
2018-04-27 20:36:43 +02:00
|
|
|
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
|
|
|
}
|