Passed
Pull Request — master (#4690)
by David
08:22
created

form_input_text_field()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 5
dl 0
loc 6
rs 10
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 and grids.
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(), USER_HOME, $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.USER_HOME);
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
            }
189
        }
190
        navbar_menu(tra("Computing"), $x);
191
    }
192
193
    navbar_menu(tra("Community"), array(
194
        array(tra("Message boards"), $url_prefix."forum_index.php"),
195
        //array(tra("Questions and Answers"), $url_prefix."forum_help_desk.php"),
196
        array(tra("Teams"), $url_prefix."team.php", tra("create or join a team")),
197
        array(tra("Profiles"), $url_prefix."profile_menu.php"),
198
        array(tra("Preferences"), $url_prefix."edit_forum_preferences_form.php"),
199
        array(tra("User search"), $url_prefix."user_search.php"),
200
        array(tra("User of the day"), $url_prefix."uotd.php"),
201
        array(tra("Certificate"), $url_prefix.cert_filename(), "", "_blank"),
202
    ));
203
    navbar_menu(tra("Site"), array(
204
        array(tra("Site search"), $url_prefix."site_search.php"),
205
        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...
206
    ));
207
208
    // add your own menu here if you want
209
210
    navbar_right($user);
211
    navbar_end();
212
}
213
214
// output a panel.
215
// $content_func is a function that generates the panel contents
216
//
217
function panel($title, $content_func, $class="panel-primary", $body_class="") {
218
    echo sprintf('<div class="panel %s">
219
        ', $class
220
    );
221
    if ($title) {
222
        echo '
223
            <div class="panel-heading">
224
                <h1 class="panel-title">'.$title.'</h1>
225
            </div>
226
        ';
227
    }
228
    echo sprintf('<div class="panel-body %s">
229
        ', $body_class
230
    );
231
    $content_func();
232
    echo '
233
        </div>
234
        </div>
235
    ';
236
}
237
238
// grid layout with a full-width row followed by two equal columns
239
// $top_func, $left_func, and $right_func
240
// are functions that generate the top, left, and right content
241
// $left_width is the width of left column in 1/12 units.
242
//
243
function grid($top_func, $left_func, $right_func, $left_width=6) {
244
    echo '
245
        <div class="container-fluid">
246
    ';
247
    if ($top_func) {
248
        echo '
249
            <div class="row">
250
            <div class="col-sm-12">
251
        ';
252
        $top_func();
253
        echo '
254
            </div>
255
            </div>
256
        ';
257
    }
258
    $right_width = 12-$left_width;
259
    echo '
260
        <div class="row">
261
        <div class="col-sm-'.$left_width.'">
262
    ';
263
    $left_func();
264
    echo '
265
        </div>
266
        <div class="col-sm-'.$right_width.'">
267
    ';
268
    $right_func();
269
    echo '
270
        </div>
271
        </div>
272
        </div>
273
    ';
274
}
275
276
// to upload files:
277
//  use method = POST and extra=ENCTYPE="multipart/form-data"
278
// to have initial focus on input field foo:
279
//      use extra = "name=x"
280
//      call forum_focus(x, foo) after defining the field
281
//
282
function form_start($action, $method='get', $extra='') {
283
    echo sprintf(
284
        '<div class="container-fluid">
285
        <form class="form-horizontal" method="%s" action="%s" %s>'
286
        ,
287
        $method, $action, $extra
288
    );
289
}
290
291
function form_input_hidden($name, $value) {
292
    echo '<input type="hidden" name="'.$name.'" value="'.$value.'">
293
    ';
294
}
295
296
function form_focus($form_name, $field_name) {
297
    echo "<script>document.$form_name.$field_name.focus()</script>\n";
298
299
}
300
301
function form_end() {
302
    echo '</form>
303
        </div>
304
    ';
305
}
306
307
define('FORM_LEFT_CLASS', 'col-sm-3');
308
define('FORM_LEFT_OFFSET', 'col-sm-offset-3');
309
define('FORM_RIGHT_CLASS', 'col-sm-9');
310
311
// just the input field
312
//
313
function form_input_text_field(
314
    $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...
315
) {
316
    return sprintf(
317
        '<input %s type="%s" class="form-control" name="%s" value="%s">%s',
318
        $attrs, $type, $name, $value, $extra
319
    );
320
}
321
322
// the whole row
323
//
324
function form_input_text(
325
    $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...
326
) {
327
    echo sprintf('
328
        <div class="form-group">
329
            <label align=right class="%s">%s</label>
330
            <div class="%s">
331
                %s
332
            </div>
333
        </div>
334
        ',
335
        FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS,
336
        form_input_text_field($name, $value, $type, $attrs, $extra)
337
    );
338
}
339
340
// display name/value with same formatting as form
341
//
342
function form_attr($name, $value) {
343
    echo sprintf('
344
        <div class="form-group">
345
            <div class="%s text-right">%s</div>
346
            <div class="%s">%s</div>
347
        </div>
348
        ',
349
        FORM_LEFT_CLASS, $name, FORM_RIGHT_CLASS, $value
350
    );
351
}
352
353
function form_input_textarea($label, $name, $value='', $nrows=4) {
354
    echo sprintf('
355
        <div class="form-group">
356
            <label align=right class="%s" for="%s">%s</label>
357
            <div class="%s">
358
                <textarea rows="%d" class="form-control" id="%s" name="%s">%s</textarea>
359
            </div>
360
        </div>
361
        ',
362
        FORM_LEFT_CLASS, $name, $label, FORM_RIGHT_CLASS,
363
        $nrows, $name, $name, $value
364
    );
365
}
366
367
// $items is either a string of <option> elements, or an array
368
//
369
function form_select($label, $name, $items) {
370
    echo sprintf('
371
        <div class="form-group">
372
            <label align=right class="%s" for="%s">%s</label>
373
            <div class="%s">
374
                <select class="form-control" id="%s" name="%s">
375
        ',
376
        FORM_LEFT_CLASS, $name, $label, FORM_RIGHT_CLASS, $name, $name
377
    );
378
    if (is_array($items)) {
379
        foreach ($items as $i) {
380
            echo '<option value="'.$i[0].'">'.$i[1].'</option>
381
            ';
382
        }
383
    } else {
384
        echo $items;
385
    }
386
    echo "</select></div></div>\n";
387
}
388
389
// same, for multiple select.
390
// flags, if non-null, says which ones are selected
391
//
392
function form_select_multiple($label, $name, $items, $flags) {
393
    echo sprintf('
394
        <div class="form-group">
395
            <label align=right class="%s" for="%s">%s</label>
396
            <div class="%s">
397
                <select multiple class="form-control" id="%s" name="%s[]">
398
        ',
399
        FORM_LEFT_CLASS, $name, $label, FORM_RIGHT_CLASS, $name, $name
400
    );
401
    $n = 0;
402
    foreach ($items as $i) {
403
        $s = ($flags && $flags[$n])?'selected':'';
404
        echo '<option '.$s.' value="'.$i[0].'">'.$i[1].'</option>
405
        ';
406
        $n++;
407
    }
408
    echo "</select></div></div>\n";
409
}
410
411
// return a list of string for checkbox items
412
//
413
function checkbox_item_strings($items, $attrs='') {
414
    $x = [];
415
    foreach ($items as $i) {
416
        $x[] = sprintf('<input %s type="checkbox" name="%s" %s> %s
417
            ',
418
            $attrs, $i[0], $i[2]?"checked":"", $i[1]
419
        );
420
    }
421
    return $x;
422
}
423
424
// $items is list of (name, label, checked)
425
//
426
function form_checkboxes($label, $items, $attrs='') {
427
    echo sprintf('
428
        <div class="form-group">
429
            <label align=right class="%s">%s</label>
430
            <div class="%s">
431
        ',
432
        FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS
433
    );
434
    $x = checkbox_item_strings($items, $attrs);
435
    echo implode($x, '<br>');
0 ignored issues
show
Unused Code introduced by
The call to implode() has too many arguments starting with '<br>'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

435
    echo /** @scrutinizer ignore-call */ implode($x, '<br>');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
436
    echo '</div>
437
        </div>
438
    ';
439
}
440
441
// $items is list of (value, label)
442
//
443
function form_radio_buttons($label, $name, $items, $selected) {
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
    foreach ($items as $i) {
452
        $checked = ($selected == $i[0])?"checked":"";
453
        echo sprintf('<input type="radio" name="%s" value="%s" %s> %s <br>
454
            ',
455
            $name, $i[0], $checked, $i[1]
456
        );
457
    }
458
    echo '</div>
459
        </div>
460
    ';
461
}
462
463
function form_general($label, $item) {
464
    echo '
465
        <div class="form-group">
466
    ';
467
    if (strlen($label)) {
468
        echo sprintf(
469
'           <label align=right class="%s">%s</label>
470
            <div class="%s">%s</div>
471
        ',
472
            FORM_LEFT_CLASS, $label, FORM_RIGHT_CLASS, $item
473
        );
474
    } else {
475
        echo sprintf(
476
'           <div class="%s %s">%s</div>
477
        ',
478
            FORM_LEFT_OFFSET, FORM_RIGHT_CLASS, $item
479
        );
480
    }
481
    echo '</div>
482
';
483
}
484
485
function form_submit($text, $attrs='') {
486
    form_general(
487
        "",
488
        sprintf('<button %s type="submit" class="btn btn-success">%s</button>',
489
            $attrs, $text
490
        )
491
    );
492
}
493
494
function form_checkbox($label, $name, $checked=false) {
495
    echo sprintf('
496
        <div class="form-group">
497
            <input type="checkbox" name="%s" %s> &nbsp; <span class="lead">%s</span>
498
        </div>
499
        ', $name, $checked?"checked":"", $label
500
    );
501
}
502