check json format for custom fields, add position menu for custom fields, fix issues with boolean types

This commit is contained in:
Diego Najar 2022-05-03 19:45:01 +02:00
parent c8fa8d5dd6
commit 66ff89c3ab
7 changed files with 135 additions and 41 deletions

View file

@ -131,12 +131,21 @@
// Ctrl+S or Command+S
if ((event.ctrlKey || event.metaKey) && event.which == 83) {
event.preventDefault();
// Parse all custom fields inputs
var customFields = {}
$('input[name^="custom"]').each(function() {
var field = $(this).data('field')
var value = $(this).val()
customFields[field] = value
});
$('select[name^="custom"]').each(function() {
var field = $(this).data('field');
var value = $(this).val();
customFields[field] = value;
});
// Create array with all inputs
var args = {
slug: $('#friendlyURL').val(),
title: $('#title').val(),
@ -186,12 +195,20 @@
});
$('#btnSave').on('click', function() {
// Parse all custom fields inputs
var customFields = {}
$('input[name^="custom"]').each(function() {
var field = $(this).data('field')
var value = $(this).val()
customFields[field] = value
});
$('select[name^="custom"]').each(function() {
var field = $(this).data('field');
var value = $(this).val();
customFields[field] = value;
});
// Create array with all inputs
var args = {
slug: $('#friendlyURL').val(),
title: $('#title').val(),
@ -383,13 +400,20 @@
return false;
}
// Parse all custom fields inputs
var customFields = {}
$('input[name^="custom"]').each(function() {
var field = $(this).data('field')
var value = $(this).val()
customFields[field] = value
});
$('select[name^="custom"]').each(function() {
var field = $(this).data('field');
var value = $(this).val();
customFields[field] = value;
});
// Create array with all inputs
var args = {
slug: $('#friendlyURL').val(),
title: $('#title').val(),
@ -687,12 +711,12 @@
'data' => array('field' => $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'=>(($pageKey && $page->custom($field))?true:false),
'labelForCheckbox'=>(isset($options['tip'])?$options['tip']:''),
echo Bootstrap::formSelectBlock(array(
'name' => 'custom['.$field.']',
'label' => (isset($options['label'])?$options['label']:''),
'options' => array('true' => $L->g('Enabled'), 'false' => $L->g('Disabled')),
'selected' => (($pageKey && $page->custom($field))?'true':'false'),
'tip' => (isset($options['tip'])?$options['tip']:''),
'data' => array('field' => $field)
));
}
@ -722,12 +746,12 @@
'data' => array('field' => $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'=>(($pageKey && $page->custom($field))?true:false),
'labelForCheckbox'=>(isset($options['tip'])?$options['tip']:''),
echo Bootstrap::formSelectBlock(array(
'name' => 'custom['.$field.']',
'label' => (isset($options['label'])?$options['label']:''),
'options' => array('true' => $L->g('Enabled'), 'false' => $L->g('Disabled')),
'selected' => (($pageKey && $page->custom($field))?'true':'false'),
'tip' => (isset($options['tip'])?$options['tip']:''),
'data' => array('field' => $field)
));
}
@ -800,6 +824,37 @@
</script>
<!-- End Tags -->
<!-- Custom fields menu position -->
<?php
$customFields = $site->customFields();
foreach ($customFields as $field=>$options) {
if ( isset($options['position']) && ($options['position']=='menu') ) {
if ($options['type']=="string") {
echo '<h6 class="text-uppercase mt-4">'.(isset($options['label'])?$options['label']:'').'</h6>';
echo Bootstrap::formInputTextBlock(array(
'name'=>'custom['.$field.']',
'value'=>(($pageKey && $page->custom($field))?$page->custom($field):''),
'tip'=>(isset($options['tip'])?$options['tip']:''),
'placeholder'=>(isset($options['placeholder'])?$options['placeholder']:''),
'class'=>'',
'data' => array('field' => $field)
));
} elseif ($options['type']=="bool") {
echo '<h6 class="text-uppercase mt-4">'.(isset($options['label'])?$options['label']:'').'</h6>';
echo Bootstrap::formSelectBlock(array(
'name' => 'custom['.$field.']',
'label' => '',
'options' => array('true' => $L->g('Enabled'), 'false' => $L->g('Disabled')),
'selected' => (($pageKey && $page->custom($field))?true:false),
'tip' => (isset($options['tip'])?$options['tip']:''),
'data' => array('field' => $field)
));
}
}
}
?>
<h6 class="text-uppercase mt-4"><?php $L->p('More options') ?></h6>
<ul class="list-group">
<li class="list-group-item p-0 pt-3 bg-transparent border-0"><a onclick="fmOpen()" href="#"><i class="bi bi-files"></i><?php $L->p('Files & images') ?></a></li>

View file

@ -24,9 +24,10 @@
});
</script>
<?php
<img class="mx-auto d-block w-25 mb-4" alt="logo" src="<?php echo HTML_PATH_CORE_IMG . 'logo.svg' ?>" />
<h1 class="text-center text-uppercase mb-4"><?php echo $site->title() ?></h1>
echo '<h1 class="text-center fw-normal mb-5">'.$site->title().'</h1>';
<?php
echo Bootstrap::formOpen(array('name'=>'login'));
@ -60,7 +61,7 @@ echo Bootstrap::formOpen(array('name'=>'login'));
</div>
<div class="mt-4">
<button type="submit" class="btn btn-primary btn-lg me-2 w-100" name="save">'.$L->g('Login').'</button>
<button type="submit" class="btn btn-secondary btn-lg me-2 w-100" name="save">'.$L->g('Login').'</button>
</div>
';

View file

@ -48,6 +48,12 @@
args[key] = value;
});
if (!isJson($('#customFields').val())) {
logs('Invalid JSON format for custom fields.');
showAlertError("<?php $L->p('Invalid JSON format for custom fields') ?>");
return false;
}
api.saveSettings(args).then(function(response) {
if (response.status == 0) {
logs('Settings saved.');
@ -685,7 +691,8 @@
'value' => json_encode($site->customFields(), JSON_PRETTY_PRINT),
'tip' => $L->g('define-custom-fields-for-the-content'),
'rows' => 15,
'data' => array('save' => 'true')
'data' => array('save' => 'true'),
'disable-current-value' => false
));
?>
</div>

View file

@ -7,17 +7,16 @@ define('BLUDIT_RELEASE_DATE', '2021-05-23');
define('BLUDIT_BUILD', '20210523');
// Debug mode
// Change to FALSE, for prevent warning or errors on browser
define('DEBUG_MODE', TRUE);
define('DEBUG_TYPE', 'INFO'); // INFO, TRACE
error_reporting(0); // Turn off all error reporting
ini_set("display_errors", 0); // Turn off display errors in browser
ini_set('display_startup_errors', 0);
if (DEBUG_MODE) {
// Turn on all error reporting
ini_set("display_errors", 0);
ini_set('display_startup_errors',0);
// Turn on all error reporting, will be display in log server
ini_set("html_errors", 1);
ini_set('log_errors', 1);
error_reporting(E_ALL | E_STRICT | E_NOTICE);
error_reporting(E_ALL);
}
// PHP paths

View file

@ -84,15 +84,30 @@ EOF;
$class = $class.' '.$args['class'];
}
$html = '<div>';
if (!empty($args['label'])) {
$html .= '<label for="'.$id.'">'.$args['label'].'</label>';
$tip = '';
if (!empty($args['tip'])) {
$tip = '<div class="form-text">'.$args['tip'].'</div>';
}
$html .= '<select id="'.$id.'" name="'.$name.'" class="'.$class.'">';
$data = 'data-current-value="'.$args['selected'].'"';
if (isset($args['data'])) {
if (is_array($args['data'])) {
foreach ($args['data'] as $x => $y) {
$data .= 'data-'.$x.' = "'.$y.'"';
}
}
}
$html = '<div class="mb-2">';
if (!empty($args['label'])) {
$html .= '<label class="col-sm-2 col-form-label" for="'.$id.'">'.$args['label'].'</label>';
}
$html .= '<select id="'.$id.'" name="'.$name.'" class="'.$class.'" '.$data.'>';
foreach ($args['options'] as $key=>$value) {
$html .= '<option '.(($key==$args['selected'])?'selected':'').' value="'.$key.'">'.$value.'</option>';
}
$html .= '</select>';
$html .= $tip;
$html .= '</div>';
return $html;
@ -121,14 +136,16 @@ EOF;
$class = $class.' '.$args['class'];
}
$data = 'data-current-value="'.$value.'"';
if (isset($args['data'])) {
if (is_array($args['data'])) {
foreach ($args['data'] as $x => $y) {
$data .= ' data-'.$x.' = "'.$y.'"';
}
}
}
if (!isset($args['disable-current-value'])) {
$data = 'data-current-value="'.$value.'"';
}
if (isset($args['data'])) {
if (is_array($args['data'])) {
foreach ($args['data'] as $x => $y) {
$data .= ' data-'.$x.' = "'.$y.'"';
}
}
}
return <<<EOF
<div class="mb-3 row">

View file

@ -1,5 +1,5 @@
/*
Javascript global functions
Javascript global functions
*/
function sanitizeHTML(text) {
@ -55,4 +55,16 @@ function formatBytes(bytes, decimals = 2) {
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
/*
Returns TRUE if the JSON text is valid, FALSE otherwise
*/
function isJson(text) {
try {
JSON.parse(text);
} catch (e) {
return false;
}
return true;
}

View file

@ -213,7 +213,15 @@ class Pages extends dbJSON {
$html = Sanitize::html($customValue);
// Store the custom field as defined type
if (isset($customFields[$customField])) {
settype($html, $customFields[$customField]['type']);
if ($customFields[$customField]['type']=='bool') {
if ($html==='false' or $html===false) {
$html = false;
} else {
$html = true;
}
} else {
settype($html, $customFields[$customField]['type']);
}
$row['custom'][$customField]['value'] = $html;
}
}
@ -853,12 +861,8 @@ class Pages extends dbJSON {
// 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)
public function setCustomFields($customFields)
{
$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])) {
@ -870,7 +874,6 @@ class Pages extends dbJSON {
}
}
}
return $this->save();
}