You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

510 lines
15 KiB
PHTML

<?php
// Methods to build a html form.
use Carbon\Carbon;
/**
* Renders a hidden input
*
* @param string $name Name of the input
* @param string $value The value
* @return string rendered html
*/
8 years ago
function form_hidden($name, $value)
{
return '<input type="hidden" name="' . $name . '" value="' . htmlspecialchars((string)$value) . '" />';
}
/**
* Rendert ein Zahlenfeld mit Buttons zum verstellen
*
* @param string $name
* @param string $label
* @param string $value
* @return string
*/
8 years ago
function form_spinner($name, $label, $value)
{
$value = htmlspecialchars((string)$value);
8 years ago
return form_element($label, '
<div class="input-group">
<input id="spinner-' . $name . '" class="form-control" name="' . $name . '" value="' . $value . '" />
<button id="spinner-' . $name . '-down" class="btn btn-secondary" type="button">
3 years ago
' . icon('dash-lg') . '
</button>
<button id="spinner-' . $name . '-up" class="btn btn-secondary" type="button">
' . icon('plus-lg') . '
</button>
</div>
<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>
');
}
/**
* Render a bootstrap datepicker
*
* @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
*/
8 years ago
function form_date($name, $label, $value, $start_date = '', $end_date = '')
{
$dom_id = $name . '-date';
$value = ($value instanceof Carbon) ? $value->getTimestamp() : $value;
8 years ago
$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) : '';
8 years ago
return form_element($label, '
<div class="input-group date" id="' . $dom_id . '" data-min-date="' . $start_date . '" data-max-date="' . $end_date . '" data-target-input="nearest">
<input type="date" placeholder="YYYY-MM-DD" name="' . $name . '" class="form-control" value="' . htmlspecialchars((string)$value) . '" autocomplete="off">'
. '<span class="input-group-text">' . icon('grid-3x3-gap-fill') . '</span>
</div>
', $dom_id);
}
/**
* 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" data-target-input="nearest">
<input type="datetime-local" placeholder="YYYY-MM-DD HH:MM" name="%s"
class="form-control" value="%s" autocomplete="off" data-target="#%s">'
. '<span class="input-group-text">' . icon('grid-3x3-gap-fill') . '</span>
</div>
', $dom_id, $name, htmlspecialchars($value ? $value->format('Y-m-d H:i') : ''), $dom_id), $dom_id);
}
/**
* Rendert eine Liste von Checkboxen für ein Formular
*
* @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
*/
8 years ago
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;
}
/**
* Rendert eine Tabelle von Checkboxen für ein Formular
*
* @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
*/
8 years ago
function form_multi_checkboxes($names, $label, $items, $selected, $disabled = [])
{
$html = '<table><thead><tr>';
8 years ago
foreach ($names as $title) {
$html .= '<th>' . $title . '</th>';
8 years ago
}
$html .= '</tr></thead><tbody>';
8 years ago
foreach ($items as $key => $item) {
$html .= '<tr>';
$dom_id = '';
8 years ago
foreach ($names as $name => $title) {
$dom_id = $name . '_' . $key;
$sel = array_search($key, $selected[$name]) !== false ? ' checked="checked"' : '';
8 years ago
if (!empty($disabled) && !empty($disabled[$name]) && array_search($key, $disabled[$name]) !== false) {
8 years ago
$sel .= ' disabled="disabled"';
}
$html .= '<td style="text-align: center;">'
. sprintf(
'<input type="checkbox" id="%s" name="%s[]" value="%s" %s />',
$dom_id,
$name,
$key,
$sel
)
. '</td>';
8 years ago
}
$html .= '<td><label for="' . $dom_id . '">' . $item . '</label></td></tr>';
}
$html .= '</tbody></table>';
8 years ago
return form_element($label, $html);
}
/**
* Rendert eine Checkbox
*
* @param string $name
* @param string $label
* @param string $selected
* @param string $value
* @param string $html_id
* @return string
*/
function form_checkbox($name, $label, $selected, $value = 'checked', $html_id = null)
8 years ago
{
if (is_null($html_id)) {
$html_id = $name;
}
return '<div class="checkbox"><label>'
. '<input type="checkbox" id="' . $html_id . '" name="' . $name . '" value="' . htmlspecialchars((string)$value) . '" '
. ($selected ? ' checked="checked"' : '') . ' /> '
. $label
. '</label></div>';
}
/**
* Rendert einen Radio
*
* @param string $name
* @param string $label
* @param string $selected
* @param string $value
* @return string
*/
8 years ago
function form_radio($name, $label, $selected, $value)
{
return '<div class="radio">'
. '<label><input type="radio" id="' . $name . '" name="' . $name . '" value="' . htmlspecialchars((string)$value) . '" '
. ($selected ? ' checked="checked"' : '') . ' /> '
. $label
. '</label></div>';
}
/**
* Rendert einen Infotext in das Formular
*
* @param string $label
* @param string $text
* @return string
*/
function form_info($label, $text = '')
8 years ago
{
if ($label == '') {
return '<span class="help-block">' . icon('info-circle') . $text . '</span>';
8 years ago
}
if ($text == '') {
8 years ago
return '<h4>' . $label . '</h4>';
}
return form_element($label, '<p class="form-control-static">' . $text . '</p>', '');
}
/**
* Rendert den Absenden-Button eines Formulars
*
* @param string $name
* @param string $label
* @param string $class
* @param bool $wrapForm
* @param string $buttonType
* @return string
*/
function form_submit($name, $label, $class = '', $wrapForm = true, $buttonType = 'primary')
8 years ago
{
$button = '<button class="btn btn-' . $buttonType . ($class ? ' ' . $class : '') . '" type="submit" name="' . $name . '">'
. $label
. '</button>';
if (!$wrapForm) {
return $button;
}
8 years ago
return form_element(
null,
$button
8 years ago
);
}
/**
* Rendert ein Formular-Textfeld
*
* @param string $name
* @param string $label
* @param string $value
* @param bool $disabled
* @param int|null $maxlength
* @param string|null $autocomplete
* @param string|null $class
*
* @return string
*/
function form_text($name, $label, $value, $disabled = false, $maxlength = null, $autocomplete = null, $class = '')
8 years ago
{
$disabled = $disabled ? ' disabled="disabled"' : '';
$maxlength = $maxlength ? ' maxlength=' . (int)$maxlength : '';
$autocomplete = $autocomplete ? ' autocomplete="' . $autocomplete . '"' : '';
8 years ago
return form_element(
$label,
'<input class="form-control" id="form_' . $name . '" type="text" name="' . $name
. '" value="' . htmlspecialchars((string)$value) . '"' . $maxlength . $disabled . $autocomplete . '/>',
'form_' . $name,
$class
8 years ago
);
}
/**
* Renders a text input with placeholder instead of label.
*
* @param String $name Input name
* @param String $placeholder Placeholder
* @param String $value The value
* @param Boolean $disabled Is the field enabled?
* @return string
*/
8 years ago
function form_text_placeholder($name, $placeholder, $value, $disabled = false)
{
$disabled = $disabled ? ' disabled="disabled"' : '';
8 years ago
return form_element('',
'<input class="form-control" id="form_' . $name . '" type="text" name="' . $name
. '" value="' . htmlspecialchars((string)$value) . '" placeholder="' . $placeholder
. '" ' . $disabled . '/>'
);
}
/**
* Rendert ein Formular-Emailfeld
*
* @param string $name
* @param string $label
* @param string $value
* @param bool $disabled
* @param string|null $autocomplete
* @param int|null $maxlength
*
* @return string
*/
function form_email($name, $label, $value, $disabled = false, $autocomplete = null, $maxlength = null)
8 years ago
{
$disabled = $disabled ? ' disabled="disabled"' : '';
$autocomplete = $autocomplete ? ' autocomplete="' . $autocomplete . '"' : '';
$maxlength = $maxlength ? ' maxlength=' . (int)$maxlength : '';
8 years ago
return form_element(
$label,
'<input class="form-control" id="form_' . $name . '" type="email" name="' . $name . '" value="'
. htmlspecialchars((string)$value) . '" ' . $disabled . $autocomplete . $maxlength . '/>',
8 years ago
'form_' . $name
);
}
/**
* Rendert ein Formular-Dateifeld
*
* @param string $name
* @param string $label
* @return string
*/
8 years ago
function form_file($name, $label)
{
return form_element(
$label,
sprintf('<input id="form_%1$s" type="file" name="%1$s" />', $name),
'form_' . $name
);
}
/**
* Rendert ein Formular-Passwortfeld
*
* @param string $name
* @param string $label
* @param bool $disabled
* @return string
*/
8 years ago
function form_password($name, $label, $disabled = false)
{
$disabled = $disabled ? ' disabled="disabled"' : '';
8 years ago
return form_element(
$label,
sprintf(
4 years ago
'<input class="form-control" id="form_%1$s" type="password" name="%1$s" minlength="%2$s" value=""%3$s/>',
$name,
4 years ago
config('min_password_length'),
$disabled
),
8 years ago
'form_' . $name
);
}
/**
* Renders a password input with placeholder instead of label.
*
* @param string $name
* @param string $placeholder
* @param bool $disabled
* @return string
*/
8 years ago
function form_password_placeholder($name, $placeholder, $disabled = false)
{
$disabled = $disabled ? ' disabled="disabled"' : '';
8 years ago
return form_element(
'',
'<input class="form-control" id="form_' . $name . '" type="password" name="'
. $name . '" value="" placeholder="' . $placeholder . '" ' . $disabled . '/>',
8 years ago
'form_' . $name
);
}
/**
* Rendert ein Formular-Textfeld
*
* @param string $name
* @param string $label
* @param string $value
* @param bool $disabled
* @return string
*/
8 years ago
function form_textarea($name, $label, $value, $disabled = false)
{
$disabled = $disabled ? ' disabled="disabled"' : '';
8 years ago
return form_element(
$label,
'<textarea rows="5" class="form-control" id="form_' . $name . '" name="'
. $name . '" ' . $disabled . '>' . htmlspecialchars((string)$value) . '</textarea>',
8 years ago
'form_' . $name
);
}
/**
* Rendert ein Formular-Auswahlfeld
*
* @param string $name
* @param string $label
* @param string[] $values
* @param string $selected
* @param string $selectText
* @param string $class
* @return string
*/
function form_select($name, $label, $values, $selected, $selectText = '', $class = '')
8 years ago
{
return form_element(
$label,
html_select_key('form_' . $name, $name, $values, $selected, $selectText),
'form_' . $name,
$class
);
}
/**
* Rendert ein Formular-Element
*
* @param string $label
* @param string $input
* @param string $for
* @param string $class
* @return string
*/
function form_element($label, $input, $for = '', $class = '')
8 years ago
{
$class = $class ? ' ' . $class : '';
if (empty($label)) {
return '<div class="mb-3' . $class . '">' . $input . '</div>';
8 years ago
}
8 years ago
return '<div class="mb-3' . $class . '">'
. '<label class="form-label" for="' . $for . '">' . $label . '</label>'
. $input
. '</div>';
}
/**
* Rendert ein Formular
*
* @param string[] $elements
* @param string $action
* @param bool $inline
* @return string
*/
function form($elements, $action = '', $inline = false, $btnGroup = false)
8 years ago
{
3 years ago
return '<form action="' . $action . '" enctype="multipart/form-data" method="post"'
. ($btnGroup ? ' class="btn-group"' : '')
. ($inline ? ' style="float:left"' : '') . '>'
. join($elements)
. form_csrf()
. '</form>';
}
/**
* @return string
*/
function form_csrf()
{
return form_hidden('_token', session()->get('_token'));
}
/**
* @param string $name
* @param String[] $options
* @param string $selected
* @return string
*/
function html_options($name, $options, $selected = '')
8 years ago
{
$html = '';
8 years ago
foreach ($options as $value => $label) {
$html .= '<input type="radio"' . ($value == $selected ? ' checked="checked"' : '') . ' name="'
. $name . '" value="' . $value . '"> ' . $label;
8 years ago
}
8 years ago
8 years ago
return $html;
}
/**
* @param string $dom_id
* @param string $name
* @param string[] $rows
* @param string $selected
* @param string $selectText
* @return string
*/
function html_select_key($dom_id, $name, $rows, $selected, $selectText = '')
8 years ago
{
$html = '<select class="form-control" id="' . $dom_id . '" name="' . $name . '">';
if (!empty($selectText)) {
$html .= '<option value="">' . $selectText . '</option>';
}
8 years ago
foreach ($rows as $key => $row) {
if (($key == $selected) || ($row === $selected)) {
8 years ago
$html .= '<option value="' . $key . '" selected="selected">' . $row . '</option>';
} else {
$html .= '<option value="' . $key . '">' . $row . '</option>';
}
}
8 years ago
$html .= '</select>';
return $html;
}