Plugin custom fields parser

This commit is contained in:
Diego Najar 2019-10-17 18:32:04 +02:00
parent 4f3f40a6c4
commit 30737fef7b
3 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,7 @@
{
"plugin-data":
{
"name": "Custom fields parser",
"description": "Define code for the custom fields and parse the content of the pages."
}
}

View File

@ -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": ""
}

View File

@ -0,0 +1,71 @@
<?php
class pluginCustomFieldsParser extends Plugin {
public function init()
{
$this->dbFields = array(
'label'=>'Custom fields parser',
'jsondb'=>json_encode(array())
);
}
public function form()
{
global $L;
global $site;
$html = '<div class="alert alert-primary" role="alert">';
$html .= $this->description();
$html .= '</div>';
$jsondb = $this->getValue('jsondb', false);
$database = json_decode($jsondb, true);
$customFields = $site->customFields();
foreach ($customFields as $field=>$options) {
if ($options['type']=="string") {
$html .= '<div>';
$html .= '<label>'.$options['label'].'</label>';
$html .= '<textarea name="'.$field.'">'.(isset($database[$field])?$database[$field]:'').'</textarea>';
$html .= '</div>';
}
}
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];
}
}
}