| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright U.S. PIRG Education Fund (c) 2007 | |
| 7 | | Licensed to CiviCRM under the Academic Free License version 3.0. | |
| 8 | +--------------------------------------------------------------------+ |
| 9 | | This file is a part of CiviCRM. | |
| 10 | | | |
| 11 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 12 | | under the terms of the GNU Affero General Public License | |
| 13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 14 | | | |
| 15 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 18 | | See the GNU Affero General Public License for more details. | |
| 19 | | | |
| 20 | | You should have received a copy of the GNU Affero General Public | |
| 21 | | License and the CiviCRM Licensing Exception along | |
| 22 | | with this program; if not, contact CiviCRM LLC | |
| 23 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 24 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 26 | +--------------------------------------------------------------------+ |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * |
| 31 | * @package CRM |
| 32 | * @copyright U.S. PIRG 2007 |
| 33 | */ |
| 34 | class CRM_Contact_BAO_GroupNesting extends CRM_Contact_DAO_GroupNesting { |
| 35 | |
| 36 | /** |
| 37 | * Add Dashboard. |
| 38 | * |
| 39 | * @param array $params |
| 40 | * Values. |
| 41 | * |
| 42 | * |
| 43 | * @return object |
| 44 | */ |
| 45 | public static function create($params) { |
| 46 | $hook = empty($params['id']) ? 'create' : 'edit'; |
| 47 | CRM_Utils_Hook::pre($hook, 'GroupNesting', CRM_Utils_Array::value('id', $params), $params); |
| 48 | $dao = new CRM_Contact_BAO_GroupNesting(); |
| 49 | $dao->copyValues($params); |
| 50 | if (empty($params['id'])) { |
| 51 | $dao->find(TRUE); |
| 52 | } |
| 53 | $dao->save(); |
| 54 | CRM_Utils_Hook::post($hook, 'GroupNesting', $dao->id, $dao); |
| 55 | return $dao; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Adds a new group nesting record. |
| 60 | * |
| 61 | * @param int $parentID |
| 62 | * Id of the group to add the child to. |
| 63 | * @param int $childID |
| 64 | * Id of the new child group. |
| 65 | * |
| 66 | * @return \CRM_Contact_DAO_GroupNesting |
| 67 | */ |
| 68 | public static function add($parentID, $childID) { |
| 69 | $dao = new CRM_Contact_DAO_GroupNesting(); |
| 70 | $dao->child_group_id = $childID; |
| 71 | $dao->parent_group_id = $parentID; |
| 72 | if (!$dao->find(TRUE)) { |
| 73 | $dao->save(); |
| 74 | } |
| 75 | return $dao; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Removes a child group from it's parent. |
| 80 | * |
| 81 | * Does not delete child group, just the association between the two |
| 82 | * |
| 83 | * @param int $parentID |
| 84 | * The id of the group to remove the child from. |
| 85 | * @param int $childID |
| 86 | * The id of the child group being removed. |
| 87 | */ |
| 88 | public static function remove($parentID, $childID) { |
| 89 | $dao = new CRM_Contact_DAO_GroupNesting(); |
| 90 | $dao->child_group_id = $childID; |
| 91 | $dao->parent_group_id = $parentID; |
| 92 | if ($dao->find(TRUE)) { |
| 93 | $dao->delete(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Checks whether the association between parent and child is present. |
| 99 | * |
| 100 | * @param int $parentID |
| 101 | * The parent id of the association. |
| 102 | * |
| 103 | * @param int $childID |
| 104 | * The child id of the association. |
| 105 | * |
| 106 | * @return bool |
| 107 | * True if association is found, false otherwise. |
| 108 | */ |
| 109 | public static function isParentChild($parentID, $childID) { |
| 110 | $dao = new CRM_Contact_DAO_GroupNesting(); |
| 111 | $dao->child_group_id = $childID; |
| 112 | $dao->parent_group_id = $parentID; |
| 113 | if ($dao->find()) { |
| 114 | return TRUE; |
| 115 | } |
| 116 | return FALSE; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Checks whether groupId has 1 or more parent groups. |
| 121 | * |
| 122 | * @param int $groupId |
| 123 | * The id of the group to check for parent groups. |
| 124 | * |
| 125 | * @return bool |
| 126 | * True if 1 or more parent groups are found, false otherwise. |
| 127 | */ |
| 128 | public static function hasParentGroups($groupId) { |
| 129 | $dao = new CRM_Contact_DAO_GroupNesting(); |
| 130 | $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id = $groupId LIMIT 1"; |
| 131 | $dao->query($query); |
| 132 | if ($dao->fetch()) { |
| 133 | return TRUE; |
| 134 | } |
| 135 | return FALSE; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns array of group ids of child groups of the specified group. |
| 140 | * |
| 141 | * @param array $groupIds |
| 142 | * An array of valid group ids (passed by reference). |
| 143 | * |
| 144 | * @return array |
| 145 | * List of groupIds that represent the requested group and its children |
| 146 | */ |
| 147 | public static function getChildGroupIds($groupIds) { |
| 148 | if (!is_array($groupIds)) { |
| 149 | $groupIds = array($groupIds); |
| 150 | } |
| 151 | $dao = new CRM_Contact_DAO_GroupNesting(); |
| 152 | $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")"; |
| 153 | $dao->query($query); |
| 154 | $childGroupIds = array(); |
| 155 | while ($dao->fetch()) { |
| 156 | $childGroupIds[] = $dao->child_group_id; |
| 157 | } |
| 158 | return $childGroupIds; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns array of group ids of parent groups of the specified group. |
| 163 | * |
| 164 | * @param array $groupIds |
| 165 | * An array of valid group ids (passed by reference). |
| 166 | * |
| 167 | * @return array |
| 168 | * List of groupIds that represent the requested group and its parents |
| 169 | */ |
| 170 | public static function getParentGroupIds($groupIds) { |
| 171 | if (!is_array($groupIds)) { |
| 172 | $groupIds = array($groupIds); |
| 173 | } |
| 174 | $dao = new CRM_Contact_DAO_GroupNesting(); |
| 175 | $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")"; |
| 176 | $dao->query($query); |
| 177 | $parentGroupIds = array(); |
| 178 | while ($dao->fetch()) { |
| 179 | $parentGroupIds[] = $dao->parent_group_id; |
| 180 | } |
| 181 | return $parentGroupIds; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Returns array of group ids of descendent groups of the specified group. |
| 186 | * |
| 187 | * @param array $groupIds |
| 188 | * An array of valid group ids (passed by reference). |
| 189 | * |
| 190 | * @param bool $includeSelf |
| 191 | * |
| 192 | * @return array |
| 193 | * List of groupIds that represent the requested group and its descendents |
| 194 | */ |
| 195 | public static function getDescendentGroupIds($groupIds, $includeSelf = TRUE) { |
| 196 | if (!is_array($groupIds)) { |
| 197 | $groupIds = array($groupIds); |
| 198 | } |
| 199 | $dao = new CRM_Contact_DAO_GroupNesting(); |
| 200 | $query = "SELECT child_group_id, parent_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")"; |
| 201 | $dao->query($query); |
| 202 | $tmpGroupIds = array(); |
| 203 | $childGroupIds = array(); |
| 204 | if ($includeSelf) { |
| 205 | $childGroupIds = $groupIds; |
| 206 | } |
| 207 | while ($dao->fetch()) { |
| 208 | // make sure we're not following any cyclical references |
| 209 | if (!array_key_exists($dao->parent_group_id, $childGroupIds) && $dao->child_group_id != $groupIds[0]) { |
| 210 | $tmpGroupIds[] = $dao->child_group_id; |
| 211 | } |
| 212 | } |
| 213 | if (!empty($tmpGroupIds)) { |
| 214 | $newChildGroupIds = self::getDescendentGroupIds($tmpGroupIds); |
| 215 | $childGroupIds = array_merge($childGroupIds, $newChildGroupIds); |
| 216 | } |
| 217 | return $childGroupIds; |
| 218 | } |
| 219 | |
| 220 | } |