engelsystem/includes/sys_form.php

500 lines
14 KiB
PHP
Raw Normal View History

2016-11-14 18:14:23 +01:00
<?php
// 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($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)
{
2017-12-25 23:12:52 +01:00
$value = htmlspecialchars($value);
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="spinner-' . $name . '" class="form-control" name="' . $name . '" value="' . $value . '" />
<div class="input-group-btn">
<button id="spinner-' . $name . '-down" class="btn btn-default" type="button">
<span class="glyphicon glyphicon-minus"></span>
</button>
<button id="spinner-' . $name . '-up" class="btn btn-default" type="button">
<span class="glyphicon glyphicon-plus"></span>
</button>
</div>
2016-11-14 18:14:23 +01:00
</div>
2020-05-13 18:26:32 +02:00
<script type="text/javascript">
$(\'#spinner-' . $name . '-down\').click(function() {
var spinner = $(\'#spinner-' . $name . '\');
spinner.val(parseInt(spinner.val()) - 1);
});
$(\'#spinner-' . $name . '-up\').click(function() {
var spinner = $(\'#spinner-' . $name . '\');
spinner.val(parseInt(spinner.val()) + 1);
});
</script>
');
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 $value Unix Timestamp
* @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) : '';
2017-01-02 03:57:23 +01:00
return form_element($label, '
<div class="input-group date" id="' . $dom_id . '" data-min-date="' . $start_date . '" data-max-date="' . $end_date . '">
<input type="date" placeholder="YYYY-MM-DD" name="' . $name . '" class="form-control" value="' . htmlspecialchars($value) . '" autocomplete="off">'
. '<span class="input-group-addon">' . glyph('th') . '</span>
2016-11-14 18:14:23 +01:00
</div>
', $dom_id);
}
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('
<div class="input-group datetime" id="%s">
<input type="datetime-local" placeholder="YYYY-MM-DD HH:MM" name="%s"
class="form-control" value="%s" autocomplete="off">'
. '<span class="input-group-addon">' . glyph('th') . '</span>
</div>
', $dom_id, $name, htmlspecialchars($value ? $value->format('Y-m-d H:i') : '')), $dom_id);
}
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, array_search($key, $selected) !== false);
}
return $html;
2016-11-14 18:14:23 +01:00
}
/**
* Rendert eine Tabelle von Checkboxen für ein Formular
*
2017-01-03 03:22:48 +01:00
* @param string[] $names Assoziatives Array mit Namen der Checkboxen als Keys und Überschriften als Values
* @param string $label Die Beschriftung der gesamten Tabelle
* @param string[] $items Array mit den Beschriftungen der Zeilen
* @param array[] $selected Mehrdimensionales Array, wobei $selected[foo] ein Array der in der Datenreihe foo
* markierten Checkboxen ist
* @param array $disabled Wie selected, nur dass die entsprechenden Checkboxen deaktiviert statt markiert sind
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_multi_checkboxes($names, $label, $items, $selected, $disabled = [])
{
2017-01-03 14:12:17 +01:00
$html = '<table><thead><tr>';
2017-01-02 03:57:23 +01:00
foreach ($names as $title) {
2017-01-03 14:12:17 +01:00
$html .= '<th>' . $title . '</th>';
2017-01-02 03:57:23 +01:00
}
2017-01-03 14:12:17 +01:00
$html .= '</tr></thead><tbody>';
2017-01-02 03:57:23 +01:00
foreach ($items as $key => $item) {
2017-01-03 14:12:17 +01:00
$html .= '<tr>';
2017-01-03 03:22:48 +01:00
$dom_id = '';
2017-01-02 03:57:23 +01:00
foreach ($names as $name => $title) {
$dom_id = $name . '_' . $key;
2017-01-03 14:12:17 +01:00
$sel = array_search($key, $selected[$name]) !== false ? ' checked="checked"' : '';
2017-01-02 15:43:36 +01:00
if (!empty($disabled) && !empty($disabled[$name]) && array_search($key, $disabled[$name]) !== false) {
2017-01-02 03:57:23 +01:00
$sel .= ' disabled="disabled"';
}
2017-01-03 03:22:48 +01:00
$html .= '<td style="text-align: center;">'
2017-12-25 23:12:52 +01:00
. sprintf(
'<input type="checkbox" id="%s" name="%s[]" value="%s" %s />',
$dom_id,
$name,
$key,
$sel
)
2017-01-03 03:22:48 +01:00
. '</td>';
2017-01-02 03:57:23 +01:00
}
$html .= '<td><label for="' . $dom_id . '">' . $item . '</label></td></tr>';
2016-11-14 18:14:23 +01:00
}
2017-01-03 14:12:17 +01:00
$html .= '</tbody></table>';
2017-01-02 03:57:23 +01:00
return form_element($label, $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;
}
2017-01-03 14:12:17 +01:00
return '<div class="checkbox"><label>'
2018-08-06 13:10:53 +02:00
. '<input type="checkbox" id="' . $html_id . '" name="' . $name . '" value="' . htmlspecialchars($value) . '" '
2017-01-03 14:12:17 +01:00
. ($selected ? ' checked="checked"' : '') . ' /> '
. $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)
{
2017-01-03 14:12:17 +01:00
return '<div class="radio">'
. '<label><input type="radio" id="' . $name . '" name="' . $name . '" value="' . htmlspecialchars($value) . '" '
2017-01-03 14:12:17 +01:00
. ($selected ? ' checked="checked"' : '') . ' /> '
. $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 == '') {
2017-01-02 03:57:23 +01:00
return '<span class="help-block">' . glyph('info-sign') . $text . '</span>';
}
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
*
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)
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
. '" value="' . htmlspecialchars($value) . '"' . $maxlength . $disabled . $autocomplete . '/>',
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 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"' : '';
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="text" name="' . $name
. '" value="' . htmlspecialchars($value) . '" placeholder="' . $placeholder
. '" ' . $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="'
2020-12-02 14:43:11 +01:00
. htmlspecialchars($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="'
. $name . '" ' . $disabled . '>' . htmlspecialchars($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
2017-01-03 03:22:48 +01:00
* @return string
2016-11-14 18:14:23 +01:00
*/
2018-08-29 20:25:29 +02:00
function form_select($name, $label, $values, $selected, $selectText = '')
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
);
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
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-03 14:12:17 +01:00
function form_element($label, $input, $for = '')
2017-01-02 03:57:23 +01:00
{
if (empty($label)) {
return '<div class="mb-3">' . $input . '</div>';
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
return '<div class="mb-3">' . '<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)
2017-01-02 03:57:23 +01:00
{
return '<form action="' . $action . '" enctype="multipart/form-data" method="post"' . ($inline ? ' style="float:left"' : '') . '>'
2018-09-03 16:33:13 +02:00
. form_csrf()
. join($elements)
. '</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) {
2017-01-03 14:12:17 +01:00
$html .= '<input type="radio"' . ($value == $selected ? ' checked="checked"' : '') . ' name="'
. $name . '" value="' . $value . '"> ' . $label;
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
}