Passed
Push — dpa_web21 ( c18d53 )
by David
09:16
created

form_select2_multi()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 8
nc 2
nop 5
dl 0
loc 17
rs 10
c 1
b 1
f 0
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
Coding Style introduced by
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
Coding Style introduced by
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
Coding Style introduced by
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
Coding Style introduced by
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
Coding Style introduced by
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
Coding Style introduced by
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
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 4 spaces but found 8
Loading history...
151
) {
152
    global $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
        if (defined('REMOTE_JOB_SUBMISSION') && REMOTE_JOB_SUBMISSION) {
186
            if ($user && BoincUserSubmit::lookup_userid($user->id)) {
187
                $x[] = array("Job submission", $url_prefix."submit.php");
188
                $x[] = ["File sandbox", "sandbox.php"];
189
            }
190
        }
191
        navbar_menu(tra("Computing"), $x);
192
    }
193
194
    navbar_menu(tra("Community"), array(
195
        array(tra("Message boards"), $url_prefix."forum_index.php"),
196
        //array(tra("Questions and Answers"), $url_prefix."forum_help_desk.php"),
197
        array(tra("Teams"), $url_prefix."team.php", tra("create or join a team")),
198
        array(tra("Profiles"), $url_prefix."profile_menu.php"),
199
        array(tra("Preferences"), $url_prefix."edit_forum_preferences_form.php"),
200
        array(tra("User search"), $url_prefix."user_search.php"),
201
        array(tra("User of the day"), $url_prefix."uotd.php"),
202
        array(tra("Certificate"), $url_prefix.cert_filename(), "", "_blank"),
203
    ));
204
    navbar_menu(tra("Site"), array(
205
        array(tra("Site search"), $url_prefix."site_search.php"),
206
        array(tra("Languages"), $url_prefix."language_select.php")
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
207
    ));
208
209
    // add your own menu here if you want
210
211
    navbar_right($user);
212
    navbar_end();
213
}
214
215
// output a panel.
216
// $content_func is a function that generates the panel contents
217
//
218
function panel($title, $content_func, $class="panel-primary", $body_class="") {
219
    echo sprintf('<div class="panel %s">
220
        ', $class
221
    );
222
    if ($title) {
223
        echo '
224
            <div class="panel-heading">
225
                <h1 class="panel-title">'.$title.'</h1>
226
            </div>
227
        ';
228
    }
229
    echo sprintf('<div class="panel-body %s">
230
        ', $body_class
231
    );
232
    $content_func();
233
    echo '
234
        </div>
235
        </div>
236
    ';
237
}
238
239
// grid layout with a full-width row followed by two columns.
240
// $top_func, $left_func, and $right_func
241
// are functions that generate the top, left, and right content.
242
// $left_width is the width of left column in 1/12 units.
243
// $arg is passed to the functions.
244
//
245
function grid($top_func, $left_func, $right_func, $left_width=6, $arg=null) {
246
    echo '
247
        <div class="container-fluid">
248
    ';
249
    if ($top_func) {
250
        echo '
251
            <div class="row">
252
            <div class="col-sm-12">
253
        ';
254
        $top_func($arg);
255
        echo '
256
            </div>
257
            </div>
258
        ';
259
    }
260
    $right_width = 12-$left_width;
261
    echo '
262
        <div class="row">
263
        <div class="col-sm-'.$left_width.'">
264
    ';
265
    $left_func($arg);
266
    echo '
267
        </div>
268
        <div class="col-sm-'.$right_width.'">
269
    ';
270
    $right_func($arg);
271
    echo '
272
        </div>
273
        </div>
274
        </div>
275
    ';
276
}
277
278
// to upload files:
279
//  use method = POST and extra=ENCTYPE="multipart/form-data"
280
// to have initial focus on input field foo:
281
//      use extra = "name=x"
282
//      call forum_focus(x, foo) after defining the field
283
//
284
function form_start($action, $method='get', $extra='') {
285
    echo sprintf(
286
        '<div class="container-fluid">
287
        <form class="form-horizontal" method="%s" action="%s" %s>'
288
        ,
289
        $method, $action, $extra
290
    );
291
}
292
293
function form_input_hidden($name, $value) {
294
    echo '<input type="hidden" name="'.$name.'" value="'.$value.'">
295
    ';
296
}
297
298
function form_focus($form_name, $field_name) {
299
    echo "<script>document.$form_name.$field_name.focus()</script>\n";
300
301
}
302
303
function form_end() {
304
    echo '</form>
305
        </div>
306
    ';
307
}
308
309
define('FORM_LEFT_CLASS', 'col-sm-3');
310
define('FORM_LEFT_OFFSET', 'col-sm-offset-3');
311
define('FORM_RIGHT_CLASS', 'col-sm-9');
312
313
// just the input field
314
//
315
function form_input_text_field(
316
    $name, $value='', $type='text', $attrs='', $extra=''
0 ignored issues
show
Coding Style introduced by
Multi-line function declarations must define one parameter per line
Loading history...
317
) {
318
    return sprintf(
319
        '<input %s type="%s" class="form-control" name="%s" value="%s">%s',
320
        $attrs, $type, $name, $value, $extra
321
    );
322
}
323
324
// the whole row
325
//
326
function form_input_text(
327
    $label, $name, $value='', $type='text', $attrs='', $extra=''
0 ignored issues
show
Coding Style introduced by
Multi-line function declarations must define one parameter per line
Loading history...
328
) {
329
    echo sprintf('
330
        <div class="form-group">
331
            <label align=right class="%s">%s</label>
332
            <div class="%s">
333
                %s
334
            </div>
335
        </div>
336
        ',
337
        FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS,
338
        form_input_text_field($name, $value, $type, $attrs, $extra)
339
    );
340
}
341
342
// display name/value with same formatting as form
343
//
344
function form_attr($name, $value) {
345
    echo sprintf('
346
        <div class="form-group">
347
            <div class="%s text-right">%s</div>
348
            <div class="%s">%s</div>
349
        </div>
350
        ',
351
        FORM_LEFT_CLASS, $name, FORM_RIGHT_CLASS, $value
352
    );
353
}
354
355
function form_input_textarea($label, $name, $value='', $nrows=4) {
356
    echo sprintf('
357
        <div class="form-group">
358
            <label align=right class="%s" for="%s">%s</label>
359
            <div class="%s">
360
                <textarea rows="%d" class="form-control" id="%s" name="%s">%s</textarea>
361
            </div>
362
        </div>
363
        ',
364
        FORM_LEFT_CLASS, $name, $label, FORM_RIGHT_CLASS,
365
        $nrows, $name, $name, $value
366
    );
367
}
368
369
// $items is either a string of <option> elements, or an array
370
//
371
function form_select($label, $name, $items, $selected=null) {
372
    echo sprintf('
373
        <div class="form-group">
374
            <label align=right class="%s" for="%s">%s</label>
375
            <div class="%s">
376
                <select class="form-control" id="%s" name="%s">
377
        ',
378
        FORM_LEFT_CLASS, $name, $label, FORM_RIGHT_CLASS, $name, $name
379
    );
380
    if (is_array($items)) {
381
        foreach ($items as $i) {
382
            echo sprintf(
383
                '<option %s value=%s>%s</option>',
384
                ($i[0]==$selected)?'selected':'',
385
                $i[0], $i[1]
386
            );
387
        }
388
    } else {
389
        echo $items;
390
    }
391
    echo "</select></div></div>\n";
392
}
393
394
// same, for multiple select.
395
// $selected, if non-null, is a list of selected values
396
//
397
function form_select_multiple($label, $name, $items, $selected=null, $size=0) {
398
    echo sprintf('
399
        <div class="form-group">
400
            <label align=right class="%s" for="%s">%s</label>
401
            <div class="%s">
402
                <select multiple class="form-control" id="%s" name="%s[]" size=%d>
403
        ',
404
        FORM_LEFT_CLASS, $name, $label, FORM_RIGHT_CLASS, $name, $name, $size
405
    );
406
    foreach ($items as $i) {
407
        echo sprintf(
408
            '<option %s value=%s>%s</option>',
409
            ($selected && in_array($i[0], $selected))?'selected':'',
410
            $i[0], $i[1]
411
        );
412
    }
413
    echo "</select></div></div>\n";
414
}
415
416
// return a list of strings for checkbox items
417
//
418
function checkbox_item_strings($items, $attrs='') {
419
    $x = [];
420
    foreach ($items as $i) {
421
        $x[] = sprintf('<input %s type="checkbox" name="%s" %s> %s
422
            ',
423
            $attrs, $i[0], $i[2]?"checked":"", $i[1]
424
        );
425
    }
426
    return $x;
427
}
428
429
// $items is list of (name, label, checked)
430
//
431
function form_checkboxes($label, $items, $attrs='') {
432
    echo sprintf('
433
        <div class="form-group">
434
            <label align=right class="%s">%s</label>
435
            <div class="%s">
436
        ',
437
        FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS
438
    );
439
    $x = checkbox_item_strings($items, $attrs);
440
    echo implode('<br>', $x);
441
    echo '</div>
442
        </div>
443
    ';
444
}
445
446
// $items is list of [value, label]
447
//
448
function form_radio_buttons(
449
    $label, $name, $items, $selected,
0 ignored issues
show
Coding Style introduced by
Multi-line function declarations must define one parameter per line
Loading history...
450
    $assign_ids=false       // assign IDs to buttons based on names
451
) {
452
    echo sprintf('
453
        <div class="form-group">
454
            <label align=right class="%s">%s</label>
455
            <div class="%s">
456
        ',
457
        FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS
458
    );
459
    foreach ($items as $i) {
460
        $checked = ($selected == $i[0])?"checked":"";
461
        if ($assign_ids) {
462
            $id = sprintf('id="%s_%s"', $name, $i[0]);
463
        } else {
464
            $id = '';
465
        }
466
        echo sprintf('<input type="radio" name="%s" value="%s" %s %s> %s <br>
467
            ',
468
            $name, $i[0], $checked, $id, $i[1]
469
        );
470
    }
471
    echo '</div>
472
        </div>
473
    ';
474
}
475
476
function form_general($label, $item) {
477
    echo '
478
        <div class="form-group">
479
    ';
480
    if (strlen($label)) {
481
        echo sprintf(
482
'           <label align=right class="%s">%s</label>
483
            <div class="%s">%s</div>
484
        ',
485
            FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS, $item
486
        );
487
    } else {
488
        echo sprintf(
489
'           <div class="%s %s">%s</div>
490
        ',
491
            FORM_LEFT_OFFSET, FORM_RIGHT_CLASS, $item
492
        );
493
    }
494
    echo '</div>
495
';
496
}
497
498
function form_submit($text, $attrs='') {
499
    form_general(
500
        "",
501
        sprintf(
502
            '<button %s type="submit" class="btn" %s>%s</button>',
503
            $attrs, button_style(), $text
504
        )
505
    );
506
}
507
508
function form_checkbox($label, $name, $checked=false) {
509
    echo sprintf('
510
        <div class="form-group">
511
            <label align=right class="%s">%s</label>
512
            <div class="%s">
513
        ',
514
        FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS
515
    );
516
    echo sprintf('
517
        <input type="checkbox" name="%s" %s>
518
        </div>
519
        ', $name, $checked?"checked":""
520
    );
521
}
522
523
// 'select2' is replacement for select boxed:
524
// https://github.com/select2/select2
525
//
526
// To use it you must use this version of page_head():
527
528
function page_head_select2($title) {
529
    $head_extra = '
530
        <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
531
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
532
    ';
533
    page_head($title, null, false, '', $head_extra);
534
    echo "<script>
535
        $(document).ready(function() {
536
            $('.js-example-basic-multiple').select2();
537
        });
538
        </script>
539
    ";
540
}
541
542
// show a multi-select using select2.
543
// $items is a list of [val, label] pairs;
544
// $selected is the list of selected values.
545
// $extra is e.g. id=foo
546
//
547
function form_select2_multi($label, $name, $items, $selected=null, $extra='') {
548
    echo sprintf('
549
        <div class="form-group">
550
            <label align=right class="%s" for="%s">%s</label>
551
            <div class="%s">
552
                <select class="js-example-basic-multiple" name="%s[]" multiple="multiple" style="width: 100%%" %s>
553
        ',
554
        FORM_LEFT_CLASS, $name, $label, FORM_RIGHT_CLASS, $name, $extra
555
    );
556
    foreach ($items as $i) {
557
        echo sprintf(
558
            '<option %s value=%s>%s</option>',
559
            ($selected && in_array($i[0], $selected))?'selected':'',
560
            $i[0], $i[1]
561
        );
562
    }
563
    echo "</select></div></div>\n";
564
}
565
566
?>
567