Passed
Push — master ( 6fae8e...4198df )
by Kevin
01:15 queued 23s
created

PREF_CONSENT::show_value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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