Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
bc77d7c0 TO |
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 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { | |
18 | ||
19 | /** | |
67d19299 | 20 | * Class constructor. |
6a488035 | 21 | */ |
00be9182 | 22 | public function __construct() { |
6a488035 TO |
23 | parent::__construct(); |
24 | } | |
25 | ||
26 | /** | |
fe482240 EM |
27 | * Retrieve DB object based on input parameters. |
28 | * | |
29 | * It also stores all the retrieved values in the default array. | |
6a488035 | 30 | * |
77c5b619 TO |
31 | * @param array $params |
32 | * (reference ) an assoc array of name/value pairs. | |
33 | * @param array $defaults | |
34 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 35 | * |
16b10e64 | 36 | * @return CRM_Contact_BAO_Group |
6a488035 | 37 | */ |
00be9182 | 38 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
39 | $group = new CRM_Contact_DAO_Group(); |
40 | $group->copyValues($params); | |
41 | if ($group->find(TRUE)) { | |
42 | CRM_Core_DAO::storeValues($group, $defaults); | |
43 | return $group; | |
44 | } | |
6a488035 TO |
45 | } |
46 | ||
47 | /** | |
67d19299 | 48 | * Delete the group and all the object that connect to this group. |
49 | * | |
50 | * Incredibly destructive. | |
6a488035 | 51 | * |
67d19299 | 52 | * @param int $id Group id. |
6a488035 | 53 | */ |
00be9182 | 54 | public static function discard($id) { |
29d68d51 | 55 | if (!$id || !is_numeric($id)) { |
56 | throw new CRM_Core_Exception('Invalid group request attempted'); | |
57 | } | |
6a488035 TO |
58 | CRM_Utils_Hook::pre('delete', 'Group', $id, CRM_Core_DAO::$_nullArray); |
59 | ||
60 | $transaction = new CRM_Core_Transaction(); | |
61 | ||
62 | // added for CRM-1631 and CRM-1794 | |
63 | // delete all subscribed mails with the selected group id | |
64 | $subscribe = new CRM_Mailing_Event_DAO_Subscribe(); | |
65 | $subscribe->group_id = $id; | |
66 | $subscribe->delete(); | |
67 | ||
68 | // delete all Subscription records with the selected group id | |
69 | $subHistory = new CRM_Contact_DAO_SubscriptionHistory(); | |
70 | $subHistory->group_id = $id; | |
71 | $subHistory->delete(); | |
72 | ||
73 | // delete all crm_group_contact records with the selected group id | |
74 | $groupContact = new CRM_Contact_DAO_GroupContact(); | |
75 | $groupContact->group_id = $id; | |
76 | $groupContact->delete(); | |
77 | ||
78 | // make all the 'add_to_group_id' field of 'civicrm_uf_group table', pointing to this group, as null | |
be2fb01f | 79 | $params = [1 => [$id, 'Integer']]; |
6a488035 TO |
80 | $query = "UPDATE civicrm_uf_group SET `add_to_group_id`= NULL WHERE `add_to_group_id` = %1"; |
81 | CRM_Core_DAO::executeQuery($query, $params); | |
82 | ||
83 | $query = "UPDATE civicrm_uf_group SET `limit_listings_group_id`= NULL WHERE `limit_listings_group_id` = %1"; | |
84 | CRM_Core_DAO::executeQuery($query, $params); | |
85 | ||
86 | // make sure u delete all the entries from civicrm_mailing_group and civicrm_campaign_group | |
87 | // CRM-6186 | |
88 | $query = "DELETE FROM civicrm_mailing_group where entity_table = 'civicrm_group' AND entity_id = %1"; | |
89 | CRM_Core_DAO::executeQuery($query, $params); | |
90 | ||
91 | $query = "DELETE FROM civicrm_campaign_group where entity_table = 'civicrm_group' AND entity_id = %1"; | |
92 | CRM_Core_DAO::executeQuery($query, $params); | |
93 | ||
94 | $query = "DELETE FROM civicrm_acl_entity_role where entity_table = 'civicrm_group' AND entity_id = %1"; | |
95 | CRM_Core_DAO::executeQuery($query, $params); | |
96 | ||
6a488035 TO |
97 | // delete from group table |
98 | $group = new CRM_Contact_DAO_Group(); | |
99 | $group->id = $id; | |
100 | $group->delete(); | |
101 | ||
102 | $transaction->commit(); | |
103 | ||
104 | CRM_Utils_Hook::post('delete', 'Group', $id, $group); | |
105 | ||
106 | // delete the recently created Group | |
be2fb01f | 107 | $groupRecent = [ |
6a488035 TO |
108 | 'id' => $id, |
109 | 'type' => 'Group', | |
be2fb01f | 110 | ]; |
6a488035 TO |
111 | CRM_Utils_Recent::del($groupRecent); |
112 | } | |
113 | ||
114 | /** | |
115 | * Returns an array of the contacts in the given group. | |
ad37ac8e | 116 | * |
117 | * @param int $id | |
6a488035 | 118 | */ |
00be9182 | 119 | public static function getGroupContacts($id) { |
be2fb01f CW |
120 | $params = [['group', 'IN', [1 => $id], 0, 0]]; |
121 | list($contacts, $_) = CRM_Contact_BAO_Query::apiQuery($params, ['contact_id']); | |
6a488035 TO |
122 | return $contacts; |
123 | } | |
124 | ||
125 | /** | |
fe482240 | 126 | * Get the count of a members in a group with the specific status. |
6a488035 | 127 | * |
77c5b619 TO |
128 | * @param int $id |
129 | * Group id. | |
3f8d2862 CW |
130 | * @param string $status |
131 | * status of members in group | |
80ad33e7 | 132 | * @param bool $countChildGroups |
6a488035 | 133 | * |
a6c01b45 CW |
134 | * @return int |
135 | * count of members in the group with above status | |
6a488035 | 136 | */ |
00be9182 | 137 | public static function memberCount($id, $status = 'Added', $countChildGroups = FALSE) { |
6a488035 | 138 | $groupContact = new CRM_Contact_DAO_GroupContact(); |
be2fb01f | 139 | $groupIds = [$id]; |
6a488035 TO |
140 | if ($countChildGroups) { |
141 | $groupIds = CRM_Contact_BAO_GroupNesting::getDescendentGroupIds($groupIds); | |
142 | } | |
143 | $count = 0; | |
144 | ||
145 | $contacts = self::getGroupContacts($id); | |
146 | ||
147 | foreach ($groupIds as $groupId) { | |
148 | ||
149 | $groupContacts = self::getGroupContacts($groupId); | |
150 | foreach ($groupContacts as $gcontact) { | |
151 | if ($groupId != $id) { | |
152 | // Loop through main group's contacts | |
153 | // and subtract from the count for each contact which | |
154 | // matches one in the present group, if it is not the | |
155 | // main group | |
156 | foreach ($contacts as $contact) { | |
157 | if ($contact['contact_id'] == $gcontact['contact_id']) { | |
158 | $count--; | |
159 | } | |
160 | } | |
161 | } | |
162 | } | |
163 | $groupContact->group_id = $groupId; | |
164 | if (isset($status)) { | |
165 | $groupContact->status = $status; | |
166 | } | |
167 | $groupContact->_query['condition'] = 'WHERE contact_id NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)'; | |
168 | $count += $groupContact->count(); | |
169 | } | |
170 | return $count; | |
171 | } | |
172 | ||
173 | /** | |
fe482240 | 174 | * Get the list of member for a group id. |
6a488035 | 175 | * |
100fef9d | 176 | * @param int $groupID |
80ad33e7 | 177 | * @param bool $useCache |
4c8b4719 | 178 | * @param int $limit |
179 | * Number to limit to (or 0 for unlimited). | |
80ad33e7 | 180 | * |
a6c01b45 CW |
181 | * @return array |
182 | * this array contains the list of members for this group id | |
6a488035 | 183 | */ |
4c8b4719 | 184 | public static function getMember($groupID, $useCache = TRUE, $limit = 0) { |
be2fb01f CW |
185 | $params = [['group', '=', $groupID, 0, 0]]; |
186 | $returnProperties = ['contact_id']; | |
4c8b4719 | 187 | list($contacts) = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, $limit, $useCache); |
6a488035 | 188 | |
be2fb01f | 189 | $aMembers = []; |
6a488035 TO |
190 | foreach ($contacts as $contact) { |
191 | $aMembers[$contact['contact_id']] = 1; | |
192 | } | |
193 | ||
194 | return $aMembers; | |
195 | } | |
196 | ||
197 | /** | |
198 | * Returns array of group object(s) matching a set of one or Group properties. | |
199 | * | |
77c5b619 TO |
200 | * @param array $params |
201 | * Limits the set of groups returned. | |
202 | * @param array $returnProperties | |
203 | * Which properties should be included in the returned group objects. | |
3f8d2862 CW |
204 | * (member_count should be last element.) |
205 | * @param string $sort | |
206 | * @param int $offset | |
207 | * @param int $rowCount | |
80ad33e7 | 208 | * |
a6c01b45 | 209 | * @return array |
16b10e64 | 210 | * Array of group objects. |
6a488035 | 211 | * |
6a488035 TO |
212 | * |
213 | * @todo other BAO functions that use returnProperties (e.g. Query Objects) receive the array flipped & filled with 1s and | |
214 | * add in essential fields (e.g. id). This should follow a regular pattern like the others | |
215 | */ | |
2da40d21 | 216 | public static function getGroups( |
6a488035 TO |
217 | $params = NULL, |
218 | $returnProperties = NULL, | |
219 | $sort = NULL, | |
220 | $offset = NULL, | |
221 | $rowCount = NULL | |
222 | ) { | |
223 | $dao = new CRM_Contact_DAO_Group(); | |
6ce76461 CW |
224 | if (!isset($params['is_active'])) { |
225 | $dao->is_active = 1; | |
226 | } | |
6a488035 TO |
227 | if ($params) { |
228 | foreach ($params as $k => $v) { | |
229 | if ($k == 'name' || $k == 'title') { | |
230 | $dao->whereAdd($k . ' LIKE "' . CRM_Core_DAO::escapeString($v) . '"'); | |
231 | } | |
6ce76461 CW |
232 | elseif ($k == 'group_type') { |
233 | foreach ((array) $v as $type) { | |
234 | $dao->whereAdd($k . " LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . (int) $type . CRM_Core_DAO::VALUE_SEPARATOR . "%'"); | |
235 | } | |
236 | } | |
6a488035 | 237 | elseif (is_array($v)) { |
6ce76461 CW |
238 | foreach ($v as &$num) { |
239 | $num = (int) $num; | |
240 | } | |
6a488035 TO |
241 | $dao->whereAdd($k . ' IN (' . implode(',', $v) . ')'); |
242 | } | |
243 | else { | |
244 | $dao->$k = $v; | |
245 | } | |
246 | } | |
247 | } | |
248 | ||
249 | if ($offset || $rowCount) { | |
250 | $offset = ($offset > 0) ? $offset : 0; | |
251 | $rowCount = ($rowCount > 0) ? $rowCount : 25; | |
252 | $dao->limit($offset, $rowCount); | |
253 | } | |
254 | ||
255 | if ($sort) { | |
256 | $dao->orderBy($sort); | |
257 | } | |
258 | ||
259 | // return only specific fields if returnproperties are sent | |
260 | if (!empty($returnProperties)) { | |
261 | $dao->selectAdd(); | |
262 | $dao->selectAdd(implode(',', $returnProperties)); | |
263 | } | |
264 | $dao->find(); | |
265 | ||
266 | $flag = $returnProperties && in_array('member_count', $returnProperties) ? 1 : 0; | |
267 | ||
be2fb01f | 268 | $groups = []; |
6a488035 TO |
269 | while ($dao->fetch()) { |
270 | $group = new CRM_Contact_DAO_Group(); | |
271 | if ($flag) { | |
272 | $dao->member_count = CRM_Contact_BAO_Group::memberCount($dao->id); | |
273 | } | |
274 | $groups[] = clone($dao); | |
275 | } | |
276 | return $groups; | |
277 | } | |
278 | ||
279 | /** | |
fe482240 | 280 | * Make sure that the user has permission to access this group. |
6a488035 | 281 | * |
77c5b619 TO |
282 | * @param int $id |
283 | * The id of the object. | |
addbec40 | 284 | * @param bool $excludeHidden |
285 | * Should hidden groups be excluded. | |
286 | * Logically this is the wrong place to filter hidden groups out as that is | |
287 | * not a permission issue. However, as other functions may rely on that defaulting to | |
288 | * FALSE for now & only the api call is calling with true. | |
6a488035 | 289 | * |
addbec40 | 290 | * @return array |
291 | * The permission that the user has (or NULL) | |
6a488035 | 292 | */ |
addbec40 | 293 | public static function checkPermission($id, $excludeHidden = FALSE) { |
294 | $allGroups = CRM_Core_PseudoConstant::allGroup(NULL, $excludeHidden); | |
6a488035 TO |
295 | |
296 | $permissions = NULL; | |
297 | if (CRM_Core_Permission::check('edit all contacts') || | |
298 | CRM_ACL_API::groupPermission(CRM_ACL_API::EDIT, $id, NULL, | |
299 | 'civicrm_saved_search', $allGroups | |
300 | ) | |
301 | ) { | |
302 | $permissions[] = CRM_Core_Permission::EDIT; | |
303 | } | |
304 | ||
305 | if (CRM_Core_Permission::check('view all contacts') || | |
306 | CRM_ACL_API::groupPermission(CRM_ACL_API::VIEW, $id, NULL, | |
307 | 'civicrm_saved_search', $allGroups | |
308 | ) | |
309 | ) { | |
310 | $permissions[] = CRM_Core_Permission::VIEW; | |
311 | } | |
312 | ||
313 | if (!empty($permissions) && CRM_Core_Permission::check('delete contacts')) { | |
314 | // Note: using !empty() in if condition, restricts the scope of delete | |
315 | // permission to groups/contacts that are editable/viewable. | |
316 | // We can remove this !empty condition once we have ACL support for delete functionality. | |
317 | $permissions[] = CRM_Core_Permission::DELETE; | |
318 | } | |
319 | ||
320 | return $permissions; | |
321 | } | |
322 | ||
323 | /** | |
fe482240 | 324 | * Create a new group. |
6a488035 | 325 | * |
77c5b619 | 326 | * @param array $params |
6a488035 | 327 | * |
72b3a70c CW |
328 | * @return CRM_Contact_BAO_Group|NULL |
329 | * The new group BAO (if created) | |
6a488035 | 330 | */ |
c3137c08 | 331 | public static function create(&$params) { |
6a488035 | 332 | |
a7488080 | 333 | if (!empty($params['id'])) { |
6a488035 TO |
334 | CRM_Utils_Hook::pre('edit', 'Group', $params['id'], $params); |
335 | } | |
336 | else { | |
337 | CRM_Utils_Hook::pre('create', 'Group', NULL, $params); | |
338 | } | |
339 | ||
e616e67d | 340 | // If title isn't specified, retrieve it because we use it later, e.g. |
341 | // for RecentItems. But note we use array_key_exists not isset or empty | |
342 | // since otherwise there would be no way to blank out an existing title. | |
343 | // I'm not sure what the use-case is for that, but you're allowed to do it | |
344 | // currently. | |
345 | if (!empty($params['id']) && !array_key_exists('title', $params)) { | |
346 | try { | |
347 | $groupTitle = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $params['id'], 'title', 'id'); | |
348 | $params['title'] = $groupTitle; | |
349 | } | |
350 | catch (CRM_Core_Exception $groupTitleException) { | |
351 | // don't set title | |
352 | } | |
353 | } | |
354 | ||
d023b622 | 355 | // dev/core#287 Disable child groups if all parents are disabled. |
08b5ed10 | 356 | if (!empty($params['id'])) { |
230d43a4 | 357 | $allChildGroupIds = self::getChildGroupIds($params['id']); |
358 | foreach ($allChildGroupIds as $childKey => $childValue) { | |
359 | $parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($childValue); | |
360 | $activeParentsCount = civicrm_api3('Group', 'getcount', [ | |
361 | 'id' => ['IN' => $parentIds], | |
362 | 'is_active' => 1, | |
363 | ]); | |
08b5ed10 | 364 | if (count($parentIds) >= 1 && $activeParentsCount <= 1) { |
564df369 | 365 | $setDisable = self::setIsActive($childValue, CRM_Utils_Array::value('is_active', $params, 1)); |
230d43a4 | 366 | } |
367 | } | |
368 | } | |
6a488035 | 369 | // form the name only if missing: CRM-627 |
9c1bc317 | 370 | $nameParam = $params['name'] ?? NULL; |
8cc574cf | 371 | if (!$nameParam && empty($params['id'])) { |
6a488035 TO |
372 | $params['name'] = CRM_Utils_String::titleToVar($params['title']); |
373 | } | |
374 | ||
30208fab | 375 | if (!empty($params['parents'])) { |
376 | $params['parents'] = CRM_Utils_Array::convertCheckboxFormatToArray((array) $params['parents']); | |
377 | } | |
378 | ||
6a488035 TO |
379 | // convert params if array type |
380 | if (isset($params['group_type'])) { | |
30208fab | 381 | $params['group_type'] = CRM_Utils_Array::convertCheckboxFormatToArray((array) $params['group_type']); |
6a488035 TO |
382 | } |
383 | else { | |
82d7eb3d | 384 | $params['group_type'] = NULL; |
6a488035 TO |
385 | } |
386 | ||
481a74f4 | 387 | $session = CRM_Core_Session::singleton(); |
d0dfb649 PJ |
388 | $cid = $session->get('userID'); |
389 | // this action is add | |
390 | if ($cid && empty($params['id'])) { | |
6a488035 TO |
391 | $params['created_id'] = $cid; |
392 | } | |
d0dfb649 PJ |
393 | // this action is update |
394 | if ($cid && !empty($params['id'])) { | |
395 | $params['modified_id'] = $cid; | |
396 | } | |
6a488035 | 397 | |
d4fba42a SL |
398 | // CRM-19068. |
399 | // Validate parents parameter when creating group. | |
ec7846f5 | 400 | if (!empty($params['parents'])) { |
026e0308 FG |
401 | $parents = is_array($params['parents']) ? array_keys($params['parents']) : (array) $params['parents']; |
402 | foreach ($parents as $parent) { | |
403 | CRM_Utils_Type::validate($parent, 'Integer'); | |
d4fba42a SL |
404 | } |
405 | } | |
6a488035 | 406 | $group = new CRM_Contact_BAO_Group(); |
fc944198 | 407 | $group->copyValues($params); |
30208fab | 408 | |
a7488080 | 409 | if (empty($params['id']) && |
e2b7c67d | 410 | !$nameParam |
0aab99e7 | 411 | ) { |
6a488035 TO |
412 | $group->name .= "_tmp"; |
413 | } | |
414 | $group->save(); | |
415 | ||
416 | if (!$group->id) { | |
417 | return NULL; | |
418 | } | |
419 | ||
a7488080 | 420 | if (empty($params['id']) && |
e2b7c67d | 421 | !$nameParam |
0aab99e7 | 422 | ) { |
6a488035 TO |
423 | $group->name = substr($group->name, 0, -4) . "_{$group->id}"; |
424 | } | |
425 | ||
6a488035 TO |
426 | $group->save(); |
427 | ||
428 | // add custom field values | |
a7488080 | 429 | if (!empty($params['custom'])) { |
6a488035 TO |
430 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_group', $group->id); |
431 | } | |
432 | ||
433 | // make the group, child of domain/site group by default. | |
434 | $domainGroupID = CRM_Core_BAO_Domain::getGroupId(); | |
435 | if (CRM_Utils_Array::value('no_parent', $params) !== 1) { | |
436 | if (empty($params['parents']) && | |
437 | $domainGroupID != $group->id && | |
aaffa79f | 438 | Civi::settings()->get('is_enabled') && |
6a488035 TO |
439 | !CRM_Contact_BAO_GroupNesting::hasParentGroups($group->id) |
440 | ) { | |
441 | // if no parent present and the group doesn't already have any parents, | |
442 | // make sure site group goes as parent | |
be2fb01f | 443 | $params['parents'] = [$domainGroupID]; |
6a488035 TO |
444 | } |
445 | ||
446 | if (!empty($params['parents'])) { | |
30208fab | 447 | foreach ($params['parents'] as $parentId) { |
1069895e SL |
448 | if ($parentId && !CRM_Contact_BAO_GroupNesting::isParentChild($parentId, $group->id)) { |
449 | CRM_Contact_BAO_GroupNesting::add($parentId, $group->id); | |
6a488035 TO |
450 | } |
451 | } | |
452 | } | |
453 | ||
6a488035 TO |
454 | // this is always required, since we don't know when a |
455 | // parent group is removed | |
456 | CRM_Contact_BAO_GroupNestingCache::update(); | |
457 | ||
458 | // update group contact cache for all parent groups | |
459 | $parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($group->id); | |
460 | foreach ($parentIds as $parentId) { | |
461 | CRM_Contact_BAO_GroupContactCache::add($parentId); | |
462 | } | |
463 | } | |
464 | ||
a7488080 | 465 | if (!empty($params['organization_id'])) { |
1321b65b SL |
466 | // dev/core#382 Keeping the id here can cause db errors as it tries to update the wrong record in the Organization table |
467 | $groupOrg = [ | |
468 | 'group_id' => $group->id, | |
469 | 'organization_id' => $params['organization_id'], | |
470 | ]; | |
6a488035 TO |
471 | CRM_Contact_BAO_GroupOrganization::add($groupOrg); |
472 | } | |
473 | ||
6d054a8e | 474 | self::flushCaches(); |
6a488035 TO |
475 | CRM_Contact_BAO_GroupContactCache::add($group->id); |
476 | ||
a7488080 | 477 | if (!empty($params['id'])) { |
6a488035 TO |
478 | CRM_Utils_Hook::post('edit', 'Group', $group->id, $group); |
479 | } | |
480 | else { | |
481 | CRM_Utils_Hook::post('create', 'Group', $group->id, $group); | |
482 | } | |
483 | ||
be2fb01f | 484 | $recentOther = []; |
6a488035 TO |
485 | if (CRM_Core_Permission::check('edit groups')) { |
486 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=update&id=' . $group->id); | |
487 | // currently same permission we are using for delete a group | |
488 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=delete&id=' . $group->id); | |
489 | } | |
490 | ||
491 | // add the recently added group (unless hidden: CRM-6432) | |
492 | if (!$group->is_hidden) { | |
493 | CRM_Utils_Recent::add($group->title, | |
494 | CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id), | |
495 | $group->id, | |
496 | 'Group', | |
497 | NULL, | |
498 | NULL, | |
499 | $recentOther | |
500 | ); | |
501 | } | |
502 | return $group; | |
503 | } | |
504 | ||
6a488035 | 505 | /** |
fe482240 | 506 | * Defines a new smart group. |
6a488035 | 507 | * |
77c5b619 TO |
508 | * @param array $params |
509 | * Associative array of parameters. | |
6a488035 | 510 | * |
72b3a70c CW |
511 | * @return CRM_Contact_BAO_Group|NULL |
512 | * The new group BAO (if created) | |
6a488035 TO |
513 | */ |
514 | public static function createSmartGroup(&$params) { | |
a7488080 | 515 | if (!empty($params['formValues'])) { |
6a488035 TO |
516 | $ssParams = $params; |
517 | unset($ssParams['id']); | |
518 | if (isset($ssParams['saved_search_id'])) { | |
519 | $ssParams['id'] = $ssParams['saved_search_id']; | |
520 | } | |
fe38e286 | 521 | $params['form_values'] = $params['formValues']; |
6a488035 TO |
522 | $savedSearch = CRM_Contact_BAO_SavedSearch::create($params); |
523 | ||
524 | $params['saved_search_id'] = $savedSearch->id; | |
525 | } | |
526 | else { | |
527 | return NULL; | |
528 | } | |
529 | ||
530 | return self::create($params); | |
531 | } | |
532 | ||
533 | /** | |
fe482240 | 534 | * Update the is_active flag in the db. |
6a488035 | 535 | * |
77c5b619 TO |
536 | * @param int $id |
537 | * Id of the database record. | |
538 | * @param bool $isActive | |
539 | * Value we want to set the is_active field. | |
6a488035 | 540 | * |
8a4fede3 | 541 | * @return bool |
542 | * true if we found and updated the object, else false | |
6a488035 | 543 | */ |
00be9182 | 544 | public static function setIsActive($id, $isActive) { |
6a488035 TO |
545 | return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Group', $id, 'is_active', $isActive); |
546 | } | |
547 | ||
548 | /** | |
100fef9d | 549 | * Build the condition to retrieve groups. |
6a488035 | 550 | * |
77c5b619 TO |
551 | * @param string $groupType |
552 | * Type of group(Access/Mailing) OR the key of the group. | |
3f8d2862 | 553 | * @param bool $excludeHidden exclude hidden groups. |
6a488035 | 554 | * |
a6c01b45 | 555 | * @return string |
6a488035 | 556 | */ |
00be9182 | 557 | public static function groupTypeCondition($groupType = NULL, $excludeHidden = TRUE) { |
6a488035 TO |
558 | $value = NULL; |
559 | if ($groupType == 'Mailing') { | |
560 | $value = CRM_Core_DAO::VALUE_SEPARATOR . '2' . CRM_Core_DAO::VALUE_SEPARATOR; | |
561 | } | |
562 | elseif ($groupType == 'Access') { | |
563 | $value = CRM_Core_DAO::VALUE_SEPARATOR . '1' . CRM_Core_DAO::VALUE_SEPARATOR; | |
564 | } | |
9b873358 | 565 | elseif (!empty($groupType)) { |
6a488035 TO |
566 | // ie we have been given the group key |
567 | $value = CRM_Core_DAO::VALUE_SEPARATOR . $groupType . CRM_Core_DAO::VALUE_SEPARATOR; | |
568 | } | |
569 | ||
570 | $condition = NULL; | |
571 | if ($excludeHidden) { | |
572 | $condition = "is_hidden = 0"; | |
573 | } | |
574 | ||
575 | if ($value) { | |
576 | if ($condition) { | |
577 | $condition .= " AND group_type LIKE '%$value%'"; | |
578 | } | |
579 | else { | |
580 | $condition = "group_type LIKE '%$value%'"; | |
581 | } | |
582 | } | |
583 | ||
584 | return $condition; | |
585 | } | |
586 | ||
47c89d6b | 587 | /** |
fe482240 | 588 | * Get permission relevant clauses. |
aaac0e0b | 589 | * |
47c89d6b EM |
590 | * @return array |
591 | */ | |
bc95ea0d | 592 | public static function getPermissionClause() { |
593 | if (!isset(Civi::$statics[__CLASS__]['permission_clause'])) { | |
9486be34 | 594 | if (CRM_Core_Permission::check('view all contacts') || CRM_Core_Permission::check('edit all contacts')) { |
595 | $clause = 1; | |
47c89d6b | 596 | } |
aaac0e0b | 597 | else { |
9486be34 | 598 | //get the allowed groups for the current user |
599 | $groups = CRM_ACL_API::group(CRM_ACL_API::VIEW); | |
600 | if (!empty($groups)) { | |
601 | $groupList = implode(', ', array_values($groups)); | |
d9d23a91 | 602 | $clause = "`groups`.id IN ( $groupList ) "; |
9486be34 | 603 | } |
604 | else { | |
605 | $clause = '1 = 0'; | |
606 | } | |
aaac0e0b | 607 | } |
bc95ea0d | 608 | Civi::$statics[__CLASS__]['permission_clause'] = $clause; |
47c89d6b | 609 | } |
bc95ea0d | 610 | return Civi::$statics[__CLASS__]['permission_clause']; |
47c89d6b EM |
611 | } |
612 | ||
6d054a8e | 613 | /** |
614 | * Flush caches that hold group data. | |
615 | * | |
616 | * (Actually probably some overkill at the moment.) | |
617 | */ | |
618 | protected static function flushCaches() { | |
619 | CRM_Utils_System::flushCache(); | |
be2fb01f | 620 | $staticCaches = [ |
6d054a8e | 621 | 'CRM_Core_PseudoConstant' => 'groups', |
622 | 'CRM_ACL_API' => 'group_permission', | |
623 | 'CRM_ACL_BAO_ACL' => 'permissioned_groups', | |
bc95ea0d | 624 | 'CRM_Contact_BAO_Group' => 'permission_clause', |
be2fb01f | 625 | ]; |
6d054a8e | 626 | foreach ($staticCaches as $class => $key) { |
627 | if (isset(Civi::$statics[$class][$key])) { | |
628 | unset(Civi::$statics[$class][$key]); | |
629 | } | |
630 | } | |
631 | } | |
632 | ||
86538308 EM |
633 | /** |
634 | * @return string | |
635 | */ | |
6a488035 TO |
636 | public function __toString() { |
637 | return $this->title; | |
638 | } | |
639 | ||
640 | /** | |
641 | * This function create the hidden smart group when user perform | |
b44e3f84 | 642 | * contact search and want to send mailing to search contacts. |
6a488035 | 643 | * |
77c5b619 TO |
644 | * @param array $params |
645 | * ( reference ) an assoc array of name/value pairs. | |
6a488035 | 646 | * |
a6c01b45 CW |
647 | * @return array |
648 | * ( smartGroupId, ssId ) smart group id and saved search id | |
6a488035 | 649 | */ |
00be9182 | 650 | public static function createHiddenSmartGroup($params) { |
9c1bc317 | 651 | $ssId = $params['saved_search_id'] ?? NULL; |
6a488035 TO |
652 | |
653 | //add mapping record only for search builder saved search | |
654 | $mappingId = NULL; | |
655 | if ($params['search_context'] == 'builder') { | |
656 | //save the mapping for search builder | |
657 | if (!$ssId) { | |
658 | //save record in mapping table | |
be2fb01f | 659 | $mappingParams = [ |
95f52e3b | 660 | 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'), |
be2fb01f | 661 | ]; |
c997dfa8 | 662 | $mapping = CRM_Core_BAO_Mapping::add($mappingParams); |
353ffa53 | 663 | $mappingId = $mapping->id; |
6a488035 TO |
664 | } |
665 | else { | |
666 | //get the mapping id from saved search | |
667 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
668 | $savedSearch->id = $ssId; | |
669 | $savedSearch->find(TRUE); | |
670 | $mappingId = $savedSearch->mapping_id; | |
671 | } | |
672 | ||
673 | //save mapping fields | |
674 | CRM_Core_BAO_Mapping::saveMappingFields($params['form_values'], $mappingId); | |
675 | } | |
676 | ||
677 | //create/update saved search record. | |
678 | $savedSearch = new CRM_Contact_BAO_SavedSearch(); | |
679 | $savedSearch->id = $ssId; | |
93287bd4 | 680 | $formValues = $params['search_context'] === 'builder' ? $params['form_values'] : CRM_Contact_BAO_Query::convertFormValues($params['form_values']); |
681 | $savedSearch->form_values = serialize($formValues); | |
6a488035 | 682 | $savedSearch->mapping_id = $mappingId; |
9c1bc317 | 683 | $savedSearch->search_custom_id = $params['search_custom_id'] ?? NULL; |
6a488035 TO |
684 | $savedSearch->save(); |
685 | ||
686 | $ssId = $savedSearch->id; | |
687 | if (!$ssId) { | |
688 | return NULL; | |
689 | } | |
690 | ||
691 | $smartGroupId = NULL; | |
a7488080 | 692 | if (!empty($params['saved_search_id'])) { |
6a488035 TO |
693 | $smartGroupId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $ssId, 'id', 'saved_search_id'); |
694 | } | |
695 | else { | |
696 | //create group only when new saved search. | |
be2fb01f | 697 | $groupParams = [ |
6a488035 TO |
698 | 'title' => "Hidden Smart Group {$ssId}", |
699 | 'is_active' => CRM_Utils_Array::value('is_active', $params, 1), | |
700 | 'is_hidden' => CRM_Utils_Array::value('is_hidden', $params, 1), | |
6b409353 CW |
701 | 'group_type' => $params['group_type'] ?? NULL, |
702 | 'visibility' => $params['visibility'] ?? NULL, | |
6a488035 | 703 | 'saved_search_id' => $ssId, |
be2fb01f | 704 | ]; |
6a488035 TO |
705 | |
706 | $smartGroup = self::create($groupParams); | |
707 | $smartGroupId = $smartGroup->id; | |
708 | } | |
709 | ||
c997dfa8 TS |
710 | // Update mapping with the name and description of the hidden smart group. |
711 | if ($mappingId) { | |
be2fb01f | 712 | $mappingParams = [ |
c997dfa8 TS |
713 | 'id' => $mappingId, |
714 | 'name' => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $smartGroupId, 'name', 'id'), | |
715 | 'description' => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $smartGroupId, 'description', 'id'), | |
95f52e3b | 716 | 'mapping_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Mapping', 'mapping_type_id', 'Search Builder'), |
be2fb01f | 717 | ]; |
c997dfa8 TS |
718 | CRM_Core_BAO_Mapping::add($mappingParams); |
719 | } | |
720 | ||
be2fb01f | 721 | return [$smartGroupId, $ssId]; |
6a488035 TO |
722 | } |
723 | ||
724 | /** | |
fe482240 | 725 | * wrapper for ajax group selector. |
6a488035 | 726 | * |
77c5b619 TO |
727 | * @param array $params |
728 | * Associated array for params record id. | |
6a488035 | 729 | * |
a6c01b45 CW |
730 | * @return array |
731 | * associated array of group list | |
16b10e64 CW |
732 | * -rp = rowcount |
733 | * -page= offset | |
d3e86119 TO |
734 | * @todo there seems little reason for the small number of functions that call this to pass in |
735 | * params that then need to be translated in this function since they are coding them when calling | |
6a488035 | 736 | */ |
69078420 | 737 | public static function getGroupListSelector(&$params) { |
6a488035 | 738 | // format the params |
353ffa53 | 739 | $params['offset'] = ($params['page'] - 1) * $params['rp']; |
6a488035 | 740 | $params['rowCount'] = $params['rp']; |
9c1bc317 | 741 | $params['sort'] = $params['sortBy'] ?? NULL; |
6a488035 TO |
742 | |
743 | // get groups | |
744 | $groups = CRM_Contact_BAO_Group::getGroupList($params); | |
745 | ||
746 | //skip total if we are making call to show only children | |
a7488080 | 747 | if (empty($params['parent_id'])) { |
6a488035 TO |
748 | // add total |
749 | $params['total'] = CRM_Contact_BAO_Group::getGroupCount($params); | |
750 | ||
751 | // get all the groups | |
752 | $allGroups = CRM_Core_PseudoConstant::allGroup(); | |
753 | } | |
754 | ||
755 | // format params and add links | |
be2fb01f | 756 | $groupList = []; |
3b448eca | 757 | foreach ($groups as $id => $value) { |
be2fb01f | 758 | $group = []; |
3b448eca JL |
759 | $group['group_id'] = $value['id']; |
760 | $group['count'] = $value['count']; | |
761 | $group['title'] = $value['title']; | |
762 | ||
763 | // append parent names if in search mode | |
764 | if (empty($params['parent_id']) && !empty($value['parents'])) { | |
765 | $group['parent_id'] = $value['parents']; | |
766 | $groupIds = explode(',', $value['parents']); | |
be2fb01f | 767 | $title = []; |
3b448eca JL |
768 | foreach ($groupIds as $gId) { |
769 | $title[] = $allGroups[$gId]; | |
770 | } | |
771 | $group['title'] .= '<div class="crm-row-parent-name"><em>' . ts('Child of') . '</em>: ' . implode(', ', $title) . '</div>'; | |
be2fb01f | 772 | $value['class'] = array_diff($value['class'], ['crm-row-parent']); |
3b448eca JL |
773 | } |
774 | $group['DT_RowId'] = 'row_' . $value['id']; | |
46d33c7a | 775 | if (empty($params['parentsOnly'])) { |
fe6e4e4b | 776 | foreach ($value['class'] as $id => $class) { |
be632a8a | 777 | if ($class == 'crm-group-parent') { |
3b448eca | 778 | unset($value['class'][$id]); |
6a488035 | 779 | } |
6a488035 | 780 | } |
3b448eca JL |
781 | } |
782 | $group['DT_RowClass'] = 'crm-entity ' . implode(' ', $value['class']); | |
be2fb01f | 783 | $group['DT_RowAttr'] = []; |
3b448eca JL |
784 | $group['DT_RowAttr']['data-id'] = $value['id']; |
785 | $group['DT_RowAttr']['data-entity'] = 'group'; | |
6a488035 | 786 | |
9c1bc317 | 787 | $group['description'] = $value['description'] ?? NULL; |
6a488035 | 788 | |
3b448eca JL |
789 | if (!empty($value['group_type'])) { |
790 | $group['group_type'] = $value['group_type']; | |
791 | } | |
792 | else { | |
793 | $group['group_type'] = ''; | |
6a488035 | 794 | } |
3b448eca JL |
795 | |
796 | $group['visibility'] = $value['visibility']; | |
797 | $group['links'] = $value['action']; | |
9c1bc317 CW |
798 | $group['org_info'] = $value['org_info'] ?? NULL; |
799 | $group['created_by'] = $value['created_by'] ?? NULL; | |
3b448eca JL |
800 | |
801 | $group['is_parent'] = $value['is_parent']; | |
802 | ||
1e223804 | 803 | array_push($groupList, $group); |
6a488035 | 804 | } |
3b448eca | 805 | |
be2fb01f | 806 | $groupsDT = []; |
3b448eca | 807 | $groupsDT['data'] = $groupList; |
adf9de83 TS |
808 | $groupsDT['recordsTotal'] = !empty($params['total']) ? $params['total'] : NULL; |
809 | $groupsDT['recordsFiltered'] = !empty($params['total']) ? $params['total'] : NULL; | |
3b448eca JL |
810 | |
811 | return $groupsDT; | |
6a488035 TO |
812 | } |
813 | ||
814 | /** | |
fe482240 | 815 | * This function to get list of groups. |
6a488035 | 816 | * |
77c5b619 TO |
817 | * @param array $params |
818 | * Associated array for params. | |
80ad33e7 EM |
819 | * |
820 | * @return array | |
6a488035 | 821 | */ |
00be9182 | 822 | public static function getGroupList(&$params) { |
6a488035 | 823 | $whereClause = self::whereClause($params, FALSE); |
f90c40b9 | 824 | |
adf9de83 | 825 | $limit = ""; |
6a488035 TO |
826 | if (!empty($params['rowCount']) && |
827 | $params['rowCount'] > 0 | |
828 | ) { | |
829 | $limit = " LIMIT {$params['offset']}, {$params['rowCount']} "; | |
830 | } | |
831 | ||
d9d23a91 | 832 | $orderBy = ' ORDER BY `groups`.title asc'; |
21d32567 DL |
833 | if (!empty($params['sort'])) { |
834 | $orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($params['sort'], 'String'); | |
1d19a8f2 CW |
835 | |
836 | // CRM-16905 - Sort by count cannot be done with sql | |
837 | if (strpos($params['sort'], 'count') === 0) { | |
838 | $orderBy = $limit = ''; | |
839 | } | |
6a488035 TO |
840 | } |
841 | ||
842 | $select = $from = $where = ""; | |
843 | $groupOrg = FALSE; | |
844 | if (CRM_Core_Permission::check('administer Multiple Organizations') && | |
845 | CRM_Core_Permission::isMultisiteEnabled() | |
846 | ) { | |
847 | $select = ", contact.display_name as org_name, contact.id as org_id"; | |
848 | $from = " LEFT JOIN civicrm_group_organization gOrg | |
d9d23a91 | 849 | ON gOrg.group_id = `groups`.id |
6a488035 TO |
850 | LEFT JOIN civicrm_contact contact |
851 | ON contact.id = gOrg.organization_id "; | |
852 | ||
853 | //get the Organization ID | |
a3d827a7 | 854 | $orgID = CRM_Utils_Request::retrieve('oid', 'Positive'); |
6a488035 TO |
855 | if ($orgID) { |
856 | $where = " AND gOrg.organization_id = {$orgID}"; | |
857 | } | |
858 | ||
859 | $groupOrg = TRUE; | |
860 | } | |
861 | ||
862 | $query = " | |
d9d23a91 SL |
863 | SELECT `groups`.*, createdBy.sort_name as created_by {$select} |
864 | FROM civicrm_group `groups` | |
7f4b344c | 865 | LEFT JOIN civicrm_contact createdBy |
d9d23a91 | 866 | ON createdBy.id = `groups`.created_id |
7f4b344c | 867 | {$from} |
6a488035 TO |
868 | WHERE $whereClause {$where} |
869 | {$orderBy} | |
870 | {$limit}"; | |
871 | ||
872 | $object = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contact_DAO_Group'); | |
873 | ||
874 | //FIXME CRM-4418, now we are handling delete separately | |
875 | //if we introduce 'delete for group' make sure to handle here. | |
be2fb01f | 876 | $groupPermissions = [CRM_Core_Permission::VIEW]; |
6a488035 TO |
877 | if (CRM_Core_Permission::check('edit groups')) { |
878 | $groupPermissions[] = CRM_Core_Permission::EDIT; | |
879 | $groupPermissions[] = CRM_Core_Permission::DELETE; | |
880 | } | |
881 | ||
882 | // CRM-9936 | |
883 | $reservedPermission = CRM_Core_Permission::check('administer reserved groups'); | |
884 | ||
fecb40f5 | 885 | $links = self::actionLinks($params); |
6a488035 TO |
886 | |
887 | $allTypes = CRM_Core_OptionGroup::values('group_type'); | |
be2fb01f | 888 | $values = []; |
6a488035 | 889 | |
e3c75a92 | 890 | $visibility = CRM_Core_SelectValues::ufVisibility(); |
891 | ||
6a488035 | 892 | while ($object->fetch()) { |
6d054a8e | 893 | $newLinks = $links; |
be2fb01f CW |
894 | $values[$object->id] = [ |
895 | 'class' => [], | |
6d054a8e | 896 | 'count' => '0', |
be2fb01f | 897 | ]; |
6d054a8e | 898 | CRM_Core_DAO::storeValues($object, $values[$object->id]); |
899 | ||
900 | if ($object->saved_search_id) { | |
901 | $values[$object->id]['title'] .= ' (' . ts('Smart Group') . ')'; | |
902 | // check if custom search, if so fix view link | |
903 | $customSearchID = CRM_Core_DAO::getFieldValue( | |
904 | 'CRM_Contact_DAO_SavedSearch', | |
905 | $object->saved_search_id, | |
906 | 'search_custom_id' | |
cbe32e2f | 907 | ); |
6d054a8e | 908 | |
909 | if ($customSearchID) { | |
910 | $newLinks[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/search/custom'; | |
911 | $newLinks[CRM_Core_Action::VIEW]['qs'] = "reset=1&force=1&ssID={$object->saved_search_id}"; | |
6a488035 | 912 | } |
6d054a8e | 913 | } |
6a488035 | 914 | |
6d054a8e | 915 | $action = array_sum(array_keys($newLinks)); |
6a488035 | 916 | |
6d054a8e | 917 | // CRM-9936 |
aebf3542 | 918 | if (property_exists($object, 'is_reserved')) { |
6d054a8e | 919 | //if group is reserved and I don't have reserved permission, suppress delete/edit |
920 | if ($object->is_reserved && !$reservedPermission) { | |
921 | $action -= CRM_Core_Action::DELETE; | |
922 | $action -= CRM_Core_Action::UPDATE; | |
923 | $action -= CRM_Core_Action::DISABLE; | |
6a488035 | 924 | } |
6d054a8e | 925 | } |
6a488035 | 926 | |
aebf3542 | 927 | if (property_exists($object, 'is_active')) { |
6d054a8e | 928 | if ($object->is_active) { |
929 | $action -= CRM_Core_Action::ENABLE; | |
6a488035 | 930 | } |
6d054a8e | 931 | else { |
932 | $values[$object->id]['class'][] = 'disabled'; | |
933 | $action -= CRM_Core_Action::VIEW; | |
934 | $action -= CRM_Core_Action::DISABLE; | |
935 | } | |
936 | } | |
6a488035 | 937 | |
6d054a8e | 938 | $action = $action & CRM_Core_Action::mask($groupPermissions); |
6a488035 | 939 | |
6d054a8e | 940 | $values[$object->id]['visibility'] = $visibility[$values[$object->id]['visibility']]; |
e3c75a92 | 941 | |
6d054a8e | 942 | if (isset($values[$object->id]['group_type'])) { |
943 | $groupTypes = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
944 | substr($values[$object->id]['group_type'], 1, -1) | |
6a488035 | 945 | ); |
be2fb01f | 946 | $types = []; |
6d054a8e | 947 | foreach ($groupTypes as $type) { |
9c1bc317 | 948 | $types[] = $allTypes[$type] ?? NULL; |
6a488035 | 949 | } |
6d054a8e | 950 | $values[$object->id]['group_type'] = implode(', ', $types); |
951 | } | |
887688b9 JG |
952 | if ($action) { |
953 | $values[$object->id]['action'] = CRM_Core_Action::formLink($newLinks, | |
954 | $action, | |
be2fb01f | 955 | [ |
887688b9 JG |
956 | 'id' => $object->id, |
957 | 'ssid' => $object->saved_search_id, | |
be2fb01f | 958 | ], |
887688b9 JG |
959 | ts('more'), |
960 | FALSE, | |
961 | 'group.selector.row', | |
962 | 'Group', | |
963 | $object->id | |
964 | ); | |
965 | } | |
6a488035 | 966 | |
6d054a8e | 967 | // If group has children, add class for link to view children |
968 | $values[$object->id]['is_parent'] = FALSE; | |
969 | if (array_key_exists('children', $values[$object->id])) { | |
970 | $values[$object->id]['class'][] = "crm-group-parent"; | |
971 | $values[$object->id]['is_parent'] = TRUE; | |
972 | } | |
6a488035 | 973 | |
6d054a8e | 974 | // If group is a child, add child class |
975 | if (array_key_exists('parents', $values[$object->id])) { | |
976 | $values[$object->id]['class'][] = "crm-group-child"; | |
977 | } | |
978 | ||
979 | if ($groupOrg) { | |
980 | if ($object->org_id) { | |
981 | $contactUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$object->org_id}"); | |
982 | $values[$object->id]['org_info'] = "<a href='{$contactUrl}'>{$object->org_name}</a>"; | |
6a488035 TO |
983 | } |
984 | else { | |
69078420 SL |
985 | // Empty cell |
986 | $values[$object->id]['org_info'] = ''; | |
6a488035 TO |
987 | } |
988 | } | |
6d054a8e | 989 | else { |
69078420 SL |
990 | // Collapsed column if all cells are NULL |
991 | $values[$object->id]['org_info'] = NULL; | |
6d054a8e | 992 | } |
993 | if ($object->created_id) { | |
994 | $contactUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$object->created_id}"); | |
995 | $values[$object->id]['created_by'] = "<a href='{$contactUrl}'>{$object->created_by}</a>"; | |
996 | } | |
6a488035 | 997 | |
01ef4db9 | 998 | // By default, we try to get a count of the contacts in each group |
b804448d JM |
999 | // to display to the user on the Manage Group page. However, if |
1000 | // that will result in the cache being regenerated, then dipslay | |
1001 | // "unknown" instead to avoid a long wait for the user. | |
1002 | if (CRM_Contact_BAO_GroupContactCache::shouldGroupBeRefreshed($object->id)) { | |
a19f5834 JM |
1003 | $values[$object->id]['count'] = ts('unknown'); |
1004 | } | |
1005 | else { | |
be2fb01f | 1006 | $values[$object->id]['count'] = civicrm_api3('Contact', 'getcount', ['group' => $object->id]); |
a19f5834 | 1007 | } |
cbe32e2f CW |
1008 | } |
1009 | ||
1d19a8f2 CW |
1010 | // CRM-16905 - Sort by count cannot be done with sql |
1011 | if (!empty($params['sort']) && strpos($params['sort'], 'count') === 0) { | |
1012 | usort($values, function($a, $b) { | |
1013 | return $a['count'] - $b['count']; | |
1014 | }); | |
1015 | if (strpos($params['sort'], 'desc')) { | |
1016 | $values = array_reverse($values, TRUE); | |
1017 | } | |
1018 | return array_slice($values, $params['offset'], $params['rowCount']); | |
1019 | } | |
1020 | ||
6a488035 TO |
1021 | return $values; |
1022 | } | |
1023 | ||
1024 | /** | |
1025 | * This function to get hierarchical list of groups (parent followed by children) | |
1026 | * | |
77c5b619 TO |
1027 | * @param array $groupIDs |
1028 | * Array of group ids. | |
80ad33e7 | 1029 | * |
e97c66ff | 1030 | * @param string $parents |
80ad33e7 EM |
1031 | * @param string $spacer |
1032 | * @param bool $titleOnly | |
f828fa2c | 1033 | * |
80ad33e7 | 1034 | * @return array |
6a488035 | 1035 | */ |
2da40d21 | 1036 | public static function getGroupsHierarchy( |
f828fa2c | 1037 | $groupIDs, |
90d1fee5 | 1038 | $parents = NULL, |
6a488035 TO |
1039 | $spacer = '<span class="child-indent"></span>', |
1040 | $titleOnly = FALSE | |
90d1fee5 | 1041 | ) { |
f828fa2c | 1042 | if (empty($groupIDs)) { |
be2fb01f | 1043 | return []; |
f828fa2c DL |
1044 | } |
1045 | ||
1046 | $groupIdString = '(' . implode(',', array_keys($groupIDs)) . ')'; | |
90d1fee5 | 1047 | // <span class="child-icon"></span> |
1048 | // need to return id, title (w/ spacer), description, visibility | |
1049 | ||
1050 | // We need to build a list of tags ordered by hierarchy and sorted by | |
b44e3f84 | 1051 | // name. The hierarchy will be communicated by an accumulation of |
90d1fee5 | 1052 | // separators in front of the name to give it a visual offset. |
1053 | // Instead of recursively making mysql queries, we'll make one big | |
b44e3f84 | 1054 | // query and build the hierarchy with the algorithm below. |
be2fb01f CW |
1055 | $groups = []; |
1056 | $args = [1 => [$groupIdString, 'String']]; | |
90d1fee5 | 1057 | $query = " |
78d0090b | 1058 | SELECT id, title, description, visibility, parents, saved_search_id |
f828fa2c DL |
1059 | FROM civicrm_group |
1060 | WHERE id IN $groupIdString | |
1061 | "; | |
90d1fee5 | 1062 | if ($parents) { |
b901b84e | 1063 | // group can have > 1 parent so parents may be comma separated list (eg. '1,2,5'). |
90d1fee5 | 1064 | $parentArray = explode(',', $parents); |
ea5f6013 | 1065 | $parent = self::filterActiveGroups($parentArray); |
be2fb01f | 1066 | $args[2] = [$parent, 'Integer']; |
90d1fee5 | 1067 | $query .= " AND SUBSTRING_INDEX(parents, ',', 1) = %2"; |
1068 | } | |
1069 | $query .= " ORDER BY title"; | |
1070 | $dao = CRM_Core_DAO::executeQuery($query, $args); | |
1071 | ||
1072 | // Sort the groups into the correct storage by the parent | |
1073 | // $roots represent the current leaf nodes that need to be checked for | |
1074 | // children. $rows represent the unplaced nodes | |
f71d7bd3 | 1075 | // $tree contains the child nodes based on their parent_id. |
be2fb01f CW |
1076 | $roots = []; |
1077 | $tree = []; | |
90d1fee5 | 1078 | while ($dao->fetch()) { |
f71d7bd3 JJ |
1079 | if ($dao->parents) { |
1080 | $parentArray = explode(',', $dao->parents); | |
ea5f6013 | 1081 | $parent = self::filterActiveGroups($parentArray); |
be2fb01f | 1082 | $tree[$parent][] = [ |
90d1fee5 | 1083 | 'id' => $dao->id, |
78d0090b | 1084 | 'title' => empty($dao->saved_search_id) ? $dao->title : '* ' . $dao->title, |
f71d7bd3 JJ |
1085 | 'visibility' => $dao->visibility, |
1086 | 'description' => $dao->description, | |
be2fb01f | 1087 | ]; |
8f658465 JJ |
1088 | } |
1089 | else { | |
be2fb01f | 1090 | $roots[] = [ |
90d1fee5 | 1091 | 'id' => $dao->id, |
78d0090b | 1092 | 'title' => empty($dao->saved_search_id) ? $dao->title : '* ' . $dao->title, |
f71d7bd3 JJ |
1093 | 'visibility' => $dao->visibility, |
1094 | 'description' => $dao->description, | |
be2fb01f | 1095 | ]; |
90d1fee5 | 1096 | } |
1097 | } | |
90d1fee5 | 1098 | |
be2fb01f | 1099 | $hierarchy = []; |
8f658465 | 1100 | for ($i = 0; $i < count($roots); $i++) { |
f71d7bd3 | 1101 | self::buildGroupHierarchy($hierarchy, $roots[$i], $tree, $titleOnly, $spacer, 0); |
90d1fee5 | 1102 | } |
956b9960 | 1103 | |
f71d7bd3 JJ |
1104 | return $hierarchy; |
1105 | } | |
90d1fee5 | 1106 | |
f71d7bd3 JJ |
1107 | /** |
1108 | * Build a list with groups on alphabetical order and child groups after the parent group. | |
1109 | * | |
1110 | * This is a recursive function filling the $hierarchy parameter. | |
1111 | * | |
1112 | * @param $hierarchy | |
1113 | * @param $group | |
1114 | * @param $tree | |
1115 | * @param $titleOnly | |
1116 | * @param $spacer | |
1117 | * @param $level | |
1118 | */ | |
1119 | private static function buildGroupHierarchy(&$hierarchy, $group, $tree, $titleOnly, $spacer, $level) { | |
1120 | $spaces = str_repeat($spacer, $level); | |
1121 | ||
1122 | if ($titleOnly) { | |
1123 | $hierarchy[$group['id']] = $spaces . $group['title']; | |
90d1fee5 | 1124 | } |
f71d7bd3 | 1125 | else { |
78d0090b MD |
1126 | $hierarchy[] = [ |
1127 | 'id' => $group['id'], | |
1128 | 'text' => $spaces . $group['title'], | |
f71d7bd3 JJ |
1129 | 'description' => $group['description'], |
1130 | 'visibility' => $group['visibility'], | |
ba2ad46f | 1131 | ]; |
90d1fee5 | 1132 | } |
1133 | ||
f71d7bd3 JJ |
1134 | // For performance reasons we use a for loop rather than a foreach. |
1135 | // Metrics for performance in an installation with 2867 groups a foreach | |
1136 | // caused the function getGroupsHierarchy with a foreach execution takes | |
1137 | // around 2.2 seoonds (2,200 ms). | |
1138 | // Changing to a for loop execustion takes around 0.02 seconds (20 ms). | |
d10c0875 JJ |
1139 | if (isset($tree[$group['id']]) && is_array($tree[$group['id']])) { |
1140 | for ($i = 0; $i < count($tree[$group['id']]); $i++) { | |
cca81cba | 1141 | self::buildGroupHierarchy($hierarchy, $tree[$group['id']][$i], $tree, $titleOnly, $spacer, $level + 1); |
d10c0875 | 1142 | } |
f71d7bd3 | 1143 | } |
6a488035 TO |
1144 | } |
1145 | ||
86538308 | 1146 | /** |
c490a46a | 1147 | * @param array $params |
86538308 | 1148 | * |
e60f24eb | 1149 | * @return NULL|string |
86538308 | 1150 | */ |
00be9182 | 1151 | public static function getGroupCount(&$params) { |
6a488035 | 1152 | $whereClause = self::whereClause($params, FALSE); |
d9d23a91 | 1153 | $query = "SELECT COUNT(*) FROM civicrm_group `groups`"; |
6a488035 | 1154 | |
a7488080 | 1155 | if (!empty($params['created_by'])) { |
6a488035 TO |
1156 | $query .= " |
1157 | INNER JOIN civicrm_contact createdBy | |
d9d23a91 | 1158 | ON createdBy.id = `groups`.created_id"; |
6a488035 TO |
1159 | } |
1160 | $query .= " | |
1161 | WHERE {$whereClause}"; | |
1162 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
1163 | } | |
1164 | ||
47c89d6b | 1165 | /** |
fe482240 | 1166 | * Generate permissioned where clause for group search. |
c490a46a | 1167 | * @param array $params |
47c89d6b EM |
1168 | * @param bool $sortBy |
1169 | * @param bool $excludeHidden | |
1170 | * | |
1171 | * @return string | |
1172 | */ | |
00be9182 | 1173 | public static function whereClause(&$params, $sortBy = TRUE, $excludeHidden = TRUE) { |
be2fb01f | 1174 | $values = []; |
9c1bc317 | 1175 | $title = $params['title'] ?? NULL; |
6a488035 | 1176 | if ($title) { |
d9d23a91 | 1177 | $clauses[] = "`groups`.title LIKE %1"; |
6a488035 | 1178 | if (strpos($title, '%') !== FALSE) { |
be2fb01f | 1179 | $params[1] = [$title, 'String', FALSE]; |
6a488035 TO |
1180 | } |
1181 | else { | |
be2fb01f | 1182 | $params[1] = [$title, 'String', TRUE]; |
6a488035 TO |
1183 | } |
1184 | } | |
1185 | ||
9c1bc317 | 1186 | $groupType = $params['group_type'] ?? NULL; |
6a488035 TO |
1187 | if ($groupType) { |
1188 | $types = explode(',', $groupType); | |
1189 | if (!empty($types)) { | |
d9d23a91 | 1190 | $clauses[] = '`groups`.group_type LIKE %2'; |
6a488035 | 1191 | $typeString = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $types) . CRM_Core_DAO::VALUE_SEPARATOR; |
be2fb01f | 1192 | $params[2] = [$typeString, 'String', TRUE]; |
6a488035 TO |
1193 | } |
1194 | } | |
1195 | ||
9c1bc317 | 1196 | $visibility = $params['visibility'] ?? NULL; |
6a488035 | 1197 | if ($visibility) { |
d9d23a91 | 1198 | $clauses[] = '`groups`.visibility = %3'; |
be2fb01f | 1199 | $params[3] = [$visibility, 'String']; |
6a488035 TO |
1200 | } |
1201 | ||
9c1bc317 | 1202 | $groupStatus = $params['status'] ?? NULL; |
6a488035 TO |
1203 | if ($groupStatus) { |
1204 | switch ($groupStatus) { | |
1205 | case 1: | |
d9d23a91 | 1206 | $clauses[] = '`groups`.is_active = 1'; |
be2fb01f | 1207 | $params[4] = [$groupStatus, 'Integer']; |
6a488035 TO |
1208 | break; |
1209 | ||
1210 | case 2: | |
d9d23a91 | 1211 | $clauses[] = '`groups`.is_active = 0'; |
be2fb01f | 1212 | $params[4] = [$groupStatus, 'Integer']; |
6a488035 TO |
1213 | break; |
1214 | ||
1215 | case 3: | |
d9d23a91 | 1216 | $clauses[] = '(`groups`.is_active = 0 OR `groups`.is_active = 1 )'; |
6a488035 TO |
1217 | break; |
1218 | } | |
1219 | } | |
1220 | ||
9c1bc317 | 1221 | $parentsOnly = $params['parentsOnly'] ?? NULL; |
6a488035 | 1222 | if ($parentsOnly) { |
d9d23a91 | 1223 | $clauses[] = '`groups`.parents IS NULL'; |
6a488035 TO |
1224 | } |
1225 | ||
effbcf85 SP |
1226 | $savedSearch = CRM_Utils_Array::value('savedSearch', $params); |
1227 | if ($savedSearch == 1) { | |
1228 | $clauses[] = 'groups.saved_search_id IS NOT NULL'; | |
1229 | } | |
f33893fd | 1230 | elseif ($savedSearch == 2) { |
effbcf85 SP |
1231 | $clauses[] = 'groups.saved_search_id IS NULL'; |
1232 | } | |
1233 | ||
6a488035 | 1234 | // only show child groups of a specific parent group |
9c1bc317 | 1235 | $parent_id = $params['parent_id'] ?? NULL; |
6a488035 | 1236 | if ($parent_id) { |
d9d23a91 | 1237 | $clauses[] = '`groups`.id IN (SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id = %5)'; |
be2fb01f | 1238 | $params[5] = [$parent_id, 'Integer']; |
6a488035 TO |
1239 | } |
1240 | ||
1241 | if ($createdBy = CRM_Utils_Array::value('created_by', $params)) { | |
1242 | $clauses[] = "createdBy.sort_name LIKE %6"; | |
1243 | if (strpos($createdBy, '%') !== FALSE) { | |
be2fb01f | 1244 | $params[6] = [$createdBy, 'String', FALSE]; |
6a488035 TO |
1245 | } |
1246 | else { | |
be2fb01f | 1247 | $params[6] = [$createdBy, 'String', TRUE]; |
6a488035 TO |
1248 | } |
1249 | } | |
1250 | ||
6a488035 | 1251 | if (empty($clauses)) { |
d9d23a91 | 1252 | $clauses[] = '`groups`.is_active = 1'; |
6a488035 TO |
1253 | } |
1254 | ||
1255 | if ($excludeHidden) { | |
d9d23a91 | 1256 | $clauses[] = '`groups`.is_hidden = 0'; |
6a488035 | 1257 | } |
33421d01 | 1258 | |
aaac0e0b | 1259 | $clauses[] = self::getPermissionClause(); |
6a488035 TO |
1260 | |
1261 | return implode(' AND ', $clauses); | |
1262 | } | |
1263 | ||
1264 | /** | |
fe482240 | 1265 | * Define action links. |
6a488035 | 1266 | * |
a6c01b45 CW |
1267 | * @return array |
1268 | * array of action links | |
6a488035 | 1269 | */ |
fecb40f5 MW |
1270 | public static function actionLinks($params) { |
1271 | // If component_mode is set we change the "View" link to match the requested component type | |
1272 | if (!isset($params['component_mode'])) { | |
1273 | $params['component_mode'] = CRM_Contact_BAO_Query::MODE_CONTACTS; | |
1274 | } | |
1275 | $modeValue = CRM_Contact_Form_Search::getModeValue($params['component_mode']); | |
be2fb01f CW |
1276 | $links = [ |
1277 | CRM_Core_Action::VIEW => [ | |
fecb40f5 | 1278 | 'name' => $modeValue['selectorLabel'], |
6a488035 | 1279 | 'url' => 'civicrm/group/search', |
fecb40f5 | 1280 | 'qs' => 'reset=1&force=1&context=smog&gid=%%id%%&component_mode=' . $params['component_mode'], |
6a488035 | 1281 | 'title' => ts('Group Contacts'), |
be2fb01f CW |
1282 | ], |
1283 | CRM_Core_Action::UPDATE => [ | |
6a488035 TO |
1284 | 'name' => ts('Settings'), |
1285 | 'url' => 'civicrm/group', | |
1286 | 'qs' => 'reset=1&action=update&id=%%id%%', | |
1287 | 'title' => ts('Edit Group'), | |
be2fb01f CW |
1288 | ], |
1289 | CRM_Core_Action::DISABLE => [ | |
6a488035 | 1290 | 'name' => ts('Disable'), |
4d17a233 | 1291 | 'ref' => 'crm-enable-disable', |
6a488035 | 1292 | 'title' => ts('Disable Group'), |
be2fb01f CW |
1293 | ], |
1294 | CRM_Core_Action::ENABLE => [ | |
6a488035 | 1295 | 'name' => ts('Enable'), |
4d17a233 | 1296 | 'ref' => 'crm-enable-disable', |
6a488035 | 1297 | 'title' => ts('Enable Group'), |
be2fb01f CW |
1298 | ], |
1299 | CRM_Core_Action::DELETE => [ | |
6a488035 TO |
1300 | 'name' => ts('Delete'), |
1301 | 'url' => 'civicrm/group', | |
1302 | 'qs' => 'reset=1&action=delete&id=%%id%%', | |
1303 | 'title' => ts('Delete Group'), | |
be2fb01f CW |
1304 | ], |
1305 | ]; | |
6a488035 TO |
1306 | |
1307 | return $links; | |
1308 | } | |
1309 | ||
86538308 EM |
1310 | /** |
1311 | * @param $whereClause | |
100fef9d | 1312 | * @param array $whereParams |
86538308 EM |
1313 | * |
1314 | * @return string | |
1315 | */ | |
00be9182 | 1316 | public function pagerAtoZ($whereClause, $whereParams) { |
6a488035 | 1317 | $query = " |
d9d23a91 SL |
1318 | SELECT DISTINCT UPPER(LEFT(`groups`.title, 1)) as sort_name |
1319 | FROM civicrm_group `groups` | |
6a488035 | 1320 | WHERE $whereClause |
d9d23a91 | 1321 | ORDER BY LEFT(`groups`.title, 1) |
6a488035 TO |
1322 | "; |
1323 | $dao = CRM_Core_DAO::executeQuery($query, $whereParams); | |
1324 | ||
1325 | return CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE); | |
1326 | } | |
96025800 | 1327 | |
c955c26c SL |
1328 | /** |
1329 | * Assign Test Value. | |
1330 | * | |
1331 | * @param string $fieldName | |
1332 | * @param array $fieldDef | |
1333 | * @param int $counter | |
1334 | */ | |
1335 | protected function assignTestValue($fieldName, &$fieldDef, $counter) { | |
94183dd6 | 1336 | if ($fieldName == 'children' || $fieldName == 'parents') { |
c955c26c SL |
1337 | $this->{$fieldName} = "NULL"; |
1338 | } | |
1339 | else { | |
94183dd6 | 1340 | parent::assignTestValues($fieldName, $fieldDef, $counter); |
c955c26c SL |
1341 | } |
1342 | } | |
1343 | ||
883e1e76 | 1344 | /** |
1345 | * Get child group ids | |
1346 | * | |
783144b0 | 1347 | * @param array $regularGroupIDs |
883e1e76 | 1348 | * Parent Group IDs |
1349 | * | |
1350 | * @return array | |
1351 | */ | |
783144b0 | 1352 | public static function getChildGroupIds($regularGroupIDs) { |
be2fb01f | 1353 | $childGroupIDs = []; |
783144b0 | 1354 | |
cc949606 | 1355 | foreach ((array) $regularGroupIDs as $regularGroupID) { |
783144b0 | 1356 | // temporary store the child group ID(s) of regular group identified by $id, |
1357 | // later merge with main child group array | |
be2fb01f | 1358 | $tempChildGroupIDs = []; |
783144b0 | 1359 | // check that the regular group has any child group, if not then continue |
1360 | if ($childrenFound = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $regularGroupID, 'children')) { | |
1361 | $tempChildGroupIDs[] = $childrenFound; | |
1362 | } | |
1363 | else { | |
1364 | continue; | |
1365 | } | |
1366 | // as civicrm_group.children stores multiple group IDs in comma imploded string format, | |
1367 | // so we need to convert it into array of child group IDs first | |
1368 | $tempChildGroupIDs = explode(',', implode(',', $tempChildGroupIDs)); | |
1369 | $childGroupIDs = array_merge($childGroupIDs, $tempChildGroupIDs); | |
1370 | // recursively fetch the child group IDs | |
1371 | while (count($tempChildGroupIDs)) { | |
1372 | $tempChildGroupIDs = self::getChildGroupIds($tempChildGroupIDs); | |
1373 | if (count($tempChildGroupIDs)) { | |
1374 | $childGroupIDs = array_merge($childGroupIDs, $tempChildGroupIDs); | |
1375 | } | |
883e1e76 | 1376 | } |
1377 | } | |
1378 | ||
783144b0 | 1379 | return $childGroupIDs; |
883e1e76 | 1380 | } |
1381 | ||
ea5f6013 E |
1382 | /** |
1383 | * Check parent groups and filter out the disabled ones. | |
1384 | * | |
1385 | * @param array $parentArray | |
1386 | * Array of group Ids. | |
1387 | * | |
1388 | * @return int | |
1389 | */ | |
1390 | public static function filterActiveGroups($parentArray) { | |
1391 | if (count($parentArray) > 1) { | |
be2fb01f CW |
1392 | $result = civicrm_api3('Group', 'get', [ |
1393 | 'id' => ['IN' => $parentArray], | |
b4957876 | 1394 | 'is_active' => TRUE, |
1395 | 'return' => 'id', | |
be2fb01f | 1396 | ]); |
b4957876 | 1397 | $activeParentGroupIDs = CRM_Utils_Array::collect('id', $result['values']); |
1398 | foreach ($parentArray as $key => $groupID) { | |
1399 | if (!array_key_exists($groupID, $activeParentGroupIDs)) { | |
ea5f6013 E |
1400 | unset($parentArray[$key]); |
1401 | } | |
1402 | } | |
1403 | } | |
b4957876 | 1404 | |
ea5f6013 E |
1405 | return reset($parentArray); |
1406 | } | |
1407 | ||
6a488035 | 1408 | } |