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