engelsystem/includes/sys_template.php

192 lines
5.6 KiB
PHP
Raw Normal View History

2011-06-02 00:48:29 +02:00
<?php
2011-12-21 22:20:06 +01:00
/**
* Rendert eine Liste von Checkboxen für ein Formular
* @param name Die Namen der Checkboxen werden aus name_key gebildet
* @param label Die Beschriftung der Liste
* @param items Array mit den einzelnen Checkboxen
* @param selected Array mit den Keys, die ausgewählt sind
*/
function form_checkboxes($name, $label, $items, $selected) {
$html = "<ul>";
foreach ($items as $key => $item) {
$id = $name . '_' . $key;
$sel = array_search($key, $selected) !== false ? ' checked="checked"' : "";
$html .= '<li><input type="checkbox" id="' . $id . '" name="' . $id . '" value="checked"' . $sel . ' /><label for="' . $id . '">' . $item . '</label></li>';
}
$html .= "</ul>";
return form_element($label, $html);
}
/**
* Rendert eine Checkbox
*/
function form_checkbox($name, $label, $selected, $value = 'checked') {
return form_element("", '<input type="checkbox" id="' . $name . '" name="' . $name . '" value="' . $value . '"' . ($selected ? ' checked="checked"' : '') . ' /><label for="' . $name . '">' . $label . '</label>');
}
/**
* Rendert einen Infotext in das Formular
*/
function form_info($label, $text) {
return form_element($label, $text, "");
}
/**
* Rendert den Absenden-Button eines Formulars
*/
function form_submit($name, $label) {
return form_element('<input class="button save ' . $name . '" type="submit" name="' . $name . '" value="' . $label . '" />', "");
}
/**
* Rendert ein Formular-Textfeld
*/
function form_text($name, $label, $value, $disabled = false) {
$disabled = $disabled ? ' disabled="disabled"' : '';
return form_element($label, '<input id="form_' . $name . '" type="text" name="' . $name . '" value="' . $value . '" ' . $disabled . '/>', 'form_' . $name);
}
2011-12-26 15:55:17 +01:00
/**
* Rendert ein Formular-Passwortfeld
*/
function form_password($name, $label, $disabled = false) {
$disabled = $disabled ? ' disabled="disabled"' : '';
return form_element($label, '<input id="form_' . $name . '" type="password" name="' . $name . '" value="" ' . $disabled . '/>', 'form_' . $name);
}
2011-12-21 22:20:06 +01:00
/**
* Rendert ein Formular-Textfeld
*/
function form_textarea($name, $label, $value, $disabled = false) {
$disabled = $disabled ? ' disabled="disabled"' : '';
return form_element($label, '<textarea id="form_' . $name . '" type="text" name="' . $name . '" ' . $disabled . '>' . $value . '</textarea>', 'form_' . $name);
}
/**
* Rendert ein Formular-Auswahlfeld
*/
function form_select($name, $label, $values, $selected) {
return form_element($label, html_select_key('form_' . $name, $name, $values, $selected), 'form_' . $name);
}
/**
* Rendert ein Formular-Element
*/
function form_element($label, $input, $for = "") {
return '<div class="form_element">' . '<label for="' . $for . '" class="form_label">' . $label . '</label><div class="form_input">' . $input . '</div></div>';
}
/**
* Rendert ein Formular
*/
function form($elements, $action = "") {
return '<form action="' . $action . '" enctype="multipart/form-data" method="post"><div class="form">' . join($elements) . '</div></form>';
}
/**
* Generiert HTML Code für eine "Seite". Fügt dazu die übergebenen Elemente zusammen.
*/
function page($elements) {
return join($elements);
}
/**
* Rendert eine Datentabelle
*/
function table($columns, $rows, $data = true) {
if (count($rows) == 0)
return info("No data available.", true);
$html = "";
$html .= '<table' . ($data ? ' class="data"' : '') . '>';
$html .= '<thead><tr>';
foreach ($columns as $key => $column)
$html .= '<th>' . $column . '</th>';
$html .= '</tr></thead>';
$html .= '<tbody>';
foreach ($rows as $row) {
$html .= '<tr>';
foreach ($columns as $key => $column)
if (isset ($row[$key]))
$html .= '<td class="' . $key . '">' . $row[$key] . '</td>';
else
$html .= '<td class="' . $key . '">&nbsp;</td>';
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
return $html;
}
/**
* Rendert einen Knopf
*/
function button($href, $label, $class = "") {
return '<a href="' . $href . '" class="button ' . $class . '">' . $label . '</a>';
}
/**
* Rendert eine Toolbar mit Knöpfen
*/
function buttons($buttons = array ()) {
return '<div class="toolbar">' . join($buttons) . '</div>';
}
2011-06-02 00:48:29 +02:00
// Load and render template
function template_render($file, $data) {
if (file_exists($file)) {
$template = file_get_contents($file);
if (is_array($data))
foreach ($data as $name => $content) {
$template = str_replace("%" . $name . "%", $content, $template);
}
return $template;
} else {
die('Cannot find template file &laquo;' . $file . '&raquo;.');
}
}
2011-06-02 16:56:45 +02:00
2011-07-13 14:29:40 +02:00
function shorten($str) {
if (strlen($str) < 50)
return $str;
2011-07-13 15:01:17 +02:00
return '<span title="' . htmlentities($str, ENT_COMPAT, 'UTF-8') . '">' . substr($str, 0, 47) . '...</span>';
2011-07-13 14:29:40 +02:00
}
2011-07-11 20:40:27 +02:00
function table_body($array) {
$html = "";
foreach ($array as $line) {
$html .= "<tr>";
if (is_array($line)) {
2011-07-11 21:58:06 +02:00
foreach ($line as $td)
$html .= "<td>" . $td . "</td>";
2011-07-11 20:40:27 +02:00
} else {
$html .= "<td>" . $line . "</td>";
}
$html .= "</tr>";
}
return $html;
}
2011-06-02 16:56:45 +02:00
function html_options($name, $options, $selected = "") {
$html = "";
foreach ($options as $value => $label)
2011-06-03 07:44:50 +02:00
$html .= '<input type="radio"' . ($value == $selected ? ' checked="checked"' : '') . ' name="' . $name . '" value="' . $value . '"> ' . $label;
2011-06-02 16:56:45 +02:00
return $html;
}
2011-06-02 20:18:01 +02:00
2011-12-26 15:55:17 +01:00
function html_select_key($id, $name, $rows, $selected) {
$html = '<select id="' . $id . '" name="' . $name . '">';
foreach ($rows as $key => $row) {
if (($key == $selected) || ($row == $selected)) {
2011-06-02 20:18:01 +02:00
$html .= '<option value="' . $key . '" selected="selected">' . $row . '</option>';
2011-12-26 15:55:17 +01:00
} else {
2011-06-02 20:18:01 +02:00
$html .= '<option value="' . $key . '">' . $row . '</option>';
2011-12-26 15:55:17 +01:00
}
}
2011-06-02 20:18:01 +02:00
$html .= '</select>';
return $html;
}
?>