Commit 2fd6648349a6e255cb91c381f4d4da6b7e15a4ec
1 parent
24a1f3e8
GroupUtil provides high-level Group functions, as well as providing for
other common Group-related needs. git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3410 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
199 additions
and
0 deletions
lib/groups/GroupUtil.php
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * $Id$ | ||
| 4 | + * | ||
| 5 | + * Utility functions regarding groups and membership | ||
| 6 | + * | ||
| 7 | + * Copyright (c) 2005 Jam Warehouse http://www.jamwarehouse.com | ||
| 8 | + * | ||
| 9 | + * This program is free software; you can redistribute it and/or modify | ||
| 10 | + * it under the terms of the GNU General Public License as published by | ||
| 11 | + * the Free Software Foundation; either version 2 of the License, or | ||
| 12 | + * (at your option) any later version. | ||
| 13 | + * | ||
| 14 | + * This program is distributed in the hope that it will be useful, | ||
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | + * GNU General Public License for more details. | ||
| 18 | + * | ||
| 19 | + * You should have received a copy of the GNU General Public License | ||
| 20 | + * along with this program; if not, write to the Free Software | ||
| 21 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 22 | + * | ||
| 23 | + * @version $Revision$ | ||
| 24 | + * @author Neil Blakey-Milner, Jam Warehouse (Pty) Ltd, South Africa | ||
| 25 | + */ | ||
| 26 | + | ||
| 27 | +require_once(KT_LIB_DIR . "/groups/Group.inc"); | ||
| 28 | + | ||
| 29 | +// {{{ GroupUtil | ||
| 30 | +class GroupUtil { | ||
| 31 | + // {{{ filterCyclicalGroups | ||
| 32 | + /** | ||
| 33 | + * This utility function takes a group whose membership is being | ||
| 34 | + * considered, and a dictionary with group ids as keys and a list of | ||
| 35 | + * their member groups as values. | ||
| 36 | + * | ||
| 37 | + * $aGroupMembership = array( | ||
| 38 | + * 1 => array(2, 3, 4), | ||
| 39 | + * 2 => array(5, 3), | ||
| 40 | + * 3 => array(5), | ||
| 41 | + * } | ||
| 42 | + * | ||
| 43 | + * This function returns a list of group ids from the group | ||
| 44 | + * membership array that may safely to added to the original group. | ||
| 45 | + */ | ||
| 46 | + // STATIC | ||
| 47 | + function filterCyclicalGroups ($iTargetGroupID, $aGroupMemberships) { | ||
| 48 | + $aReturnGroupIDs = array(); | ||
| 49 | + | ||
| 50 | + // PHP5: clone/copy | ||
| 51 | + $aLocalGroupMemberships = $aGroupMemberships; | ||
| 52 | + | ||
| 53 | + // In case we get given ourself, we know we can't add ourselves | ||
| 54 | + // to each other. | ||
| 55 | + unset($aLocalGroupMemberships[$iTargetGroupID]); | ||
| 56 | + | ||
| 57 | + // Groups that have no group members can safely be added to the | ||
| 58 | + // group. Simplifies debugging of later code. | ||
| 59 | + foreach ($aLocalGroupMemberships as $k => $v) { | ||
| 60 | + if (is_null($v) || (!count($v))) { | ||
| 61 | + unset($aLocalGroupMemberships[$k]); | ||
| 62 | + $aReturnGroupIDs[] = $k; | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + $aBadGroupIDs = GroupUtil::listBadGroups($iTargetGroupID, $aLocalGroupMemberships); | ||
| 67 | + | ||
| 68 | + foreach ($aLocalGroupMemberships as $k => $v) { | ||
| 69 | + if (!in_array($k, $aBadGroupIDs)) { | ||
| 70 | + $aReturnGroupIDs[] = $k; | ||
| 71 | + } | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + return $aReturnGroupIDs; | ||
| 75 | + } | ||
| 76 | + // }}} | ||
| 77 | + | ||
| 78 | + // {{{ | ||
| 79 | + /** | ||
| 80 | + * This utility function takes a group whose membership is being | ||
| 81 | + * considered, and a dictionary with group ids as keys and a list of | ||
| 82 | + * their member groups as values. | ||
| 83 | + * | ||
| 84 | + * $aGroupMembership = array( | ||
| 85 | + * 1 => array(2, 3, 4), | ||
| 86 | + * 2 => array(5, 3), | ||
| 87 | + * 3 => array(5), | ||
| 88 | + * } | ||
| 89 | + * | ||
| 90 | + * This function returns a list of group ids from the group | ||
| 91 | + * membership array that can't be safely to added to the original | ||
| 92 | + * group. | ||
| 93 | + */ | ||
| 94 | + // STATIC | ||
| 95 | + function listBadGroups ($iTargetGroupID, $aGroupMemberships) { | ||
| 96 | + // PHP5: clone/copy | ||
| 97 | + $aLocalGroupMemberships = $aGroupMemberships; | ||
| 98 | + | ||
| 99 | + // Two ways to do this - either expand the list we're given of | ||
| 100 | + // immediate children to all children, OR mark group IDs as bad | ||
| 101 | + // (starting with the group we're planning to add the groups | ||
| 102 | + // into), and cycle while we're finding new bad groups. | ||
| 103 | + // | ||
| 104 | + // Marking bad group IDs seems like the easier-to-understand | ||
| 105 | + // option. | ||
| 106 | + | ||
| 107 | + $aBadGroupIDs = array($iTargetGroupID); | ||
| 108 | + $aLastBadGroupCount = 0; | ||
| 109 | + | ||
| 110 | + // While we've discovered new bad groups... | ||
| 111 | + while (count($aBadGroupIDs) > $aLastBadGroupCount) { | ||
| 112 | + $aLastBadGroupCount = count($aBadGroupIDs); | ||
| 113 | + foreach ($aLocalGroupMemberships as $iThisGroupID => $aGroupIDs) { | ||
| 114 | + | ||
| 115 | + // This check isn't strictly necessary, as the groups | ||
| 116 | + // should be removed from the local list of groups in | ||
| 117 | + // the later check, but who knows whether one can unset | ||
| 118 | + // array keys while iterating over the list. | ||
| 119 | + | ||
| 120 | + if (in_array($iThisGroupID, $aBadGroupIDs)) { | ||
| 121 | + // print "Not considering $iThisGroupID, it is in bad group list: " . print_r($aBadGroupIDs, true); | ||
| 122 | + unset($aLocalGroupMemberships[$iThisGroupID]); | ||
| 123 | + continue; | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + foreach ($aGroupIDs as $k) { | ||
| 127 | + if (in_array($k, $aBadGroupIDs)) { | ||
| 128 | + // print "Adding $iThisGroupID to bad list, because it contains $k, which is in bad group list: " . print_r($aBadGroupIDs, true); | ||
| 129 | + unset($aLocalGroupMemberships[$iThisGroupID]); | ||
| 130 | + $aBadGroupIDs[] = $iThisGroupID; | ||
| 131 | + break; | ||
| 132 | + } | ||
| 133 | + } | ||
| 134 | + } | ||
| 135 | + } | ||
| 136 | + return $aBadGroupIDs; | ||
| 137 | + } | ||
| 138 | + // }}} | ||
| 139 | + | ||
| 140 | + // {{{ addGroup | ||
| 141 | + function add($aGroupDetails) { | ||
| 142 | + $aDefaultDetails = array( | ||
| 143 | + "is_unit_admin" => false, | ||
| 144 | + "is_system_admin" => false, | ||
| 145 | + ); | ||
| 146 | + $aDetails = array_merge($aDefaultDetails, $aGroupDetails); | ||
| 147 | + if (is_null(KTUtil::arrayGet($aDetails, "name"))) { | ||
| 148 | + return PEAR::raiseError("Needed key name is not provided"); | ||
| 149 | + } | ||
| 150 | + $oGroup = new Group($aDetails["name"], | ||
| 151 | + $aDetails["is_unit_admin"], | ||
| 152 | + $aDetails["is_system_admin"]); | ||
| 153 | + $ret = $oGroup->create(); | ||
| 154 | + if ($ret === false) { | ||
| 155 | + return PEAR::raiseError("Legacy error creating group, may be: " . $_SESSION["errorMessage"]); | ||
| 156 | + } | ||
| 157 | + if (PEAR::isError($ret)) { | ||
| 158 | + return $ret; | ||
| 159 | + } | ||
| 160 | + if ($ret !== true) { | ||
| 161 | + return PEAR::raiseError("Non-true and non-error return value"); | ||
| 162 | + } | ||
| 163 | + return true; | ||
| 164 | + } | ||
| 165 | + // }}} | ||
| 166 | + | ||
| 167 | + // {{{ list | ||
| 168 | + function listGroups($aGivenOptions = null) { | ||
| 169 | + if (is_null($aGivenOptions)) { | ||
| 170 | + $aGivenOptions = array(); | ||
| 171 | + } | ||
| 172 | + $aDefaultOptions = array( | ||
| 173 | + //"active" => true, | ||
| 174 | + ); | ||
| 175 | + $aOptions = array_merge($aDefaultOptions, $aGivenOptions); | ||
| 176 | + | ||
| 177 | + $aWhere = array(); | ||
| 178 | + /* if ($aOptions["active"] === true) { | ||
| 179 | + $aWhere[] = array("active = ?", true); | ||
| 180 | + } */ | ||
| 181 | + | ||
| 182 | + $sWhere = KTUtil::whereToString($aWhere); | ||
| 183 | + | ||
| 184 | + return Group::getList($sWhere); | ||
| 185 | + } | ||
| 186 | + // }}} | ||
| 187 | + | ||
| 188 | + // {{{ | ||
| 189 | + function getNameForID($id) { | ||
| 190 | + global $default; | ||
| 191 | + $sName = lookupField($default->groups_table, "name", "id", $id); | ||
| 192 | + return $sName; | ||
| 193 | + } | ||
| 194 | + // }}} | ||
| 195 | + | ||
| 196 | +} | ||
| 197 | +// }}} | ||
| 198 | + | ||
| 199 | +?> |