]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Repository/Groups.php
9839f6db86a0499889f09d36b29b1d1dbee5ae00
[galette.git] / galette / lib / Galette / Repository / Groups.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Groups entity
7 *
8 * PHP version 5
9 *
10 * Copyright © 2011-2021 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Repository
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2011-2021 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7dev - 2011-10-25
35 */
36
37 namespace Galette\Repository;
38
39 use Throwable;
40 use Analog\Analog;
41 use Laminas\Db\Sql\Expression;
42 use Laminas\Db\Sql\Predicate\PredicateSet;
43 use Galette\Entity\Group;
44 use Galette\Entity\Adherent;
45 use Galette\Core\Login;
46 use Galette\Core\Db;
47
48 /**
49 * Groups entitiy
50 *
51 * @category Repository
52 * @name Groups
53 * @package Galette
54 * @author Johan Cwiklinski <johan@x-tnd.be>
55 * @copyright 2011-2021 The Galette Team
56 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
57 * @link http://galette.tuxfamily.org
58 * @since Available since 0.7dev - 2011-10-25
59 */
60 class Groups
61 {
62
63 /**
64 * Constructor
65 *
66 * @param Db $zdb Database instance
67 * @param Login $login Login instance
68 */
69 public function __construct(Db $zdb, Login $login)
70 {
71 $this->zdb = $zdb;
72 $this->login = $login;
73 }
74
75 /**
76 * Get simple groups list (only id and names)
77 *
78 * @param boolean $as_groups Retrieve Group[]
79 *
80 * @return array
81 */
82 public static function getSimpleList($as_groups = false)
83 {
84 global $zdb;
85
86 try {
87 $select = $zdb->select(Group::TABLE);
88 if ($as_groups === false) {
89 $select->columns(
90 array(Group::PK, 'group_name')
91 );
92 }
93 $groups = array();
94 $gpk = Group::PK;
95
96 $results = $zdb->execute($select);
97
98 foreach ($results as $row) {
99 if ($as_groups === false) {
100 $groups[$row->$gpk] = $row->group_name;
101 } else {
102 $groups[$row->$gpk] = new Group($row);
103 }
104 }
105 return $groups;
106 } catch (Throwable $e) {
107 Analog::log(
108 'Cannot list groups (simple) | ' . $e->getMessage(),
109 Analog::WARNING
110 );
111 throw $e;
112 }
113 }
114
115 /**
116 * Get groups list
117 *
118 * @param boolean $full Return full list or root only
119 * @param int $id Group ID to retrieve
120 *
121 * @return Group[]
122 */
123 public function getList($full = true, $id = null)
124 {
125 try {
126 $select = $this->zdb->select(Group::TABLE, 'a');
127 $select->join(
128 array('b' => PREFIX_DB . Group::GROUPSUSERS_TABLE),
129 'a.' . Group::PK . '=b.' . Group::PK,
130 array('members' => new Expression('count(b.' . Group::PK . ')')),
131 $select::JOIN_LEFT
132 );
133
134 if (!$this->login->isAdmin() && !$this->login->isStaff() && $full === true) {
135 $select->join(
136 array('c' => PREFIX_DB . Group::GROUPSMANAGERS_TABLE),
137 'a.' . Group::PK . '=c.' . Group::PK,
138 array()
139 )->where(['c.' . Adherent::PK => $this->login->id]);
140 }
141
142 if ($full !== true) {
143 $select->where('parent_group IS NULL');
144 }
145
146 if ($id !== null) {
147 $select->where(
148 array(
149 'a.' . Group::PK => $id,
150 'a.parent_group' => $id
151 ),
152 PredicateSet::OP_OR
153 );
154 }
155
156 $select->group('a.' . Group::PK)
157 ->group('a.group_name')
158 ->group('a.creation_date')
159 ->group('a.parent_group')
160 ->order('a.group_name ASC');
161
162 $groups = array();
163
164 $results = $this->zdb->execute($select);
165
166 foreach ($results as $row) {
167 $group = new Group($row);
168 $group->setLogin($this->login);
169 $groups[$group->getFullName()] = $group;
170 }
171 if ($full) { // Order by tree name instead of name
172 ksort($groups);
173 Analog::log(
174 'Groups SORTED: ' . print_r(array_keys($groups), true),
175 Analog::DEBUG
176 );
177 }
178 return $groups;
179 } catch (Throwable $e) {
180 Analog::log(
181 'Cannot list groups | ' . $e->getMessage(),
182 Analog::WARNING
183 );
184 throw $e;
185 }
186 }
187
188 /**
189 * Loads managed groups for specific member
190 *
191 * @param int $id Memebr id
192 * @param boolean $as_group Retrieve Group[] or int[]
193 *
194 * @return array
195 */
196 public static function loadManagedGroups($id, $as_group = true)
197 {
198 return self::loadGroups($id, true, $as_group);
199 }
200
201 /**
202 * Loads groups for specific member
203 *
204 * @param int $id Member id
205 * @param boolean $managed Retrieve managed groups (defaults to false)
206 * @param boolean $as_group Retrieve Group[] or int[]
207 *
208 * @return array
209 */
210 public static function loadGroups($id, $managed = false, $as_group = true)
211 {
212 global $zdb;
213 try {
214 $join_table = ($managed) ?
215 Group::GROUPSMANAGERS_TABLE : Group::GROUPSUSERS_TABLE;
216
217 $select = $zdb->select(Group::TABLE, 'a');
218 $select->join(
219 array(
220 'b' => PREFIX_DB . $join_table
221 ),
222 'a.' . Group::PK . '=b.' . Group::PK,
223 array()
224 )->where(array('b.' . Adherent::PK => $id));
225
226 $results = $zdb->execute($select);
227
228 $groups = array();
229 foreach ($results as $r) {
230 if ($as_group === true) {
231 $groups[] = new Group($r);
232 } else {
233 $gpk = Group::PK;
234 $groups[] = $r->$gpk;
235 }
236 }
237 return $groups;
238 } catch (Throwable $e) {
239 Analog::log(
240 'Cannot load member groups for id `' . $id . '` | ' .
241 $e->getMessage(),
242 Analog::WARNING
243 );
244 throw $e;
245 }
246 }
247
248 /**
249 * Add a member to specified groups
250 *
251 * @param Adherent $adh Member
252 * @param array $groups Groups Groups list. Each entry must contain
253 * the group id, name each value separated
254 * by a pipe.
255 * @param boolean $manager Add member as manager, defaults to false
256 * @param boolean $transaction Does a SQL transaction already exists? Defaults
257 * to false.
258 *
259 * @return boolean
260 */
261 public static function addMemberToGroups($adh, $groups, $manager = false, $transaction = false)
262 {
263 global $zdb;
264 try {
265 if ($transaction === false) {
266 $zdb->connection->beginTransaction();
267 }
268
269 $table = null;
270 if ($manager === true) {
271 $table = Group::GROUPSMANAGERS_TABLE;
272 } else {
273 $table = Group::GROUPSUSERS_TABLE;
274 }
275
276 //first, remove current groups members
277 $delete = $zdb->delete($table);
278 $delete->where([Adherent::PK => $adh->id]);
279 $zdb->execute($delete);
280
281 $msg = null;
282 if ($manager === true) {
283 $msg = 'Member `' . $adh->sname . '` has been detached from groups he manages';
284 } else {
285 $msg = 'Member `' . $adh->sname . '` has been detached of its groups';
286 }
287 Analog::log(
288 $msg . ', we can now store new ones.',
289 Analog::INFO
290 );
291
292 //we proceed, if groups has been specified
293 if (is_array($groups)) {
294 $insert = $zdb->insert($table);
295 $insert->values(
296 array(
297 Group::PK => ':group',
298 Adherent::PK => ':adh'
299 )
300 );
301 $stmt = $zdb->sql->prepareStatementForSqlObject($insert);
302
303 foreach ($groups as $group) {
304 list($gid, $gname) = explode('|', $group);
305
306 $result = $stmt->execute(
307 array(
308 'group' => $gid,
309 'adh' => $adh->id
310 )
311 );
312
313 if ($result) {
314 $msg = 'Member `' . $adh->sname . '` attached to group `' .
315 $gname . '` (' . $gid . ')';
316 if ($manager === true) {
317 $msg .= ' as a manager';
318 }
319 Analog::log(
320 $msg,
321 Analog::DEBUG
322 );
323 } else {
324 $msg = 'Unable to attach member `' .
325 $adh->sname . '` (' . $adh->id . ') to group `' .
326 $gname . '` (' . $gid . ').';
327 if ($manager === true) {
328 $msg .= ' as a manager';
329 }
330 Analog::log(
331 $msg,
332 Analog::ERROR
333 );
334 throw new \Exception($msg);
335 }
336 }
337 }
338 if ($transaction === false) {
339 //commit all changes
340 $zdb->connection->commit();
341 }
342 return true;
343 } catch (Throwable $e) {
344 if ($transaction === false) {
345 $zdb->connection->rollBack();
346 }
347 $msg = 'Unable to add member `' . $adh->sname . '` (' . $adh->id .
348 ') to specified groups ' . print_r($groups, true);
349 if ($manager === true) {
350 $msg .= ' as a manager';
351 }
352 do {
353 $messages[] = $e->getMessage();
354 } while ($e = $e->getPrevious());
355 Analog::log(
356 $msg . ' |' . implode("\n", $messages),
357 Analog::ERROR
358 );
359 throw $e;
360 }
361 }
362
363 /**
364 * Remove members from all their groups
365 *
366 * @param array $ids Members ids
367 *
368 * @return void
369 */
370 public static function removeMembersFromGroups(array $ids)
371 {
372 global $zdb;
373
374 try {
375 $del_qry = $zdb->delete(Group::GROUPSUSERS_TABLE);
376 $del_qry->where->in(Adherent::PK, $ids);
377 $zdb->execute($del_qry);
378
379 $del_qry = $zdb->delete(Group::GROUPSMANAGERS_TABLE);
380 $del_qry->where->in(Adherent::PK, $ids);
381 $zdb->execute($del_qry);
382 } catch (Throwable $e) {
383 Analog::log(
384 'Unable to remove member #' . $id . ' from his groups: ' .
385 $e->getMessage(),
386 Analog::ERROR
387 );
388 throw $e;
389 }
390 }
391
392 /**
393 * Remove member from all his groups
394 *
395 * @param int $id Member's id
396 *
397 * @return void
398 */
399 public static function removeMemberFromGroups($id)
400 {
401 self::removeMembersFromGroups([$id]);
402 }
403
404 /**
405 * Check if groupname is unique
406 *
407 * @param Db $zdb Database instance
408 * @param string $name Requested name
409 *
410 * @return boolean
411 */
412 public static function isUnique(Db $zdb, $name)
413 {
414 try {
415 $select = $zdb->select(Group::TABLE);
416 $select->columns(
417 array('group_name')
418 )->where(array('group_name' => $name));
419 $results = $zdb->execute($select);
420 return !($results->count() > 0);
421 } catch (Throwable $e) {
422 Analog::log(
423 'Cannot list groups (simple) | ' . $e->getMessage(),
424 Analog::WARNING
425 );
426 throw $e;
427 }
428 }
429
430 /**
431 * Get managed users id list
432 *
433 * @param array $groups List of managed groups.
434 * If empty, Groups::loadManagedGroups() will be called
435 *
436 * @return array|false
437 */
438 public function getManagerUsers(array $groups = [])
439 {
440 if (!$this->login->isGroupManager()) {
441 return false;
442 }
443 if (!count($groups)) {
444 $groups = self::loadManagedGroups($this->login->id, false);
445 }
446
447 $select = $this->zdb->select(Adherent::TABLE, 'a');
448 $select->columns(
449 [Adherent::PK]
450 )->join(
451 array('b' => PREFIX_DB . Group::GROUPSUSERS_TABLE),
452 'a.' . Adherent::PK . '=b.' . Adherent::PK,
453 []
454 )->where->in('b.' . Group::PK, $groups);
455
456 $results = $this->zdb->execute($select);
457
458 $ids_adh = array();
459 foreach ($results as $r) {
460 $ids_adh[] = $r->id_adh;
461 }
462 return $ids_adh;
463 }
464 }