Commit aced656ee3c0f6bc77ceaf6100b40736d2bd5e13

Authored by Neil Blakey-Milner
1 parent d0243769

Nested group utility functions.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3540 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 97 additions and 0 deletions
lib/groups/GroupUtil.php
... ... @@ -213,6 +213,103 @@ class GroupUtil {
213 213 return $aGroups;
214 214 }
215 215 // }}}
  216 +
  217 + function _invertGroupArray($aGroupArray) {
  218 + $aRet = array();
  219 + foreach ($aGroupArray as $k => $aArray) {
  220 + foreach ($aArray as $v) {
  221 + $aRet[$v] = KTUtil::arrayGet($aRet, $v, array());
  222 + $aRet[$v][] = $k;
  223 + }
  224 + }
  225 + return $aRet;
  226 + }
  227 +
  228 + // {{{ _listGroupsIDsForUserExpand
  229 + function _listGroupIDsForUserExpand ($oUser) {
  230 + global $default;
  231 + $aGroupArray = GroupUtil::_invertGroupArray(GroupUtil::buildGroupArray());
  232 + $aDirectGroups = GroupUtil::listGroupsForUser($oUser);
  233 + $sQuery = "SELECT group_id FROM $default->users_groups_table WHERE user_id = ?";
  234 + $aParams = array($oUser->getID());
  235 + $aGroupIDs = DBUtil::getResultArrayKey(array($sQuery, $aParams), "group_id");
  236 + foreach ($aGroupIDs as $iGroupID) {
  237 + $aGroupIDs = array_merge($aGroupIDs, KTUtil::arrayGet($aGroupArray, $iGroupID));
  238 + }
  239 + $aGroupIDs = array_unique($aGroupIDs);
  240 + sort($aGroupIDs);
  241 + return $aGroupIDs;
  242 + }
  243 + // }}}
  244 +
  245 + // {{{ listGroupsForUserExpand
  246 + function listGroupsForUserExpand ($oUser) {
  247 + $aGroupIDs = GroupUtil::_listGroupIDsForUserExpand($oUser);
  248 + foreach ($aGroupIDs as $iGroupID) {
  249 + $oGroup = Group::get($iGroupID);
  250 + if (PEAR::isError($oGroup)) {
  251 + continue;
  252 + }
  253 + if ($oGroup === false) {
  254 + continue;
  255 + }
  256 + $aGroups[] = $oGroup;
  257 + }
  258 + return $aGroups;
  259 + }
  260 + // }}}
  261 +
  262 + // {{{
  263 + function buildGroupArray() {
  264 + global $default;
  265 + $aDirectGroups = array();
  266 + $aGroupMemberships = DBUtil::getResultArray("SELECT parent_group_id, member_group_id FROM $default->groups_groups_table");
  267 + $aGroups =& Group::getList();
  268 + foreach ($aGroups as $oGroup) {
  269 + $aDirectGroups[$oGroup->getID()] = array();
  270 + }
  271 + foreach ($aGroupMemberships as $aRow) {
  272 + $aList = KTUtil::arrayGet($aDirectGroups, $aRow['parent_group_id'], array());
  273 + $aList[] = $aRow['member_group_id'];
  274 + $aDirectGroups[$aRow['parent_group_id']] = $aList;
  275 + }
  276 +
  277 + return GroupUtil::expandGroupArray($aDirectGroups);
  278 + }
  279 + // }}}
  280 +
  281 + // {{{ expandGroupArray
  282 + function expandGroupArray($aDirectGroups) {
  283 + // XXX: PHP5 clone
  284 + $aExpandedGroups = $aDirectGroups;
  285 + $iNum = 0;
  286 + foreach ($aExpandedGroups as $k => $v) {
  287 + $iNum += count($v);
  288 + }
  289 + $iLastNum = 0;
  290 + while ($iNum !== $iLastNum) {
  291 + $iLastNum = $iNum;
  292 +
  293 + foreach ($aExpandedGroups as $k => $v) {
  294 + foreach ($v as $iGroupID) {
  295 + $aStuff = KTUtil::arrayGet($aExpandedGroups, $iGroupID, null);
  296 + if (is_null($aStuff)) {
  297 + continue;
  298 + }
  299 + $v = array_unique(array_merge($v, $aStuff));
  300 + sort($v);
  301 + }
  302 + $aExpandedGroups[$k] = $v;
  303 + }
  304 +
  305 + $iNum = 0;
  306 + foreach ($aExpandedGroups as $k => $v) {
  307 + $iNum += count($v);
  308 + }
  309 + }
  310 + return $aExpandedGroups;
  311 + }
  312 + // }}}
216 313 }
217 314 // }}}
218 315  
... ...