Passed
Pull Request — master (#2504)
by
unknown
13:40
created

mct_toggle_field()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 17
rs 9.8333
1
<?php
2
// This file is part of BOINC.
3
// http://boinc.berkeley.edu
4
// Copyright (C) 2017 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
// web interfance for managing consent_type table
20
21
require_once('../inc/util_ops.inc');
22
23
// This function adds a row to consent_type table.
24
function add_consenttype() {
25
    $shortname = BoincDb::escape_string(post_str('add_name'));
26
    $description = BoincDb::escape_string(post_str('add_description'));
27
28
    if (empty($shortname)) {
29
        admin_error_page("The new consent type must contain a short name.</font></p>");
30
    }
31
    if (empty($description)) {
32
        admin_error_page("The new consent type must contain a description.</font></p>");
33
    }
34
35
    BoincConsentType::insert(
36
        "(shortname, description, enabled, project_specific, privacypref) VALUES ('$shortname', '$description', 0, 1, 0)"
37
    );
38
39
    echo "<h2>Consent Type added.</h2>";
40
}
41
42
// Toggles the enable flag
43
function mct_toggle_field($field) {
44
    $ctid = post_int("consent_type_id");
45
    $toggle = post_str("toggle" . $field);
46
    if ($toggle == "Click to Enable") {
47
        $state = 1;
48
        $action = "Enabled";
49
    }
50
    else {
51
        $state = 0;
52
        $action = "Disabled";
53
    }
54
55
    $consent_type = BoincConsentType::lookup("id = $ctid");
56
    if ($consent_type) {
57
        $myname = $consent_type->shortname;
58
        $consent_type->update("$field=$state where id=$ctid");
59
        echo "<h2>Consent Type ${myname} <em>$field</em> changed to <em>$action</em></h2>";
60
    }
61
}
62
63
// Builds the form for managing consent types
64
function mct_show_form() {
65
    $_consenttypes = BoincConsentType::enum(null, "ORDER BY project_specific ASC");
66
67
    if (!in_rops()) {
68
        echo "<strong>HELP: ProjectSpecific=0 consent types are defined by BOINC. You may add and remove project-specific consent types using this form.</strong>";
69
    }
70
    start_table("");
71
    table_header(
72
        "Name",
73
        "Description",
74
        "Enabled",
75
        "ProjectSpecific",
76
        "PrivacyPrefs",
77
        ""
78
    );
79
80
    foreach ($_consenttypes as $ct) {
81
        echo "<tr><form action=manage_consent_types.php method=POST>\n";
82
83
        // Name
84
        echo "<input type=hidden name=consent_type_id value=$ct->id>";
85
        echo "  <td>$ct->shortname</td>";
86
87
        // Description
88
        echo "  <td>$ct->description</td>";
89
90
        // Enabled toggle
91
        if (!in_rops()) {
92
            if (!($ct->enabled)) {
93
                echo "  <td><input class=\"btn btn-default\" name=toggleenabled type=submit value=\"Click to Enable\"></td>";
94
            }
95
            else {
96
                echo "  <td><input class=\"btn btn-default\" name=toggleenabled type=submit value=\"Click to Disable\"></td>";
97
            }
98
        }
99
        else {
100
            echo "  <td>$ct->enabled</td>";
101
        }
102
103
        // Project_specific) consent type
104
        echo "  <td>$ct->project_specific</td>";
105
106
        // Privacypref toggle
107
        if (!in_rops()) {
108
            if (!($ct->privacypref)) {
109
                echo "  <td><input class=\"btn btn-default\" name=toggleprivacypref type=submit value=\"Click to Enable\"></td>";
110
            }
111
            else {
112
                echo "  <td><input class=\"btn btn-default\" name=toggleprivacypref type=submit value=\"Click to Disable\"></td>";
113
            }
114
        }
115
        else {
116
            echo "  <td>$ct->privacypref</td>";
117
        }
118
119
        echo "</form></tr>";
120
    }
121
    end_table();
122
123
    // Entry form to create a new consent type
124
    if (in_rops()) {
125
        return;
126
    }
127
128
    echo"<P>
0 ignored issues
show
Coding Style introduced by
Language constructs must be followed by a single space; expected "echo "<P>
" but found "echo"<P>
"
Loading history...
129
        <h2>Add consent type</h2>
130
        <p>
131
        <p><strong>HELP: To add a consent type, provide a name and description. Then you may toggle the Enabled and PrivacyPrefs settings in the table above.</strong></p>
132
        <p>For Name, please stick to this convention: an ALLCAPS short name, without any spaces. Example: FORUM</p>
133
        <p>For Description: if your consent type will be part of the privacy preferences, it is best to use a full sentence with a question mark at the end. Example: Do you want SPAM, bacon, sausage, and SPAM?</p>
134
        <p><em>Why can't I delete consent types?</em> Consent types cannot be deleted once created. This is to preserve and audit trail that may be legally necessary. You may disable a consent type if it is no longer needed.</p>
135
        <form action=manage_consent_types.php method=POST>
136
    ";
137
138
    start_table("align='center' ");
139
140
    table_header("Name", "Description", "&nbsp;");
141
142
    echo "<TR>
143
            <TD> <input type='text' size='10' name='add_name' value=''> </TD>
144
            <TD> <input type='text' size='35' name='add_description' value=''> </TD>
145
            <TD align='center' >
146
                 <input type='submit' name='add_consenttype' value='Add Consent Type'></TD>
147
          </TR>\n";
148
149
    end_table();
150
    echo "</form><p>\n";
151
152
}
153
154
155
admin_page_head("Manage consent types");
156
157
if (post_str("add_consenttype", true)) {
158
    add_consenttype();
159
}
160
else if (post_str("toggleenabled", true)) {
161
    mct_toggle_field("enabled");
162
}
163
else if (post_str("toggleprivacypref", true)) {
164
    mct_toggle_field("privacypref");
165
}
166
167
// Main display function - shows the form with consent types.
168
mct_show_form();
169
170
admin_page_tail();
171
?>
172