Replace generic alert, form and table templates with new Bootstrap equivalents
This commit is contained in:
@@ -3,52 +3,42 @@
|
||||
* FormView.php
|
||||
* Contains the form template.
|
||||
*
|
||||
* Kabuki CMS (C) 2013-2015, Aaron van Geffen
|
||||
* Global Data Lab code (C) Radboud University Nijmegen
|
||||
* Programming (C) Aaron van Geffen, 2015-2022
|
||||
*****************************************************************************/
|
||||
|
||||
class FormView extends SubTemplate
|
||||
{
|
||||
private $content_below;
|
||||
private $content_above;
|
||||
private $data;
|
||||
private $missing;
|
||||
private $fields;
|
||||
private $request_method;
|
||||
private $request_url;
|
||||
private $form;
|
||||
private array $data;
|
||||
private array $missing;
|
||||
private $title;
|
||||
|
||||
public function __construct(Form $form, $title = '')
|
||||
{
|
||||
$this->form = $form;
|
||||
$this->title = $title;
|
||||
$this->request_url = $form->request_url;
|
||||
$this->request_method = $form->request_method;
|
||||
$this->fields = $form->getFields();
|
||||
$this->missing = $form->getMissing();
|
||||
$this->data = $form->getData();
|
||||
$this->content_above = $form->content_above;
|
||||
$this->content_below = $form->content_below;
|
||||
}
|
||||
|
||||
protected function html_content($exclude = [], $include = [])
|
||||
{
|
||||
if (!empty($this->title))
|
||||
echo '
|
||||
<div class="admin_box">
|
||||
<h2>', htmlspecialchars($this->title), '</h2>';
|
||||
<h1>', $this->title, '</h1>';
|
||||
|
||||
foreach ($this->_subtemplates as $template)
|
||||
$template->html_main();
|
||||
|
||||
echo '
|
||||
<form action="', $this->request_url, '" method="', $this->request_method, '" enctype="multipart/form-data">';
|
||||
<form action="', $this->form->request_url, '" method="', $this->form->request_method, '" enctype="multipart/form-data">';
|
||||
|
||||
if (isset($this->content_above))
|
||||
echo $this->content_above;
|
||||
if (isset($this->form->content_above))
|
||||
echo $this->form->content_above;
|
||||
|
||||
echo '
|
||||
<dl>';
|
||||
$this->missing = $this->form->getMissing();
|
||||
$this->data = $this->form->getData();
|
||||
|
||||
foreach ($this->fields as $field_id => $field)
|
||||
foreach ($this->form->getFields() as $field_id => $field)
|
||||
{
|
||||
// Either we have a blacklist
|
||||
if (!empty($exclude) && in_array($field_id, $exclude))
|
||||
@@ -62,107 +52,230 @@ class FormView extends SubTemplate
|
||||
}
|
||||
|
||||
echo '
|
||||
</dl>
|
||||
<input type="hidden" name="', Session::getSessionTokenKey(), '" value="', Session::getSessionToken(), '">
|
||||
<div style="clear: both">
|
||||
<button type="submit" class="btn btn-primary">Save information</button>';
|
||||
<div class="form-group">
|
||||
<div class="offset-sm-2 col-sm-10">
|
||||
<button type="submit" name="submit" class="btn btn-primary">', $this->form->getSubmitButtonCaption(), '</button>';
|
||||
|
||||
if (isset($this->content_below))
|
||||
if (isset($this->form->content_below))
|
||||
echo '
|
||||
', $this->content_below;
|
||||
', $this->form->content_below;
|
||||
|
||||
echo '
|
||||
</div>
|
||||
</div>
|
||||
</form>';
|
||||
|
||||
if (!empty($this->title))
|
||||
echo '
|
||||
</div>';
|
||||
}
|
||||
|
||||
protected function renderField($field_id, $field)
|
||||
protected function renderField($field_id, array $field)
|
||||
{
|
||||
if (isset($field['before_html']))
|
||||
echo '</dl>
|
||||
', $field['before_html'], '
|
||||
<dl>';
|
||||
|
||||
if ($field['type'] != 'checkbox' && isset($field['label']))
|
||||
echo '
|
||||
<dt class="cont_', $field_id, isset($field['tab_class']) ? ' target target-' . $field['tab_class'] : '', '"', in_array($field_id, $this->missing) ? ' style="color: red"' : '', '>', $field['label'], '</dt>';
|
||||
elseif ($field['type'] === 'checkbox' && isset($field['header']))
|
||||
echo '
|
||||
<dt class="cont_', $field_id, isset($field['tab_class']) ? ' target target-' . $field['tab_class'] : '', '"', in_array($field_id, $this->missing) ? ' style="color: red"' : '', '>', $field['header'], '</dt>';
|
||||
', $field['before_html'];
|
||||
|
||||
echo '
|
||||
<dd class="cont_', $field_id, isset($field['dd_class']) ? ' ' . $field['dd_class'] : '', isset($field['tab_class']) ? ' target target-' . $field['tab_class'] : '', '">';
|
||||
<div class="row mb-2">';
|
||||
|
||||
if (isset($field['before']))
|
||||
echo $field['before'];
|
||||
|
||||
if ($field['type'] !== 'checkbox')
|
||||
if (isset($field['label']))
|
||||
echo '
|
||||
<label class="col-sm-2 col-form-label" for="', $field_id, '"', in_array($field_id, $this->missing) ? ' style="color: red"' : '', '>', $field['label'], ':</label>
|
||||
<div class="', isset($field['class']) ? $field['class'] : 'col-sm-6', '">';
|
||||
else
|
||||
echo '
|
||||
<div class="offset-sm-2 ', isset($field['class']) ? $field['class'] : 'col-sm-6', '">';
|
||||
|
||||
switch ($field['type'])
|
||||
{
|
||||
case 'select':
|
||||
echo '
|
||||
<select name="', $field_id, '" id="', $field_id, '"', !empty($field['disabled']) ? ' disabled' : '', '>';
|
||||
|
||||
if (isset($field['placeholder']))
|
||||
echo '
|
||||
<option value="">', $field['placeholder'], '</option>';
|
||||
|
||||
foreach ($field['options'] as $value => $option)
|
||||
echo '
|
||||
<option value="', $value, '"', $this->data[$field_id] == $value ? ' selected' : '', '>', htmlentities($option), '</option>';
|
||||
|
||||
echo '
|
||||
</select>';
|
||||
$this->renderSelect($field_id, $field);
|
||||
break;
|
||||
|
||||
case 'radio':
|
||||
foreach ($field['options'] as $value => $option)
|
||||
echo '
|
||||
<input type="radio" name="', $field_id, '" value="', $value, '"', $this->data[$field_id] == $value ? ' checked' : '', !empty($field['disabled']) ? ' disabled' : '', '> ', htmlentities($option);
|
||||
$this->renderRadio($field_id, $field);
|
||||
break;
|
||||
|
||||
case 'checkbox':
|
||||
echo '
|
||||
<label><input type="checkbox"', $this->data[$field_id] ? ' checked' : '', !empty($field['disabled']) ? ' disabled' : '', ' name="', $field_id, '"> ', htmlentities($field['label']), '</label>';
|
||||
$this->renderCheckbox($field_id, $field);
|
||||
break;
|
||||
|
||||
case 'textarea':
|
||||
echo '
|
||||
<textarea name="', $field_id, '" id="', $field_id, '" cols="', isset($field['columns']) ? $field['columns'] : 40, '" rows="', isset($field['rows']) ? $field['rows'] : 4, '"', !empty($field['disabled']) ? ' disabled' : '', '>', $this->data[$field_id], '</textarea>';
|
||||
$this->renderTextArea($field_id, $field);
|
||||
break;
|
||||
|
||||
case 'color':
|
||||
echo '
|
||||
<input type="color" name="', $field_id, '" id="', $field_id, '" value="', htmlentities($this->data[$field_id]), '"', !empty($field['disabled']) ? ' disabled' : '', '>';
|
||||
$this->renderColor($field_id, $field);
|
||||
break;
|
||||
|
||||
case 'numeric':
|
||||
echo '
|
||||
<input type="number"', isset($field['step']) ? ' step="' . $field['step'] . '"' : '', ' min="', isset($field['min_value']) ? $field['min_value'] : '0', '" max="', isset($field['max_value']) ? $field['max_value'] : '9999', '" name="', $field_id, '" id="', $field_id, '"', isset($field['size']) ? ' size="' . $field['size'] . '"' : '', isset($field['maxlength']) ? ' maxlength="' . $field['maxlength'] . '"' : '', ' value="', htmlentities($this->data[$field_id]), '"', !empty($field['disabled']) ? ' disabled' : '', '>';
|
||||
$this->renderNumeric($field_id, $field);
|
||||
break;
|
||||
|
||||
case 'file':
|
||||
if (!empty($this->data[$field_id]))
|
||||
echo '<img src="', $this->data[$field_id], '" alt=""><br>';
|
||||
$this->renderFile($field_id, $field);
|
||||
break;
|
||||
|
||||
echo '
|
||||
<input type="file" name="', $field_id, '" id="', $field_id, '"', !empty($field['disabled']) ? ' disabled' : '', '>';
|
||||
case 'captcha':
|
||||
$this->renderCaptcha($field_id, $field);
|
||||
break;
|
||||
|
||||
case 'text':
|
||||
case 'password':
|
||||
default:
|
||||
echo '
|
||||
<input type="', $field['type'], '" name="', $field_id, '" id="', $field_id, '"', isset($field['size']) ? ' size="' . $field['size'] . '"' : '', isset($field['maxlength']) ? ' maxlength="' . $field['maxlength'] . '"' : '', ' value="', htmlentities($this->data[$field_id]), '"', !empty($field['disabled']) ? ' disabled' : '', isset($field['trigger']) ? ' class="trigger-' . $field['trigger'] . '"' : '', '>';
|
||||
$this->renderText($field_id, $field);
|
||||
}
|
||||
|
||||
if (isset($field['after']))
|
||||
echo ' ', $field['after'];
|
||||
|
||||
if ($field['type'] !== 'checkbox')
|
||||
echo '
|
||||
</div>';
|
||||
|
||||
echo '
|
||||
</dd>';
|
||||
</div>';
|
||||
}
|
||||
|
||||
private function renderCaptcha($field_id, array $field)
|
||||
{
|
||||
echo '
|
||||
<div class="g-recaptcha" data-sitekey="', RECAPTCHA_API_KEY, '"></div>
|
||||
<script src="https://www.google.com/recaptcha/api.js"></script>';
|
||||
}
|
||||
|
||||
private function renderCheckbox($field_id, array $field)
|
||||
{
|
||||
echo '
|
||||
<div class="offset-sm-2 col-sm-10">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="checkbox"', $this->data[$field_id] ? ' checked' : '', !empty($field['disabled']) ? ' disabled' : '', ' name="', $field_id, '" id="check-', $field_id, '">
|
||||
<label class="form-check-label" for="check-', $field_id, '">
|
||||
', $field['label'], '
|
||||
</label>
|
||||
</div>
|
||||
</div>';
|
||||
}
|
||||
|
||||
private function renderColor($field_id, array $field)
|
||||
{
|
||||
echo '
|
||||
<input class="form-control" type="color" name="', $field_id, '" id="', $field_id, '" value="', htmlspecialchars($this->data[$field_id]), '"', !empty($field['disabled']) ? ' disabled' : '', '>';
|
||||
}
|
||||
|
||||
private function renderFile($field_id, array $field)
|
||||
{
|
||||
if (!empty($this->data[$field_id]))
|
||||
echo 'Currently using asset <tt>', $this->data[$field_id], '</tt>. Upload to overwrite.<br>';
|
||||
|
||||
echo '
|
||||
<input class="form-control" type="file" name="', $field_id, '" id="', $field_id, '"', !empty($field['disabled']) ? ' disabled' : '', '>';
|
||||
}
|
||||
|
||||
private function renderNumeric($field_id, array $field)
|
||||
{
|
||||
echo '
|
||||
<input class="form-control" type="number"',
|
||||
isset($field['step']) ? ' step="' . $field['step'] . '"' : '',
|
||||
' min="', isset($field['min_value']) ? $field['min_value'] : '0', '"',
|
||||
' max="', isset($field['max_value']) ? $field['max_value'] : '9999', '"',
|
||||
' name="', $field_id, '" id="', $field_id, '"',
|
||||
isset($field['size']) ? ' size="' . $field['size'] . '"' : '',
|
||||
isset($field['maxlength']) ? ' maxlength="' . $field['maxlength'] . '"' : '',
|
||||
' value="', htmlspecialchars($this->data[$field_id]), '"',
|
||||
!empty($field['disabled']) ? ' disabled' : '', '>';
|
||||
}
|
||||
|
||||
private function renderRadio($field_id, array $field)
|
||||
{
|
||||
foreach ($field['options'] as $value => $option)
|
||||
echo '
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="radio" name="', $field_id, '" id="radio-', $field_id, '-', $value, '" value="', $value, '"', $this->data[$field_id] == $value ? ' checked' : '', !empty($field['disabled']) ? ' disabled' : '', '>
|
||||
<label class="form-check-label" for="radio-', $field_id, '-', $value, '">
|
||||
', htmlspecialchars($option), '
|
||||
</label>
|
||||
</div>';
|
||||
}
|
||||
|
||||
private function renderSelect($field_id, array $field)
|
||||
{
|
||||
echo '
|
||||
<select class="form-select" name="', $field_id, !empty($field['multiple']) ? '[]' : '',
|
||||
'" id="', $field_id, '"',
|
||||
!empty($field['disabled']) ? ' disabled' : '',
|
||||
!empty($field['multiple']) ? ' multiple' : '',
|
||||
!empty($field['size']) ? ' size="' . $field['size'] . '"' : '',
|
||||
'>';
|
||||
|
||||
if (isset($field['placeholder']))
|
||||
echo '
|
||||
<option value="">', $field['placeholder'], '</option>';
|
||||
|
||||
foreach ($field['options'] as $key => $value)
|
||||
{
|
||||
if (is_array($value))
|
||||
{
|
||||
assert(empty($field['multiple']));
|
||||
$this->renderSelectOptionGroup($field_id, $key, $value);
|
||||
}
|
||||
else
|
||||
$this->renderSelectOption($field_id, $value, $key, !empty($field['multiple']));
|
||||
}
|
||||
|
||||
echo '
|
||||
</select>';
|
||||
}
|
||||
|
||||
private function renderSelectOption($field_id, $label, $value, $multiple = false)
|
||||
{
|
||||
echo '
|
||||
<option value="', $value, '"',
|
||||
!$multiple && $this->data[$field_id] == $value ? ' selected' : '',
|
||||
$multiple && in_array($value, $this->data[$field_id]) ? ' selected' : '',
|
||||
'>', htmlspecialchars($label), '</option>';
|
||||
}
|
||||
|
||||
private function renderSelectOptionGroup($field_id, $label, $options)
|
||||
{
|
||||
echo '
|
||||
<optgroup label="', $label, '">';
|
||||
|
||||
foreach ($options as $value => $option)
|
||||
$this->renderSelectOption($field_id, $option, $value);
|
||||
|
||||
echo '
|
||||
</optgroup>';
|
||||
}
|
||||
|
||||
private function renderText($field_id, array $field)
|
||||
{
|
||||
echo '
|
||||
<input class="form-control" ',
|
||||
'type="', $field['type'], '" ',
|
||||
'name="', $field_id, '" ',
|
||||
'id="', $field_id, '"',
|
||||
isset($field['size']) ? ' size="' . $field['size'] . '"' : '',
|
||||
isset($field['maxlength']) ? ' maxlength="' . $field['maxlength'] . '"' : '',
|
||||
isset($this->data[$field_id]) ? ' value="' . htmlspecialchars($this->data[$field_id]) . '"' : '',
|
||||
isset($field['placeholder']) ? ' placeholder="' . $field['placeholder'] . '"' : '',
|
||||
!empty($field['disabled']) ? ' disabled' : '',
|
||||
isset($field['trigger']) ? ' class="trigger-' . $field['trigger'] . '"' : '',
|
||||
'>';
|
||||
}
|
||||
|
||||
private function renderTextArea($field_id, array $field)
|
||||
{
|
||||
echo '
|
||||
<textarea class="form-control' .
|
||||
'" name="', $field_id,
|
||||
'" id="', $field_id,
|
||||
'" cols="', isset($field['columns']) ? $field['columns'] : 40,
|
||||
'" rows="', isset($field['rows']) ? $field['rows'] : 4, '"',
|
||||
isset($field['placeholder']) ? ' placeholder="' . $field['placeholder'] . '"' : '',
|
||||
'"', !empty($field['disabled']) ? ' disabled' : '',
|
||||
'>', $this->data[$field_id], '</textarea>';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user