From e7412d05349e83a28a8a0c1c68418c949dc72ade Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Wed, 12 Dec 2018 18:18:48 +0100 Subject: [PATCH] Blocks for themes --- bl-kernel/abstract/dbjson.class.php | 8 ++++ bl-kernel/admin/controllers/blocks.php | 38 +++++++++++++++ bl-kernel/admin/themes/booty/init.php | 3 +- bl-kernel/admin/views/blocks.php | 56 ++++++++++++++++++++++ bl-kernel/admin/views/themes.php | 6 +++ bl-kernel/blocks.class.php | 64 ++++++++++++++++++++++++++ bl-kernel/boot/init.php | 3 ++ bl-kernel/functions.php | 13 ++++++ bl-themes/alternative/blocks.php | 13 ++++++ 9 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 bl-kernel/admin/controllers/blocks.php create mode 100644 bl-kernel/admin/views/blocks.php create mode 100644 bl-kernel/blocks.class.php create mode 100644 bl-themes/alternative/blocks.php diff --git a/bl-kernel/abstract/dbjson.class.php b/bl-kernel/abstract/dbjson.class.php index c7e54670..0e409558 100644 --- a/bl-kernel/abstract/dbjson.class.php +++ b/bl-kernel/abstract/dbjson.class.php @@ -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(); + } + } \ No newline at end of file diff --git a/bl-kernel/admin/controllers/blocks.php b/bl-kernel/admin/controllers/blocks.php new file mode 100644 index 00000000..44010967 --- /dev/null +++ b/bl-kernel/admin/controllers/blocks.php @@ -0,0 +1,38 @@ +$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'); \ No newline at end of file diff --git a/bl-kernel/admin/themes/booty/init.php b/bl-kernel/admin/themes/booty/init.php index 9ef973f1..c53a852c 100644 --- a/bl-kernel/admin/themes/booty/init.php +++ b/bl-kernel/admin/themes/booty/init.php @@ -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 << $label
- + $tip
diff --git a/bl-kernel/admin/views/blocks.php b/bl-kernel/admin/views/blocks.php new file mode 100644 index 00000000..4b7c08ed --- /dev/null +++ b/bl-kernel/admin/views/blocks.php @@ -0,0 +1,56 @@ + + +'jsform', 'class'=>'tab-content')); ?> + +
+
+ + p('Cancel') ?> +
+ $L->g('Blocks'), 'icon'=>'box')); ?> +
+ +'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(); + +?> diff --git a/bl-kernel/admin/views/themes.php b/bl-kernel/admin/views/themes.php index 95393b89..0d41a54a 100644 --- a/bl-kernel/admin/views/themes.php +++ b/bl-kernel/admin/views/themes.php @@ -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 ' diff --git a/bl-kernel/blocks.class.php b/bl-kernel/blocks.class.php new file mode 100644 index 00000000..41f83486 --- /dev/null +++ b/bl-kernel/blocks.class.php @@ -0,0 +1,64 @@ +'', + '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); + } + +} diff --git a/bl-kernel/boot/init.php b/bl-kernel/boot/init.php index bca3d8f7..e4fb7c61 100644 --- a/bl-kernel/boot/init.php +++ b/bl-kernel/boot/init.php @@ -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. diff --git a/bl-kernel/functions.php b/bl-kernel/functions.php index e7a9b62a..019874c9 100644 --- a/bl-kernel/functions.php +++ b/bl-kernel/functions.php @@ -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; +} \ No newline at end of file diff --git a/bl-themes/alternative/blocks.php b/bl-themes/alternative/blocks.php new file mode 100644 index 00000000..d731acf5 --- /dev/null +++ b/bl-themes/alternative/blocks.php @@ -0,0 +1,13 @@ +add(array( + 'key'=>'google-analitycs', + 'title'=>'Google Analytics', + 'value'=>'' +)); + +$blocks->add(array( + 'key'=>'level', + 'title'=>'Level', + 'value'=>'' +)); +?> \ No newline at end of file