engelsystem/includes/sys_form.php

357 lines
9.9 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)
{
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 int $value
* @param array $data_attributes
2017-01-03 03:22:48 +01:00
* @return string
2016-11-14 18:14:23 +01:00
*/
function form_spinner(string $name, string $label, int $value, array $data_attributes = [], bool $isDisabled = false)
2017-01-02 03:57:23 +01:00
{
$id = 'spinner-' . $name;
$attr = '';
foreach ($data_attributes as $attr_key => $attr_value) {
$attr .= ' data-' . $attr_key . '="' . $attr_value . '"';
}
$disabled = $isDisabled ? ' disabled' : '';
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 . '"' . $attr . $disabled . '/>
<button class="btn btn-secondary spinner-down' . $disabled . '" type="button" data-input-id="' . $id . '"' . $attr . '>
2021-08-05 01:07:15 +02:00
' . icon('dash-lg') . '
2021-08-05 01:00:12 +02:00
</button>
<button class="btn btn-secondary spinner-up' . $disabled . '" type="button" data-input-id="' . $id . '"' . $attr . '>
2021-08-05 01:00:12 +02:00
' . icon('plus-lg') . '
</button>
2016-11-14 18:14:23 +01:00
</div>
', $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 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">'
2023-12-04 23:33:07 +01:00
. '<input class="form-check-input" type="checkbox" id="' . $html_id . '" '
. 'name="' . htmlspecialchars($name) . '" value="' . $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
}
/**
2023-12-04 23:33:07 +01:00
* Renders a radio button
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)
{
$value = htmlspecialchars((string) $value);
$id = preg_replace('/\s/', '-', $name . '_' . $value);
return '<div class="form-check">'
. '<input class="form-check-input" type="radio" id="' . $id . '" name="' . $name . '" value="' . $value . '" '
. ($selected ? ' checked="checked"' : '') . ' />'
. '<label class="form-check-label" for="' . $id . '">'
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
* @param array $data_attributes
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 = '', $data_attributes = [])
2017-01-02 03:57:23 +01:00
{
$disabled = $disabled ? ' disabled="disabled"' : '';
$maxlength = $maxlength ? ' maxlength=' . (int) $maxlength : '';
$autocomplete = $autocomplete ? ' autocomplete="' . $autocomplete . '"' : '';
$attr = '';
foreach ($data_attributes as $attr_key => $attr_value) {
$attr .= ' data-' . $attr_key . '="' . $attr_value . '"';
}
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
. '" value="' . htmlspecialchars((string) $value) . '"' . $maxlength . $disabled . $autocomplete . $attr . '/>',
'form_' . $name,
$class
2017-01-02 15:43:36 +01:00
);
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 string $autocomplete
2017-01-03 03:22:48 +01:00
* @param bool $disabled
* @return string
2016-11-14 18:14:23 +01:00
*/
function form_password($name, $label, $autocomplete, $disabled = false)
2017-01-02 03:57:23 +01:00
{
$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(
2023-12-04 23:33:07 +01:00
'<input class="form-control" id="form_%1$s" type="password" name="%1$s" minlength="%2$s" value="" autocomplete="%3$s" %4$s>',
2017-12-25 23:12:52 +01:00
$name,
2020-11-24 17:27:21 +01:00
config('min_password_length'),
$autocomplete,
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
}
/**
* 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="'
. $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
2023-02-06 19:35:02 +01:00
* @param string[] $options
2017-01-03 03:22:48 +01:00
* @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)) {
2023-12-04 23:33:07 +01:00
$html .= '<option value="' . htmlspecialchars($key) . '" selected="selected">'
. htmlspecialchars($row)
. '</option>';
2017-01-02 03:57:23 +01:00
} else {
2023-12-04 23:33:07 +01:00
$html .= '<option value="' . htmlspecialchars($key) . '">'
. htmlspecialchars($row)
. '</option>';
2017-01-02 03:57:23 +01:00
}
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
}