Passed
Push — dpa_submit20 ( a65307 )
by David
11:07
created

sample_navbar()   C

Complexity

Conditions 11
Paths 42

Size

Total Lines 95
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 57
c 0
b 0
f 0
nc 42
nop 4
dl 0
loc 95
rs 6.7915

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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