Blocks for themes

This commit is contained in:
Diego Najar 2018-12-12 18:18:48 +01:00
parent 8400d7eaba
commit e7412d0534
9 changed files with 203 additions and 1 deletions

View File

@ -34,6 +34,7 @@ class dbJSON {
if (empty($array)) {
$this->db = array();
$this->dbBackup = array();
Log::set(__METHOD__.LOG_SEP.'Error trying to read the JSON file: '.$file, LOG_TYPE_ERROR);
} else {
$this->db = $array;
$this->dbBackup = $array;
@ -110,4 +111,11 @@ class dbJSON {
return $this->db;
}
// Truncate all the rows
public function truncate()
{
$this->db = array();
return $this->save();
}
}

View File

@ -0,0 +1,38 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
// ============================================================================
// Check role
// ============================================================================
checkRole(array('admin'));
// ============================================================================
// Main after POST
// ============================================================================
// ============================================================================
// POST Method
// ============================================================================
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
foreach ($_POST['key'] as $key=>$value) {
$blocks->add(array(
'title'=>$_POST['title'][$key],
'value'=>$_POST['value'][$key],
'key'=>$_POST['key'][$key]
));
}
// Add to syslog
$syslog->add(array(
'dictionaryKey'=>'blocks-configured',
'notes'=>''
));
}
// ============================================================================
// Main after POST
// ============================================================================
// Title of the page
$layout['title'] .= ' - '.$L->g('Blocks');

View File

@ -220,6 +220,7 @@ EOF;
{
$name = $args['name'];
$disabled = empty($args['disabled'])?'':'disabled';
$readonly = empty($args['readonly'])?'':'readonly';
$placeholder = isset($args['placeholder'])?$args['placeholder']:'';
$value = isset($args['value'])?$args['value']:'';
@ -252,7 +253,7 @@ return <<<EOF
<div class="form-group row">
$label
<div class="col-sm-10">
<input class="$class" id="$id" name="$name" value="$value" placeholder="$placeholder" type="$type" $disabled>
<input class="$class" id="$id" name="$name" value="$value" placeholder="$placeholder" type="$type" $disabled $readonly>
$tip
</div>
</div>

View File

@ -0,0 +1,56 @@
<?php defined('BLUDIT') or die('Bludit CMS.'); ?>
<?php echo Bootstrap::formOpen(array('id'=>'jsform', 'class'=>'tab-content')); ?>
<div class="align-middle">
<div class="float-right mt-1">
<button type="submit" class="btn btn-primary btn-sm" name="save"><?php $L->p('Save') ?></button>
<a class="btn btn-secondary btn-sm" href="<?php echo HTML_PATH_ADMIN_ROOT.'themes' ?>" role="button"><?php $L->p('Cancel') ?></a>
</div>
<?php echo Bootstrap::pageTitle(array('title'=>$L->g('Blocks'), 'icon'=>'box')); ?>
</div>
<?php
// Token CSRF
echo Bootstrap::formInputHidden(array(
'name'=>'tokenCSRF',
'value'=>$security->getTokenCSRF()
));
$list = $blocks->getDB();
foreach ($list as $blockKey => $blockData) {
echo Bootstrap::formTitle(array('title'=>$blockData['title']));
echo Bootstrap::formInputText(array(
'name'=>'key[]',
'label'=>$L->g('Key'),
'value'=>$blockKey,
'class'=>'',
'placeholder'=>'',
'tip'=>'',
'readonly'=>true
));
echo Bootstrap::formInputText(array(
'name'=>'title[]',
'label'=>$L->g('title'),
'value'=>$blockData['title'],
'class'=>'',
'placeholder'=>'',
'tip'=>''
));
echo Bootstrap::formTextarea(array(
'name'=>'value[]',
'label'=>$L->g('Value'),
'value'=>$blockData['value'],
'class'=>'',
'placeholder'=>'',
'tip'=>'',
'rows'=>5
));
}
echo Bootstrap::formClose();
?>

View File

@ -2,6 +2,12 @@
echo Bootstrap::pageTitle(array('title'=>$L->g('Themes'), 'icon'=>'eye'));
echo Bootstrap::link(array(
'title'=>$L->g('Configure Blocks'),
'href'=>HTML_PATH_ADMIN_ROOT.'blocks',
'icon'=>'box'
));
echo '
<table class="table mt-3">
<thead>

View File

@ -0,0 +1,64 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
class Blocks extends dbJSON
{
// Fields allowed for a row in the database
private $dbFields = array(
'title'=>'',
'value'=>''
);
function __construct()
{
parent::__construct(DB_BLOCKS);
}
// 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);
}
}

View File

@ -60,6 +60,7 @@ define('DB_TAGS', PATH_DATABASES.'tags.php');
define('DB_SYSLOG', PATH_DATABASES.'syslog.php');
define('DB_USERS', PATH_DATABASES.'users.php');
define('DB_SECURITY', PATH_DATABASES.'security.php');
define('DB_BLOCKS', PATH_DATABASES.'blocks.php');
// JSON pretty print
if (!defined('JSON_PRETTY_PRINT')) {
@ -81,6 +82,7 @@ include(PATH_ABSTRACT.'dblist.class.php');
include(PATH_ABSTRACT.'plugin.class.php');
// Inclde Classes
include(PATH_KERNEL.'blocks.class.php');
include(PATH_KERNEL.'pages.class.php');
include(PATH_KERNEL.'users.class.php');
include(PATH_KERNEL.'tags.class.php');
@ -131,6 +133,7 @@ $site = new Site();
$url = new Url();
$security = new Security();
$syslog = new Syslog();
$blocks = new Blocks();
// --- Relative paths ---
// This paths are relative for the user / web browsing.

View File

@ -795,10 +795,19 @@ function activateTheme($themeDirectory) {
global $site;
global $syslog;
global $L;
global $blocks;
if (Sanitize::pathFile(PATH_THEMES.$themeDirectory)) {
$site->set(array('theme'=>$themeDirectory));
// Remove all blocks
$blocks->truncate();
// Include Blocks for the theme
if (Sanitize::pathFile(PATH_THEMES.$themeDirectory.DS.'blocks.php')) {
include(PATH_THEMES.$themeDirectory.DS.'blocks.php');
}
$syslog->add(array(
'dictionaryKey'=>'new-theme-configured',
'notes'=>$themeDirectory
@ -809,3 +818,7 @@ function activateTheme($themeDirectory) {
}
return false;
}
function deleteAllBlocks() {
global $blocks;
}

View File

@ -0,0 +1,13 @@
<?php
$blocks->add(array(
'key'=>'google-analitycs',
'title'=>'Google Analytics',
'value'=>''
));
$blocks->add(array(
'key'=>'level',
'title'=>'Level',
'value'=>''
));
?>