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

View file

@ -226,54 +226,52 @@ EOF;
return '</form>'; return '</form>';
} }
public static function formInputTextBlock($args) public static function formInputTextBlock($args)
{ {
$name = $args['name']; $name = $args['name'];
$id = isset($args['id'])?$args['id']:$name;
$disabled = empty($args['disabled'])?'':'disabled'; $disabled = empty($args['disabled'])?'':'disabled';
$readonly = empty($args['readonly'])?'':'readonly';
$placeholder = isset($args['placeholder'])?$args['placeholder']:''; $placeholder = isset($args['placeholder'])?$args['placeholder']:'';
$value = isset($args['value'])?$args['value']:''; $value = isset($args['value'])?$args['value']:'';
$type = isset($args['type'])?$args['type']:'text';
$id = 'js'.$name;
if (isset($args['id'])) {
$id = $args['id'];
}
$tip = ''; $tip = '';
if (!empty($args['tip'])) { if (!empty($args['tip'])) {
$tip = '<div class="form-text">'.$args['tip'].'</div>'; $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 = ''; $label = '';
if (!empty($args['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'; $class = 'form-control';
if (isset($args['type'])) { if (isset($args['class'])) {
$type = $args['type']; $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 return <<<EOF
<div class="$class"> <div class="mb-2">
$label $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 $tip
</div> </div>
EOF; EOF;
} }
public static function formInputFile($args) public static function formInputFile($args)
{ {
$id = 'js'.$args['name']; $id = 'js'.$args['name'];
@ -335,12 +333,12 @@ EOF;
} }
$disabled = isset($args['disabled'])?'disabled':''; $disabled = isset($args['disabled'])?'disabled':'';
$class = 'mb-3 m-0'; $class = 'mb-2 m-0';
if (isset($args['class'])) { if (isset($args['class'])) {
$class = $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'])) { if (isset($args['labelClass'])) {
$labelClass = $args['labelClass']; $labelClass = $args['labelClass'];
} }
@ -358,11 +356,20 @@ EOF;
$checked = $args['checked']?'checked':''; $checked = $args['checked']?'checked':'';
$value = $checked?'1':'0'; $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 return <<<EOF
<div class="$class"> <div class="$class">
$label $label
<div class="form-check"> <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> <label class="form-check-label" for="$id">$labelForCheckbox</label>
$tip $tip
</div> </div>

View file

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