summaryrefslogtreecommitdiff
path: root/includes/sys_template.php
blob: b5846e9bc02e08a1501215934a54781138578627 (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php

/**
 * Render a stat for dashborad (big number with label).
 * If no style given, style is danger if number > 0, and success if number == 0.
 * 
 * @param string $label
 * @param string $number
 * @param string $style default, warning, danger or success. Optional.
 */
function stats($label, $number, $style = null)
{
    if(empty($style)) {
        if($number > 0) {
            $style = 'danger';
        } else {
            $style = 'success';
        }
    }
    return div('stats stats-' . $style, [
        $label,
        div('number', [
            $number
        ])
    ]);
}

/**
 * Renders tabs from the array. Array key is tab name, array value is tab content.
 * 
 * @param array $tabs
 * @param int $selected The selected tab, default 0
 * @return string HTML
 */
function tabs($tabs, $selected = 0)
{
    $tab_header = [];
    $tab_content = [];
    foreach($tabs as $header => $content) {
        $class = '';
        if(count($tab_header) == $selected) {
            $class = 'active';
        }
        $tab_header[] = '<li role="presentation" class="' . $class . '">
                <a href="#' . $header . '" aria-controls="' . $header . '" role="tab" data-toggle="tab">' 
                    . $header . '</a></li>';
        $tab_content[] = '<div role="tabpanel" class="tab-pane ' . $class . '" id="' . $header . '">' . $content . '</div>';
    }
    return div('', [
        '<ul class="nav nav-tabs" role="tablist">' . join($tab_header) . '</ul>',
        '<div class="tab-content">' . join($tab_content) . '</div>'
    ]);
}

/**
 * Display muted (grey) text.
 *
 * @param string $text
 * @return string
 */
function mute($text)
{
    return '<span class="text-muted">' . $text . '</span>';
}

/**
 * Renders a bootstrap label with given content and class.
 *
 * @param string $content The text
 * @param string $class   default, primary, info, success, warning, danger
 * @return string
 */
function label($content, $class = 'default')
{
    return '<span class="label label-' . $class . '">' . $content . '</span>';
}

/**
 * @param int    $valuemin
 * @param int    $valuemax
 * @param int    $valuenow
 * @param string $class
 * @param string $content
 * @return string
 */
function progress_bar($valuemin, $valuemax, $valuenow, $class = '', $content = '')
{
    return '<div class="progress">'
        . '<div class="progress-bar ' . $class . '" role="progressbar" '
        . 'aria-valuenow="' . $valuenow . '" aria-valuemin="' . $valuemin . '" aria-valuemax="' . $valuemax . '" '
        . 'style="width: ' . floor(($valuenow - $valuemin) * 100 / ($valuemax - $valuemin)) . '%"'
        . '>'
        . $content . ''
        . '</div>'
        . '</div>';
}

/**
 * Render glyphicon
 *
 * @param string $glyph_name
 * @return string
 */
function glyph($glyph_name)
{
    return ' <span class="glyphicon glyphicon-' . $glyph_name . '"></span> ';
}

/**
 * Renders a tick or a cross by given boolean
 *
 * @param boolean $boolean
 * @return string
 */
function glyph_bool($boolean)
{
    return '<span class="text-' . ($boolean ? 'success' : 'danger') . '">' . glyph($boolean ? 'ok' : 'remove') . '</span>';
}

/**
 * @param string $class
 * @param array  $content
 * @param string $dom_id
 * @return string
 */
function div($class, $content = [], $dom_id = '')
{
    if (is_array($content)) {
        $content = join("\n", $content);
    }
    $dom_id = $dom_id != '' ? ' id="' . $dom_id . '"' : '';
    return '<div' . $dom_id . ' class="' . $class . '">' . $content . '</div>';
}

/**
 * @param string $content
 * @param int    $number
 * @return string
 */
function heading($content, $number = 1)
{
    return '<h' . $number . '>' . $content . '</h' . $number . '>';
}

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

/**
 * @param string[] $items
 * @return string
 */
function toolbar_pills($items)
{
    return '<ul class="nav nav-pills">' . 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>';
}

/**
 * @return string
 */
function toolbar_item_divider()
{
    return '<li class="divider"></li>';
}

/**
 * @param string $glyphicon
 * @param string $label
 * @param array  $submenu
 * @param string $class
 * @return string
 */
function toolbar_dropdown($glyphicon, $label, $submenu, $class = '')
{
    return '<li class="dropdown ' . $class . '">
          <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>';
}

/**
 * @param string   $glyphicon
 * @param string   $label
 * @param string[] $content
 * @param string   $class
 * @return string
 */
function toolbar_popover($glyphicon, $label, $content, $class = '')
{
    $dom_id = md5(microtime() . $glyphicon . $label);
    return '<li class="dropdown messages ' . $class . '">
          <a id="' . $dom_id . '" href="#" tabindex="0">'
        . ($glyphicon != '' ? '<span class="glyphicon glyphicon-' . $glyphicon . '"></span> ' : '')
        . $label
        . ' <span class="caret"></span></a>
          <script type="text/javascript">
          $(function(){
              $("#' . $dom_id . '").popover({
                  trigger: "focus",
                  html: true,
                  content: "' . addslashes(join('', $content)) . '",
                  placement: "bottom",
                  container: "#navbar-collapse-1"
              })
          });
          </script></li>';
}


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

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

/**
 * Renders a description based on the data arrays key and values as label an description.
 * @param array $data
 */
function description($data) {
    $elements = [];
    foreach($data as $label => $description) {
        if(!empty($label) && !empty($description)) {
            $elements[] = '<dt>' . $label . '</dt><dd>' . $description . '</dd>';
        }
    }
    return '<dl class="dl-horizontal">' . join($elements) . '</dl>';
}

/**
 * Rendert eine Datentabelle
 *
 * @param array|string $columns
 * @param array[]      $rows_raw
 * @param bool         $data
 * @return string
 */
function table($columns, $rows_raw, $data = true)
{
    // If only one column is given
    if (!is_array($columns)) {
        $rows = [];
        foreach ($rows_raw as $row) {
            $rows[] = [
                'col' => $row
            ];
        }
        return render_table([
            'col' => $columns
        ], $rows, $data);
    }

    return render_table($columns, $rows_raw, $data);
}

/**
 * Helper for rendering a html-table.
 * use table()
 *
 * @param string[] $columns
 * @param array[]  $rows
 * @param bool     $data
 * @return string
 */
function render_table($columns, $rows, $data = true)
{
    if (count($rows) == 0) {
        return info(_('No data found.'), true);
    }

    $html = '<table class="table table-striped' . ($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) {
            $value = "&nbsp;";
            if (isset($row[$key])) {
                $value = $row[$key];
            }
            $html .= '<td class="column_' . $key . '">' . $value . '</td>';
        }
        $html .= '</tr>';
    }
    $html .= '</tbody>';
    $html .= '</table>';
    return $html;
}

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

/**
 * Rendert einen Knopf mit Glyph
 *
 * @param string $href
 * @param string $glyph
 * @param string $class
 * @return string
 */
function button_glyph($href, $glyph, $class = '')
{
    return button($href, glyph($glyph), $class);
}

/**
 * Rendert eine Toolbar mit Knöpfen
 *
 * @param array $buttons
 * @return string
 */
function buttons($buttons = [])
{
    return '<div class="form-group">' . table_buttons($buttons) . '</div>';
}

/**
 * @param array $buttons
 * @return string
 */
function table_buttons($buttons = [])
{
    return '<div class="btn-group">' . join(' ', $buttons) . '</div>';
}

/**
 * @param string $str
 * @param int    $length
 * @return string
 */
function shorten($str, $length = 50)
{
    if (strlen($str) < $length) {
        return $str;
    }
    return '<span title="' . htmlentities($str, ENT_COMPAT, 'UTF-8') . '">'
        . substr($str, 0, $length - 3)
        . '...</span>';
}

/**
 * @param array[] $array
 * @return string
 */
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;
}

/**
 * @param string $msg
 * @return mixed
 */
function ReplaceSmilies($msg)
{
    $msg = str_replace(';o))', '<img src="pic/smiles/icon_redface.gif">', $msg);
    $msg = str_replace(':-))', '<img src="pic/smiles/icon_redface.gif">', $msg);
    $msg = str_replace(';o)', '<img src="pic/smiles/icon_wind.gif">', $msg);
    $msg = str_replace(':)', '<img src="pic/smiles/icon_smile.gif">', $msg);
    $msg = str_replace(':-)', '<img src="pic/smiles/icon_smile.gif">', $msg);
    $msg = str_replace(':(', '<img src="pic/smiles/icon_sad.gif">', $msg);
    $msg = str_replace(':-(', '<img src="pic/smiles/icon_sad.gif">', $msg);
    $msg = str_replace(':o(', '<img src="pic/smiles/icon_sad.gif">', $msg);
    $msg = str_replace(':o)', '<img src="pic/smiles/icon_lol.gif">', $msg);
    $msg = str_replace(';o(', '<img src="pic/smiles/icon_cry.gif">', $msg);
    $msg = str_replace(';(', '<img src="pic/smiles/icon_cry.gif">', $msg);
    $msg = str_replace(';-(', '<img src="pic/smiles/icon_cry.gif">', $msg);
    $msg = str_replace('8)', '<img src="pic/smiles/icon_rolleyes.gif">', $msg);
    $msg = str_replace('8o)', '<img src="pic/smiles/icon_rolleyes.gif">', $msg);
    $msg = str_replace(':P', '<img src="pic/smiles/icon_evil.gif">', $msg);
    $msg = str_replace(':-P', '<img src="pic/smiles/icon_evil.gif">', $msg);
    $msg = str_replace(':oP', '<img src="pic/smiles/icon_evil.gif">', $msg);
    $msg = str_replace(';P', '<img src="pic/smiles/icon_mad.gif">', $msg);
    $msg = str_replace(';oP', '<img src="pic/smiles/icon_mad.gif">', $msg);
    $msg = str_replace('?)', '<img src="pic/smiles/icon_question.gif">', $msg);

    return $msg;
}