2018-12-12 18:18:48 +01:00
|
|
|
<?php defined('BLUDIT') or die('Bludit CMS.');
|
|
|
|
|
|
|
|
class Blocks extends dbJSON
|
|
|
|
{
|
|
|
|
// Fields allowed for a row in the database
|
2019-01-26 12:50:48 +01:00
|
|
|
protected $dbFields = array(
|
2018-12-12 18:18:48 +01:00
|
|
|
'title'=>'',
|
2018-12-17 22:13:15 +01:00
|
|
|
'value'=>'',
|
|
|
|
'description'=>''
|
2018-12-12 18:18:48 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct(DB_BLOCKS);
|
|
|
|
}
|
|
|
|
|
2018-12-17 22:13:15 +01:00
|
|
|
// Get a particular Block-Object by his key
|
2018-12-13 14:05:26 +01:00
|
|
|
public function get($key)
|
2018-12-13 14:05:07 +01:00
|
|
|
{
|
2018-12-17 22:13:15 +01:00
|
|
|
return new Block($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get an array with all the Block-Object
|
|
|
|
public function getAll()
|
|
|
|
{
|
|
|
|
$all = array();
|
|
|
|
foreach ($this->db as $key=>$fields) {
|
|
|
|
$all[$key] = new Block($key);
|
|
|
|
}
|
|
|
|
return $all;
|
2018-12-13 14:05:07 +01:00
|
|
|
}
|
|
|
|
|
2018-12-12 18:18:48 +01:00
|
|
|
// Add a row to the database
|
|
|
|
public function add($args)
|
|
|
|
{
|
|
|
|
$key = $this->generateKey($args['key']);
|
|
|
|
if (Text::isEmpty($key)) {
|
|
|
|
Log::set(__METHOD__.LOG_SEP.'Invalid key for the Block.', LOG_TYPE_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$row = array();
|
|
|
|
foreach ($this->dbFields as $field=>$defaultValue) {
|
|
|
|
if (isset($args[$field])) {
|
|
|
|
// Sanitize if will be stored on database
|
|
|
|
$value = Sanitize::html($args[$field]);
|
|
|
|
settype($value, gettype($defaultValue));
|
|
|
|
$row[$field] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Insert the row in the database
|
|
|
|
$this->db[$key] = $row;
|
|
|
|
// Save the database
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a row from the database
|
|
|
|
public function delete($key)
|
|
|
|
{
|
|
|
|
if (!$this->exists($key)) {
|
|
|
|
Log::set(__METHOD__.LOG_SEP.'The Block does not exist. Key: '.$key, LOG_TYPE_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from database
|
|
|
|
unset($this->db[$key]);
|
|
|
|
// Save the database
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a row exists
|
|
|
|
public function exists($key)
|
|
|
|
{
|
|
|
|
return isset ($this->db[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function generateKey($text)
|
|
|
|
{
|
|
|
|
return Text::cleanUrl($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|