PREF_OPT_NUM::parse_form()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
// This file is part of BOINC.
4
// http://boinc.berkeley.edu
5
// Copyright (C) 2010 University of California
6
//
7
// BOINC is free software; you can redistribute it and/or modify it
8
// under the terms of the GNU Lesser General Public License
9
// as published by the Free Software Foundation,
10
// either version 3 of the License, or (at your option) any later version.
11
//
12
// BOINC is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
// See the GNU Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public License
18
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
19
20
// classes for different kinds of global preferences.  See prefs.inc
21
// PREF_BOOL: boolean
22
// PREF_NUM: a number, possibly constrained to a range
23
// PREF_NUM2: a pair of numbers (e.g. transfer limit)
24
// PREF_HOUR_RANGE: a range of hours
25
26
require_once("../inc/consent.inc");
27
28
$venues = array("home", "school", "work");
29
30
function check_venue($x) {
31
    if ($x == "") return;
32
    if ($x == "home") return;
33
    if ($x == "work") return;
34
    if ($x == "school") return;
35
    error_page("bad venue");
36
}
37
38
function check_subset($x) {
39
    if ($x == "global") return;
40
    if ($x == "project") return;
41
    error_page("bad subset");
42
}
43
44
abstract class PREF {
45
    public $desc;       // short description
46
    public $tooltip;    // longer description, shown as tooltip
47
    public $tag;        // the pref's primary XML tag
48
    function __construct($desc, $tooltip, $tag) {
49
        $this->desc = $desc;
50
        $this->tooltip = $tooltip;
51
        $this->tag = $tag;
52
    }
53
54
    abstract function show_value($prefs);
55
    abstract function show_form($prefs, $error);
56
    abstract function parse_form(&$prefs, &$error);
57
    abstract function xml_string($prefs);
58
    abstract function xml_parse(&$prefs, $name, $text);
59
    abstract function set_default(&$prefs);
60
61
    function tooltip_tr() {
62
        if ($this->tooltip) {
63
            echo "<tr title=\"$this->tooltip\">";
64
        } else {
65
            echo "<tr>";
66
        }
67
    }
68
69
    // multi-column display (read only)
70
    //
71
    function show_cols($prefs) {
72
        global $venues;
73
        $this->tooltip_tr();
74
        echo "<td class=\"text-right \">$this->desc</td>";
75
        $tag = $this->tag;
76
        if (isset($prefs->$tag)) {
77
            $this->show_value($prefs);
78
        } else {
79
            echo "<td>---</td>";
80
        }
81
        foreach ($venues as $v) {
82
            if (isset($prefs->$v) && isset($prefs->$v->$tag)) {
83
                $this->show_value($prefs->$v);
84
            } else {
85
                echo "<td>---</td>";
86
            }
87
        }
88
        echo "</tr>\n";
89
    }
90
91
    // show read-only row
92
    //
93
    function show($prefs) {
94
        $this->tooltip_tr();
95
        echo "<td class=\"text-right \">$this->desc</td>";
96
        $tag = $this->tag;
97
        if (isset($prefs->$tag)) {
98
            $this->show_value($prefs);
99
        } else {
100
            echo "<td>---</td>";
101
        }
102
        echo "</tr>\n";
103
    }
104
105
    // show form row
106
    //
107
    function show_form_row($prefs, $error) {
108
        $this->tooltip_tr();
109
        echo "<td ".NAME_ATTRS.">$this->desc</td>";
110
        $this->show_form($prefs, $error);
111
        echo "</tr>\n";
112
    }
113
}
114
115
function readonly_checkbox($checked) {
116
    if ($checked) {
117
        return "<input type=checkbox onclick=\"return false\" checked>";
118
    } else {
119
        return "<input type=checkbox onclick=\"return false\">";
120
    }
121
}
122
123
class PREF_BOOL extends PREF {
124
    public $default;
125
    public $invert;     // show to user in opposite sense
126
    function __construct($desc, $tooltip, $tag, $default, $invert=false) {
127
        $this->default = $default;
128
        $this->invert = $invert;
129
        parent::__construct($desc, $tooltip, $tag);
130
    }
131
    function show_value($prefs) {
132
        $tag = $this->tag;
133
        $v = $this->invert?!$prefs->$tag:$prefs->$tag;
134
        echo "<td>".readonly_checkbox($v)."</td>";
135
    }
136
    function show_form($prefs, $error) {
137
        $tag = $this->tag;
138
        if ($this->invert) {
139
            $checked = !$prefs->$tag;
140
        } else {
141
            $checked = $prefs->$tag;
142
        }
143
        echo "<td ".VALUE_ATTRS.">"
144
            ."<input type=checkbox name=$this->tag "
145
            . ($checked?"checked":"")
146
            ."></td>
147
        ";
148
    }
149
    function parse_form(&$prefs, &$error) {
150
        $tag = $this->tag;
151
        $val = array_key_exists($tag, $_GET);
152
        if ($this->invert) $val = !$val;
153
        $prefs->$tag = $val;
154
    }
155
    function xml_string($prefs) {
156
        $tag = $this->tag;
157
        return "<$tag>"
158
            .($prefs->$tag?"1":"0")
159
            ."</$tag>\n";
160
    }
161
    function xml_parse(&$prefs, $name, $text) {
162
        $tag = $this->tag;
163
        if ($name != $tag) return false;
164
        $val = (trim($text) != '0');
165
        $prefs->$tag = $val;
166
        return true;
167
    }
168
    function set_default(&$prefs) {
169
        $tag = $this->tag;
170
        $prefs->$tag = $this->default;
171
    }
172
}
173
174
class PREF_CONSENT extends PREF {
175
    public $consent_type_id; // the consent_type_id from the consent_type table
176
    public $consent_name; // the consent_name to configure
177
    public $default;
178
    public $invert;     // show to user in opposite sense
179
180
    function __construct($desc, $tooltip, $tag, $consent_type_id, $consent_name, $default, $invert=false) {
181
        $this->consent_type_id = $consent_type_id;
182
        $this->consent_name = $consent_name;
183
        $this->default = $default;
184
        $this->invert = $invert;
185
        parent::__construct($desc, $tooltip, $tag);
186
    }
187
188
    // multi-column display (read only)
189
    //
190
    function consent_show_cols($user) {
191
        global $venues;
192
        $this->tooltip_tr();
193
        echo "<td class=\"text-right \">$this->desc</td>";
194
        $consent_type_id = $this->consent_type_id;
195
        $cr = BoincLatestConsent::lookup(
196
            "userid=$user->id AND consent_type_id='$consent_type_id'"
197
        );
198
        if ($cr) {
199
            $this->show_value($user);
200
        } else {
201
            echo "<td>---</td>";
202
        }
203
        foreach ($venues as $v) {
204
            echo "<td>--</td>";
205
        }
206
        echo "</tr>\n";
207
    }
208
209
    // show read-only row
210
    //
211
    function consent_show($user) {
212
        $this->tooltip_tr();
213
        echo "<td class=\"text-right \">$this->desc</td>";
214
        $consent_type_id = $this->consent_type_id;
215
        $cr = BoincLatestConsent::lookup(
216
            "userid=$user->id AND consent_type_id='$consent_type_id'"
217
        );
218
        if ($cr) {
219
            $this->show_value($user);
220
        } else {
221
            echo "<td>---</td>";
222
        }
223
        echo "</tr>\n";
224
    }
225
226
    function consent_show_form_row($user, $error) {
227
        $this->tooltip_tr();
228
        echo "<td ".NAME_ATTRS.">$this->desc</td>";
229
        $this->show_form($user, $error);
230
        echo "</tr>\n";
231
    }
232
233
    function show_value($user) {
234
        $consent_type_id = $this->consent_type_id;
235
        $cr = BoincLatestConsent::lookup(
236
            "userid=$user->id AND consent_type_id='$consent_type_id'"
237
        );
238
        if ($cr) {
239
            $value = $cr->consent_flag;
240
        } else {
241
            $value = false;
242
        }
243
        echo "<td>" . readonly_checkbox($value) . "</td>";
244
    }
245
    function show_form($user, $error) {
246
        $consent_type_id = $this->consent_type_id;
247
        $cr = BoincLatestConsent::lookup(
248
            "userid=$user->id AND consent_type_id='$consent_type_id'");
249
        if ($cr) {
250
            if ($this->invert) {
251
                $checked = !$cr->consent_flag;
252
            } else {
253
                $checked = $cr->consent_flag;
254
            }
255
        } else {
256
            $checked = ($this->invert ? true : false);
257
        }
258
        echo "<td ".VALUE_ATTRS.">"
259
            ."<input type=checkbox name=$this->tag "
260
            . ($checked?"checked":"")
261
            ."></td>
262
        ";
263
    }
264
    function parse_form(&$user, &$error) {
265
        // This function parses the form AND performs the database update
266
        $tag = $this->tag;
267
        $consent_type_id = $this->consent_type_id;
268
        $formget = array_key_exists($tag, $_GET);
269
        if ($this->invert) $formget = !$formget;
270
        $flag = ($formget ? 1 : 0);
271
272
        // Check to see if latest consent of this name is already
273
        // given, i.e., consent_flag set to "formget". If not, consent
274
        // to this consent type.
275
        $cr = BoincLatestConsent::lookup(
276
            "userid=$user->id AND consent_type_id='$consent_type_id$'"
277
        );
278
279
        if ((($cr) and ($cr->consent_flag!=$flag)) or (!$cr)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
280
            $rc = consent_to_a_policy(
281
                $user, $consent_type_id, $flag, 0, 'Webform', time()
282
            );
283
            if (!$rc) {
284
                error_page("Database error");
285
            }
286
        }
287
    }
288
    // xml_string should not be used for this class
289
    function xml_string($prefs) {
290
        return "";
291
    }
292
    // xml_parse should not be used for this class
293
    function xml_parse(&$prefs, $name, $text) {
294
        return false;
295
    }
296
    function set_default(&$user) {
297
        $consent_type_id = $this->consent_type_id;
298
299
        $rc = consent_to_a_policy(
300
            $user, $consent_type_id, $this->default, 0, 'Webform'
301
        );
302
        if (!$rc) {
303
            error_page("Database error");
304
        }
305
    }
306
307
}
308
309
class NUM_SPEC {
310
    public $suffix;
311
    public $min;
312
    public $max;
313
    public $default;
314
    public $default2;
315
        // for optional prefs where the default is zero (ignored)
316
        // this is the value if they check the box
317
    public $scale;
318
319
    function __construct($suffix, $min, $max, $default, $scale=1, $default2=0) {
320
        $this->suffix = " $suffix";
321
        $this->min = $min;
322
        $this->max = $max;
323
        $this->default = $default;
324
        $this->default2 = $default2;
325
        $this->scale = $scale;
326
    }
327
    function value_str($v) {
328
        $v /= $this->scale;
329
        if ($v < $this->min || $v > $this->max) {
330
            $v = $this->default;
331
        }
332
        if ($v == 0) {
333
            $v = "--- ";
334
        }
335
        $v .= "$this->suffix ";
336
        return $v;
337
    }
338
    function form_str($tag, $v, $had_error, $disabled=false, $id=null) {
339
        if (is_numeric($v)) {
340
            $v /= $this->scale;
341
            if (!$had_error && ($v < $this->min || $v > $this->max)) {
342
                $v = $this->default;
343
            }
344
        }
345
        if ($disabled) $v = "";
346
        $i = $id?"id=\"$id\"":"";
347
        return '<input type="text" size="5" class="form-control input-sm" name="'.$tag.'" value="'.$v."\" $disabled $i> &nbsp; $this->suffix ";
348
    }
349
    function form_convert($in, &$out, &$error) {
350
        $error = false;
351
        if ($in == "") $in = 0;
352
        if (!is_numeric($in)) {
353
            $error = true;
354
            $out = $in;
355
            return;
356
        }
357
        $out = $in*$this->scale;
358
        if ($out < $this->min || $out > $this->max) {
359
            $error = true;
360
        }
361
    }
362
    function get_default() {
363
        if ($this->default) return $this->default;
364
        return $this->default2;
365
    }
366
}
367
368
// a numeric item
369
//
370
class PREF_NUM extends PREF {
371
    public $num_spec;
372
    function __construct($desc, $tooltip, $tag, $num_spec) {
373
        $this->num_spec = $num_spec;
374
        parent::__construct($desc, $tooltip, $tag);
375
    }
376
    function show_value($prefs) {
377
        $tag = $this->tag;
378
        echo "<td>".$this->num_spec->value_str($prefs->$tag)."</td>";
379
    }
380
    function show_form($prefs, $error) {
381
        $tag = $this->tag;
382
        $had_error = isset($error->$tag);
383
        $attrs = $had_error ?VALUE_ATTRS_ERR:VALUE_ATTRS;
384
        echo "<td $attrs>"
385
            .$this->num_spec->form_str($tag, $prefs->$tag, $had_error)
386
            ."</td>
387
        ";
388
    }
389
    function parse_form(&$prefs, &$error) {
390
        $tag = $this->tag;
391
        $this->num_spec->form_convert(get_str($tag, true), $prefs->$tag, $e);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e seems to be never defined.
Loading history...
392
        if ($e) $error->$tag = true;
393
    }
394
    function xml_string($prefs) {
395
        $tag = $this->tag;
396
        $v = $prefs->$tag;
397
        if (!$v) $v = 0;
398
        return "<$tag>$v</$tag>\n";
399
    }
400
    function xml_parse(&$prefs, $name, $text) {
401
        $tag = $this->tag;
402
        if ($name != $tag) return false;
403
        $prefs->$tag = $text;
404
        return true;
405
    }
406
    function set_default(&$prefs) {
407
        $tag = $this->tag;
408
        $prefs->$tag = $this->num_spec->default;
409
    }
410
}
411
412
function checkbox_clicked_js() {
413
    echo "
414
        <script type=text/javascript>
415
        function checkbox_clicked(id, tid, d) {
416
            e = document.getElementById(id);
417
            t = document.getElementById(tid);
418
            if (e.checked) {
419
                t.disabled = false;
420
                t.value = d;
421
            } else {
422
                t.disabled = true;
423
                t.value = '';
424
            }
425
        }
426
        function checkbox_clicked2(id, tid1, tid2, d1, d2) {
427
            e = document.getElementById(id);
428
            t1 = document.getElementById(tid1);
429
            t2 = document.getElementById(tid2);
430
            if (e.checked) {
431
                t1.disabled = false;
432
                t1.value = d1;
433
                t2.disabled = false;
434
                t2.value = d2;
435
            } else {
436
                t1.disabled = true;
437
                t1.value = '';
438
                t2.disabled = true;
439
                t2.value = '';
440
            }
441
        }
442
        </script>
443
    ";
444
}
445
446
// an optional numeric item where 0 means not specified
447
//
448
class PREF_OPT_NUM extends PREF {
449
    public $num_spec;
450
    function __construct($desc, $tooltip, $tag, $num_spec) {
451
        $this->num_spec = $num_spec;
452
        parent::__construct($desc, $tooltip, $tag);
453
    }
454
    function show_value($prefs) {
455
        $tag = $this->tag;
456
        $x = $prefs->$tag;
457
        echo "<td>";
458
        echo $this->num_spec->value_str($x);
459
        echo "</td>";
460
    }
461
    function show_form($prefs, $error) {
462
        $tag = $this->tag;
463
        $had_error = isset($error->$tag);
464
        $attrs = $had_error ?VALUE_ATTRS_ERR:VALUE_ATTRS;
465
        $checkbox_id = $this->tag."_cb";
466
        $text_id = $this->tag;
467
        $default = $this->num_spec->get_default();
468
        $val = $prefs->$tag;
469
        $c = $val?"checked":"";
470
        $d = $val?"":"disabled";
471
        echo "<td $attrs>"
472
            ."<input type=checkbox id=$checkbox_id onClick=\"checkbox_clicked('$checkbox_id', '$text_id', $default)\" $c> &nbsp;"
473
            .$this->num_spec->form_str($tag, $prefs->$tag, $had_error, $d, $text_id)
474
            ."</td>
475
        ";
476
    }
477
    function parse_form(&$prefs, &$error) {
478
        $tag = $this->tag;
479
        $this->num_spec->form_convert(get_str($tag, true), $prefs->$tag, $e);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e seems to be never defined.
Loading history...
480
        if ($e) $error->$tag = true;
481
    }
482
    function xml_string($prefs) {
483
        $tag = $this->tag;
484
        $v = $prefs->$tag;
485
        if (!$v) $v = 0;
486
        return "<$tag>$v</$tag>\n";
487
    }
488
    function xml_parse(&$prefs, $name, $text) {
489
        $tag = $this->tag;
490
        if ($name != $tag) return false;
491
        $prefs->$tag = $text;
492
        return true;
493
    }
494
    function set_default(&$prefs) {
495
        $tag = $this->tag;
496
        $prefs->$tag = $this->num_spec->default;
497
    }
498
}
499
500
// optional pair of numbers
501
// for "max X MB in Y days"
502
//
503
class PREF_NUM2 extends PREF {
504
    public $tag2;
505
    public $num_spec1, $num_spec2;
506
    function __construct($desc, $tooltip, $tag1, $tag2, $num_spec1, $num_spec2) {
507
        $this->tag2 = $tag2;
508
        $this->num_spec1 = $num_spec1;
509
        $this->num_spec2 = $num_spec2;
510
        parent::__construct($desc, $tooltip, $tag1);
511
    }
512
    function show_value($prefs) {
513
        $tag = $this->tag;
514
        $tag2 = $this->tag2;
515
        $v1 = $prefs->$tag;
516
        $v2 = $prefs->$tag2;
517
        echo "<td>"
518
            .$this->num_spec1->value_str($v1)
519
            .$this->num_spec2->value_str($v2)
520
            ."</td>
521
        ";
522
    }
523
    function show_form($prefs, $error) {
524
        $tag = $this->tag;
525
        $tag2 = $this->tag2;
526
        $had_error = isset($error->$tag) || isset($error->$tag2);
527
        $attrs = $had_error ?VALUE_ATTRS_ERR:VALUE_ATTRS;
528
        $checkbox_id = $this->tag."_cb";
529
        $t1_id = $this->tag."_t1";
530
        $t2_id = $this->tag."_t2";
531
        $v1 = $prefs->$tag;
532
        $v2 = $prefs->$tag2;
533
        if ($v1 && $v2) {
534
            $c = "checked";
535
            $d = "";
536
        } else {
537
            $c = "";
538
            $d = "disabled";
539
        }
540
        $def1 = $this->num_spec1->get_default();
541
        $def2 = $this->num_spec2->get_default();
542
        echo "<td $attrs>"
543
            ."<input type=checkbox id=$checkbox_id onClick=\"checkbox_clicked2('$checkbox_id', '$t1_id', '$t2_id', $def1, $def2)\" $c> "
544
            .$this->num_spec1->form_str($tag, $prefs->$tag, $had_error, $d, $t1_id)
545
            .$this->num_spec2->form_str($tag2, $prefs->$tag2, $had_error, $d, $t2_id)
546
            ."</td>
547
        ";
548
    }
549
    function parse_form(&$prefs, &$error) {
550
        $tag = $this->tag;
551
        $tag2 = $this->tag2;
552
        $this->num_spec1->form_convert(get_str($tag, true), $prefs->$tag, $e);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e seems to be never defined.
Loading history...
553
        if ($e) $error->$tag = true;
554
        $this->num_spec2->form_convert(get_str($tag2, true), $prefs->$tag2, $e);
555
        if ($e) $error->$tag2 = $e;
556
    }
557
    function xml_string($prefs) {
558
        $tag = $this->tag;
559
        $tag2 = $this->tag2;
560
        return "<$tag>".$prefs->$tag."</$tag>\n<$tag2>".$prefs->$tag2."</$tag2>\n";
561
    }
562
    function xml_parse(&$prefs, $name, $text) {
563
        $tag = $this->tag;
564
        $tag2 = $this->tag2;
565
        if ($name == $tag) {
566
            $prefs->$tag = $text;
567
        } else if ($name == $tag2) {
568
            $prefs->$tag2 = $text;
569
        }
570
        return false;
571
    }
572
    function set_default(&$prefs) {
573
        $tag = $this->tag;
574
        $tag2 = $this->tag2;
575
        $prefs->$tag = $this->num_spec1->default;
576
        $prefs->$tag2 = $this->num_spec2->default;
577
    }
578
}
579
580
function hour_select($x, $name, $id, $d) {
581
    $s = "";
582
    $s .= "<select class=\"selectbox form-control input-sm\" name=$name id=$id $d>\n";
583
    for ($i=0; $i<24; $i++) {
584
        $sel = ($x == $i)?"selected":"";
585
        $s .= "<option value=$i $sel> $i:00";
586
    }
587
    $s .= "</select>\n";
588
    return $s;
589
}
590
591
// optional hour range
592
//
593
class PREF_HOUR_RANGE extends PREF {
594
    public $tag2;
595
    function __construct($desc, $tooltip, $tag, $tag2) {
596
        $this->tag2 = $tag2;
597
        parent::__construct($desc, $tooltip, $tag);
598
    }
599
    function show_value($prefs) {
600
        $tag = $this->tag;
601
        $tag2 = $this->tag2;
602
        $h1 = $prefs->$tag;
603
        $h2 = $prefs->$tag2;
604
        if ($h1 == $h2) {
605
            $v = "---";
606
        } else {
607
            $v = "$h1:00 ".tra("and")." $h2:00";
608
        }
609
        echo "<td>$v</td>";
610
    }
611
    function show_form($prefs, $error) {
612
        $tag = $this->tag;
613
        $tag2 = $this->tag2;
614
        $h1 = $prefs->$tag;
615
        $h2 = $prefs->$tag2;
616
        $checkbox_id = $this->tag."_cb";
617
        $t1_id = $this->tag."_t1";
618
        $t2_id = $this->tag."_t2";
619
        if ($h1 == $h2) {
620
            $c = "";
621
            $d = "disabled";
622
        } else {
623
            $c = "checked";
624
            $d = "";
625
        }
626
        echo "<td ".VALUE_ATTRS.">"
627
            ."<input type=checkbox id=$checkbox_id onClick=\"checkbox_clicked2('$checkbox_id', '$t1_id', '$t2_id', 0, 23)\" $c> &nbsp; "
628
629
            .hour_select($prefs->$tag, $tag, $t1_id, $d)
630
            ." &nbsp; "
631
            .tra("and")
632
            ." &nbsp; "
633
            .hour_select($prefs->$tag2, $tag2, $t2_id, $d)
634
            ."
635
        ";
636
    }
637
    function parse_form(&$prefs, &$error) {
638
        $tag = $this->tag;
639
        $tag2 = $this->tag2;
640
        $prefs->$tag = get_str($tag, true);
641
        $prefs->$tag2 = get_str($tag2, true);
642
    }
643
    function xml_string($prefs) {
644
        $tag = $this->tag;
645
        $tag2 = $this->tag2;
646
        $h1 = $prefs->$tag;
647
        $h2 = $prefs->$tag2;
648
        if ($h1 == $h2) return "";
649
        return "<$tag>$h1</$tag>\n<$tag2>$h2</$tag2>\n";
650
    }
651
    function xml_parse(&$prefs, $name, $text) {
652
        $tag = $this->tag;
653
        $tag2 = $this->tag2;
654
        if ($name == $tag) {
655
            $prefs->$tag = $text;
656
            return true;
657
        } else if ($name == $tag2) {
658
            $prefs->$tag2 = $text;
659
            return true;
660
        }
661
        return false;
662
    }
663
    function set_default(&$prefs) {
664
        $tag = $this->tag;
665
        $tag2 = $this->tag2;
666
        $prefs->$tag = 0;
667
        $prefs->$tag2 = 0;
668
    }
669
}
670
671
// display preference subsets as columns
672
//
673
function row_top($x) {
674
    echo "<tr class=\"bg-primary\"><th width=35%>$x</th>";
675
    echo "<th ><b>".tra("Default")."</b></th>
676
        <th ><b>".tra("Home")."</b></th>
677
        <th ><b>".tra("School")."</b></th>
678
        <th ><b>".tra("Work")."</b></th>
679
    ";
680
    echo "</tr>\n";
681
}
682
683
// row_defs - Display a value for all 4 venues in one row
684
//
685
function row_defs($pre, $item, $post, $type, $prefs, $tooltip=null) {
686
    $gen = $prefs->$item;
687
    $hom  = (isset($prefs->home) && isset($prefs->home->$item)) ? $prefs->home->$item : "--";
688
    $schl = (isset($prefs->school) && isset($prefs->school->$item)) ? $prefs->school->$item : "--";
689
    $wrk  = (isset($prefs->work) && isset($prefs->work->$item)) ? $prefs->work->$item : "--";
690
691
    if ($tooltip) {
692
        echo "<tr title=\"$tooltip\">";
693
    } else {
694
        echo "<tr>";
695
    }
696
    echo "<td ".NAME_ATTRS.">$pre</td>";
697
    row_field($gen,  $type);
698
    row_field($hom,  $type);
699
    row_field($schl, $type);
700
    row_field($wrk,  $type);
701
    echo "<td align=left>$post</td></tr>\n";
702
}
703
704
//
705
// row_field - Display each field value, with selectable display modes
706
//
707
function row_field($value, $type) {
708
    echo "<td>";
709
    $type = $value === "--" ? "--" : $type;
710
    switch($type) {
711
    case "yesno":
712
        echo $value ?tra("yes"):tra("no");
713
        break;
714
    case "noyes":
715
        echo $value ?tra("no"):tra("yes");
716
        break;
717
    case "limit":
718
        $x = max_bytes_display_mode($value);
719
        $y = "$x " . BYTE_ABBR;
720
        echo $x ? "$y" : tra("no limit");
721
        break;
722
    case "minutes":
723
        if ($value) {
724
            echo $value;
725
        } else {
726
            echo '--';
727
        }
728
        break;
729
    default:
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
730
        echo $value;
731
        break;
732
    }
733
    echo "</td>";
734
}
735
736
//
737
// row_links - Display Edit/Add/Remove links for all venues in 1 row
738
//
739
function row_links($subset, $prefs) {
740
    global $g_logged_in_user;
741
    $tokens = url_tokens($g_logged_in_user->authenticator);
742
    $pre_add  = "<a href=add_venue.php?venue=";
743
    $pre_edit  = "<a href=prefs_edit.php?venue=";
744
    $pre_remove = "<a href=prefs_remove.php?venue=";
745
    $post_add = "&subset=$subset&cols=1$tokens>".tra("Add")."</a>";
746
    $post_edit = "&subset=$subset&cols=1$tokens>".tra("Edit")."</a>";
747
    $post_remove = "&subset=$subset&cols=1$tokens>".tra("Remove")."</a>";
748
    $gen = "<a href=prefs_edit.php?subset=$subset&cols=1$tokens>".tra("Edit")."</a>";
749
750
    $hom  = isset($prefs->home) ? $pre_edit."home".$post_edit : $pre_add."home".$post_add;
751
    $schl = isset($prefs->school) ? $pre_edit."school".$post_edit : $pre_add."school".$post_add;
752
    $wrk  = isset($prefs->work) ? $pre_edit."work".$post_edit : $pre_add."work".$post_add;
753
754
    echo "<tr><td class=\"text-right \"> </td>";
755
    echo "<td>$gen</td>";
756
    echo "<td>$hom</td>";
757
    echo "<td>$schl</td>";
758
    echo "<td>$wrk</td>";
759
    echo "<td><br></td></tr>\n";
760
761
    $hom  = isset($prefs->home) ? $pre_remove."home".$post_remove : "<br>";
762
    $schl = isset($prefs->school) ? $pre_remove."school".$post_remove : "<br>";
763
    $wrk  = isset($prefs->work) ? $pre_remove."work".$post_remove : "<br>";
764
765
    echo "<tr><td class=\"text-right \"> </td>";
766
    echo "<td> </td>";
767
    echo "<td>$hom</td>";
768
    echo "<td>$schl</td>";
769
    echo "<td>$wrk</td>";
770
    echo "<td><br></td></tr>\n";
771
}
772
773
// see if we have any beta apps or app versions
774
//
775
function project_has_beta() {
776
    $apps = BoincApp::enum("deprecated=0 and beta>0");
777
    if (count($apps)) return true;
778
    $avs = BoincAppVersion::enum("deprecated=0 and beta>0");
779
    if (count($avs)) return true;
780
    return false;
781
}
782
783
?>
784