]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Repository/Groups.php
221eb32b4e1d8159d1b552711c6224bf819061a1
[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-2014 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-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
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-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
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 }
112 }
113
114 /**
115 * Get groups list
116 *
117 * @param boolean $full Return full list or root only
118 * @param int $id Group ID to retrieve
119 *
120 * @return Group[]
121 */
122 public function getList($full = true, $id = null)
123 {
124 try {
125 $select = $this->zdb->select(Group::TABLE, 'a');
126 $select->join(
127 array('b' => PREFIX_DB . Group::GROUPSUSERS_TABLE),
128 'a.' . Group::PK . '=b.' . Group::PK,
129 array('members' => new Expression('count(b.' . Group::PK . ')')),
130 $select::JOIN_LEFT
131 );
132
133 if (!$this->login->isAdmin() && !$this->login->isStaff() && $full === true) {
134 $select->join(
135 array('c' => PREFIX_DB . Group::GROUPSMANAGERS_TABLE),
136 'a.' . Group::PK . '=c.' . Group::PK,
137 array()
138 )->where('c.' . Adherent::PK . ' = ' . $this->login->id);
139 }
140
141 if ($full !== true) {
142 $select->where('parent_group IS NULL');
143 }
144
145 if ($id !== null) {
146 $select->where(
147 array(
148 'a.' . Group::PK => $id,
149 'a.parent_group' => $id
150 ),
151 PredicateSet::OP_OR
152 );
153 }
154
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');
160
161 $groups = array();
162
163 $results = $this->zdb->execute($select);
164
165 foreach ($results as $row) {
166 $group = new Group($row);
167 $group->setLogin($this->login);
168 $groups[$group->getFullName()] = $group;
169 }
170 if ($full) { // Order by tree name instead of name
171 ksort($groups);
172 Analog::log(
173 'Groups SORTED: ' . print_r(array_keys($groups), true),
174 Analog::DEBUG
175 );
176 }
177 return $groups;
178 } catch (Throwable $e) {
179 Analog::log(
180 'Cannot list groups | ' . $e->getMessage(),
181 Analog::WARNING
182 );
183 }
184 }
185
186 /**
187 * Loads managed groups for specific member
188 *
189 * @param int $id Memebr id
190 * @param boolean $as_group Retrieve Group[] or int[]
191 *
192 * @return array
193 */
194 public static function loadManagedGroups($id, $as_group = true)
195 {
196 return self::loadGroups($id, true, $as_group);
197 }
198
199 /**
200 * Loads groups for specific member
201 *
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[]
205 *
206 * @return array
207 */
208 public static function loadGroups($id, $managed = false, $as_group = true)
209 {
210 global $zdb;
211 try {
212 $join_table = ($managed) ?
213 Group::GROUPSMANAGERS_TABLE : Group::GROUPSUSERS_TABLE;
214
215 $select = $zdb->select(Group::TABLE, 'a');
216 $select->join(
217 array(
218 'b' => PREFIX_DB . $join_table
219 ),
220 'a.' . Group::PK . '=b.' . Group::PK,
221 array()
222 )->where(array('b.' . Adherent::PK => $id));
223
224 $results = $zdb->execute($select);
225
226 $groups = array();
227 foreach ($results as $r) {
228 if ($as_group === true) {
229 $groups[] = new Group($r);
230 } else {
231 $gpk = Group::PK;
232 $groups[] = $r->$gpk;
233 }
234 }
235 return $groups;
236 } catch (Throwable $e) {
237 Analog::log(
238 'Cannot load member groups for id `' . $id . '` | ' .
239 $e->getMessage(),
240 Analog::WARNING
241 );
242 return false;
243 }
244 }
245
246 /**
247 * Add a member to specified groups
248 *
249 * @param Adherent $adh Member
250 * @param array $groups Groups Groups list. Each entry must contain
251 * the group id, name each value separated
252 * by a pipe.
253 * @param boolean $manager Add member as manager, defaults to false
254 * @param boolean $transaction Does a SQL transaction already exists? Defaults
255 * to false.
256 *
257 * @return boolean
258 */
259 public static function addMemberToGroups($adh, $groups, $manager = false, $transaction = false)
260 {
261 global $zdb;
262 try {
263 if ($transaction === false) {
264 $zdb->connection->beginTransaction();
265 }
266
267 $table = null;
268 if ($manager === true) {
269 $table = Group::GROUPSMANAGERS_TABLE;
270 } else {
271 $table = Group::GROUPSUSERS_TABLE;
272 }
273
274 //first, remove current groups members
275 $delete = $zdb->delete($table);
276 $delete->where(
277 Adherent::PK . ' = ' . $adh->id
278 );
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 return false;
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 }
427 }
428
429 /**
430 * Get managed users id list
431 *
432 * @param array $groups List of managed groups.
433 * If empty, Groups::loadManagedGroups() will be called
434 *
435 * @return array|false
436 */
437 public function getManagerUsers(array $groups = [])
438 {
439 if (!$this->login->isGroupManager()) {
440 return false;
441 }
442 if (!count($groups)) {
443 $groups = self::loadManagedGroups($this->login->id, false);
444 }
445
446 $select = $this->zdb->select(Adherent::TABLE, 'a');
447 $select->columns(
448 [Adherent::PK]
449 )->join(
450 array('b' => PREFIX_DB . Group::GROUPSUSERS_TABLE),
451 'a.' . Adherent::PK . '=b.' . Adherent::PK,
452 []
453 )->where->in('b.' . Group::PK, $groups);
454
455 $results = $this->zdb->execute($select);
456
457 $ids_adh = array();
458 foreach ($results as $r) {
459 $ids_adh[] = $r->id_adh;
460 }
461 return $ids_adh;
462 }
463 }