Custom fields

This commit is contained in:
Diego Najar 2019-09-02 18:24:34 +02:00
parent 2e44c6fd8c
commit cc73f609e4
8 changed files with 154 additions and 5 deletions

View File

@ -89,6 +89,9 @@ echo Bootstrap::formOpen(array(
<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" id="nav-advanced-tab" data-toggle="tab" href="#nav-advanced" role="tab" aria-controls="advanced"><?php $L->p('Advanced') ?></a>
<?php if ($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>
<?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>
</div>
</nav>
@ -284,6 +287,27 @@ echo Bootstrap::formOpen(array(
});
</script>
</div>
<?php if ($site->customFields()!="{}"): ?>
<div id="nav-custom" class="tab-pane fade" role="tabpanel" aria-labelledby="custom-tab">
<?php
$customFields = json_decode($site->customFields(), true);
foreach($customFields as $field=>$options) {
if ($options['type']=="string") {
echo Bootstrap::formInputTextBlock(array(
'name'=>'custom['.$field.']',
'value'=>(isset($options['default'])?$options['default']:''),
'tip'=>(isset($options['tip'])?$options['tip']:''),
'label'=>(isset($options['label'])?$options['label']:''),
'placeholder'=>(isset($options['placeholder'])?$options['placeholder']:''),
'value'=>$page->custom($field)
));
}
}
?>
</div>
<?php endif ?>
<div id="nav-seo" class="tab-pane fade" role="tabpanel" aria-labelledby="seo-tab">
<?php
// Friendly URL

View File

@ -79,6 +79,9 @@ echo Bootstrap::formOpen(array(
<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" id="nav-advanced-tab" data-toggle="tab" href="#nav-advanced" role="tab" aria-controls="advanced"><?php $L->p('Advanced') ?></a>
<?php if ($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>
<?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>
</div>
</nav>
@ -260,6 +263,24 @@ echo Bootstrap::formOpen(array(
});
</script>
</div>
<?php if ($site->customFields()!="{}"): ?>
<div id="nav-custom" class="tab-pane fade" role="tabpanel" aria-labelledby="custom-tab">
<?php
$customFields = json_decode($site->customFields(), true);
foreach($customFields as $field=>$options) {
if ($options['type']=="string") {
echo Bootstrap::formInputTextBlock(array(
'name'=>'custom['.$field.']',
'value'=>(isset($options['default'])?$options['default']:''),
'tip'=>(isset($options['tip'])?$options['tip']:''),
'label'=>(isset($options['label'])?$options['label']:''),
'placeholder'=>(isset($options['placeholder'])?$options['placeholder']:'')
));
}
}
?>
</div>
<?php endif ?>
<div id="nav-seo" class="tab-pane fade" role="tabpanel" aria-labelledby="seo-tab">
<?php
// Friendly URL

View File

@ -19,7 +19,8 @@
<a class="nav-item nav-link" id="nav-social-tab" data-toggle="tab" href="#social" role="tab" aria-controls="nav-social" aria-selected="false"><?php $L->p('Social Networks') ?></a>
<a class="nav-item nav-link" id="nav-images-tab" data-toggle="tab" href="#images" role="tab" aria-controls="nav-images" aria-selected="false"><?php $L->p('Images') ?></a>
<a class="nav-item nav-link" id="nav-language-tab" data-toggle="tab" href="#language" role="tab" aria-controls="nav-language" aria-selected="false"><?php $L->p('Language') ?></a>
<a class="nav-item nav-link" id="nav-language-tab" data-toggle="tab" href="#logo" role="tab" aria-controls="nav-logo" aria-selected="false"><?php $L->p('Logo') ?></a>
<a class="nav-item nav-link" id="nav-custom-fields-tab" data-toggle="tab" href="#custom-fields" role="tab" aria-controls="nav-custom-fields" aria-selected="false"><?php $L->p('Custom fields') ?></a>
<a class="nav-item nav-link" id="nav-logo-tab" data-toggle="tab" href="#logo" role="tab" aria-controls="nav-logo" aria-selected="false"><?php $L->p('Logo') ?></a>
</div>
</nav>
@ -433,6 +434,15 @@
'placeholder'=>'',
'tip'=>''
));
echo Bootstrap::formInputText(array(
'name'=>'dribbble',
'label'=>'Dribbble',
'value'=>$site->dribbble(),
'class'=>'',
'placeholder'=>'',
'tip'=>''
));
?>
</div>
@ -515,6 +525,23 @@
?>
</div>
<!-- Custom fields -->
<div class="tab-pane" id="custom-fields" role="tabpanel" aria-labelledby="custom-fields-tab">
<?php
echo Bootstrap::formTitle(array('title'=>$L->g('Custom fields')));
echo Bootstrap::formTextarea(array(
'name'=>'customFields',
'label'=>$L->g('Custom'),
'value'=>$site->customFields(),
'class'=>'',
'placeholder'=>'',
'tip'=>'',
'rows'=>15
));
?>
</div>
<!-- Site logo tab -->
<div class="tab-pane" id="logo" role="tabpanel" aria-labelledby="logo-tab">
<?php

View File

@ -28,6 +28,8 @@ 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.');
}
var_dump($page->custom('field3'));
// Plugins after site loaded
Theme::plugins('afterSiteLoad');

View File

@ -575,6 +575,15 @@ function editSettings($args) {
$args['extremeFriendly'] = (($args['extremeFriendly']=='true')?true:false);
}
if (isset($args['customFields'])) {
// Custom fields need to be JSON format valid, also the empty JSON need to be "{}"
json_decode($args['customFields']);
if (json_last_error() != JSON_ERROR_NONE) {
return false;
}
$pages->setCustomFields($args['customFields']);
}
if ($site->set($args)) {
// Check current order-by if changed it reorder the content
if ($site->orderBy()!=ORDER_BY) {

View File

@ -64,6 +64,14 @@ class Pages extends dbJSON {
$tags = $args['tags'];
}
$finalValue = $this->generateTags($tags);
} elseif ($field=='custom') {
if (isset($args['custom'])) {
foreach ($args['custom'] as $customField=>$customValue) {
$row['custom'][$customField]['value'] = Sanitize::html($customValue);
}
unset($args['custom']);
continue;
}
} elseif (isset($args[$field])) {
// Sanitize if will be stored on database
$finalValue = Sanitize::html($args[$field]);
@ -168,6 +176,14 @@ class Pages extends dbJSON {
foreach ($this->dbFields as $field=>$value) {
if ( ($field=='tags') && isset($args['tags'])) {
$finalValue = $this->generateTags($args['tags']);
} elseif ($field=='custom') {
if (isset($args['custom'])) {
foreach ($args['custom'] as $customField=>$customValue) {
$row['custom'][$customField]['value'] = Sanitize::html($customValue);
}
unset($args['custom']);
continue;
}
} elseif (isset($args[$field])) {
// Sanitize if will be stored on database
$finalValue = Sanitize::html($args[$field]);
@ -581,8 +597,6 @@ class Pages extends dbJSON {
return $list;
}
public function sortBy()
{
if (ORDER_BY=='date') {
@ -772,4 +786,30 @@ class Pages extends dbJSON {
return $this->save();
}
// Insert custom fields to all the pages in the database
// The structure for the custom fields need to be a valid JSON format
// The custom fields are incremental, this means the custom fields are never deleted
// The pages only store the value of the custom field, the structure of the custom fields are in the database site.php
public function setCustomFields($fields)
{
$customFields = json_decode($fields, true);
if (json_last_error() != JSON_ERROR_NONE) {
return false;
}
foreach ($this->db as $pageKey=>$pageFields) {
foreach ($customFields as $customField=>$customValues) {
if (!isset($pageFields['custom'][$customField])) {
$defaultValue = '';
if (isset($customValues['default'])) {
$defaultValue = $customValues['default'];
}
$this->db[$pageKey]['custom'][$customField]['value'] = $defaultValue;
}
}
}
return $this->save();
}
}

View File

@ -563,4 +563,17 @@ class Page {
return $string ? implode(', ', $string) . ' ago' : 'Just now';
}
// Returns the value from the field, false if the fields doesn't exists
// If you set the $option as TRUE, the function returns an array with all the values of the field
public function custom($field, $options=false)
{
if (isset($this->vars['custom'][$field])) {
if ($options) {
return $this->vars['custom'][$field];
}
return $this->vars['custom'][$field]['value'];
}
return false;
}
}

View File

@ -31,6 +31,7 @@ class Site extends dbJSON {
'gitlab'=> '',
'linkedin'=> '',
'mastodon'=> '',
'dribbble'=> '',
'orderBy'=> 'date', // date or position
'extremeFriendly'=> true,
'autosaveInterval'=> 2, // minutes
@ -44,7 +45,8 @@ class Site extends dbJSON {
'thumbnailHeight'=> 400, // px
'thumbnailQuality'=> 100,
'logo'=> '',
'markdownParser'=> true
'markdownParser'=> true,
'customFields'=> '{}'
);
function __construct()
@ -185,6 +187,11 @@ class Site extends dbJSON {
return $this->getField('mastodon');
}
public function dribbble()
{
return $this->getField('dribbble');
}
public function orderBy()
{
return $this->getField('orderBy');
@ -394,4 +401,10 @@ class Site extends dbJSON {
return date_default_timezone_set($timezone);
}
}
// Returns the custom fields
public function customFields()
{
return Sanitize::htmlDecode($this->getField('customFields'));
}
}