3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2012-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/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2012-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 - 2012-01-17
37 namespace Galette\Entity
;
40 use Galette\Core\Login
;
42 use Laminas\Db\Sql\Expression
;
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2012-2014 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.7dev - 2012-01-17
58 public const TABLE
= 'groups';
59 public const PK
= 'id_group';
61 public const GROUPSUSERS_TABLE
= 'groups_members';
62 public const GROUPSMANAGERS_TABLE
= 'groups_managers';
64 public const MEMBER_TYPE
= 0;
65 public const MANAGER_TYPE
= 1;
69 private $parent_group;
73 private $creation_date;
74 private $count_members;
80 * @param null|int|ResultSet $args Either a ResultSet row or its id for to load
81 * a specific group, or null to just
84 public function __construct($args = null)
86 if ($args == null ||
is_int($args)) {
87 if (is_int($args) && $args > 0) {
90 } elseif (is_object($args)) {
91 $this->loadFromRS($args);
96 * Loads a group from its id
98 * @param int $id the identifiant for the group to load
100 * @return bool true if query succeed, false otherwise
102 public function load($id)
107 $select = $zdb->select(self
::TABLE
);
108 $select->where(array(self
::PK
=> $id));
110 $results = $zdb->execute($select);
112 if ($results->count() > 0) {
113 $this->loadFromRS($results->current());
118 } catch (Throwable
$e) {
120 'Cannot load group form id `' . $id . '` | ' . $e->getMessage(),
128 * Populate object from a resultset row
130 * @param ResultSet $r the resultset row
134 private function loadFromRS($r)
136 $this->id
= $r->id_group
;
137 $this->group_name
= $r->group_name
;
138 $this->creation_date
= $r->creation_date
;
139 if ($r->parent_group
) {
140 $this->parent_group
= new Group((int)$r->parent_group
);
142 $adhpk = Adherent
::PK
;
143 if (isset($r->members
)) {
144 //we're from a list, we just want members count
145 $this->count_members
= $r->members
;
147 //we're probably from a single group, let's load sub entities
148 //$this->loadPersons(self::MEMBER_TYPE);
149 //$this->loadPersons(self::MANAGER_TYPE);
150 //$this->loadSubGroups();
155 * Loads members for the current group
157 * @param int $type Either self::MEMBER_TYPE or self::MANAGER_TYPE
161 private function loadPersons($type)
169 case self
::MEMBER_TYPE
:
170 $join = PREFIX_DB
. self
::GROUPSUSERS_TABLE
;
172 case self
::MANAGER_TYPE
:
173 $join = PREFIX_DB
. self
::GROUPSMANAGERS_TABLE
;
177 $select = $zdb->select(Adherent
::TABLE
, 'a');
180 'g.' . Adherent
::PK
. '=a.' . Adherent
::PK
,
183 'g.' . self
::PK
. ' = ' . $this->id
189 $results = $zdb->execute($select);
198 foreach ($results as $m) {
199 $members[] = new Adherent($zdb, $m, $deps);
202 if ($type === self
::MEMBER_TYPE
) {
203 $this->members
= $members;
205 $this->managers
= $members;
207 } catch (Throwable
$e) {
209 'Cannot get group persons | ' . $e->getMessage(),
221 private function loadSubGroups()
226 $select = $zdb->select(self
::TABLE
, 'a');
228 if (!$this->login
->isAdmin() && !$this->login
->isStaff()) {
230 array('b' => PREFIX_DB
. self
::GROUPSMANAGERS_TABLE
),
231 'a.' . self
::PK
. '=b.' . self
::PK
,
233 )->where('b.' . Adherent
::PK
. ' = ' . $this->login
->id
);
236 $select->where('parent_group = ' . $this->id
)
237 ->order('group_name ASC');
239 $results = $zdb->execute($select);
242 foreach ($results as $m) {
243 $group = new Group((int)$m->$grppk);
244 $group->setLogin($this->login
);
247 $this->groups
= $groups;
248 } catch (Throwable
$e) {
250 'Cannot get subgroup for group ' . $this->group_name
.
251 ' (' . $this->id
. ')| ' . $e->getMessage(),
258 * Remove specified group
260 * @param boolean $cascade Also remove members and managers
264 public function remove($cascade = false)
267 $transaction = false;
270 if (!$zdb->connection
->inTransaction()) {
271 $zdb->connection
->beginTransaction();
275 if ($cascade === true) {
276 $subgroups = $this->getGroups();
277 if (count($subgroups) > 0) {
279 'Cascading remove ' . $this->group_name
.
280 '. Subgroups, their members and managers will be detached.',
283 foreach ($subgroups as $subgroup) {
284 $subgroup->remove(true);
289 'Cascading remove ' . $this->group_name
.
290 '. Members and managers will be detached.',
295 $delete = $zdb->delete(self
::GROUPSUSERS_TABLE
);
297 self
::PK
. ' = ' . $this->id
299 $zdb->execute($delete);
302 $delete = $zdb->delete(self
::GROUPSMANAGERS_TABLE
);
304 self
::PK
. ' = ' . $this->id
306 $zdb->execute($delete);
309 //delete group itself
310 $delete = $zdb->delete(self
::TABLE
);
312 self
::PK
. ' = ' . $this->id
314 $zdb->execute($delete);
318 $zdb->connection
->commit();
322 } catch (Throwable
$e) {
324 $zdb->connection
->rollBack();
326 if ($e->getCode() == 23000) {
331 'Group "%group" still have members!'
335 $this->isempty
= false;
338 'Unable to delete group ' . $this->group_name
.
339 ' (' . $this->id
. ') |' . $e->getMessage(),
348 * Is group empty? (after first deletion try)
352 public function isEmpty()
354 return $this->isempty
;
358 * Detach a group from its parent
362 public function detach()
367 $update = $zdb->update(self
::TABLE
);
369 array('parent_group' => new Expression('NULL'))
371 self
::PK
. ' = ' . $this->id
374 $edit = $zdb->execute($update);
376 //edit == 0 does not mean there were an error, but that there
377 //were nothing to change
378 if ($edit->count() > 0) {
379 $this->parent_group
= null;
381 _T("Group has been detached from its parent"),
387 } catch (Throwable
$e) {
389 'Something went wrong detaching group `' . $this->group_name
.
390 '` (' . $this->id
. ') from its parent:\'( | ' .
391 $e->getMessage() . "\n" .
392 $e->getTraceAsString(),
395 throw new \
Exception(_T("Unable to detach group :("));
404 public function store()
410 self
::PK
=> $this->id
,
411 'group_name' => $this->group_name
414 if ($this->parent_group
) {
415 $values['parent_group'] = $this->parent_group
->getId();
418 if (!isset($this->id
) ||
$this->id
== '') {
419 //we're inserting a new group
420 unset($values[self
::PK
]);
421 $this->creation_date
= date("Y-m-d H:i:s");
422 $values['creation_date'] = $this->creation_date
;
424 $insert = $zdb->insert(self
::TABLE
);
425 $insert->values($values);
426 $add = $zdb->execute($insert);
427 if ($add->count() > 0) {
428 $this->id
= $zdb->getLastGeneratedValue($this);
437 $hist->add(_T("Fail to add new group."));
438 throw new \
Exception(
439 'An error occurred inserting new group!'
443 //we're editing an existing group
444 $update = $zdb->update(self
::TABLE
);
447 ->where(self
::PK
. '=' . $this->id
);
449 $edit = $zdb->execute($update);
451 //edit == 0 does not mean there were an error, but that there
452 //were nothing to change
453 if ($edit->count() > 0) {
461 /** FIXME: also store members and managers? */
462 } catch (Throwable
$e) {
464 'Something went wrong :\'( | ' . $e->getMessage() . "\n" .
465 $e->getTraceAsString(),
473 * Is current logged-in user manager of the group?
475 * @param Login $login Login instance
479 public function isManager(Login
$login)
481 if ($login->isAdmin() ||
$login->isStaff()) {
482 //admins as well as staff members are managers for all groups!
485 //let's check if current logged-in user is part of group managers
486 foreach ($this->managers
as $manager) {
487 if ($login->login
== $manager->login
) {
501 public function getId()
507 * Get Level of the group
511 public function getLevel()
513 if ($this->parent_group
) {
514 return $this->parent_group
->getLevel() +
1;
520 * Get the full name of the group "foo / bar"
524 public function getFullName()
526 if ($this->parent_group
) {
527 return $this->parent_group
->getFullName() . ' / ' . $this->group_name
;
529 return $this->group_name
;
533 * Get the indented short name of the group " >> bar"
537 public function getIndentName()
539 if (($level = $this->getLevel())) {
540 return str_repeat(" ", 3 * $level) . '» ' . $this->group_name
;
542 return $this->group_name
;
550 public function getName()
552 return $this->group_name
;
560 public function getMembers()
562 if (!is_array($this->members
)) {
563 $this->loadPersons(self
::MEMBER_TYPE
);
565 return $this->members
;
569 * Get groups managers
573 public function getManagers()
575 if (!is_array($this->managers
)) {
576 $this->loadPersons(self
::MANAGER_TYPE
);
578 return $this->managers
;
586 public function getGroups()
588 if (!is_array($this->groups
)) {
589 $this->loadSubGroups();
591 return $this->groups
;
599 public function getParentGroup()
601 return $this->parent_group
;
605 * Get group creation date
607 * @param boolean $formatted Return date formatted, raw if false
611 public function getCreationDate($formatted = true)
613 if ($formatted === true) {
614 $date = new \
DateTime($this->creation_date
);
615 return $date->format(__("Y-m-d"));
617 return $this->creation_date
;
624 * @param boolean $force Force members load, defaults to false
628 public function getMemberCount($force = false)
630 if (isset($this->members
) && is_array($this->members
)) {
631 return count($this->members
);
632 } elseif (isset($this->count_members
)) {
633 return $this->count_members
;
635 if ($force === true) {
636 return count($this->getMembers());
646 * @param string $name Group name
650 public function setName($name)
652 $this->group_name
= $name;
659 * @param array $groups Groups id
663 public function setSubgroups($groups)
665 $this->groups
= $groups;
670 * check if can Set parent group
672 * @param Group $group Parent group
676 public function canSetParentGroup(Group
$group)
679 if ($group->getId() == $this->getId()) {
682 } while ($group = $group->getParentGroup());
690 * @param int $id Parent group identifier
694 public function setParentGroup($id)
696 $group = new Group((int)$id);
698 if (!$this->canSetParentGroup($group)) {
699 //does not seem to work :/
700 throw new \
Exception(
702 _T('Group `%1$s` cannot be set as parent!'),
708 $this->parent_group
= $group;
715 * @param Adherent[] $members Members list
719 public function setMembers($members)
724 $zdb->connection
->beginTransaction();
726 //first, remove current groups members
727 $delete = $zdb->delete(self
::GROUPSUSERS_TABLE
);
729 self
::PK
. ' = ' . $this->id
731 $zdb->execute($delete);
734 'Group members has been removed for `' . $this->group_name
.
735 '`, we can now store new ones.',
739 $insert = $zdb->insert(self
::GROUPSUSERS_TABLE
);
742 self
::PK
=> ':group',
743 Adherent
::PK
=> ':adh'
747 $stmt = $zdb->sql
->prepareStatementForSqlObject($insert);
749 if (is_array($members)) {
750 foreach ($members as $m) {
751 $result = $stmt->execute(
753 'group' => $this->id
,
760 'Member `' . $m->sname
. '` attached to group `' .
761 $this->group_name
. '`.',
766 'An error occurred trying to attach member `' .
767 $m->sname
. '` to group `' . $this->group_name
.
768 '` (' . $this->id
. ').',
771 throw new \
Exception(
772 'Unable to attach `' . $m->sname
. '` ' .
773 'to ' . $this->group_name
. '(' . $this->id
. ')'
779 $zdb->connection
->commit();
782 'Group members updated successfully.',
787 } catch (Throwable
$e) {
788 $zdb->connection
->rollBack();
791 $messages[] = $e->getMessage();
792 } while ($e = $e->getPrevious());
794 'Unable to attach members to group `' . $this->group_name
.
795 '` (' . $this->id
. ')|' . implode("\n", $messages),
805 * @param Adherent[] $members Managers list
809 public function setManagers($members)
814 $zdb->connection
->beginTransaction();
816 //first, remove current groups managers
817 $delete = $zdb->delete(self
::GROUPSMANAGERS_TABLE
);
819 self
::PK
. ' = ' . $this->id
821 $zdb->execute($delete);
824 'Group managers has been removed for `' . $this->group_name
.
825 '`, we can now store new ones.',
829 $insert = $zdb->insert(self
::GROUPSMANAGERS_TABLE
);
832 self
::PK
=> ':group',
833 Adherent
::PK
=> ':adh'
837 $stmt = $zdb->sql
->prepareStatementForSqlObject($insert);
839 if (is_array($members)) {
840 foreach ($members as $m) {
841 $result = $stmt->execute(
843 'group' => $this->id
,
850 'Manager `' . $m->sname
. '` attached to group `' .
851 $this->group_name
. '`.',
856 'An error occurred trying to attach manager `' .
857 $m->sname
. '` to group `' . $this->group_name
.
858 '` (' . $this->id
. ').',
861 throw new \
Exception(
862 'Unable to attach `' . $m->sname
. '` ' .
863 'to ' . $this->group_name
. '(' . $this->id
. ')'
869 $zdb->connection
->commit();
872 'Groups managers updated successfully.',
877 } catch (Throwable
$e) {
878 $zdb->connection
->rollBack();
881 $messages[] = $e->getMessage();
882 } while ($e = $e->getPrevious());
884 'Unable to attach managers to group `' . $this->group_name
.
885 '` (' . $this->id
. ')|' . implode("\n", $messages),
895 * @param Login $login Login instance
899 public function setLogin(Login
$login)
901 $this->login
= $login;