engelsystem/includes/sys_form.php

469 lines
13 KiB
PHP
Raw Normal View History

2016-11-14 18:14:23 +01:00
<?php
2022-04-13 01:02:37 +02:00
2016-11-14 18:14:23 +01:00
// Methods to build a html form.
use Carbon\Carbon;
2016-11-14 18:14:23 +01:00
/**
* Renders a hidden input
*
2017-01-03 03:22:48 +01:00
* @param string $name Name of the input
* @param string $value The value
2016-11-14 18:14:23 +01:00
* @return string rendered html
*/
2017-01-02 03:57:23 +01:00
function form_hidden($name, $value)
{
2021-12-01 00:27:46 +01:00
return '<input type="hidden" name="' . $name . '" value="' . htmlspecialchars((string)$value) . '" />';
2016-11-14 18:14:23 +01:00
}
/**
* Rendert ein Zahlenfeld mit Buttons zum verstellen
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param string $label
* @param string $value
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_spinner($name, $label, $value)
{
$id = 'spinner-' . $name;
2021-12-01 00:27:46 +01:00
$value = htmlspecialchars((string)$value);
2017-12-25 23:12:52 +01:00
2017-01-02 03:57:23 +01:00
return form_element($label, '
2020-05-13 18:26:32 +02:00
<div class="input-group">
<input id="' . $id . '" class="form-control" type="number" min="0" step="1" name="' . $name . '" value="' . $value . '" />
<button id="' . $id . '-down" class="btn btn-secondary" type="button">
2021-08-05 01:07:15 +02:00
' . icon('dash-lg') . '
2021-08-05 01:00:12 +02:00
</button>
<button id="' . $id . '-up" class="btn btn-secondary" type="button">
2021-08-05 01:00:12 +02:00
' . icon('plus-lg') . '
</button>
2016-11-14 18:14:23 +01:00
</div>
2020-05-13 18:26:32 +02:00
<script type="text/javascript">
document.getElementById("' . $id . '-down")?.addEventListener("click", () => {
document.getElementById("' . $id . '").stepDown();
2020-05-13 18:26:32 +02:00
});
document.getElementById("' . $id . '-up")?.addEventListener("click", () => {
document.getElementById("' . $id . '").stepUp();
2020-05-13 18:26:32 +02:00
});
</script>
', $id);
2016-11-14 18:14:23 +01:00
}
/**
* Render a bootstrap datepicker
*
2017-01-03 03:22:48 +01:00
* @param string $name Name of the parameter
* @param string $label Label
* @param int|Carbon $value Unix Timestamp
2017-01-03 03:22:48 +01:00
* @param string $start_date Earliest possible date
* @param string $end_date
* @return string HTML
2016-11-14 18:14:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_date($name, $label, $value, $start_date = '', $end_date = '')
{
$dom_id = $name . '-date';
$value = ($value instanceof Carbon) ? $value->getTimestamp() : $value;
2017-01-02 03:57:23 +01:00
$value = is_numeric($value) ? date('Y-m-d', $value) : '';
$start_date = is_numeric($start_date) ? date('Y-m-d', $start_date) : '';
$end_date = is_numeric($end_date) ? date('Y-m-d', $end_date) : '';
return form_element(
$label,
'<input class="form-control" id="' . $dom_id . '" type="date" placeholder="YYYY-MM-DD" pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}" min="' . $start_date . '" max="' . $end_date . '" name="' . $name . '" value="' . htmlspecialchars((string)$value) . '" autocomplete="off">',
$dom_id
);
2016-11-14 18:14:23 +01:00
}
2021-01-02 01:22:05 +01:00
/**
* Render a bootstrap datepicker
*
* @param string $name Name of the parameter
* @param string $label
* @param mixed $value
*
* @return string HTML
*/
function form_datetime(string $name, string $label, $value)
{
$dom_id = $name . '-datetime';
if ($value) {
$value = ($value instanceof Carbon) ? $value : Carbon::createFromTimestamp($value);
}
return form_element($label, sprintf('
<input class="form-control" id="%s" type="datetime-local"
pattern="[0-9]{4}-[0-9]{2}-[0-9]{2} ([01][0-9]|2[0-3]):[0-5][0-9]" placeholder="YYYY-MM-DD HH:MM"
name="%s" value="%s" autocomplete="off">
', $dom_id, $name, htmlspecialchars($value ? $value->format('Y-m-d H:i') : '')), $dom_id);
2021-01-02 01:22:05 +01:00
}
2016-11-14 18:14:23 +01:00
/**
* Rendert eine Liste von Checkboxen für ein Formular
*
2017-01-03 03:22:48 +01:00
* @param string $name Die Namen der Checkboxen werden aus name_key gebildet
* @param string $label Die Beschriftung der Liste
* @param array $items Array mit den einzelnen Checkboxen
* @param array $selected Array mit den Keys, die ausgewählt sind
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_checkboxes($name, $label, $items, $selected)
{
$html = form_element($label, '');
foreach ($items as $key => $item) {
$html .= form_checkbox($name . '_' . $key, $item, in_array($key, $selected));
2017-01-02 03:57:23 +01:00
}
return $html;
2016-11-14 18:14:23 +01:00
}
/**
* Rendert eine Checkbox
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param string $label
* @param string $selected
* @param string $value
2018-08-06 13:10:53 +02:00
* @param string $html_id
2017-01-03 03:22:48 +01:00
* @return string
2016-11-14 18:14:23 +01:00
*/
2018-08-06 13:10:53 +02:00
function form_checkbox($name, $label, $selected, $value = 'checked', $html_id = null)
2017-01-02 03:57:23 +01:00
{
2018-08-06 13:10:53 +02:00
if (is_null($html_id)) {
$html_id = $name;
}
return '<div class="form-check">'
. '<input class="form-check-input" type="checkbox" id="' . $html_id . '" name="' . $name . '" value="' . htmlspecialchars((string)$value) . '" '
. ($selected ? ' checked="checked"' : '') . ' /><label class="form-check-label" for="' . $html_id . '">'
2017-01-03 14:12:17 +01:00
. $label
. '</label></div>';
2016-11-14 18:14:23 +01:00
}
/**
* Rendert einen Radio
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param string $label
* @param string $selected
* @param string $value
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_radio($name, $label, $selected, $value)
{
return '<div class="form-check">'
. '<input class="form-check-input" type="radio" id="' . $name . '" name="' . $name . '" value="' . htmlspecialchars((string)$value) . '" '
. ($selected ? ' checked="checked"' : '') . ' />'
. '<label class="form-check-label" for="' . $name . '">'
2017-01-03 14:12:17 +01:00
. $label
. '</label></div>';
2016-11-14 18:14:23 +01:00
}
/**
* Rendert einen Infotext in das Formular
2017-01-03 03:22:48 +01:00
*
* @param string $label
* @param string $text
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-03 14:12:17 +01:00
function form_info($label, $text = '')
2017-01-02 03:57:23 +01:00
{
2017-01-03 14:12:17 +01:00
if ($label == '') {
2021-07-24 21:08:04 +02:00
return '<span class="help-block">' . icon('info-circle') . $text . '</span>';
2017-01-02 03:57:23 +01:00
}
2017-01-03 14:12:17 +01:00
if ($text == '') {
2017-01-02 03:57:23 +01:00
return '<h4>' . $label . '</h4>';
}
return form_element($label, '<p class="form-control-static">' . $text . '</p>');
2016-11-14 18:14:23 +01:00
}
/**
* Rendert den Absenden-Button eines Formulars
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param string $label
* @param string $class
* @param bool $wrapForm
* @param string $buttonType
2017-01-03 03:22:48 +01:00
* @return string
2016-11-14 18:14:23 +01:00
*/
function form_submit($name, $label, $class = '', $wrapForm = true, $buttonType = 'primary')
2017-01-02 03:57:23 +01:00
{
$button = '<button class="btn btn-' . $buttonType . ($class ? ' ' . $class : '') . '" type="submit" name="' . $name . '">'
. $label
. '</button>';
if (!$wrapForm) {
return $button;
}
2017-01-02 15:43:36 +01:00
return form_element(
null,
$button
2017-01-02 15:43:36 +01:00
);
2016-11-14 18:14:23 +01:00
}
/**
* Rendert ein Formular-Textfeld
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param string $label
* @param string $value
* @param bool $disabled
* @param int|null $maxlength
* @param string|null $autocomplete
* @param string|null $class
*
2017-01-03 03:22:48 +01:00
* @return string
2016-11-14 18:14:23 +01:00
*/
function form_text($name, $label, $value, $disabled = false, $maxlength = null, $autocomplete = null, $class = '')
2017-01-02 03:57:23 +01:00
{
$disabled = $disabled ? ' disabled="disabled"' : '';
$maxlength = $maxlength ? ' maxlength=' . (int)$maxlength : '';
$autocomplete = $autocomplete ? ' autocomplete="' . $autocomplete . '"' : '';
2017-01-02 15:43:36 +01:00
return form_element(
$label,
2017-01-03 14:12:17 +01:00
'<input class="form-control" id="form_' . $name . '" type="text" name="' . $name
2021-12-01 00:27:46 +01:00
. '" value="' . htmlspecialchars((string)$value) . '"' . $maxlength . $disabled . $autocomplete . '/>',
'form_' . $name,
$class
2017-01-02 15:43:36 +01:00
);
2016-11-14 18:14:23 +01:00
}
2016-11-18 15:36:02 +01:00
/**
* Renders a text input with placeholder instead of label.
*
2017-01-03 03:22:48 +01:00
* @param String $name Input name
* @param String $placeholder Placeholder
* @param String $value The value
* @param Boolean $disabled Is the field enabled?
* @return string
2016-11-18 15:36:02 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_text_placeholder($name, $placeholder, $value, $disabled = false)
{
$disabled = $disabled ? ' disabled="disabled"' : '';
2022-04-13 01:02:37 +02:00
return form_element(
'',
2017-01-03 14:12:17 +01:00
'<input class="form-control" id="form_' . $name . '" type="text" name="' . $name
2021-12-01 00:27:46 +01:00
. '" value="' . htmlspecialchars((string)$value) . '" placeholder="' . $placeholder
2017-01-03 14:12:17 +01:00
. '" ' . $disabled . '/>'
);
2016-11-18 15:36:02 +01:00
}
2016-11-14 18:14:23 +01:00
/**
* Rendert ein Formular-Emailfeld
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param string $label
* @param string $value
* @param bool $disabled
* @param string|null $autocomplete
2020-12-02 14:43:11 +01:00
* @param int|null $maxlength
*
2017-01-03 03:22:48 +01:00
* @return string
2016-11-14 18:14:23 +01:00
*/
2020-12-02 14:43:11 +01:00
function form_email($name, $label, $value, $disabled = false, $autocomplete = null, $maxlength = null)
2017-01-02 03:57:23 +01:00
{
$disabled = $disabled ? ' disabled="disabled"' : '';
$autocomplete = $autocomplete ? ' autocomplete="' . $autocomplete . '"' : '';
2020-12-02 14:43:11 +01:00
$maxlength = $maxlength ? ' maxlength=' . (int)$maxlength : '';
2017-01-02 15:43:36 +01:00
return form_element(
$label,
2017-01-03 14:12:17 +01:00
'<input class="form-control" id="form_' . $name . '" type="email" name="' . $name . '" value="'
2021-12-01 00:27:46 +01:00
. htmlspecialchars((string)$value) . '" ' . $disabled . $autocomplete . $maxlength . '/>',
2017-01-02 15:43:36 +01:00
'form_' . $name
);
2016-11-14 18:14:23 +01:00
}
/**
* Rendert ein Formular-Dateifeld
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param string $label
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_file($name, $label)
{
2017-12-25 23:12:52 +01:00
return form_element(
$label,
sprintf('<input id="form_%1$s" type="file" name="%1$s" />', $name),
'form_' . $name
);
2016-11-14 18:14:23 +01:00
}
/**
* Rendert ein Formular-Passwortfeld
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param string $label
* @param bool $disabled
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_password($name, $label, $disabled = false)
{
$disabled = $disabled ? ' disabled="disabled"' : '';
2017-01-02 15:43:36 +01:00
return form_element(
$label,
2017-12-25 23:12:52 +01:00
sprintf(
2020-11-24 17:27:21 +01:00
'<input class="form-control" id="form_%1$s" type="password" name="%1$s" minlength="%2$s" value=""%3$s/>',
2017-12-25 23:12:52 +01:00
$name,
2020-11-24 17:27:21 +01:00
config('min_password_length'),
2017-12-25 23:12:52 +01:00
$disabled
),
2017-01-02 15:43:36 +01:00
'form_' . $name
);
2016-11-14 18:14:23 +01:00
}
2016-11-18 15:36:02 +01:00
/**
* Renders a password input with placeholder instead of label.
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param string $placeholder
* @param bool $disabled
* @return string
2016-11-18 15:36:02 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_password_placeholder($name, $placeholder, $disabled = false)
{
$disabled = $disabled ? ' disabled="disabled"' : '';
2017-01-02 15:43:36 +01:00
return form_element(
'',
2017-01-03 14:12:17 +01:00
'<input class="form-control" id="form_' . $name . '" type="password" name="'
. $name . '" value="" placeholder="' . $placeholder . '" ' . $disabled . '/>',
2017-01-02 15:43:36 +01:00
'form_' . $name
);
2016-11-18 15:36:02 +01:00
}
2016-11-14 18:14:23 +01:00
/**
* Rendert ein Formular-Textfeld
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param string $label
* @param string $value
* @param bool $disabled
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_textarea($name, $label, $value, $disabled = false)
{
$disabled = $disabled ? ' disabled="disabled"' : '';
2017-01-02 15:43:36 +01:00
return form_element(
$label,
'<textarea rows="5" class="form-control" id="form_' . $name . '" name="'
2021-12-01 00:27:46 +01:00
. $name . '" ' . $disabled . '>' . htmlspecialchars((string)$value) . '</textarea>',
2017-01-02 15:43:36 +01:00
'form_' . $name
);
2016-11-14 18:14:23 +01:00
}
/**
* Rendert ein Formular-Auswahlfeld
2017-01-03 03:22:48 +01:00
*
* @param string $name
* @param string $label
* @param string[] $values
* @param string $selected
2018-08-29 20:25:29 +02:00
* @param string $selectText
* @param string $class
2017-01-03 03:22:48 +01:00
* @return string
2016-11-14 18:14:23 +01:00
*/
function form_select($name, $label, $values, $selected, $selectText = '', $class = '')
2017-01-02 03:57:23 +01:00
{
2018-08-29 20:25:29 +02:00
return form_element(
$label,
html_select_key('form_' . $name, $name, $values, $selected, $selectText),
'form_' . $name,
$class
2018-08-29 20:25:29 +02:00
);
2016-11-14 18:14:23 +01:00
}
/**
* Rendert ein Formular-Element
2017-01-03 03:22:48 +01:00
*
* @param string $label
* @param string $input
* @param string $for
* @param string $class
2017-01-03 03:22:48 +01:00
* @return string
2016-11-14 18:14:23 +01:00
*/
function form_element($label, $input, $for = '', $class = '')
2017-01-02 03:57:23 +01:00
{
$class = $class ? ' ' . $class : '';
if (empty($label)) {
return '<div class="mb-3' . $class . '">' . $input . '</div>';
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
return '<div class="mb-3' . $class . '">'
. '<label class="form-label" for="' . $for . '">' . $label . '</label>'
. $input
. '</div>';
2016-11-14 18:14:23 +01:00
}
/**
* Rendert ein Formular
2017-01-03 03:22:48 +01:00
*
* @param string[] $elements
* @param string $action
* @param bool $inline
2017-01-03 03:22:48 +01:00
* @return string
2016-11-14 18:14:23 +01:00
*/
function form($elements, $action = '', $inline = false, $btnGroup = false)
2017-01-02 03:57:23 +01:00
{
2021-10-23 17:19:19 +02:00
return '<form action="' . $action . '" enctype="multipart/form-data" method="post"'
. ($btnGroup ? ' class="btn-group"' : '')
. ($inline ? ' style="float:left"' : '') . '>'
2018-09-03 16:33:13 +02:00
. join($elements)
. form_csrf()
2018-09-03 16:33:13 +02:00
. '</form>';
}
/**
* @return string
*/
function form_csrf()
{
return form_hidden('_token', session()->get('_token'));
2016-11-14 18:14:23 +01:00
}
2017-01-03 03:22:48 +01:00
/**
* @param string $name
* @param String[] $options
* @param string $selected
* @return string
*/
2017-01-03 14:12:17 +01:00
function html_options($name, $options, $selected = '')
2017-01-02 03:57:23 +01:00
{
2017-01-03 14:12:17 +01:00
$html = '';
2017-01-02 03:57:23 +01:00
foreach ($options as $value => $label) {
$html .= '<div class="form-check form-check-inline">'
. '<input class="form-check-input" type="radio" id="' . $name . '_' . $value . '" name="' . $name . '"'
. ($value == $selected ? ' checked="checked"' : '') . ' value="' . $value . '" />'
. '<label class="form-check-label" for="' . $name . '_' . $value . '">' . $label . '</label>'
. '</div>';
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return $html;
2016-11-14 18:14:23 +01:00
}
2017-01-03 03:22:48 +01:00
/**
* @param string $dom_id
* @param string $name
* @param string[] $rows
* @param string $selected
2018-08-29 20:25:29 +02:00
* @param string $selectText
2017-01-03 03:22:48 +01:00
* @return string
*/
2018-08-29 20:25:29 +02:00
function html_select_key($dom_id, $name, $rows, $selected, $selectText = '')
2017-01-02 03:57:23 +01:00
{
$html = '<select class="form-control" id="' . $dom_id . '" name="' . $name . '">';
2018-08-29 20:25:29 +02:00
if (!empty($selectText)) {
$html .= '<option value="">' . $selectText . '</option>';
}
2017-01-02 03:57:23 +01:00
foreach ($rows as $key => $row) {
if (($key == $selected) || ($row === $selected)) {
2017-01-02 03:57:23 +01:00
$html .= '<option value="' . $key . '" selected="selected">' . $row . '</option>';
} else {
$html .= '<option value="' . $key . '">' . $row . '</option>';
}
2016-11-14 18:14:23 +01:00
}
2017-01-02 03:57:23 +01:00
$html .= '</select>';
return $html;
2016-11-14 18:14:23 +01:00
}