summaryrefslogtreecommitdiff
path: root/includes/sys_template.php
blob: 3366346b52cbd68a80d33dcce6fc1b80a6e6dd61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php

/**
 * Liste der verfügbaren Themes
 */
$themes = array(
    "0" => "Engelsystem light",
    "1" => "Engelsystem dark" 
);

function div($class, $content = array(), $id = "") {
  $id = $id != '' ? ' id="' . $id . '"' : '';
  return '<div' . $id . ' class="' . $class . '">' . join("\n", $content) . '</div>';
}

/**
 * Render a toolbar.
 *
 * @param array $items          
 * @return string
 */
function toolbar($items = array()) {
  return '<ul class="nav navbar-nav navbar-right">' . join("\n", $items) . '</ul>';
}

/**
 * Render a link for a toolbar.
 *
 * @param string $href          
 * @param string $glyphicon          
 * @param string $label          
 * @param bool $selected          
 * @return string
 */
function toolbar_item_link($href, $glyphicon, $label, $selected = false) {
  return '<li class="' . ($selected ? 'active' : '') . '"><a href="' . $href . '">' . ($glyphicon != '' ? '<span class="glyphicon glyphicon-' . $glyphicon . '"></span> ' : '') . $label . '</a></li>';
}

function toolbar_dropdown($glyphicon, $label, $submenu) {
  return '<li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">' . ($glyphicon != '' ? '<span class="glyphicon glyphicon-' . $glyphicon . '"></span> ' : '') . $label . ' <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">' . join("\n", $submenu) . '</ul></li>';
}

/**
 * Rendert ein Zahlenfeld mit Buttons zum verstellen
 */
function form_spinner($name, $label, $value) {
  return form_element($label, '
      <div class="input-group">
        <input id="spinner-' . $name . '" class="form-control" type="text" 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>
      </div>
      <script type="text/javascript">
        $("#spinner-' . $name . '-down").click(function(e) {
          $("#spinner-' . $name . '").val(parseInt($("#spinner-' . $name . '").val()) - 1);
        });
        $("#spinner-' . $name . '-up").click(function(e) {
          $("#spinner-' . $name . '").val(parseInt($("#spinner-' . $name . '").val()) + 1);
        });
      </script>
      ');
}

/**
 * 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 = form_element($label, '');
  foreach ($items as $key => $item)
    $html .= form_checkbox($name . '_' . $key, $item, array_search($key, $selected) !== false);
  
  return $html;
  
  $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 Tabelle von Checkboxen für ein Formular
 *
 * @param
 *          names Assoziatives Array mit Namen der Checkboxen als Keys und Überschriften als Values
 * @param
 *          label Die Beschriftung der gesamten Tabelle
 * @param
 *          items Array mit den Beschriftungen der Zeilen
 * @param
 *          selected Mehrdimensionales Array, wobei $selected[foo] ein Array der in der Datenreihe foo markierten Checkboxen ist
 * @param
 *          disabled Wie selected, nur dass die entsprechenden Checkboxen deaktiviert statt markiert sind
 */
function form_multi_checkboxes($names, $label, $items, $selected, $disabled = array()) {
  $html = "<table><thead><tr>";
  foreach ($names as $title)
    $html .= "<th>$title</th>";
  $html .= "</tr></thead><tbody>";
  foreach ($items as $key => $item) {
    $html .= "<tr>";
    foreach ($names as $name => $title) {
      $id = $name . '_' . $key;
      $sel = array_search($key, $selected[$name]) !== false ? ' checked="checked"' : "";
      if (! empty($disabled) && ! empty($disabled[$name]) && array_search($key, $disabled[$name]) !== false)
        $sel .= ' disabled="disabled"';
      $html .= '<td style="text-align: center;"><input type="checkbox" id="' . $id . '" name="' . $name . '[]" value="' . $key . '"' . $sel . ' /></td>';
    }
    $html .= '<td><label for="' . $id . '">' . $item . '</label></td></tr>';
  }
  $html .= "</tbody></table>";
  return form_element($label, $html);
}

/**
 * Rendert eine Checkbox
 */
function form_checkbox($name, $label, $selected, $value = 'checked') {
  return '<div class="checkbox"><label><input type="checkbox" id="' . $name . '" name="' . $name . '" value="' . $value . '"' . ($selected ? ' checked="checked"' : '') . ' /> ' . $label . '</label></div>';
}

/**
 * Rendert einen Radio
 */
function form_radio($name, $label, $selected, $value) {
  return '<div class="radio"><label><input type="radio" id="' . $name . '" name="' . $name . '" value="' . $value . '"' . ($selected ? ' checked="checked"' : '') . ' /> ' . $label . '</label></div>';
}

/**
 * Rendert einen Infotext in das Formular
 */
function form_info($label, $text = "") {
  if ($label == "")
    return '<span class="help-block">' . $text . '</span>';
  if ($text == "")
    return '<h4>' . $label . '</h4>';
  return form_element($label, '<p class="form-control-static">' . $text . '</p>', '');
}

/**
 * Rendert den Absenden-Button eines Formulars
 */
function form_submit($name, $label) {
  return form_element('<input class="btn btn-primary" 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 class="form-control" id="form_' . $name . '" type="text" name="' . $name . '" value="' . htmlspecialchars($value) . '" ' . $disabled . '/>', 'form_' . $name);
}

/**
 * Rendert ein Formular-Dateifeld
 */
function form_file($name, $label) {
  return form_element($label, '<input id="form_' . $name . '" type="file" name="' . $name . '" />', 'form_' . $name);
}

/**
 * Rendert ein Formular-Passwortfeld
 */
function form_password($name, $label, $disabled = false) {
  $disabled = $disabled ? ' disabled="disabled"' : '';
  return form_element($label, '<input class="form-control" id="form_' . $name . '" type="password" name="' . $name . '" value="" ' . $disabled . '/>', 'form_' . $name);
}

/**
 * Rendert ein Formular-Textfeld
 */
function form_textarea($name, $label, $value, $disabled = false) {
  $disabled = $disabled ? ' disabled="disabled"' : '';
  return form_element($label, '<textarea rows="5" class="form-control" 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-group">' . '<label for="' . $for . '">' . $label . '</label>' . $input . '</div>';
}

/**
 * Rendert ein Formular
 */
function form($elements, $action = "") {
  return '<form role="form" action="' . $action . '" enctype="multipart/form-data" method="post">' . join($elements) . '</form>';
}

/**
 * Generiert HTML Code für eine "Seite".
 * Fügt dazu die übergebenen Elemente zusammen.
 */
function page($elements) {
  return join($elements);
}

/**
 * Generiert HTML Code für eine "Seite" mit zentraler Überschrift
 * Fügt dazu die übergebenen Elemente zusammen.
 */
function page_with_title($title, $elements) {
  return '<div class="col-md-10"><h1>' . $title . '</h1>' . join($elements) . '</div>';
}

/**
 * Rendert eine Datentabelle
 */
function table($columns, $rows_raw, $data = true) {
  // If only one column is given
  if (! is_array($columns)) {
    $columns = array(
        'col' => $columns 
    );
    
    $rows = array();
    foreach ($rows_raw as $row)
      $rows[] = array(
          'col' => $row 
      );
  } else
    $rows = $rows_raw;
  
  if (count($rows) == 0)
    return info(_("No data found."), true);
  $html = "";
  $html .= '<table class="table ' . ($data ? ' data' : '') . '">';
  $html .= '<thead><tr>';
  foreach ($columns as $key => $column)
    $html .= '<th class="column_' . $key . '">' . $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="column_' . $key . '">' . $row[$key] . '</td>';
      else
        $html .= '<td class="column_' . $key . '">&nbsp;</td>';
    $html .= '</tr>';
  }
  $html .= '</tbody>';
  $html .= '</table>';
  return $html;
}

/**
 * Rendert einen Knopf
 */
function button($href, $label, $class = "") {
  return '<a href="' . $href . '" class="btn btn-default ' . $class . '">' . $label . '</a>';
}

/**
 * Rendert eine Toolbar mit Knöpfen
 */
function buttons($buttons = array ()) {
  return '<div class="form-group"><div class="btn-group">' . join(' ', $buttons) . '</div></div>';
}

// 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;.');
  }
}

function shorten($str) {
  if (strlen($str) < 50)
    return $str;
  return '<span title="' . htmlentities($str, ENT_COMPAT, 'UTF-8') . '">' . substr($str, 0, 47) . '...</span>';
}

function table_body($array) {
  $html = "";
  foreach ($array as $line) {
    $html .= "<tr>";
    if (is_array($line)) {
      foreach ($line as $td)
        $html .= "<td>" . $td . "</td>";
    } else {
      $html .= "<td>" . $line . "</td>";
    }
    $html .= "</tr>";
  }
  return $html;
}

function html_options($name, $options, $selected = "") {
  $html = "";
  foreach ($options as $value => $label)
    $html .= '<input type="radio"' . ($value == $selected ? ' checked="checked"' : '') . ' name="' . $name . '" value="' . $value . '"> ' . $label;
  
  return $html;
}

function html_select_key($id, $name, $rows, $selected) {
  $html = '<select class="form-control" id="' . $id . '" name="' . $name . '">';
  foreach ($rows as $key => $row) {
    if (($key == $selected) || ($row == $selected)) {
      $html .= '<option value="' . $key . '" selected="selected">' . $row . '</option>';
    } else {
      $html .= '<option value="' . $key . '">' . $row . '</option>';
    }
  }
  $html .= '</select>';
  return $html;
}

function img_button($link, $icon, $text, $extra_text = '') {
  return '<a href="' . htmlspecialchars($link) . '"><img src="pic/icons/' . htmlspecialchars($icon) . '.png" alt="' . $text . '" ' . (empty($text) ? '' : 'title="' . $text . '"') . '>' . (empty($extra_text) ? '' : ' ' . $extra_text) . '</a>';
}

function ReplaceSmilies($neueckig) {
  $neueckig = str_replace(";o))", "<img src=\"pic/smiles/icon_redface.gif\">", $neueckig);
  $neueckig = str_replace(":-))", "<img src=\"pic/smiles/icon_redface.gif\">", $neueckig);
  $neueckig = str_replace(";o)", "<img src=\"pic/smiles/icon_wind.gif\">", $neueckig);
  $neueckig = str_replace(":)", "<img src=\"pic/smiles/icon_smile.gif\">", $neueckig);
  $neueckig = str_replace(":-)", "<img src=\"pic/smiles/icon_smile.gif\">", $neueckig);
  $neueckig = str_replace(":(", "<img src=\"pic/smiles/icon_sad.gif\">", $neueckig);
  $neueckig = str_replace(":-(", "<img src=\"pic/smiles/icon_sad.gif\">", $neueckig);
  $neueckig = str_replace(":o(", "<img src=\"pic/smiles/icon_sad.gif\">", $neueckig);
  $neueckig = str_replace(":o)", "<img src=\"pic/smiles/icon_lol.gif\">", $neueckig);
  $neueckig = str_replace(";o(", "<img src=\"pic/smiles/icon_cry.gif\">", $neueckig);
  $neueckig = str_replace(";(", "<img src=\"pic/smiles/icon_cry.gif\">", $neueckig);
  $neueckig = str_replace(";-(", "<img src=\"pic/smiles/icon_cry.gif\">", $neueckig);
  $neueckig = str_replace("8)", "<img src=\"pic/smiles/icon_rolleyes.gif\">", $neueckig);
  $neueckig = str_replace("8o)", "<img src=\"pic/smiles/icon_rolleyes.gif\">", $neueckig);
  $neueckig = str_replace(":P", "<img src=\"pic/smiles/icon_evil.gif\">", $neueckig);
  $neueckig = str_replace(":-P", "<img src=\"pic/smiles/icon_evil.gif\">", $neueckig);
  $neueckig = str_replace(":oP", "<img src=\"pic/smiles/icon_evil.gif\">", $neueckig);
  $neueckig = str_replace(";P", "<img src=\"pic/smiles/icon_mad.gif\">", $neueckig);
  $neueckig = str_replace(";oP", "<img src=\"pic/smiles/icon_mad.gif\">", $neueckig);
  $neueckig = str_replace("?)", "<img src=\"pic/smiles/icon_question.gif\">", $neueckig);
  
  return $neueckig;
}
?>