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
66 * @param Db $zdb Database instance
67 * @param Login $login Login instance
69 public function __construct(Db
$zdb, Login
$login)
72 $this->login
= $login;
76 * Get simple groups list (only id and names)
78 * @param boolean $as_groups Retrieve Group[]
82 public static function getSimpleList($as_groups = false)
87 $select = $zdb->select(Group
::TABLE
);
88 if ($as_groups === false) {
90 array(Group
::PK
, 'group_name')
96 $results = $zdb->execute($select);
98 foreach ($results as $row) {
99 if ($as_groups === false) {
100 $groups[$row->$gpk] = $row->group_name
;
102 $groups[$row->$gpk] = new Group($row);
106 } catch (Throwable
$e) {
108 'Cannot list groups (simple) | ' . $e->getMessage(),
118 * @param boolean $full Return full list or root only
119 * @param int $id Group ID to retrieve
123 public function getList($full = true, $id = null)
126 $select = $this->zdb
->select(Group
::TABLE
, 'a');
128 array('b' => PREFIX_DB
. Group
::GROUPSUSERS_TABLE
),
129 'a.' . Group
::PK
. '=b.' . Group
::PK
,
130 array('members' => new Expression('count(b.' . Group
::PK
. ')')),
134 if (!$this->login
->isAdmin() && !$this->login
->isStaff() && $full === true) {
136 array('c' => PREFIX_DB
. Group
::GROUPSMANAGERS_TABLE
),
137 'a.' . Group
::PK
. '=c.' . Group
::PK
,
139 )->where(['c.' . Adherent
::PK
=> $this->login
->id
]);
142 if ($full !== true) {
143 $select->where('parent_group IS NULL');
149 'a.' . Group
::PK
=> $id,
150 'a.parent_group' => $id
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');
164 $results = $this->zdb
->execute($select);
166 foreach ($results as $row) {
167 $group = new Group($row);
168 $group->setLogin($this->login
);
169 $groups[$group->getFullName()] = $group;
171 if ($full) { // Order by tree name instead of name
174 'Groups SORTED: ' . print_r(array_keys($groups), true),
179 } catch (Throwable
$e) {
181 'Cannot list groups | ' . $e->getMessage(),
189 * Loads managed groups for specific member
191 * @param int $id Memebr id
192 * @param boolean $as_group Retrieve Group[] or int[]
196 public static function loadManagedGroups($id, $as_group = true)
198 return self
::loadGroups($id, true, $as_group);
202 * Loads groups for specific member
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[]
210 public static function loadGroups($id, $managed = false, $as_group = true)
214 $join_table = ($managed) ?
215 Group
::GROUPSMANAGERS_TABLE
: Group
::GROUPSUSERS_TABLE
;
217 $select = $zdb->select(Group
::TABLE
, 'a');
220 'b' => PREFIX_DB
. $join_table
222 'a.' . Group
::PK
. '=b.' . Group
::PK
,
224 )->where(array('b.' . Adherent
::PK
=> $id));
226 $results = $zdb->execute($select);
229 foreach ($results as $r) {
230 if ($as_group === true) {
231 $groups[] = new Group($r);
234 $groups[] = $r->$gpk;
238 } catch (Throwable
$e) {
240 'Cannot load member groups for id `' . $id . '` | ' .
249 * Add a member to specified groups
251 * @param Adherent $adh Member
252 * @param array $groups Groups Groups list. Each entry must contain
253 * the group id, name each value separated
255 * @param boolean $manager Add member as manager, defaults to false
256 * @param boolean $transaction Does a SQL transaction already exists? Defaults
261 public static function addMemberToGroups($adh, $groups, $manager = false, $transaction = false)
265 if ($transaction === false) {
266 $zdb->connection
->beginTransaction();
270 if ($manager === true) {
271 $table = Group
::GROUPSMANAGERS_TABLE
;
273 $table = Group
::GROUPSUSERS_TABLE
;
276 //first, remove current groups members
277 $delete = $zdb->delete($table);
278 $delete->where([Adherent
::PK
=> $adh->id
]);
279 $zdb->execute($delete);
282 if ($manager === true) {
283 $msg = 'Member `' . $adh->sname
. '` has been detached from groups he manages';
285 $msg = 'Member `' . $adh->sname
. '` has been detached of its groups';
288 $msg . ', we can now store new ones.',
292 //we proceed, if groups has been specified
293 if (is_array($groups)) {
294 $insert = $zdb->insert($table);
297 Group
::PK
=> ':group',
298 Adherent
::PK
=> ':adh'
301 $stmt = $zdb->sql
->prepareStatementForSqlObject($insert);
303 foreach ($groups as $group) {
304 list($gid, $gname) = explode('|', $group);
306 $result = $stmt->execute(
314 $msg = 'Member `' . $adh->sname
. '` attached to group `' .
315 $gname . '` (' . $gid . ')';
316 if ($manager === true) {
317 $msg .= ' as a manager';
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';
334 throw new \
Exception($msg);
338 if ($transaction === false) {
340 $zdb->connection
->commit();
343 } catch (Throwable
$e) {
344 if ($transaction === false) {
345 $zdb->connection
->rollBack();
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';
353 $messages[] = $e->getMessage();
354 } while ($e = $e->getPrevious());
356 $msg . ' |' . implode("\n", $messages),
364 * Remove members from all their groups
366 * @param array $ids Members ids
370 public static function removeMembersFromGroups(array $ids)
375 $del_qry = $zdb->delete(Group
::GROUPSUSERS_TABLE
);
376 $del_qry->where
->in(Adherent
::PK
, $ids);
377 $zdb->execute($del_qry);
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) {
384 'Unable to remove member #' . $id . ' from his groups: ' .
393 * Remove member from all his groups
395 * @param int $id Member's id
399 public static function removeMemberFromGroups($id)
401 self
::removeMembersFromGroups([$id]);
405 * Check if groupname is unique
407 * @param Db $zdb Database instance
408 * @param string $name Requested name
412 public static function isUnique(Db
$zdb, $name)
415 $select = $zdb->select(Group
::TABLE
);
418 )->where(array('group_name' => $name));
419 $results = $zdb->execute($select);
420 return !($results->count() > 0);
421 } catch (Throwable
$e) {
423 'Cannot list groups (simple) | ' . $e->getMessage(),
431 * Get managed users id list
433 * @param array $groups List of managed groups.
434 * If empty, Groups::loadManagedGroups() will be called
436 * @return array|false
438 public function getManagerUsers(array $groups = [])
440 if (!$this->login
->isGroupManager()) {
443 if (!count($groups)) {
444 $groups = self
::loadManagedGroups($this->login
->id
, false);
447 $select = $this->zdb
->select(Adherent
::TABLE
, 'a');
451 array('b' => PREFIX_DB
. Group
::GROUPSUSERS_TABLE
),
452 'a.' . Adherent
::PK
. '=b.' . Adherent
::PK
,
454 )->where
->in('b.' . Group
::PK
, $groups);
456 $results = $this->zdb
->execute($select);
459 foreach ($results as $r) {
460 $ids_adh[] = $r->id_adh
;