3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2011-2014 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-2014 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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2011-10-25
38 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-2014 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 (\Exception
$e) {
108 'Cannot list groups (simple) | ' . $e->getMessage(),
117 * @param boolean $full Return full list or root only
118 * @param int $id Group ID to retrieve
122 public function getList($full = true, $id = null)
125 $select = $this->zdb
->select(Group
::TABLE
, 'a');
127 array('b' => PREFIX_DB
. Group
::GROUPSUSERS_TABLE
),
128 'a.' . Group
::PK
. '=b.' . Group
::PK
,
129 array('members' => new Expression('count(b.' . Group
::PK
. ')')),
133 if (!$this->login
->isAdmin() && !$this->login
->isStaff() && $full === true) {
135 array('c' => PREFIX_DB
. Group
::GROUPSMANAGERS_TABLE
),
136 'a.' . Group
::PK
. '=c.' . Group
::PK
,
138 )->where('c.' . Adherent
::PK
. ' = ' . $this->login
->id
);
141 if ($full !== true) {
142 $select->where('parent_group IS NULL');
148 'a.' . Group
::PK
=> $id,
149 'a.parent_group' => $id
155 $select->group('a.' . Group
::PK
)
156 ->group('a.group_name')
157 ->group('a.creation_date')
158 ->group('a.parent_group')
159 ->order('a.group_name ASC');
163 $results = $this->zdb
->execute($select);
165 foreach ($results as $row) {
166 $group = new Group($row);
167 $group->setLogin($this->login
);
168 $groups[$group->getFullName()] = $group;
170 if ($full) { // Order by tree name instead of name
173 'Groups SORTED: ' . print_r(array_keys($groups), true),
178 } catch (\Exception
$e) {
180 'Cannot list groups | ' . $e->getMessage(),
187 * Loads managed groups for specific member
189 * @param int $id Memebr id
190 * @param boolean $as_group Retrieve Group[] or int[]
194 public static function loadManagedGroups($id, $as_group = true)
196 return self
::loadGroups($id, true, $as_group);
200 * Loads groups for specific member
202 * @param int $id Member id
203 * @param boolean $managed Retrieve managed groups (defaults to false)
204 * @param boolean $as_group Retrieve Group[] or int[]
208 public static function loadGroups($id, $managed = false, $as_group = true)
212 $join_table = ($managed) ?
213 Group
::GROUPSMANAGERS_TABLE
:
214 Group
::GROUPSUSERS_TABLE
;
216 $select = $zdb->select(Group
::TABLE
, 'a');
219 'b' => PREFIX_DB
. $join_table
221 'a.' . Group
::PK
. '=b.' . Group
::PK
,
223 )->where(array('b.' . Adherent
::PK
=> $id));
225 $results = $zdb->execute($select);
228 foreach ($results as $r) {
229 if ($as_group === true) {
230 $groups[] = new Group($r);
233 $groups[] = $r->$gpk;
237 } catch (\Exception
$e) {
239 'Cannot load member groups for id `' . $id . '` | ' .
248 * Add a member to specified groups
250 * @param Adherent $adh Member
251 * @param array $groups Groups Groups list. Each entry must contain
252 * the group id, name each value separated
254 * @param boolean $manager Add member as manager, defaults to false
255 * @param boolean $transaction Does a SQL transaction already exists? Defaults
260 public static function addMemberToGroups($adh, $groups, $manager = false, $transaction = false)
264 if ($transaction === false) {
265 $zdb->connection
->beginTransaction();
269 if ($manager === true) {
270 $table = Group
::GROUPSMANAGERS_TABLE
;
272 $table = Group
::GROUPSUSERS_TABLE
;
275 //first, remove current groups members
276 $delete = $zdb->delete($table);
278 Adherent
::PK
. ' = ' . $adh->id
280 $zdb->execute($delete);
283 if ($manager === true) {
284 $msg = 'Member `' . $adh->sname
. '` has been detached from groups he manages';
286 $msg = 'Member `' . $adh->sname
. '` has been detached of its groups';
289 $msg . ', we can now store new ones.',
293 //we proceed, if groups has been specified
294 if (is_array($groups)) {
295 $insert = $zdb->insert($table);
298 Group
::PK
=> ':group',
299 Adherent
::PK
=> ':adh'
302 $stmt = $zdb->sql
->prepareStatementForSqlObject($insert);
304 foreach ($groups as $group) {
305 list($gid, $gname) = explode('|', $group);
307 $result = $stmt->execute(
310 Adherent
::PK
=> $adh->id
315 $msg = 'Member `' . $adh->sname
. '` attached to group `' .
316 $gname . '` (' . $gid . ')';
317 if ($manager === true) {
318 $msg .= ' as a manager';
325 $msg = 'Unable to attach member `' .
326 $adh->sname
. '` (' . $adh->id
. ') to group `' .
327 $gname . '` (' . $gid . ').';
328 if ($manager === true) {
329 $msg .= ' as a manager';
335 throw new \
Exception($msg);
339 if ($transaction === false) {
341 $zdb->connection
->commit();
344 } catch (\Exception
$e) {
345 if ($transaction === false) {
346 $zdb->connection
->rollBack();
348 $msg = 'Unable to add member `' . $adh->sname
. '` (' . $adh->id
.
349 ') to specified groups ' . print_r($groups, true);
350 if ($manager === true) {
351 $msg .= ' as a manager';
354 $messages[] = $e->getMessage();
355 } while ($e = $e->getPrevious());
357 $msg . ' |' . implode("\n", $messages),
365 * Remove members from all their groups
367 * @param array $ids Members ids
371 public static function removeMembersFromGroups(array $ids)
376 $del_qry = $zdb->delete(Group
::GROUPSUSERS_TABLE
);
377 $del_qry->where
->in(Adherent
::PK
, $ids);
378 $zdb->execute($del_qry);
380 $del_qry = $zdb->delete(Group
::GROUPSMANAGERS_TABLE
);
381 $del_qry->where
->in(Adherent
::PK
, $ids);
382 $zdb->execute($del_qry);
383 } catch (\Exception
$e) {
385 'Unable to remove member #' . $id . ' from his groups: ' .
394 * Remove member from all his groups
396 * @param int $id Member's id
400 public static function removeMemberFromGroups($id)
402 self
::removeMembersFromGroups([$id]);
406 * Check if groupname is unique
408 * @param Db $zdb Database instance
409 * @param string $name Requested name
413 public static function isUnique(Db
$zdb, $name)
416 $select = $zdb->select(Group
::TABLE
);
419 )->where(array('group_name' => $name));
420 $results = $zdb->execute($select);
421 return !($results->count() > 0);
422 } catch (\Exception
$e) {
424 '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
;