aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/visibility/UserPrivacy.php
blob: 165ac69244a3170929ade885aa3227538478b263 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
 * UserPrivacy.php - Represents the privacy settings for a user
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Florian Bieringer <florian.bieringer@uni-passau.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 */
class UserPrivacy
{
    /**
     * @var User object
     */
    private $user;
    /**
     * @var array Privacysettingstree
     */
    private $profileSettings;

    /**
     * Builds a visibility setting for a specific userid
     * @param type $userid the specific userid
     */
    public function __construct($userid = null)
    {
        if ($userid == null) {
            $this->user = User::findCurrent();
        } else {
            $this->user = User::find($userid);
        }
    }

    /**
     * Returns all the categorys and it's items
     * @return array categorys and it's items
     */
    public function getProfileSettings()
    {
        if (!isset($this->profileSettings)) {
            // if the default categories have not been created, do this now
            if (User_Visibility_Settings::countBySQL('user_id = ? AND category = 0', [$this->user->id]) == 0) {
                Visibility::createDefaultCategories($this->user->id);
            }
            $this->profileSettings = User_Visibility_Settings::findBySQL("user_id = ? AND parent_id = 0 AND identifier <> 'plugins'", [$this->user->id]);
            foreach ($this->profileSettings as $i => $vis) {
                $vis->loadChildren();
                // remap child settings to default categories
                if ($vis->category == 1) {
                    $idmap[$vis->identifier] = $vis;
                    unset($this->profileSettings[$i]);
                }
            }

            $elements = $this->user->getHomepageElements();

            foreach ($elements as $key => $element) {
                foreach ($this->profileSettings as $vis) {
                    if ($vis->identifier === $element['identifier']) {
                        foreach ($vis->children as $child) {
                            if ($child->identifier === $key) {
                                $child->setDisplayed();
                                $child->name = $element['name'];
                                break 2;
                            }
                        }

                        $child = $idmap[$key] ?: new User_Visibility_Settings();
                        $child->setData([
                            'user_id'    => $this->user->id,
                            'parent_id'  => $vis->id,
                            'category'   => 1,
                            'name'       => $element['name'],
                            'state'      => $element['visibility'],
                            'identifier' => $key
                        ]);
                        $child->store();
                        $child->parent = $vis;
                        $child->setDisplayed();
                        $vis->children[] = $child;
                        break;
                    }
                }
            }
        }
        return $this->profileSettings;
    }

    /**
     * Takes the new_priv_settings request and stores it into the database
     * @param array $data The given requestdata produced by the privacySettings
     * view
     */
    public function updateAllFromRequest($data)
    {
        $db = DBManager::get();

        /*
         * This is really interesting! A single update query with CASE construct
         * is performed in about half the time of multiple queries with WHERE
         */
        $sql = "UPDATE `user_visibility_settings` SET `state` = CASE `visibilityid` ";
        foreach (array_map('addslashes', $data) as $key => $ps) {
            $sql .= "WHEN '$key' THEN '$ps' ";
        }
        $sql .= "ELSE `state` END;";
        $db->exec($sql);
    }

    /**
     * Updates a PrivacySetting in the DB
     *
     * @param type $key The Settings Identifier
     * @param type $state The wanted state
     * @param type $db Optional an open database connection
     * @param type $userid Optional the users id
     */
    public function update($key, $state, $db = null, $userid = null)
    {
        if ($db == null) {
            $db = DBManager::get();
        }
        $params = [];
        $sql = "UPDATE user_visibility_settings SET state = ? WHERE visibilityid = ? ";
        $params[] = $state;
        $params[] = $key;
        if ($userid != null) {
            $sql .= " AND userid = ?";
            $params[] = $userid;
        }
        $st = $db->prepare($sql);
        $st->execute($params);
    }

    /**
     * Returns all Arguments for the SettingsPage
     * @return array Arguments for the SettingsPage
     */
    public function getHTMLArgs()
    {
        $privacy_states = VisibilitySettings::getInstance();
        $result['header_colspan'] = $privacy_states->count() + 1;
        $result['row_colspan'] = $privacy_states->count();
        $result['header_names'] = $privacy_states->getAllNames();
        $result['states'] = $privacy_states->getAllKeys();
        $result['entry'] = [];
        foreach ($this->getProfileSettings() as $child) {
            $child->getHTMLArgs($result['entry']);
        }
        return $result;
    }
}