3 +--------------------------------------------------------------------+
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. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright U.S. PIRG 2007
34 class CRM_Contact_BAO_GroupNesting
extends CRM_Contact_DAO_GroupNesting
{
39 * @param array $params
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'])) {
54 CRM_Utils_Hook
::post($hook, 'GroupNesting', $dao->id
, $dao);
59 * Adds a new group nesting record.
61 * @param int $parentID
62 * Id of the group to add the child to.
64 * Id of the new child group.
66 * @return \CRM_Contact_DAO_GroupNesting
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)) {
79 * Removes a child group from it's parent.
81 * Does not delete child group, just the association between the two
83 * @param int $parentID
84 * The id of the group to remove the child from.
86 * The id of the child group being removed.
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)) {
98 * Checks whether the association between parent and child is present.
100 * @param int $parentID
101 * The parent id of the association.
103 * @param int $childID
104 * The child id of the association.
107 * True if association is found, false otherwise.
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;
120 * Checks whether groupId has 1 or more parent groups.
122 * @param int $groupId
123 * The id of the group to check for parent groups.
126 * True if 1 or more parent groups are found, false otherwise.
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";
139 * Returns array of group ids of child groups of the specified group.
141 * @param array $groupIds
142 * An array of valid group ids (passed by reference).
145 * List of groupIds that represent the requested group and its children
147 public static function getChildGroupIds($groupIds) {
148 if (!is_array($groupIds)) {
149 $groupIds = [$groupIds];
151 $dao = new CRM_Contact_DAO_GroupNesting();
152 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
155 while ($dao->fetch()) {
156 $childGroupIds[] = $dao->child_group_id
;
158 return $childGroupIds;
162 * Returns array of group ids of parent groups of the specified group.
164 * @param array $groupIds
165 * An array of valid group ids (passed by reference).
168 * List of groupIds that represent the requested group and its parents
170 public static function getParentGroupIds($groupIds) {
171 if (!is_array($groupIds)) {
172 $groupIds = [$groupIds];
174 $dao = new CRM_Contact_DAO_GroupNesting();
175 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
177 $parentGroupIds = [];
178 while ($dao->fetch()) {
179 $parentGroupIds[] = $dao->parent_group_id
;
181 return $parentGroupIds;
185 * Returns array of group ids of descendent groups of the specified group.
187 * @param array $groupIds
188 * An array of valid group ids (passed by reference).
190 * @param bool $includeSelf
193 * List of groupIds that represent the requested group and its descendents
195 public static function getDescendentGroupIds($groupIds, $includeSelf = TRUE) {
196 if (!is_array($groupIds)) {
197 $groupIds = [$groupIds];
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) . ")";
205 $childGroupIds = $groupIds;
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
;
213 if (!empty($tmpGroupIds)) {
214 $newChildGroupIds = self
::getDescendentGroupIds($tmpGroupIds);
215 $childGroupIds = array_merge($childGroupIds, $newChildGroupIds);
217 return $childGroupIds;