3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Contact_BAO_GroupNestingCache
{
22 * @throws \CRM_Core_Exception
24 public static function update() {
25 // lets build the tree in memory first
28 SELECT n.child_group_id as child ,
29 n.parent_group_id as parent
30 FROM civicrm_group_nesting n,
33 WHERE n.child_group_id = gc.id
34 AND n.parent_group_id = gp.id
37 $dao = CRM_Core_DAO
::executeQuery($sql);
40 while ($dao->fetch()) {
41 if (!array_key_exists($dao->child
, $tree)) {
42 $tree[$dao->child
] = [
48 if (!array_key_exists($dao->parent
, $tree)) {
49 $tree[$dao->parent
] = [
55 $tree[$dao->child
]['parents'][] = $dao->parent
;
56 $tree[$dao->parent
]['children'][] = $dao->child
;
59 if (self
::checkCyclicGraph($tree)) {
60 throw new CRM_Core_Exception(ts('We detected a cycle which we can\'t handle. aborting'));
63 // first reset the current cache entries
69 CRM_Core_DAO
::executeQuery($sql);
72 foreach (array_keys($tree) as $id) {
73 $parents = implode(',', $tree[$id]['parents']);
74 $children = implode(',', $tree[$id]['children']);
75 $parents = $parents == NULL ?
'null' : "'$parents'";
76 $children = $children == NULL ?
'null' : "'$children'";
79 SET parents = $parents ,
83 CRM_Core_DAO
::executeQuery($sql);
86 // this tree stuff is quite useful, so lets store it in the cache
87 Civi
::cache('groups')->set('nestable tree hierarchy', $tree);
95 public static function checkCyclicGraph(&$tree) {
96 // lets keep this simple, we should probably use a graph algorithm here at some stage
98 // foreach group that has a parent or a child, ensure that
99 // the ancestors and descendants dont intersect
100 foreach ($tree as $id => $dontCare) {
101 if (self
::isCyclic($tree, $id)) {
115 public static function isCyclic(&$tree, $id) {
116 $parents = $children = [];
117 self
::getAll($parent, $tree, $id, 'parents');
118 self
::getAll($child, $tree, $id, 'children');
120 $one = array_intersect($parents, $children);
121 $two = array_intersect($children, $parents);
125 CRM_Core_Error
::debug($id, $tree);
126 CRM_Core_Error
::debug($id, $one);
127 CRM_Core_Error
::debug($id, $two);
135 * @param array $groups
138 * @throws \CRM_Core_Exception
140 public static function getPotentialCandidates($id, &$groups) {
141 $tree = Civi
::cache('groups')->get('nestable tree hierarchy');
143 if ($tree === NULL) {
145 $tree = Civi
::cache('groups')->get('nestable tree hierarchy');
148 $potential = $groups;
150 // remove all descendants
151 self
::invalidate($potential, $tree, $id, 'children');
153 // remove all ancestors
154 self
::invalidate($potential, $tree, $id, 'parents');
156 return array_keys($potential);
165 public static function invalidate(&$potential, &$tree, $id, $token) {
166 unset($potential[$id]);
168 if (!isset($tree[$id]) ||
169 empty($tree[$id][$token])
174 foreach ($tree[$id][$token] as $tokenID) {
175 self
::invalidate($potential, $tree, $tokenID, $token);
185 public static function getAll(&$all, &$tree, $id, $token) {
186 // if seen before, dont do anything
187 if (isset($all[$id])) {
192 if (!isset($tree[$id]) ||
193 empty($tree[$id][$token])
198 foreach ($tree[$id][$token] as $tokenID) {
199 self
::getAll($all, $tree, $tokenID, $token);
206 * @throws \CRM_Core_Exception
208 public static function json() {
209 $tree = Civi
::cache('groups')->get('nestable tree hierarchy');
211 if ($tree === NULL) {
213 $tree = Civi
::cache('groups')->get('nestable tree hierarchy');
216 // get all the groups
217 $groups = CRM_Core_PseudoConstant
::group();
219 foreach ($groups as $id => $name) {
220 $string = "id:'$id', name:'$name'";
221 if (isset($tree[$id])) {
223 if (!empty($tree[$id]['children'])) {
224 foreach ($tree[$id]['children'] as $child) {
225 $children[] = "{_reference:'$child'}";
227 $children = implode(',', $children);
228 $string .= ", children:[$children]";
229 if (empty($tree[$id]['parents'])) {
230 $string .= ", type:'rootGroup'";
233 $string .= ", type:'middleGroup'";
237 $string .= ", type:'leafGroup'";
241 $string .= ", children:[], type:'rootGroup'";
243 $values[] = "{ $string }";
246 $items = implode(",\n", $values);