engelsystem/includes/sys_form.php

452 lines
13 KiB
PHP
Raw Normal View History

2016-11-14 18:14:23 +01:00
<?php
// Methods to build a html form.
/**
* 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, '
2016-11-14 18:14:23 +01:00
<div class="input-group">
2017-12-25 23:12:52 +01:00
<input id="spinner-' . $name . '" class="form-control" name="' . $name . '" value="' . $value . '" />
2016-11-14 18:14:23 +01:00
<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>
</div>
<script type="text/javascript">
2017-01-03 03:22:48 +01:00
$("#spinner-' . $name . '-down").click(function() {
2017-01-03 15:32:12 +01:00
var spinner = $("#spinner-' . $name . '");
2017-01-03 03:22:48 +01:00
spinner.val(parseInt(spinner.val()) - 1);
2016-11-14 18:14:23 +01:00
});
2017-01-03 03:22:48 +01:00
$("#spinner-' . $name . '-up").click(function() {
2017-01-03 15:32:12 +01:00
var spinner = $("#spinner-' . $name . '");
2017-01-03 03:22:48 +01:00
spinner.val(parseInt(spinner.val()) + 1);
2016-11-14 18:14:23 +01:00
});
</script>
');
}
/**
* 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 = 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) : '';
$locale = $locale = session()->get('locale');
$shortLocale = substr($locale, 0, 2);
2017-01-02 03:57:23 +01:00
return form_element($label, '
2016-11-14 18:14:23 +01:00
<div class="input-group date" id="' . $dom_id . '">
<input name="' . $name . '" class="form-control" value="' . htmlspecialchars($value) . '">'
. '<span class="input-group-addon">' . glyph('th') . '</span>
2016-11-14 18:14:23 +01:00
</div>
<script type="text/javascript">
$(function(){
$("#' . $dom_id . '").datepicker({
language: "' . $shortLocale . '",
2016-11-14 18:14:23 +01:00
todayBtn: "linked",
format: "yyyy-mm-dd",
startDate: "' . $start_date . '",
endDate: "' . $end_date . '",
orientation: "bottom"
2016-11-14 18:14:23 +01:00
});
});
</script>
', $dom_id);
}
/**
* 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
* @param string $id
2017-01-03 03:22:48 +01:00
* @return string
2016-11-14 18:14:23 +01:00
*/
function form_checkbox($name, $label, $selected, $value = 'checked', $id = null)
2017-01-02 03:57:23 +01:00
{
if (is_null($id)) {
$id = $name;
}
2017-01-03 14:12:17 +01:00
return '<div class="checkbox"><label>'
. '<input type="checkbox" id="' . $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
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_submit($name, $label)
{
2017-01-02 15:43:36 +01:00
return form_element(
'<button class="btn btn-primary" type="submit" name="' . $name . '">' . $label . '</button>',
2017-01-03 14:12:17 +01:00
''
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
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_text($name, $label, $value, $disabled = false)
{
$disabled = $disabled ? ' disabled="disabled"' : '';
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) . '" ' . $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 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
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-02 03:57:23 +01:00
function form_email($name, $label, $value, $disabled = false)
{
$disabled = $disabled ? ' disabled="disabled"' : '';
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="'
. htmlspecialchars($value) . '" ' . $disabled . '/>',
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(
'<input class="form-control" id="form_%1$s" type="password" name="%1$s" value=""%s/>',
$name,
$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 ($label == '') {
return '<div class="form-group">' . $input . '</div>';
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return '<div class="form-group">' . '<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
* @return string
2016-11-14 18:14:23 +01:00
*/
2017-01-03 14:12:17 +01:00
function form($elements, $action = '')
2017-01-02 03:57:23 +01:00
{
return '<form action="' . $action . '" enctype="multipart/form-data" method="post">' . join($elements) . '</form>';
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
}