File: group.inc.php

package info (click to toggle)
zoph 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,632 kB
  • sloc: php: 28,044; javascript: 10,435; sql: 527; sh: 153; makefile: 4
file content (273 lines) | stat: -rw-r--r-- 7,692 bytes parent folder | download
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
 * A class representing a group of users.
 *
 * This file is part of Zoph.
 *
 * Zoph 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.
 *
 * Zoph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @author Jeroen Roos
 * @package Zoph
 */

use db\select;
use db\insert;
use db\delete;
use db\param;
use db\clause;

use conf\conf;

use template\template;

/**
 * A class representing a group of users
 *
 * @author Jeroen Roos
 * @package Zoph
 */
class group extends zophTable {
    /** @var string The name of the database table */
    protected static $tableName="groups";
    /** @var array List of primary keys */
    protected static $primaryKeys=array("group_id");
    /** @var array Fields that may not be empty */
    protected static $notNull=array("group_name");
    /** @var bool keep keys with insert. In most cases the keys are set by
                  the db with auto_increment */
    protected static $keepKeys = false;
    /** @var string URL for this class */
    protected static $url="group?group_id=";

    /**
     * Delete a group
     * Also remove all users from this group and remove any permissions set for this group
     */
    public function delete() {
        parent::delete(array("groups_users", "group_permissions"));
    }

    /**
     * Get name of group
     * @return string name
     */
    public function getName() {
        return $this->get("group_name");
    }

    /**
     * Get permissions for a group
     * @param album Album to lookup permissions for
     * @return permissions Permissions object
     */
    public function getGroupPermissions(album $album) {
        $gp = new permissions($this->getId(), $album->getId());
        if ($gp->lookup()) {
            return $gp;
        }

        return null;
    }

    /**
     * Get albums associated with this permissions object
     * @return array of albums
     */
    public function getAlbums() {
        $qry=new select(array("gp" => "group_permissions"));
        $qry->addFields(array("album_id"));
        $qry->where(new clause("group_id=:groupid"));
        $qry->addParam(new param(":groupid", (int) $this->getId(), PDO::PARAM_INT));
        return album::getRecordsFromQuery($qry);
    }

    /**
     * Get display array
     * Get an array of properties to display
     * @return array properties
     */
    public function getDisplayArray() {
        return array(
            translate("group") => $this->get("group_name"),
            translate("description") => $this->get("description"),
            translate("members") => implode("<br>", $this->getMemberLinks())
        );
    }

    /**
     * Create an array describing permissions for all groups
     * for display or edit
     * @param bool Return array of albums instead of array of permissions
     * @return array permissions
     */
    public function getPermissionArray($getAlbum=false) {
        $albums = album::getSelectArray();
        $perms=array();
        foreach ($albums as $id => $name) {
            if (!$id || $id == 1) {
                continue;
            }
            $album=new album((int) $id);
            $permissions = $this->getGroupPermissions($album);
            if ($permissions) {
                if ($getAlbum) {
                    $perms[]=$album;
                } else {
                    $perms[]=$permissions;
                }
            }
        }
        return $perms;
    }

    /**
     * Get members of this group
     * @return array of users
     */
    public function getMembers() {
        $qry=new select(array("gu" => "groups_users"));
        $qry->addFields(array("user_id"));
        $qry->where(new clause("group_id=:groupid"));
        $qry->addParam(new param(":groupid", (int) $this->getId(), PDO::PARAM_INT));

        $members=user::getRecordsFromQuery($qry);
        $return=array();
        foreach ($members as $member) {
            $member->lookup();
            $return[]=$member;
        }
        return $return;
    }

    /**
     * Add a member to a group
     * @param user User to add
     */
    public function addMember(user $user) {
        $qry=new insert(array("gu" => "groups_users"));
        $qry->addParams(array(
            new param(":group_id", (int) $this->getId(), PDO::PARAM_INT),
            new param(":user_id", (int) $user->getId(), PDO::PARAM_INT)
        ));

        $qry->execute();

    }

    /**
     * Remove a member from a group
     * @param user User to remove
     */
    public function removeMember(user $user) {

        $qry=new delete(array("gu" => "groups_users"));

        $where=new clause("group_id=:groupid");
        $where->addAnd(new clause("user_id=:userid"));

        $qry->addParams(array(
            new param(":groupid", (int) $this->getId(), PDO::PARAM_INT),
            new param(":userid", $user->getId(), PDO::PARAM_INT)
        ));

        $qry->where($where);

        $qry->execute();
    }

    /**
     * Get an array of users that are NOT a member of this group
     * @return array of users
     */
    private function getNonMembers() {
        $userIds=array();
        $memberIds=array();

        $users=user::getAll();
        $members=$this->getMembers();

        foreach ($users as $user) {
            $userIds[]=$user->getId();
        }
        if ($members) {
            foreach ($members as $member) {
                $memberIds[]=$member->getId();
            }
            $nonMemberIds=array_diff($userIds, $memberIds);
        } else {
            $nonMemberIds=$userIds;
        }

        $nonMembers=array();

        foreach ($nonMemberIds as $id) {
            $nonMembers[]=new user($id);
        }
        return $nonMembers;

    }

    /**
     * Create a dropdown to add new members to this group
     * @param string name for the dropdown field
     * @return template Dropdown
     */
    public function getNewMemberDropdown($name) {
        $valueArray=array();

        $newMembers=$this->getNonMembers();
        $valueArray[0]=null;
        foreach ($newMembers as $nm) {
            $nm->lookup();
            $valueArray[$nm->getId()]=$nm->getName();
        }
        return template::createDropdown($name, null, $valueArray);
    }

    /**
     * Get links to all members of this group
     * @return array array of links
     */
    public function getMemberLinks() {
        $links=array();
        $members=$this->getMembers();
        if ($members) {
            foreach ($members as $member) {
                $member->lookup();
                $links[]=$member->getLink();
            }
        }
        return $links;
    }

    public static function getByName(string $name) {
        $groups = static::getRecords(null, array("group_name" => $name));
        if (empty($groups)) {
            throw new groupNotFoundException("Group not found");
        } else {
            return $groups[0];
        }
    }

    public static function exists(string $name) {
        try {
            self::getByName($name);
        } catch (groupNotFoundException $e) {
            return false;
        }
        return true;
    }
}

?>