3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2011-2021 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
27 * @category Repository
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
37 namespace Galette\Repository
;
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
;
51 * @category Repository
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
70 * @param Db $zdb Database instance
71 * @param Login $login Login instance
73 public function __construct(Db
$zdb, Login
$login)
76 $this->login
= $login;
80 * Get simple groups list (only id and names)
82 * @param boolean $as_groups Retrieve Group[]
86 public static function getSimpleList(bool $as_groups = false): array
91 $select = $zdb->select(Group
::TABLE
);
92 if ($as_groups === false) {
94 array(Group
::PK
, 'group_name')
100 $results = $zdb->execute($select);
102 foreach ($results as $row) {
103 if ($as_groups === false) {
104 $groups[$row->$gpk] = $row->group_name
;
106 $groups[$row->$gpk] = new Group($row);
110 } catch (Throwable
$e) {
112 'Cannot list groups (simple) | ' . $e->getMessage(),
122 * @param boolean $full Return full list or root only
123 * @param int|null $id Group ID to retrieve
127 public function getList(bool $full = true, int $id = null): array
130 $select = $this->zdb
->select(Group
::TABLE
, 'ggroup');
132 if (!$this->login
->isAdmin() && !$this->login
->isStaff() && $full === true) {
134 array('gmanagers' => PREFIX_DB
. Group
::GROUPSMANAGERS_TABLE
),
135 'ggroup.' . Group
::PK
. '=gmanagers.' . Group
::PK
,
137 )->where(['gmanagers.' . Adherent
::PK
=> $this->login
->id
]);
141 array('gusers' => PREFIX_DB
. Group
::GROUPSUSERS_TABLE
),
142 'ggroup.' . Group
::PK
. '=gusers.' . Group
::PK
,
143 array('members' => new Expression('count(gusers.' . Group
::PK
. ')')),
147 if ($full !== true) {
148 $select->where('ggroup.parent_group IS NULL');
154 ->equalTo('ggroup.' . Group
::PK
, $id)
156 ->equalTo('ggroup.parent_group', $id)
161 $select->group('ggroup.' . Group
::PK
)
162 ->group('ggroup.group_name')
163 ->group('ggroup.creation_date')
164 ->group('ggroup.parent_group')
165 ->order('ggroup.group_name ASC');
169 $results = $this->zdb
->execute($select);
171 foreach ($results as $row) {
172 $group = new Group($row);
173 $group->setLogin($this->login
);
174 $groups[$group->getFullName()] = $group;
176 if ($full) { // Order by tree name instead of name
180 } catch (Throwable
$e) {
182 'Cannot list groups | ' . $e->getMessage(),
190 * Loads managed groups for specific member
192 * @param int $id Member id
193 * @param boolean $as_group Retrieve Group[] or int[]
197 public static function loadManagedGroups(int $id, bool $as_group = true): array
199 return self
::loadGroups($id, true, $as_group);
203 * Loads groups for specific member
205 * @param int $id Member id
206 * @param boolean $managed Retrieve managed groups (defaults to false)
207 * @param boolean $as_group Retrieve Group[] or int[]
211 public static function loadGroups(int $id, bool $managed = false, bool $as_group = true): array
215 $join_table = ($managed) ?
216 Group
::GROUPSMANAGERS_TABLE
: Group
::GROUPSUSERS_TABLE
;
218 $select = $zdb->select(Group
::TABLE
, 'group');
221 'b' => PREFIX_DB
. $join_table
223 'group.' . Group
::PK
. '=b.' . Group
::PK
,
225 )->where(array('b.' . Adherent
::PK
=> $id));
227 $results = $zdb->execute($select);
230 foreach ($results as $r) {
231 if ($as_group === true) {
232 $groups[] = new Group($r);
235 $groups[] = $r->$gpk;
239 } catch (Throwable
$e) {
241 'Cannot load member groups for id `' . $id . '` | ' .
250 * Add a member to specified groups
252 * @param Adherent $adh Member
253 * @param array $groups Groups Groups list. Each entry must contain
254 * the group id, name each value separated
256 * @param boolean $manager Add member as manager, defaults to false
257 * @param boolean $transaction Does a SQL transaction already exists? Defaults
262 public static function addMemberToGroups($adh, $groups, $manager = false, $transaction = false)
266 $managed_groups = [];
267 if (!$login->isSuperAdmin() && !$login->isAdmin() && !$login->isStaff()) {
268 $managed_groups = $login->getManagedGroups();
272 if ($transaction === false) {
273 $zdb->connection
->beginTransaction();
277 if ($manager === true) {
278 $table = Group
::GROUPSMANAGERS_TABLE
;
280 $table = Group
::GROUPSUSERS_TABLE
;
283 //first, remove current groups members
284 $delete = $zdb->delete($table);
285 $delete->where([Adherent
::PK
=> $adh->id
]);
286 if (count($managed_groups)) {
287 $delete->where
->in(Group
::PK
, $managed_groups);
289 $zdb->execute($delete);
292 if ($manager === true) {
293 $msg = 'Member `' . $adh->sname
. '` has been detached from groups he manages';
295 $msg = 'Member `' . $adh->sname
. '` has been detached of its groups';
298 $msg . ', we can now store new ones.',
302 //we proceed, if groups has been specified
303 if (is_array($groups)) {
304 $insert = $zdb->insert($table);
307 Group
::PK
=> ':group',
308 Adherent
::PK
=> ':adh'
311 $stmt = $zdb->sql
->prepareStatementForSqlObject($insert);
313 foreach ($groups as $group) {
314 list($gid, $gname) = explode('|', $group);
316 if (count($managed_groups) && !in_array($gid, $managed_groups)) {
320 $result = $stmt->execute(
328 $msg = 'Member `' . $adh->sname
. '` attached to group `' .
329 $gname . '` (' . $gid . ')';
330 if ($manager === true) {
331 $msg .= ' as a manager';
338 $msg = 'Unable to attach member `' .
339 $adh->sname
. '` (' . $adh->id
. ') to group `' .
340 $gname . '` (' . $gid . ').';
341 if ($manager === true) {
342 $msg .= ' as a manager';
348 throw new \
Exception($msg);
352 if ($transaction === false) {
354 $zdb->connection
->commit();
357 } catch (Throwable
$e) {
358 if ($transaction === false) {
359 $zdb->connection
->rollBack();
361 $msg = 'Unable to add member `' . $adh->sname
. '` (' . $adh->id
.
362 ') to specified groups ' . print_r($groups, true);
363 if ($manager === true) {
364 $msg .= ' as a manager';
367 $messages[] = $e->getMessage();
368 } while ($e = $e->getPrevious());
370 $msg . ' |' . implode("\n", $messages),
378 * Remove members from all their groups
380 * @param array $ids Members ids
384 public static function removeMembersFromGroups(array $ids)
389 $del_qry = $zdb->delete(Group
::GROUPSUSERS_TABLE
);
390 $del_qry->where
->in(Adherent
::PK
, $ids);
391 $zdb->execute($del_qry);
393 $del_qry = $zdb->delete(Group
::GROUPSMANAGERS_TABLE
);
394 $del_qry->where
->in(Adherent
::PK
, $ids);
395 $zdb->execute($del_qry);
396 } catch (Throwable
$e) {
398 'Unable to remove member #' . implode(', ', $ids) . ' from his groups: ' .
407 * Remove member from all his groups
409 * @param int $id Member's id
413 public static function removeMemberFromGroups($id)
415 self
::removeMembersFromGroups([$id]);
419 * Check if groupname is unique
421 * @param Db $zdb Database instance
422 * @param string $name Requested name
423 * @param int|null $parent Parent groupe (defaults to null)
424 * @param int|null $current Current ID to be excluded (defaults to null)
428 public static function isUnique(Db
$zdb, string $name, int $parent = null, int $current = null)
431 $select = $zdb->select(Group
::TABLE
);
432 $select->columns(['group_name'])
433 ->where(['group_name' => $name]);
435 if ($parent === null) {
436 $select->where('parent_group IS NULL');
438 $select->where(['parent_group' => $parent]);
441 if ($current !== null) {
442 $select->where
->notEqualTo(Group
::PK
, $current);
445 $results = $zdb->execute($select);
446 return !($results->count() > 0);
447 } catch (Throwable
$e) {
449 'Cannot check group name uniqueness | ' . $e->getMessage(),
457 * Get managed users id list
459 * @param array $groups List of managed groups.
460 * If empty, Groups::loadManagedGroups() will be called
462 * @return array|false
464 public function getManagerUsers(array $groups = [])
466 if (!$this->login
->isGroupManager()) {
469 if (!count($groups)) {
470 $groups = self
::loadManagedGroups($this->login
->id
, false);
473 $select = $this->zdb
->select(Adherent
::TABLE
, 'adh');
477 array('b' => PREFIX_DB
. Group
::GROUPSUSERS_TABLE
),
478 'adh.' . Adherent
::PK
. '=b.' . Adherent
::PK
,
480 )->where
->in('b.' . Group
::PK
, $groups);
482 $results = $this->zdb
->execute($select);
485 foreach ($results as $r) {
486 $ids_adh[] = $r->id_adh
;