Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 27 | */ |
6a488035 TO |
28 | |
29 | /** | |
30 | * | |
31 | * @package CRM | |
32 | * @copyright U.S. PIRG 2007 | |
6a488035 | 33 | */ |
5390f75f | 34 | class CRM_Contact_BAO_GroupNesting extends CRM_Contact_DAO_GroupNesting { |
6a488035 | 35 | |
1237d8d7 | 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 | ||
6a488035 | 58 | /** |
5390f75f | 59 | * Adds a new group nesting record. |
6a488035 | 60 | * |
77c5b619 TO |
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. | |
d84c62c9 | 65 | * |
66 | * @return \CRM_Contact_DAO_GroupNesting | |
6a488035 | 67 | */ |
00be9182 | 68 | public static function add($parentID, $childID) { |
6a488035 | 69 | $dao = new CRM_Contact_DAO_GroupNesting(); |
d84c62c9 | 70 | $dao->child_group_id = $childID; |
71 | $dao->parent_group_id = $parentID; | |
72 | if (!$dao->find(TRUE)) { | |
73 | $dao->save(); | |
74 | } | |
75 | return $dao; | |
6a488035 TO |
76 | } |
77 | ||
78 | /** | |
35e8e592 | 79 | * Removes a child group from it's parent. |
6a488035 | 80 | * |
5390f75f | 81 | * Does not delete child group, just the association between the two |
6a488035 | 82 | * |
35e8e592 | 83 | * @param int $parentID |
84 | * The id of the group to remove the child from. | |
77c5b619 TO |
85 | * @param int $childID |
86 | * The id of the child group being removed. | |
6a488035 | 87 | */ |
5390f75f | 88 | public static function remove($parentID, $childID) { |
6a488035 | 89 | $dao = new CRM_Contact_DAO_GroupNesting(); |
d84c62c9 | 90 | $dao->child_group_id = $childID; |
91 | $dao->parent_group_id = $parentID; | |
92 | if ($dao->find(TRUE)) { | |
93 | $dao->delete(); | |
94 | } | |
6a488035 TO |
95 | } |
96 | ||
97 | /** | |
d84c62c9 | 98 | * Checks whether the association between parent and child is present. |
6a488035 | 99 | * |
35e8e592 | 100 | * @param int $parentID |
77c5b619 | 101 | * The parent id of the association. |
35e8e592 | 102 | * |
103 | * @param int $childID | |
77c5b619 | 104 | * The child id of the association. |
6a488035 | 105 | * |
3bdca100 | 106 | * @return bool |
2b37475d | 107 | * True if association is found, false otherwise. |
6a488035 | 108 | */ |
00be9182 | 109 | public static function isParentChild($parentID, $childID) { |
6a488035 | 110 | $dao = new CRM_Contact_DAO_GroupNesting(); |
d84c62c9 | 111 | $dao->child_group_id = $childID; |
112 | $dao->parent_group_id = $parentID; | |
113 | if ($dao->find()) { | |
6a488035 TO |
114 | return TRUE; |
115 | } | |
116 | return FALSE; | |
117 | } | |
118 | ||
6a488035 | 119 | /** |
d84c62c9 | 120 | * Checks whether groupId has 1 or more parent groups. |
6a488035 | 121 | * |
d84c62c9 | 122 | * @param int $groupId |
77c5b619 | 123 | * The id of the group to check for parent groups. |
6a488035 | 124 | * |
3bdca100 | 125 | * @return bool |
2b37475d | 126 | * True if 1 or more parent groups are found, false otherwise. |
6a488035 | 127 | */ |
00be9182 | 128 | public static function hasParentGroups($groupId) { |
6a488035 TO |
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 | ||
6a488035 TO |
138 | /** |
139 | * Returns array of group ids of child groups of the specified group. | |
140 | * | |
5a4f6742 | 141 | * @param array $groupIds |
77c5b619 | 142 | * An array of valid group ids (passed by reference). |
6a488035 | 143 | * |
2b37475d | 144 | * @return array |
364c80f1 | 145 | * List of groupIds that represent the requested group and its children |
6a488035 | 146 | */ |
00be9182 | 147 | public static function getChildGroupIds($groupIds) { |
6a488035 | 148 | if (!is_array($groupIds)) { |
be2fb01f | 149 | $groupIds = [$groupIds]; |
6a488035 TO |
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); | |
be2fb01f | 154 | $childGroupIds = []; |
6a488035 TO |
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 | * | |
5a4f6742 | 164 | * @param array $groupIds |
77c5b619 | 165 | * An array of valid group ids (passed by reference). |
6a488035 | 166 | * |
2b37475d | 167 | * @return array |
364c80f1 | 168 | * List of groupIds that represent the requested group and its parents |
6a488035 | 169 | */ |
00be9182 | 170 | public static function getParentGroupIds($groupIds) { |
6a488035 | 171 | if (!is_array($groupIds)) { |
be2fb01f | 172 | $groupIds = [$groupIds]; |
6a488035 TO |
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); | |
be2fb01f | 177 | $parentGroupIds = []; |
6a488035 TO |
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 | * | |
5a4f6742 | 187 | * @param array $groupIds |
77c5b619 | 188 | * An array of valid group ids (passed by reference). |
6a488035 | 189 | * |
2a6da8d7 | 190 | * @param bool $includeSelf |
5390f75f | 191 | * |
2b37475d | 192 | * @return array |
364c80f1 | 193 | * List of groupIds that represent the requested group and its descendents |
6a488035 | 194 | */ |
00be9182 | 195 | public static function getDescendentGroupIds($groupIds, $includeSelf = TRUE) { |
6a488035 | 196 | if (!is_array($groupIds)) { |
be2fb01f | 197 | $groupIds = [$groupIds]; |
6a488035 TO |
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); | |
be2fb01f CW |
202 | $tmpGroupIds = []; |
203 | $childGroupIds = []; | |
6a488035 TO |
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 | ||
6a488035 | 220 | } |