Passed
Push — dpa_buda13 ( 0241f6 )
by David
10:52
created

sample_navbar()   B

Complexity

Conditions 8
Paths 18

Size

Total Lines 85
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 50
c 0
b 0
f 0
nc 18
nop 4
dl 0
loc 85
rs 7.8464

How to fix   Long Method   

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