Issues (1963)

html/inc/bootstrap.inc (13 issues)

1
<?php
2
// This file is part of BOINC.
3
// http://boinc.berkeley.edu
4
// Copyright (C) 2008 University of California
5
//
6
// BOINC is free software; you can redistribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License
8
// as published by the Free Software Foundation,
9
// either version 3 of the License, or (at your option) any later version.
10
//
11
// BOINC is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// You should have received a copy of the GNU Lesser General Public License
17
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
19
// An interface to bootstrap navbars, grids, and forms.
20
21
$fixed_navbar = false;
22
23
if (defined('REMOTE_JOB_SUBMISSION') && REMOTE_JOB_SUBMISSION) {
24
    require_once("../inc/submit_db.inc");
25
}
26
27
////////////// NAVBAR ////////////////
28
29
// call this to start the navbar.
30
// $brand: the text or image to show at left of navbar
31
// If text, put it in <a class="navbar-brand" ...
32
//
33
function navbar_start($brand, $fixed, $inverse) {
34
    global $fixed_navbar;
35
    $class = "navbar";
36
    if ($inverse) {
37
        $class .= " navbar-inverse";
38
    } else {
39
        $class .= " navbar-default";
40
    }
41
    if ($fixed) {
42
        $class .= " navbar-fixed-top";
43
        $fixed_navbar = true;
44
    }
45
    echo "<nav class=\"$class\">\n";
46
    echo '
47
  <div class="container-fluid">
48
    <div class="navbar-header">
49
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
50
        <span class="icon-bar"></span>
51
        <span class="icon-bar"></span>
52
        <span class="icon-bar"></span>
53
      </button>
54
      '.$brand.'
55
    </div>
56
    <div class="collapse navbar-collapse" id="myNavbar">
57
      <ul class="nav navbar-nav">
58
    ';
59
}
60
61
// call this to end it
62
//
63
function navbar_end() {
64
    echo '
65
      </ul>
66
    </div>
67
  </div>
68
</nav>
69
    ';
70
}
71
72
// put the login/logout stuff at the right side of navbar
73
//
74
function navbar_right($user) {
75
    global $is_login_page;
76
    echo '
77
      </ul>
78
      <ul class="nav navbar-nav navbar-right">
79
    ';
80
    if (!$is_login_page) {
81
        if ($user) {
82
            echo sprintf('
83
                <li><a href=%s%s>%s</a></li>
84
                ', url_base(), HOME_PAGE, $user->name
85
            );
86
            $url_tokens = url_tokens($user->authenticator);
87
            echo sprintf('<li><a href="%slogout.php?%s">Log out</a></li>',
88
                url_base(), $url_tokens
89
            );
90
        } else {
91
            echo sprintf('
92
                <li><a href="%ssignup.php">%s</a></li>
93
                <li><a href="%slogin_form.php">%s</a></li>
94
                ', url_base(),
95
                tra("Join"),
96
                url_base(),
97
                tra("Login")
98
            );
99
        }
100
    }
101
}
102
103
// add a dropdown menu
104
//
105
function navbar_menu($name, $items) {
106
    echo '
107
      <li class="dropdown">
108
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">'.$name.'
109
        <span class="caret"></span></a>
110
        <ul class="dropdown-menu">
111
    ';
112
    foreach ($items as $item) {
113
        if (is_array($item)) {
114
            echo '<li><a href="'.$item[1].'">'.$item[0].'</a></li>
115
            ';
116
        } else {
117
            echo '<li class="dropdown-header">'.$item.'</li>
118
            ';
119
        }
120
    }
121
    echo '
122
        </ul>
123
      </li>
124
    ';
125
}
126
127
// add a single item (not menu)
128
//
129
function navbar_item($name, $url) {
130
    echo '<li><a href="'.$url.'">'.$name.'</a></li>
131
    ';
132
}
133
134
// A generic navbar.
135
// Call this from project_banner().
136
// If you want to customized it, copy it to your project.inc
137
// and give it a new name
138
//
139
function sample_navbar(
140
    $url_prefix,
141
        // prefix for links; needed for pages not in top dir
0 ignored issues
show
Multi-line function declaration not indented correctly; expected 4 spaces but found 8
Loading history...
142
    $user,
143
        // logged-in user, if any
0 ignored issues
show
Multi-line function declaration not indented correctly; expected 4 spaces but found 8
Loading history...
144
    $fixed=false,
145
        // if true, navbar is fixed at top of page.
0 ignored issues
show
Multi-line function declaration not indented correctly; expected 4 spaces but found 8
Loading history...
146
        // NOTE: if you do this, you must set a global var $fixed_navbar
0 ignored issues
show
Multi-line function declaration not indented correctly; expected 4 spaces but found 8
Loading history...
147
        // to true at compile time
0 ignored issues
show
Multi-line function declaration not indented correctly; expected 4 spaces but found 8
Loading history...
148
        // (it needs to be set when page_head() is called).
0 ignored issues
show
Multi-line function declaration not indented correctly; expected 4 spaces but found 8
Loading history...
149
    $inverse=false
150
        // white on black?
0 ignored issues
show
Multi-line function declaration not indented correctly; expected 4 spaces but found 8
Loading history...
151
) {
152
    $master_url = master_url();
153
154
    $brand = "<a class=\"navbar-brand\" href=$master_url>".PROJECT."</a>";
155
    navbar_start($brand, $fixed, $inverse);
156
157
    $x = array();
158
    if ($user) {
159
        $x[] = array(tra("Account"), $url_prefix.HOME_PAGE);
160
        $x[] = array(tra("Join"), $url_prefix."join.php");
161
        $x[] = array(tra("Preferences"), $url_prefix."prefs.php?subset=project");
162
    }
163
    $x[] = array(tra("About %1", PROJECT), $url_prefix."about.php");
164
    $x[] = array(tra("Help"), $url_prefix."welcome.php");
165
    navbar_menu(tra("Project"), $x);
166
167
    if (NO_COMPUTING) {
168
        // this is for projects that don't do computing, e.g. BOSSA-based
169
        //
170
        if (defined('BOSSA')) {
171
            navbar_menu(tra("Participate"), array(
172
                array(tra("Do work"), $url_prefix."bossa_apps.php"),
173
            ));
174
        }
175
    } else {
176
        $x = array(
177
            array(tra("Preferences"), $url_prefix."prefs.php?subset=global"),
178
            array(tra("Server status"), $url_prefix."server_status.php"),
179
            array(tra("Credit statistics"), $url_prefix."stats.php"),
180
            array(tra("Applications"), $url_prefix."apps.php"),
181
            array(tra("GPU models"), $url_prefix."gpu_list.php"),
182
            array(tra("CPU models"), $url_prefix."cpu_list.php"),
183
            array(tra("Computer types"), $url_prefix."host_stats.php"),
184
        );
185
        navbar_menu(tra("Computing"), $x);
186
187
        if (defined('REMOTE_JOB_SUBMISSION') && REMOTE_JOB_SUBMISSION) {
188
            $user_submit = null;
189
            if ($user) {
190
                $user_submit = BoincUserSubmit::lookup_userid($user->id);
191
            }
192
            if ($user_submit) {
193
                $x = [];
194
                $x[] = ["File sandbox", "sandbox.php"];
195
                $x[] = ["Submit jobs", $url_prefix."submit.php"];
196
                $x[] = ["Job status", $url_prefix."submit.php?action=status"];
197
                if ($user_submit->manage_all) {
198
                    $x[] = ["Manage apps", $url_prefix."submit.php?action=admin"];
199
                    $x[] = ["Manage permissions", $url_prefix."manage_project.php"];
200
                } else {
201
                    // if user has manage permissions for apps, show links
202
                    $usas = BoincUserSubmitApp::enum("user_id=$user->id");
203
                    foreach ($usas as $usa) {
204
                        if ($usa->manage) {
205
                            $app = BoincApp::lookup_id($usa->app_id);
206
                            if ($app->name == 'buda') {
207
                                $x[] = [
208
                                    'Manage BUDA apps',
209
                                    'buda.php'
0 ignored issues
show
There should be a trailing comma after the last value of an array declaration.
Loading history...
210
                                ];
211
                            } else {
212
                                $x[] = [
213
                                    "Manage $app->name",
214
                                    "manage_project.php"
0 ignored issues
show
There should be a trailing comma after the last value of an array declaration.
Loading history...
215
                                ];
216
                            }
217
                        }
218
                    }
219
                }
220
                navbar_menu(tra("Job submission"), $x);
221
            }
222
        }
223
    }
224
225
    navbar_menu(tra("Community"), array(
226
        array(tra("Message boards"), $url_prefix."forum_index.php"),
227
        //array(tra("Questions and Answers"), $url_prefix."forum_help_desk.php"),
228
        array(tra("Teams"), $url_prefix."team.php", tra("create or join a team")),
229
        array(tra("Profiles"), $url_prefix."profile_menu.php"),
230
        array(tra("Preferences"), $url_prefix."edit_forum_preferences_form.php"),
231
        array(tra("User search"), $url_prefix."user_search.php"),
232
        array(tra("User of the day"), $url_prefix."uotd.php"),
233
        array(tra("Certificate"), $url_prefix.cert_filename(), "", "_blank"),
234
    ));
235
    navbar_menu(tra("Site"), array(
236
        array(tra("Site search"), $url_prefix."site_search.php"),
237
        array(tra("Languages"), $url_prefix."language_select.php")
0 ignored issues
show
There should be a trailing comma after the last value of an array declaration.
Loading history...
238
    ));
239
240
    // add your own menu here if you want
241
242
    navbar_right($user);
243
    navbar_end();
244
}
245
246
// output a panel.
247
// $content_func is a function that generates the panel contents
248
//
249
function panel($title, $content_func, $class="panel-primary", $body_class="") {
250
    echo sprintf('<div class="panel %s">
251
        ', $class
252
    );
253
    if ($title) {
254
        echo '
255
            <div class="panel-heading">
256
                <h1 class="panel-title">'.$title.'</h1>
257
            </div>
258
        ';
259
    }
260
    echo sprintf('<div class="panel-body %s">
261
        ', $body_class
262
    );
263
    $content_func();
264
    echo '
265
        </div>
266
        </div>
267
    ';
268
}
269
270
// grid layout with a full-width row followed by two columns.
271
// $top_func, $left_func, and $right_func
272
// are functions that generate the top, left, and right content.
273
// $left_width is the width of left column in 1/12 units.
274
// $arg is passed to the functions.
275
//
276
function grid($top_func, $left_func, $right_func, $left_width=6, $arg=null) {
277
    echo '
278
        <div class="container-fluid">
279
    ';
280
    if ($top_func) {
281
        echo '
282
            <div class="row">
283
            <div class="col-sm-12">
284
        ';
285
        $top_func($arg);
286
        echo '
287
            </div>
288
            </div>
289
        ';
290
    }
291
    $right_width = 12-$left_width;
292
    echo '
293
        <div class="row">
294
        <div class="col-sm-'.$left_width.'">
295
    ';
296
    $left_func($arg);
297
    echo '
298
        </div>
299
        <div class="col-sm-'.$right_width.'">
300
    ';
301
    $right_func($arg);
302
    echo '
303
        </div>
304
        </div>
305
        </div>
306
    ';
307
}
308
309
// to upload files:
310
//  use method = POST and extra=ENCTYPE="multipart/form-data"
311
// to have initial focus on input field foo:
312
//      use extra = "name=x"
313
//      call forum_focus(x, foo) after defining the field
314
//
315
function form_start($action, $method='get', $extra='') {
316
    echo sprintf(
317
        '<div class="container-fluid">
318
        <form class="form-horizontal" method="%s" action="%s" %s>'
319
        ,
320
        $method, $action, $extra
321
    );
322
}
323
324
function form_input_hidden($name, $value) {
325
    echo '<input type="hidden" name="'.$name.'" value="'.$value.'">
326
    ';
327
}
328
329
function form_focus($form_name, $field_name) {
330
    echo "<script>document.$form_name.$field_name.focus()</script>\n";
331
332
}
333
334
function form_end() {
335
    echo '</form>
336
        </div>
337
    ';
338
}
339
340
define('FORM_LEFT_CLASS', 'col-sm-3');
341
define('FORM_LEFT_OFFSET', 'col-sm-offset-3');
342
define('FORM_RIGHT_CLASS', 'col-sm-9');
343
344
// just the input field
345
//
346
function form_input_text_field(
347
    $name, $value='', $type='text', $attrs='', $extra=''
0 ignored issues
show
Multi-line function declarations must define one parameter per line
Loading history...
348
) {
349
    return sprintf(
350
        '<input %s type="%s" class="form-control" name="%s" value="%s">%s',
351
        $attrs, $type, $name, $value, $extra
352
    );
353
}
354
355
// the whole row
356
//
357
function form_input_text(
358
    $label, $name, $value='', $type='text', $attrs='', $extra=''
0 ignored issues
show
Multi-line function declarations must define one parameter per line
Loading history...
359
) {
360
    echo sprintf('
361
        <div class="form-group">
362
            <label align=right class="%s">%s</label>
363
            <div class="%s">
364
                %s
365
            </div>
366
        </div>
367
        ',
368
        FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS,
369
        form_input_text_field($name, $value, $type, $attrs, $extra)
370
    );
371
}
372
373
// display name/value with same formatting as form
374
//
375
function form_attr($name, $value) {
376
    echo sprintf('
377
        <div class="form-group">
378
            <div class="%s text-right">%s</div>
379
            <div class="%s">%s</div>
380
        </div>
381
        ',
382
        FORM_LEFT_CLASS, $name, FORM_RIGHT_CLASS, $value
383
    );
384
}
385
386
function form_input_textarea($label, $name, $value='', $nrows=4) {
387
    echo sprintf('
388
        <div class="form-group">
389
            <label align=right class="%s" for="%s">%s</label>
390
            <div class="%s">
391
                <textarea rows="%d" class="form-control" id="%s" name="%s">%s</textarea>
392
            </div>
393
        </div>
394
        ',
395
        FORM_LEFT_CLASS, $name, $label, FORM_RIGHT_CLASS,
396
        $nrows, $name, $name, $value
397
    );
398
}
399
400
// $items is either a string of <option> elements,
401
// or an array of [value, name] pairs
402
//
403
function form_select($label, $name, $items, $selected=null) {
404
    echo sprintf('
405
        <div class="form-group">
406
            <label align=right class="%s" for="%s">%s</label>
407
            <div class="%s">
408
                <select class="form-control" id="%s" name="%s">
409
        ',
410
        FORM_LEFT_CLASS, $name, $label, FORM_RIGHT_CLASS, $name, $name
411
    );
412
    if (is_array($items)) {
413
        foreach ($items as $i) {
414
            echo sprintf(
415
                '<option %s value=%s>%s</option>',
416
                ($i[0]==$selected)?'selected':'',
417
                $i[0], $i[1]
418
            );
419
        }
420
    } else {
421
        echo $items;
422
    }
423
    echo "</select></div></div>\n";
424
}
425
426
// same, for multiple select.
427
// $selected, if non-null, is a list of selected values
428
//
429
function form_select_multiple($label, $name, $items, $selected=null, $size=0) {
430
    echo sprintf('
431
        <div class="form-group">
432
            <label align=right class="%s" for="%s">%s</label>
433
            <div class="%s">
434
                <select multiple class="form-control" id="%s" name="%s[]" size=%d>
435
        ',
436
        FORM_LEFT_CLASS, $name, $label, FORM_RIGHT_CLASS, $name, $name, $size
437
    );
438
    foreach ($items as $i) {
439
        echo sprintf(
440
            '<option %s value=%s>%s</option>',
441
            ($selected && in_array($i[0], $selected))?'selected':'',
442
            $i[0], $i[1]
443
        );
444
    }
445
    echo "</select></div></div>\n";
446
}
447
448
// return a list of strings for checkbox items
449
//
450
function checkbox_item_strings($items, $attrs='') {
451
    $x = [];
452
    foreach ($items as $i) {
453
        $x[] = sprintf('<input %s type="checkbox" name="%s" %s> %s
454
            ',
455
            $attrs, $i[0], $i[2]?"checked":"", $i[1]
456
        );
457
    }
458
    return $x;
459
}
460
461
// $items is list of (name, label, checked)
462
//
463
function form_checkboxes($label, $items, $attrs='') {
464
    echo sprintf('
465
        <div class="form-group">
466
            <label align=right class="%s">%s</label>
467
            <div class="%s">
468
        ',
469
        FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS
470
    );
471
    $x = checkbox_item_strings($items, $attrs);
472
    echo implode('<br>', $x);
473
    echo '</div>
474
        </div>
475
    ';
476
}
477
478
// $items is list of [value, label]
479
//
480
function form_radio_buttons(
481
    $label, $name, $items, $selected,
0 ignored issues
show
Multi-line function declarations must define one parameter per line
Loading history...
482
    $assign_ids=false       // assign IDs to buttons based on names
483
) {
484
    echo sprintf('
485
        <div class="form-group">
486
            <label align=right class="%s">%s</label>
487
            <div class="%s">
488
        ',
489
        FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS
490
    );
491
    foreach ($items as $i) {
492
        $checked = ($selected == $i[0])?"checked":"";
493
        if ($assign_ids) {
494
            $id = sprintf('id="%s_%s"', $name, $i[0]);
495
        } else {
496
            $id = '';
497
        }
498
        echo sprintf('<input type="radio" name="%s" value="%s" %s %s> %s <br>
499
            ',
500
            $name, $i[0], $checked, $id, $i[1]
501
        );
502
    }
503
    echo '</div>
504
        </div>
505
    ';
506
}
507
508
function form_general($label, $item) {
509
    echo '
510
        <div class="form-group">
511
    ';
512
    if (strlen($label)) {
513
        echo sprintf(
514
'           <label align=right class="%s">%s</label>
515
            <div class="%s">%s</div>
516
        ',
517
            FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS, $item
518
        );
519
    } else {
520
        echo sprintf(
521
'           <div class="%s %s">%s</div>
522
        ',
523
            FORM_LEFT_OFFSET, FORM_RIGHT_CLASS, $item
524
        );
525
    }
526
    echo '</div>
527
';
528
}
529
530
function form_submit($text, $attrs='') {
531
    form_general(
532
        "",
533
        sprintf(
534
            '<button %s type="submit" class="btn" %s>%s</button>',
535
            $attrs, button_style(), $text
536
        )
537
    );
538
}
539
540
function form_checkbox($label, $name, $checked=false) {
541
    echo sprintf('
542
        <div class="form-group">
543
            <label align=right class="%s">%s</label>
544
            <div class="%s">
545
        ',
546
        FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS
547
    );
548
    echo sprintf('
549
              <input type="checkbox" name="%s" %s>
550
           </div>
551
        </div>
552
        ', $name, $checked?"checked":""
553
    );
554
}
555
556
// 'select2' is replacement for multiple select with a nicer UI:
557
// https://github.com/select2/select2
558
//
559
// To use it you must use this version of page_head():
560
561
function page_head_select2($title) {
562
    $head_extra = '
563
        <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
564
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
565
    ';
566
    page_head($title, null, false, '', $head_extra);
567
    echo "<script>
568
        $(document).ready(function() {
569
            $('.js-example-basic-multiple').select2({placeholder: 'click to select'});
570
        });
571
        </script>
572
    ";
573
}
574
575
// show a multi-select using select2.
576
// $items is a list of [value, name] pairs;
577
// $selected is the list of selected values.
578
// $extra is e.g. id=foo
579
//
580
function form_select2_multi($label, $name, $items, $selected=null, $extra='') {
581
    echo sprintf('
582
        <div class="form-group">
583
            <label align=right class="%s" for="%s">%s</label>
584
            <div class="%s">
585
                <select class="js-example-basic-multiple" name="%s[]" multiple="multiple" style="width: 100%%" %s>
586
        ',
587
        FORM_LEFT_CLASS, $name, $label, FORM_RIGHT_CLASS, $name, $extra
588
    );
589
    foreach ($items as $i) {
590
        echo sprintf(
591
            '<option %s value=%s>%s</option>',
592
            ($selected && in_array($i[0], $selected))?'selected':'',
593
            $i[0], $i[1]
594
        );
595
    }
596
    echo "</select></div></div>\n";
597
}
598
599
?>
600