From 30737fef7b572e79efa03b957c8e33b1abe41fca Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Thu, 17 Oct 2019 18:32:04 +0200 Subject: [PATCH] Plugin custom fields parser --- .../custom-fields-parser/languages/en.json | 7 ++ bl-plugins/custom-fields-parser/metadata.json | 10 +++ bl-plugins/custom-fields-parser/plugin.php | 71 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 bl-plugins/custom-fields-parser/languages/en.json create mode 100644 bl-plugins/custom-fields-parser/metadata.json create mode 100644 bl-plugins/custom-fields-parser/plugin.php diff --git a/bl-plugins/custom-fields-parser/languages/en.json b/bl-plugins/custom-fields-parser/languages/en.json new file mode 100644 index 00000000..1974ccb5 --- /dev/null +++ b/bl-plugins/custom-fields-parser/languages/en.json @@ -0,0 +1,7 @@ +{ + "plugin-data": + { + "name": "Custom fields parser", + "description": "Define code for the custom fields and parse the content of the pages." + } +} \ No newline at end of file diff --git a/bl-plugins/custom-fields-parser/metadata.json b/bl-plugins/custom-fields-parser/metadata.json new file mode 100644 index 00000000..85cc658e --- /dev/null +++ b/bl-plugins/custom-fields-parser/metadata.json @@ -0,0 +1,10 @@ +{ + "author": "Bludit", + "email": "", + "website": "https://plugins.bludit.com", + "version": "3.10.0", + "releaseDate": "2019-09-25", + "license": "MIT", + "compatible": "3.10.0", + "notes": "" +} \ No newline at end of file diff --git a/bl-plugins/custom-fields-parser/plugin.php b/bl-plugins/custom-fields-parser/plugin.php new file mode 100644 index 00000000..1d5e7e20 --- /dev/null +++ b/bl-plugins/custom-fields-parser/plugin.php @@ -0,0 +1,71 @@ +dbFields = array( + 'label'=>'Custom fields parser', + 'jsondb'=>json_encode(array()) + ); + } + + public function form() + { + global $L; + global $site; + + $html = ''; + + $jsondb = $this->getValue('jsondb', false); + $database = json_decode($jsondb, true); + + $customFields = $site->customFields(); + + foreach ($customFields as $field=>$options) { + if ($options['type']=="string") { + $html .= '
'; + $html .= ''; + $html .= ''; + $html .= '
'; + } + } + + return $html; + } + + public function post() + { + $this->db['jsondb'] = Sanitize::html(json_encode($_POST)); + return $this->save(); + } + + public function parse($page) + { + $jsondb = $this->getValue('jsondb', false); + $database = json_decode($jsondb, true); + $parsedCode = array(); + + foreach ($database as $field=>$code) { + $value = $page->custom($field); + $parsedCode['{{ '.$field.' }}'] = str_replace('{{ value }}', $value, $code); + } + + $content = $page->contentRaw(); + return str_replace(array_keys($parsedCode), array_values($parsedCode), $content); + } + + public function beforeSiteLoad() + { + if ($GLOBALS['WHERE_AM_I']=='page') { + $GLOBALS['page']->setField('content', $this->parse($GLOBALS['page'])); + } else { + foreach ($GLOBALS['content'] as $key=>$page) { + $GLOBALS['content'][$key]->setField('content', $this->parse($GLOBALS['content'][$key])); + } + $GLOBALS['page'] = $GLOBALS['content'][0]; + } + } +} \ No newline at end of file