Merge in 5.20
[civicrm-core.git] / CRM / Contact / BAO / GroupNestingCache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contact_BAO_GroupNestingCache {
18
19 /**
20 * Update cache.
21 *
22 * @throws \Exception
23 */
24 public static function update() {
25 // lets build the tree in memory first
26
27 $sql = "
28 SELECT n.child_group_id as child ,
29 n.parent_group_id as parent
30 FROM civicrm_group_nesting n,
31 civicrm_group gc,
32 civicrm_group gp
33 WHERE n.child_group_id = gc.id
34 AND n.parent_group_id = gp.id
35 ";
36
37 $dao = CRM_Core_DAO::executeQuery($sql);
38
39 $tree = [];
40 while ($dao->fetch()) {
41 if (!array_key_exists($dao->child, $tree)) {
42 $tree[$dao->child] = [
43 'children' => [],
44 'parents' => [],
45 ];
46 }
47
48 if (!array_key_exists($dao->parent, $tree)) {
49 $tree[$dao->parent] = [
50 'children' => [],
51 'parents' => [],
52 ];
53 }
54
55 $tree[$dao->child]['parents'][] = $dao->parent;
56 $tree[$dao->parent]['children'][] = $dao->child;
57 }
58
59 if (self::checkCyclicGraph($tree)) {
60 CRM_Core_Error::fatal(ts("We detected a cycle which we can't handle. aborting"));
61 }
62
63 // first reset the current cache entries
64 $sql = "
65 UPDATE civicrm_group
66 SET parents = null,
67 children = null
68 ";
69 CRM_Core_DAO::executeQuery($sql);
70
71 $values = [];
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'";
77 $sql = "
78 UPDATE civicrm_group
79 SET parents = $parents ,
80 children = $children
81 WHERE id = $id
82 ";
83 CRM_Core_DAO::executeQuery($sql);
84 }
85
86 // this tree stuff is quite useful, so lets store it in the cache
87 Civi::cache('groups')->set('nestable tree hierarchy', $tree);
88 }
89
90 /**
91 * @param $tree
92 *
93 * @return bool
94 */
95 public static function checkCyclicGraph(&$tree) {
96 // lets keep this simple, we should probably use a graph algorithm here at some stage
97
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)) {
102 return TRUE;
103 }
104 }
105
106 return FALSE;
107 }
108
109 /**
110 * @param $tree
111 * @param int $id
112 *
113 * @return bool
114 */
115 public static function isCyclic(&$tree, $id) {
116 $parents = $children = [];
117 self::getAll($parent, $tree, $id, 'parents');
118 self::getAll($child, $tree, $id, 'children');
119
120 $one = array_intersect($parents, $children);
121 $two = array_intersect($children, $parents);
122 if (!empty($one) ||
123 !empty($two)
124 ) {
125 CRM_Core_Error::debug($id, $tree);
126 CRM_Core_Error::debug($id, $one);
127 CRM_Core_Error::debug($id, $two);
128 return TRUE;
129 }
130 return FALSE;
131 }
132
133 /**
134 * @param int $id
135 * @param $groups
136 *
137 * @return array
138 */
139 public static function getPotentialCandidates($id, &$groups) {
140 $tree = Civi::cache('groups')->get('nestable tree hierarchy');
141
142 if ($tree === NULL) {
143 self::update();
144 $tree = Civi::cache('groups')->get('nestable tree hierarchy');
145 }
146
147 $potential = $groups;
148
149 // remove all descendants
150 self::invalidate($potential, $tree, $id, 'children');
151
152 // remove all ancestors
153 self::invalidate($potential, $tree, $id, 'parents');
154
155 return array_keys($potential);
156 }
157
158 /**
159 * @param $potential
160 * @param $tree
161 * @param int $id
162 * @param $token
163 */
164 public static function invalidate(&$potential, &$tree, $id, $token) {
165 unset($potential[$id]);
166
167 if (!isset($tree[$id]) ||
168 empty($tree[$id][$token])
169 ) {
170 return;
171 }
172
173 foreach ($tree[$id][$token] as $tokenID) {
174 self::invalidate($potential, $tree, $tokenID, $token);
175 }
176 }
177
178 /**
179 * @param $all
180 * @param $tree
181 * @param int $id
182 * @param $token
183 */
184 public static function getAll(&$all, &$tree, $id, $token) {
185 // if seen before, dont do anything
186 if (isset($all[$id])) {
187 return;
188 }
189
190 $all[$id] = 1;
191 if (!isset($tree[$id]) ||
192 empty($tree[$id][$token])
193 ) {
194 return;
195 }
196
197 foreach ($tree[$id][$token] as $tokenID) {
198 self::getAll($all, $tree, $tokenID, $token);
199 }
200 }
201
202 /**
203 * @return string
204 */
205 public static function json() {
206 $tree = Civi::cache('groups')->get('nestable tree hierarchy');
207
208 if ($tree === NULL) {
209 self::update();
210 $tree = Civi::cache('groups')->get('nestable tree hierarchy');
211 }
212
213 // get all the groups
214 $groups = CRM_Core_PseudoConstant::group();
215
216 foreach ($groups as $id => $name) {
217 $string = "id:'$id', name:'$name'";
218 if (isset($tree[$id])) {
219 $children = [];
220 if (!empty($tree[$id]['children'])) {
221 foreach ($tree[$id]['children'] as $child) {
222 $children[] = "{_reference:'$child'}";
223 }
224 $children = implode(',', $children);
225 $string .= ", children:[$children]";
226 if (empty($tree[$id]['parents'])) {
227 $string .= ", type:'rootGroup'";
228 }
229 else {
230 $string .= ", type:'middleGroup'";
231 }
232 }
233 else {
234 $string .= ", type:'leafGroup'";
235 }
236 }
237 else {
238 $string .= ", children:[], type:'rootGroup'";
239 }
240 $values[] = "{ $string }";
241 }
242
243 $items = implode(",\n", $values);
244 $json = "{
245 identifier:'id',
246 label:'name',
247 items:[ $items ]
248 }";
249 return $json;
250 }
251
252 }