Merge pull request #9225 from colemanw/CRM-19338
[civicrm-core.git] / CRM / Contact / BAO / GroupNesting.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
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 34class CRM_Contact_BAO_GroupNesting extends CRM_Contact_DAO_GroupNesting {
6a488035
TO
35
36 /**
5390f75f 37 * Adds a new group nesting record.
6a488035 38 *
77c5b619
TO
39 * @param int $parentID
40 * Id of the group to add the child to.
41 * @param int $childID
42 * Id of the new child group.
d84c62c9 43 *
44 * @return \CRM_Contact_DAO_GroupNesting
6a488035 45 */
00be9182 46 public static function add($parentID, $childID) {
6a488035 47 $dao = new CRM_Contact_DAO_GroupNesting();
d84c62c9 48 $dao->child_group_id = $childID;
49 $dao->parent_group_id = $parentID;
50 if (!$dao->find(TRUE)) {
51 $dao->save();
52 }
53 return $dao;
6a488035
TO
54 }
55
56 /**
35e8e592 57 * Removes a child group from it's parent.
6a488035 58 *
5390f75f 59 * Does not delete child group, just the association between the two
6a488035 60 *
35e8e592 61 * @param int $parentID
62 * The id of the group to remove the child from.
77c5b619
TO
63 * @param int $childID
64 * The id of the child group being removed.
6a488035 65 */
5390f75f 66 public static function remove($parentID, $childID) {
6a488035 67 $dao = new CRM_Contact_DAO_GroupNesting();
d84c62c9 68 $dao->child_group_id = $childID;
69 $dao->parent_group_id = $parentID;
70 if ($dao->find(TRUE)) {
71 $dao->delete();
72 }
6a488035
TO
73 }
74
75 /**
d84c62c9 76 * Checks whether the association between parent and child is present.
6a488035 77 *
35e8e592 78 * @param int $parentID
77c5b619 79 * The parent id of the association.
35e8e592 80 *
81 * @param int $childID
77c5b619 82 * The child id of the association.
6a488035 83 *
3bdca100 84 * @return bool
2b37475d 85 * True if association is found, false otherwise.
6a488035 86 */
00be9182 87 public static function isParentChild($parentID, $childID) {
6a488035 88 $dao = new CRM_Contact_DAO_GroupNesting();
d84c62c9 89 $dao->child_group_id = $childID;
90 $dao->parent_group_id = $parentID;
91 if ($dao->find()) {
6a488035
TO
92 return TRUE;
93 }
94 return FALSE;
95 }
96
6a488035 97 /**
d84c62c9 98 * Checks whether groupId has 1 or more parent groups.
6a488035 99 *
d84c62c9 100 * @param int $groupId
77c5b619 101 * The id of the group to check for parent groups.
6a488035 102 *
3bdca100 103 * @return bool
2b37475d 104 * True if 1 or more parent groups are found, false otherwise.
6a488035 105 */
00be9182 106 public static function hasParentGroups($groupId) {
6a488035
TO
107 $dao = new CRM_Contact_DAO_GroupNesting();
108 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id = $groupId LIMIT 1";
109 $dao->query($query);
110 if ($dao->fetch()) {
111 return TRUE;
112 }
113 return FALSE;
114 }
115
6a488035
TO
116 /**
117 * Returns array of group ids of child groups of the specified group.
118 *
5a4f6742 119 * @param array $groupIds
77c5b619 120 * An array of valid group ids (passed by reference).
6a488035 121 *
2b37475d 122 * @return array
364c80f1 123 * List of groupIds that represent the requested group and its children
6a488035 124 */
00be9182 125 public static function getChildGroupIds($groupIds) {
6a488035
TO
126 if (!is_array($groupIds)) {
127 $groupIds = array($groupIds);
128 }
129 $dao = new CRM_Contact_DAO_GroupNesting();
130 $query = "SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
131 $dao->query($query);
132 $childGroupIds = array();
133 while ($dao->fetch()) {
134 $childGroupIds[] = $dao->child_group_id;
135 }
136 return $childGroupIds;
137 }
138
139 /**
140 * Returns array of group ids of parent groups of the specified group.
141 *
5a4f6742 142 * @param array $groupIds
77c5b619 143 * An array of valid group ids (passed by reference).
6a488035 144 *
2b37475d 145 * @return array
364c80f1 146 * List of groupIds that represent the requested group and its parents
6a488035 147 */
00be9182 148 public static function getParentGroupIds($groupIds) {
6a488035
TO
149 if (!is_array($groupIds)) {
150 $groupIds = array($groupIds);
151 }
152 $dao = new CRM_Contact_DAO_GroupNesting();
153 $query = "SELECT parent_group_id FROM civicrm_group_nesting WHERE child_group_id IN (" . implode(',', $groupIds) . ")";
154 $dao->query($query);
155 $parentGroupIds = array();
156 while ($dao->fetch()) {
157 $parentGroupIds[] = $dao->parent_group_id;
158 }
159 return $parentGroupIds;
160 }
161
162 /**
163 * Returns array of group ids of descendent groups of the specified group.
164 *
5a4f6742 165 * @param array $groupIds
77c5b619 166 * An array of valid group ids (passed by reference).
6a488035 167 *
2a6da8d7 168 * @param bool $includeSelf
5390f75f 169 *
2b37475d 170 * @return array
364c80f1 171 * List of groupIds that represent the requested group and its descendents
6a488035 172 */
00be9182 173 public static function getDescendentGroupIds($groupIds, $includeSelf = TRUE) {
6a488035
TO
174 if (!is_array($groupIds)) {
175 $groupIds = array($groupIds);
176 }
177 $dao = new CRM_Contact_DAO_GroupNesting();
178 $query = "SELECT child_group_id, parent_group_id FROM civicrm_group_nesting WHERE parent_group_id IN (" . implode(',', $groupIds) . ")";
179 $dao->query($query);
180 $tmpGroupIds = array();
181 $childGroupIds = array();
182 if ($includeSelf) {
183 $childGroupIds = $groupIds;
184 }
185 while ($dao->fetch()) {
186 // make sure we're not following any cyclical references
187 if (!array_key_exists($dao->parent_group_id, $childGroupIds) && $dao->child_group_id != $groupIds[0]) {
188 $tmpGroupIds[] = $dao->child_group_id;
189 }
190 }
191 if (!empty($tmpGroupIds)) {
192 $newChildGroupIds = self::getDescendentGroupIds($tmpGroupIds);
193 $childGroupIds = array_merge($childGroupIds, $newChildGroupIds);
194 }
195 return $childGroupIds;
196 }
197
6a488035 198}