Custom fields: Boolean type

This commit is contained in:
Diego Najar 2019-09-03 18:35:30 +02:00
parent cc73f609e4
commit 72b6908899
7 changed files with 45 additions and 18 deletions

View File

@ -264,7 +264,7 @@ EOF;
{ {
$labelForCheckbox = isset($args['labelForCheckbox'])?$args['labelForCheckbox']:''; $labelForCheckbox = isset($args['labelForCheckbox'])?$args['labelForCheckbox']:'';
$placeholder = isset($args['placeholder'])?$args['placeholder']:''; $placeholder = isset($args['placeholder'])?$args['placeholder']:'';
$tip = isset($args['tip'])?$args['tip']:'&nbsp;'; $tip = isset($args['tip'])?'<small class="form-text text-muted">'.$args['tip'].'</small>':'';
$value = isset($args['value'])?$args['value']:''; $value = isset($args['value'])?$args['value']:'';
$name = $args['name']; $name = $args['name'];
$id = 'js'.$name; $id = 'js'.$name;
@ -273,7 +273,7 @@ EOF;
} }
$disabled = isset($args['disabled'])?'disabled':''; $disabled = isset($args['disabled'])?'disabled':'';
$class = 'form-group'; $class = 'form-group m-0';
if (isset($args['class'])) { if (isset($args['class'])) {
$class = $class.' '.$args['class']; $class = $class.' '.$args['class'];
} }
@ -289,14 +289,15 @@ EOF;
} }
$checked = $args['checked']?'checked':''; $checked = $args['checked']?'checked':'';
$value = $checked?'1':'0';
return <<<EOF return <<<EOF
<div class="$class"> <div class="$class">
$label $label
<div class="form-check"> <div class="form-check">
<input name="$name" class="form-check-input" type="checkbox" id="$id" $checked> <input type="hidden" name="$name" value="$value"><input id="$id" type="checkbox" class="form-check-input" onclick="this.previousSibling.value=1-this.previousSibling.value" $checked>
<label class="form-check-label" for="$id">$labelForCheckbox</label> <label class="form-check-label" for="$id">$labelForCheckbox</label>
<small class="form-text text-muted">$tip</small> $tip
</div> </div>
</div> </div>
EOF; EOF;

View File

@ -89,7 +89,7 @@ echo Bootstrap::formOpen(array(
<div class="nav nav-tabs" id="nav-tab" role="tablist"> <div class="nav nav-tabs" id="nav-tab" role="tablist">
<a class="nav-link active show" id="nav-general-tab" data-toggle="tab" href="#nav-general" role="tab" aria-controls="general"><?php $L->p('General') ?></a> <a class="nav-link active show" id="nav-general-tab" data-toggle="tab" href="#nav-general" role="tab" aria-controls="general"><?php $L->p('General') ?></a>
<a class="nav-link" id="nav-advanced-tab" data-toggle="tab" href="#nav-advanced" role="tab" aria-controls="advanced"><?php $L->p('Advanced') ?></a> <a class="nav-link" id="nav-advanced-tab" data-toggle="tab" href="#nav-advanced" role="tab" aria-controls="advanced"><?php $L->p('Advanced') ?></a>
<?php if ($site->customFields()!="{}"): ?> <?php if (!empty($site->customFields())): ?>
<a class="nav-link" id="nav-custom-tab" data-toggle="tab" href="#nav-custom" role="tab" aria-controls="custom"><?php $L->p('Custom') ?></a> <a class="nav-link" id="nav-custom-tab" data-toggle="tab" href="#nav-custom" role="tab" aria-controls="custom"><?php $L->p('Custom') ?></a>
<?php endif ?> <?php endif ?>
<a class="nav-link" id="nav-seo-tab" data-toggle="tab" href="#nav-seo" role="tab" aria-controls="seo"><?php $L->p('SEO') ?></a> <a class="nav-link" id="nav-seo-tab" data-toggle="tab" href="#nav-seo" role="tab" aria-controls="seo"><?php $L->p('SEO') ?></a>
@ -288,10 +288,10 @@ echo Bootstrap::formOpen(array(
</script> </script>
</div> </div>
<?php if ($site->customFields()!="{}"): ?> <?php if (!empty($site->customFields())): ?>
<div id="nav-custom" class="tab-pane fade" role="tabpanel" aria-labelledby="custom-tab"> <div id="nav-custom" class="tab-pane fade" role="tabpanel" aria-labelledby="custom-tab">
<?php <?php
$customFields = json_decode($site->customFields(), true); $customFields = $site->customFields();
foreach($customFields as $field=>$options) { foreach($customFields as $field=>$options) {
if ($options['type']=="string") { if ($options['type']=="string") {
echo Bootstrap::formInputTextBlock(array( echo Bootstrap::formInputTextBlock(array(
@ -302,6 +302,14 @@ echo Bootstrap::formOpen(array(
'placeholder'=>(isset($options['placeholder'])?$options['placeholder']:''), 'placeholder'=>(isset($options['placeholder'])?$options['placeholder']:''),
'value'=>$page->custom($field) 'value'=>$page->custom($field)
)); ));
} elseif ($options['type']=="bool") {
echo Bootstrap::formCheckbox(array(
'name'=>'custom['.$field.']',
'label'=>(isset($options['label'])?$options['label']:''),
'placeholder'=>(isset($options['placeholder'])?$options['placeholder']:''),
'checked'=>$page->custom($field),
'labelForCheckbox'=>(isset($options['tip'])?$options['tip']:'')
));
} }
} }
?> ?>

View File

@ -79,7 +79,7 @@ echo Bootstrap::formOpen(array(
<div class="nav nav-tabs" id="nav-tab" role="tablist"> <div class="nav nav-tabs" id="nav-tab" role="tablist">
<a class="nav-link active show" id="nav-general-tab" data-toggle="tab" href="#nav-general" role="tab" aria-controls="general"><?php $L->p('General') ?></a> <a class="nav-link active show" id="nav-general-tab" data-toggle="tab" href="#nav-general" role="tab" aria-controls="general"><?php $L->p('General') ?></a>
<a class="nav-link" id="nav-advanced-tab" data-toggle="tab" href="#nav-advanced" role="tab" aria-controls="advanced"><?php $L->p('Advanced') ?></a> <a class="nav-link" id="nav-advanced-tab" data-toggle="tab" href="#nav-advanced" role="tab" aria-controls="advanced"><?php $L->p('Advanced') ?></a>
<?php if ($site->customFields()!="{}"): ?> <?php if (!empty($site->customFields())): ?>
<a class="nav-link" id="nav-custom-tab" data-toggle="tab" href="#nav-custom" role="tab" aria-controls="custom"><?php $L->p('Custom') ?></a> <a class="nav-link" id="nav-custom-tab" data-toggle="tab" href="#nav-custom" role="tab" aria-controls="custom"><?php $L->p('Custom') ?></a>
<?php endif ?> <?php endif ?>
<a class="nav-link" id="nav-seo-tab" data-toggle="tab" href="#nav-seo" role="tab" aria-controls="seo"><?php $L->p('SEO') ?></a> <a class="nav-link" id="nav-seo-tab" data-toggle="tab" href="#nav-seo" role="tab" aria-controls="seo"><?php $L->p('SEO') ?></a>
@ -263,19 +263,27 @@ echo Bootstrap::formOpen(array(
}); });
</script> </script>
</div> </div>
<?php if ($site->customFields()!="{}"): ?> <?php if (!empty($site->customFields())): ?>
<div id="nav-custom" class="tab-pane fade" role="tabpanel" aria-labelledby="custom-tab"> <div id="nav-custom" class="tab-pane fade" role="tabpanel" aria-labelledby="custom-tab">
<?php <?php
$customFields = json_decode($site->customFields(), true); $customFields = $site->customFields();
foreach($customFields as $field=>$options) { foreach($customFields as $field=>$options) {
if ($options['type']=="string") { if ($options['type']=="string") {
echo Bootstrap::formInputTextBlock(array( echo Bootstrap::formInputTextBlock(array(
'name'=>'custom['.$field.']', 'name'=>'custom['.$field.']',
'label'=>(isset($options['label'])?$options['label']:''),
'value'=>(isset($options['default'])?$options['default']:''), 'value'=>(isset($options['default'])?$options['default']:''),
'tip'=>(isset($options['tip'])?$options['tip']:''), 'tip'=>(isset($options['tip'])?$options['tip']:''),
'label'=>(isset($options['label'])?$options['label']:''),
'placeholder'=>(isset($options['placeholder'])?$options['placeholder']:'') 'placeholder'=>(isset($options['placeholder'])?$options['placeholder']:'')
)); ));
} elseif ($options['type']=="bool") {
echo Bootstrap::formCheckbox(array(
'name'=>'custom['.$field.']',
'label'=>(isset($options['label'])?$options['label']:''),
'placeholder'=>(isset($options['placeholder'])?$options['placeholder']:''),
'checked'=>(isset($options['checked'])?true:false),
'labelForCheckbox'=>(isset($options['tip'])?$options['tip']:'')
));
} }
} }
?> ?>

View File

@ -533,7 +533,7 @@
echo Bootstrap::formTextarea(array( echo Bootstrap::formTextarea(array(
'name'=>'customFields', 'name'=>'customFields',
'label'=>$L->g('Custom'), 'label'=>$L->g('Custom'),
'value'=>$site->customFields(), 'value'=>json_encode($site->customFields(), JSON_PRETTY_PRINT),
'class'=>'', 'class'=>'',
'placeholder'=>'', 'placeholder'=>'',
'tip'=>'', 'tip'=>'',

View File

@ -28,8 +28,6 @@ if (Sanitize::pathFile(PATH_THEMES, $site->theme().DS.'index.php')) {
$L->p('Please check your theme configuration in the admin panel. Check for an active theme.'); $L->p('Please check your theme configuration in the admin panel. Check for an active theme.');
} }
var_dump($page->custom('field3'));
// Plugins after site loaded // Plugins after site loaded
Theme::plugins('afterSiteLoad'); Theme::plugins('afterSiteLoad');

View File

@ -66,8 +66,13 @@ class Pages extends dbJSON {
$finalValue = $this->generateTags($tags); $finalValue = $this->generateTags($tags);
} elseif ($field=='custom') { } elseif ($field=='custom') {
if (isset($args['custom'])) { if (isset($args['custom'])) {
global $site;
$customFields = $site->customFields();
foreach ($args['custom'] as $customField=>$customValue) { foreach ($args['custom'] as $customField=>$customValue) {
$row['custom'][$customField]['value'] = Sanitize::html($customValue); $html = Sanitize::html($customValue);
// Store the custom field as defined type
settype($html, $customFields[$customField]['type']);
$row['custom'][$customField]['value'] = $html;
} }
unset($args['custom']); unset($args['custom']);
continue; continue;
@ -79,6 +84,7 @@ class Pages extends dbJSON {
// Default value for the field if not defined // Default value for the field if not defined
$finalValue = $value; $finalValue = $value;
} }
// Store the value as defined type
settype($finalValue, gettype($value)); settype($finalValue, gettype($value));
$row[$field] = $finalValue; $row[$field] = $finalValue;
} }
@ -178,8 +184,13 @@ class Pages extends dbJSON {
$finalValue = $this->generateTags($args['tags']); $finalValue = $this->generateTags($args['tags']);
} elseif ($field=='custom') { } elseif ($field=='custom') {
if (isset($args['custom'])) { if (isset($args['custom'])) {
global $site;
$customFields = $site->customFields();
foreach ($args['custom'] as $customField=>$customValue) { foreach ($args['custom'] as $customField=>$customValue) {
$row['custom'][$customField]['value'] = Sanitize::html($customValue); $html = Sanitize::html($customValue);
// Store the custom field as defined type
settype($html, $customFields[$customField]['type']);
$row['custom'][$customField]['value'] = $html;
} }
unset($args['custom']); unset($args['custom']);
continue; continue;

View File

@ -401,10 +401,11 @@ class Site extends dbJSON {
return date_default_timezone_set($timezone); return date_default_timezone_set($timezone);
} }
// Returns the custom fields // Returns the custom fields as array
public function customFields() public function customFields()
{ {
return Sanitize::htmlDecode($this->getField('customFields')); $customFields = Sanitize::htmlDecode($this->getField('customFields'));
return json_decode($customFields, true);
} }
} }