Commit d0243769ab7208eccfe75a0df8160a9d2f94a664

Authored by Neil Blakey-Milner
1 parent c0814026

Allow groups to be members and parents of other groups in the model.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3539 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 73 additions and 0 deletions
lib/groups/Group.inc
@@ -200,6 +200,22 @@ class Group extends KTEntity { @@ -200,6 +200,22 @@ class Group extends KTEntity {
200 return $aMembers; 200 return $aMembers;
201 } 201 }
202 202
  203 + function &getMemberGroups() {
  204 + global $default;
  205 + $sQuery = "SELECT member_group_id FROM $default->groups_groups_table WHERE parent_group_id = ?";
  206 + $aParams = array($this->getID());
  207 + $aGroupIDs = DBUtil::getResultArrayKey(array($sQuery, $aParams), "member_group_id");
  208 + $aMembers = array();
  209 + foreach ($aGroupIDs as $iGroupID) {
  210 + $oGroup = Group::get($iGroupID);
  211 + if ($oGroup !== false) {
  212 + $aMembers[] = $oGroup;
  213 + }
  214 + }
  215 + return $aMembers;
  216 + }
  217 +
  218 +
203 function delete() { 219 function delete() {
204 global $default; 220 global $default;
205 221
@@ -270,6 +286,63 @@ class Group extends KTEntity { @@ -270,6 +286,63 @@ class Group extends KTEntity {
270 return true; 286 return true;
271 } 287 }
272 // }}} 288 // }}}
  289 +
  290 + // {{{ addMemberGroup
  291 + function addMemberGroup($oGroup) {
  292 + global $default;
  293 + if ($this->hasMemberGroup($oGroup)) {
  294 + return true;
  295 + }
  296 + $aParams = array(
  297 + "parent_group_id" => $this->getID(),
  298 + "member_group_id" => $oGroup->getID(),
  299 + );
  300 + $res = DBUtil::autoInsert($default->groups_groups_table, $aParams);
  301 + if (PEAR::isError($res)) {
  302 + return $res;
  303 + }
  304 + return true;
  305 + }
  306 + // }}}
  307 + //
  308 + // {{{ removeMemberGroup
  309 + function removeMemberGroup($oGroup) {
  310 + global $default;
  311 + if (!$this->hasMemberGroup($oGroup)) {
  312 + return true;
  313 + }
  314 + $aParams = array(
  315 + "parent_group_id" => $this->getID(),
  316 + "member_group_id" => $oGroup->getID(),
  317 + );
  318 + $res = DBUtil::whereDelete($default->groups_groups_table, $aParams);
  319 + if (PEAR::isError($res)) {
  320 + return $res;
  321 + }
  322 + if ($this->hasMember($oGroup)) {
  323 + return PEAR::raiseError("Tried to remove member from database, apparently successfully, but hasMember thinks they're still members?");
  324 + }
  325 + return true;
  326 + }
  327 + // }}}
  328 +
  329 + // {{{ hasMemberGroup
  330 + function hasMemberGroup($oGroup) {
  331 + global $default;
  332 +
  333 + $sQuery = "SELECT COUNT(*) AS number_of_entries FROM $default->groups_groups_table
  334 + WHERE parent_group_id = ? AND member_group_id = ?";
  335 + $aParams = array($this->getID(), $oGroup->getID());
  336 + $res = (int)DBUtil::getOneResultKey(array($sQuery, $aParams), "number_of_entries");
  337 + if (PEAR::isError($res)) {
  338 + return $res;
  339 + }
  340 + if ($res === 1) {
  341 + return true;
  342 + }
  343 + return false;
  344 + }
  345 + // }}}
273 } 346 }
274 347
275 /** 348 /**