Add custom fields

This commit is contained in:
Diego Najar 2022-02-21 16:21:32 +01:00
parent 8e7a7eaf18
commit 52ecf89b77
3 changed files with 145 additions and 41 deletions

View file

@ -60,7 +60,6 @@
// Save the current page
// This function set the global variable "_pageKey"
function savePage(args = []) {
console.log(_pageKey);
if (_pageKey == null) {
logs('Error, page not created.');
@ -132,15 +131,23 @@
// Ctrl+S or Command+S
if ((event.ctrlKey || event.metaKey) && event.which == 83) {
event.preventDefault();
var customFields = {}
$('input[name^="custom"]').each(function() {
var field = $(this).data('field')
var value = $(this).val()
customFields[field] = value
});
var args = {
title: $('#title').val(),
content: editorGetContent(),
custom: customFields,
coverImage: $('#coverImage').val(),
category: $('#category option:selected').val(),
tags: $("#tags option:selected").map(function() {
return this.value
}).get().join(",")
}
savePage(args);
disableBtnSave();
return false;
@ -178,9 +185,17 @@
});
$('#btnSave').on('click', function() {
var customFields = {}
$('input[name^="custom"]').each(function() {
var field = $(this).data('field')
var value = $(this).val()
customFields[field] = value
});
var args = {
title: $('#title').val(),
content: editorGetContent(),
custom: customFields,
coverImage: $('#coverImage').val(),
category: $('#category option:selected').val(),
tags: $("#tags option:selected").map(function() {
@ -358,7 +373,25 @@
if (content.length < 100) {
return false;
}
savePage();
var customFields = {}
$('input[name^="custom"]').each(function() {
var field = $(this).data('field')
var value = $(this).val()
customFields[field] = value
});
var args = {
title: $('#title').val(),
content: content,
custom: customFields,
coverImage: $('#coverImage').val(),
category: $('#category option:selected').val(),
tags: $("#tags option:selected").map(function() {
return this.value
}).get().join(",")
}
savePage(args);
disableBtnSave();
}, 1000 * 60 * AUTOSAVE_INTERVAL);
@ -628,10 +661,70 @@
</div>
<!-- End Title -->
<!-- Custom fields without position or top position -->
<?php
$customFields = $site->customFields();
foreach ($customFields as $field=>$options) {
if ( !isset($options['position']) || ($options['position']=='top') ) {
if ($options['type']=="string") {
echo Bootstrap::formInputTextBlock(array(
'name'=>'custom['.$field.']',
'label'=>(isset($options['label'])?$options['label']:''),
'value'=>(($pageKey && $page->custom($field))?$page->custom($field):''),
'tip'=>(isset($options['tip'])?$options['tip']:''),
'placeholder'=>(isset($options['placeholder'])?$options['placeholder']:''),
'class'=>'form-control-lg',
'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']:''),
'data' => array('field' => $field)
));
}
}
}
?>
<!-- Editor -->
<textarea class="form-control flex-grow-1" placeholder="" id="editor"><?php echo ($pageKey ? $page->contentRaw() : '') ?></textarea>
<!-- End Editor -->
<div class="mb-2"></div>
<!-- Custom fields bottom position -->
<?php
$customFields = $site->customFields();
foreach ($customFields as $field=>$options) {
if ( isset($options['position']) && ($options['position']=='bottom') ) {
if ($options['type']=="string") {
echo Bootstrap::formInputTextBlock(array(
'name'=>'custom['.$field.']',
'label'=>(isset($options['label'])?$options['label']:''),
'value'=>(($pageKey && $page->custom($field))?$page->custom($field):''),
'tip'=>(isset($options['tip'])?$options['tip']:''),
'placeholder'=>(isset($options['placeholder'])?$options['placeholder']:''),
'class'=>'form-control-lg',
'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']:''),
'data' => array('field' => $field)
));
}
}
}
?>
</div> <!-- End <div class="col-sm-9 h-100"> -->
<div class="col-sm-3 h-100 mt-2">

View file

@ -226,54 +226,52 @@ EOF;
return '</form>';
}
public static function formInputTextBlock($args)
{
$name = $args['name'];
$id = isset($args['id'])?$args['id']:$name;
$disabled = empty($args['disabled'])?'':'disabled';
$readonly = empty($args['readonly'])?'':'readonly';
$placeholder = isset($args['placeholder'])?$args['placeholder']:'';
$value = isset($args['value'])?$args['value']:'';
$id = 'js'.$name;
if (isset($args['id'])) {
$id = $args['id'];
}
$type = isset($args['type'])?$args['type']:'text';
$tip = '';
if (!empty($args['tip'])) {
$tip = '<div class="form-text">'.$args['tip'].'</div>';
}
$class = 'mb-3 m-0';
if (isset($args['class'])) {
$class = $args['class'];
}
$labelClass = 'mt-4 mb-2 pb-2 border-bottom text-uppercase w-100';
if (isset($args['labelClass'])) {
$labelClass = $args['labelClass'];
}
$label = '';
if (!empty($args['label'])) {
$label = '<label class="'.$labelClass.'" for="'.$id.'">'.$args['label'].'</label>';
$label = '<label for="'.$id.'" class="col-sm-2 col-form-label">'.$args['label'].'</label>';
}
$type = 'text';
if (isset($args['type'])) {
$type = $args['type'];
$class = 'form-control';
if (isset($args['class'])) {
$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.'"';
}
}
}
return <<<EOF
<div class="$class">
<div class="mb-2">
$label
<input type="text" value="$value" class="form-control" id="$id" name="$name" placeholder="$placeholder" $disabled>
<input class="$class" $data id="$id" name="$name" value="$value" placeholder="$placeholder" type="$type" $disabled $readonly>
$tip
</div>
EOF;
}
public static function formInputFile($args)
{
$id = 'js'.$args['name'];
@ -335,12 +333,12 @@ EOF;
}
$disabled = isset($args['disabled'])?'disabled':'';
$class = 'mb-3 m-0';
$class = 'mb-2 m-0';
if (isset($args['class'])) {
$class = $args['class'];
}
$labelClass = 'mt-4 mb-2 pb-2 border-bottom text-uppercase w-100';
$labelClass = 'mb-2 pb-2 border-bottom text-uppercase w-100';
if (isset($args['labelClass'])) {
$labelClass = $args['labelClass'];
}
@ -358,11 +356,20 @@ EOF;
$checked = $args['checked']?'checked':'';
$value = $checked?'1':'0';
$data = '';
if (isset($args['data'])) {
if (is_array($args['data'])) {
foreach ($args['data'] as $x => $y) {
$data .= ' data-'.$x.' = "'.$y.'"';
}
}
}
return <<<EOF
<div class="$class">
$label
<div class="form-check">
<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>
<input type="hidden" $data 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>
$tip
</div>

View file

@ -83,9 +83,11 @@ class Pages extends dbJSON {
foreach ($args['custom'] as $customField=>$customValue) {
$html = Sanitize::html($customValue);
// Store the custom field as defined type
if (isset($customFields[$customField])) {
settype($html, $customFields[$customField]['type']);
$row['custom'][$customField]['value'] = $html;
}
}
unset($args['custom']);
continue;
}
@ -204,9 +206,11 @@ class Pages extends dbJSON {
foreach ($args['custom'] as $customField=>$customValue) {
$html = Sanitize::html($customValue);
// Store the custom field as defined type
if (isset($customFields[$customField])) {
settype($html, $customFields[$customField]['type']);
$row['custom'][$customField]['value'] = $html;
}
}
unset($args['custom']);
continue;
}