From d0243769ab7208eccfe75a0df8160a9d2f94a664 Mon Sep 17 00:00:00 2001 From: Neil Blakey-Milner Date: Tue, 2 Aug 2005 13:10:30 +0000 Subject: [PATCH] Allow groups to be members and parents of other groups in the model. --- lib/groups/Group.inc | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+), 0 deletions(-) diff --git a/lib/groups/Group.inc b/lib/groups/Group.inc index 1dc9750..1cbd0ed 100644 --- a/lib/groups/Group.inc +++ b/lib/groups/Group.inc @@ -200,6 +200,22 @@ class Group extends KTEntity { return $aMembers; } + function &getMemberGroups() { + global $default; + $sQuery = "SELECT member_group_id FROM $default->groups_groups_table WHERE parent_group_id = ?"; + $aParams = array($this->getID()); + $aGroupIDs = DBUtil::getResultArrayKey(array($sQuery, $aParams), "member_group_id"); + $aMembers = array(); + foreach ($aGroupIDs as $iGroupID) { + $oGroup = Group::get($iGroupID); + if ($oGroup !== false) { + $aMembers[] = $oGroup; + } + } + return $aMembers; + } + + function delete() { global $default; @@ -270,6 +286,63 @@ class Group extends KTEntity { return true; } // }}} + + // {{{ addMemberGroup + function addMemberGroup($oGroup) { + global $default; + if ($this->hasMemberGroup($oGroup)) { + return true; + } + $aParams = array( + "parent_group_id" => $this->getID(), + "member_group_id" => $oGroup->getID(), + ); + $res = DBUtil::autoInsert($default->groups_groups_table, $aParams); + if (PEAR::isError($res)) { + return $res; + } + return true; + } + // }}} + // + // {{{ removeMemberGroup + function removeMemberGroup($oGroup) { + global $default; + if (!$this->hasMemberGroup($oGroup)) { + return true; + } + $aParams = array( + "parent_group_id" => $this->getID(), + "member_group_id" => $oGroup->getID(), + ); + $res = DBUtil::whereDelete($default->groups_groups_table, $aParams); + if (PEAR::isError($res)) { + return $res; + } + if ($this->hasMember($oGroup)) { + return PEAR::raiseError("Tried to remove member from database, apparently successfully, but hasMember thinks they're still members?"); + } + return true; + } + // }}} + + // {{{ hasMemberGroup + function hasMemberGroup($oGroup) { + global $default; + + $sQuery = "SELECT COUNT(*) AS number_of_entries FROM $default->groups_groups_table + WHERE parent_group_id = ? AND member_group_id = ?"; + $aParams = array($this->getID(), $oGroup->getID()); + $res = (int)DBUtil::getOneResultKey(array($sQuery, $aParams), "number_of_entries"); + if (PEAR::isError($res)) { + return $res; + } + if ($res === 1) { + return true; + } + return false; + } + // }}} } /** -- libgit2 0.21.4