ddae644d62858ca1693dd3fef3772239861673b4
[civicrm-core.git] / CRM / Contact / BAO / GroupContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 use Civi\Api4\SubscriptionHistory;
13 use Civi\Core\Event\PostEvent;
14 use Civi\Core\HookInterface;
15
16 /**
17 *
18 * @package CRM
19 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 */
21 class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact implements HookInterface {
22
23 /**
24 * Deprecated add function
25 *
26 * @param array $params
27 *
28 * @return CRM_Contact_DAO_GroupContact
29 * @throws \CRM_Core_Exception
30 *
31 * @deprecated
32 */
33 public static function add(array $params): CRM_Contact_DAO_GroupContact {
34 return self::writeRecord($params);
35 }
36
37 /**
38 * Callback for hook_civicrm_post().
39 *
40 * @param \Civi\Core\Event\PostEvent $event
41 *
42 * @noinspection PhpUnused
43 * @noinspection UnknownInspectionInspection
44 */
45 public static function self_hook_civicrm_post(PostEvent $event): void {
46 if (is_object($event->object) && in_array($event->action, ['create', 'edit'], TRUE)) {
47 // Lookup existing info for the sake of subscription history
48 if ($event->action === 'edit') {
49 $event->object->find(TRUE);
50 }
51
52 try {
53 if (empty($event->object->group_id) || empty($event->object->contact_id) || empty($event->object->status)) {
54 $event->object->find(TRUE);
55 }
56 SubscriptionHistory::save(FALSE)->setRecords([
57 [
58 'group_id' => $event->object->group_id,
59 'contact_id' => $event->object->contact_id,
60 'status' => $event->object->status,
61 ],
62 ])->execute();
63 }
64 catch (API_Exception $e) {
65 // A failure to create the history might be a deadlock or similar
66 // This record is not important enough to trigger a larger fail.
67 Civi::log()->warning('Failed to add civicrm_subscription_history record with error :error', ['error' => $e->getMessage()]);
68 }
69 }
70 }
71
72 /**
73 * Given the list of params in the params array, fetch the object
74 * and store the values in the values array
75 *
76 * @param array $params
77 * Input parameters to find object.
78 * @param array $values
79 * Output values of the object.
80 *
81 * @return array
82 * (reference) the values that could be potentially assigned to smarty
83 */
84 public static function getValues($params, &$values) {
85 if (empty($params)) {
86 return NULL;
87 }
88 $values['group']['data'] = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'],
89 'Added',
90 3
91 );
92
93 // get the total count of groups
94 $values['group']['totalCount'] = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'],
95 'Added',
96 NULL,
97 TRUE
98 );
99
100 return NULL;
101 }
102
103 /**
104 * Given an array of contact ids, add all the contacts to the group
105 *
106 * @param array $contactIds
107 * The array of contact ids to be added.
108 * @param int $groupId
109 * The id of the group.
110 * @param string $method
111 * @param string $status
112 * @param int $tracking
113 *
114 * @return array
115 * (total, added, notAdded) count of contacts added to group
116 */
117 public static function addContactsToGroup(
118 $contactIds,
119 $groupId,
120 $method = 'Admin',
121 $status = 'Added',
122 $tracking = NULL
123 ) {
124 if (empty($contactIds) || empty($groupId)) {
125 return [];
126 }
127
128 CRM_Utils_Hook::pre('create', 'GroupContact', $groupId, $contactIds);
129
130 $result = self::bulkAddContactsToGroup($contactIds, $groupId, $method, $status, $tracking);
131 CRM_Contact_BAO_GroupContactCache::invalidateGroupContactCache($groupId);
132 CRM_Contact_BAO_Contact_Utils::clearContactCaches();
133
134 CRM_Utils_Hook::post('create', 'GroupContact', $groupId, $contactIds);
135
136 return [count($contactIds), $result['count_added'], $result['count_not_added']];
137 }
138
139 /**
140 * Given an array of contact ids, remove all the contacts from the group
141 *
142 * @param array $contactIds
143 * (reference ) the array of contact ids to be removed.
144 * @param int $groupId
145 * The id of the group.
146 *
147 * @param string $method
148 * @param string $status
149 * @param string $tracking
150 *
151 * @return array
152 * (total, removed, notRemoved) count of contacts removed to group
153 */
154 public static function removeContactsFromGroup(
155 &$contactIds,
156 $groupId,
157 $method = 'Admin',
158 $status = 'Removed',
159 $tracking = NULL
160 ) {
161 if (!is_array($contactIds)) {
162 return [0, 0, 0];
163 }
164 if ($status == 'Removed' || $status == 'Deleted') {
165 $op = 'delete';
166 }
167 else {
168 $op = 'edit';
169 }
170
171 CRM_Utils_Hook::pre($op, 'GroupContact', $groupId, $contactIds);
172
173 $date = date('YmdHis');
174 $numContactsRemoved = 0;
175 $numContactsNotRemoved = 0;
176
177 $group = new CRM_Contact_DAO_Group();
178 $group->id = $groupId;
179 $group->find(TRUE);
180
181 foreach ($contactIds as $contactId) {
182 if ($status == 'Deleted') {
183 $query = "DELETE FROM civicrm_group_contact WHERE contact_id = %1 AND group_id = %2";
184 $dao = CRM_Core_DAO::executeQuery($query, [
185 1 => [$contactId, 'Positive'],
186 2 => [$groupId, 'Positive'],
187 ]);
188 $historyParams = [
189 'group_id' => $groupId,
190 'contact_id' => $contactId,
191 'status' => $status,
192 'method' => $method,
193 'date' => $date,
194 'tracking' => $tracking,
195 ];
196 CRM_Contact_BAO_SubscriptionHistory::create($historyParams);
197 // Removing a row from civicrm_group_contact for a smart group may mean a contact
198 // Is now back in a group based on criteria so we will invalidate the cache if it is there
199 // So that accurate group cache is created next time it is needed.
200 CRM_Contact_BAO_GroupContactCache::invalidateGroupContactCache($groupId);
201 }
202 else {
203 $groupContact = new CRM_Contact_DAO_GroupContact();
204 $groupContact->group_id = $groupId;
205 $groupContact->contact_id = $contactId;
206 // check if the selected contact id already a member, or if this is
207 // an opt-out of a smart group.
208 // if not a member remove to groupContact else keep the count of contacts that are not removed
209 if ($groupContact->find(TRUE) || $group->saved_search_id) {
210 // remove the contact from the group
211 $numContactsRemoved++;
212 }
213 else {
214 $numContactsNotRemoved++;
215 }
216
217 //now we grant the negative membership to contact if not member. CRM-3711
218 $historyParams = [
219 'group_id' => $groupId,
220 'contact_id' => $contactId,
221 'status' => $status,
222 'method' => $method,
223 'date' => $date,
224 'tracking' => $tracking,
225 ];
226 CRM_Contact_BAO_SubscriptionHistory::create($historyParams);
227 $groupContact->status = $status;
228 $groupContact->save();
229 // Remove any rows from the group contact cache so it disappears straight away from smart groups.
230 CRM_Contact_BAO_GroupContactCache::removeContact($contactId, $groupId);
231 }
232 }
233
234 CRM_Contact_BAO_Contact_Utils::clearContactCaches();
235
236 CRM_Utils_Hook::post($op, 'GroupContact', $groupId, $contactIds);
237
238 return [count($contactIds), $numContactsRemoved, $numContactsNotRemoved];
239 }
240
241 /**
242 * Get list of all the groups and groups for a contact.
243 *
244 * @param int $contactId
245 * Contact id.
246 *
247 * @param bool $visibility
248 *
249 *
250 * @return array
251 * this array has key-> group id and value group title
252 */
253 public static function getGroupList($contactId = 0, $visibility = FALSE) {
254 $select = 'SELECT civicrm_group.id, civicrm_group.title ';
255 $from = ' FROM civicrm_group ';
256 $where = " WHERE civicrm_group.is_active = 1 ";
257 if ($contactId) {
258 $from .= ' , civicrm_group_contact ';
259 $where .= " AND civicrm_group.id = civicrm_group_contact.group_id
260 AND civicrm_group_contact.contact_id = " . CRM_Utils_Type::escape($contactId, 'Integer');
261 }
262
263 if ($visibility) {
264 $where .= " AND civicrm_group.visibility != 'User and User Admin Only'";
265 }
266 $groupBy = " GROUP BY civicrm_group.id";
267
268 $orderby = " ORDER BY civicrm_group.name";
269 $sql = $select . $from . $where . $groupBy . $orderby;
270
271 $group = CRM_Core_DAO::executeQuery($sql);
272
273 $values = [];
274 while ($group->fetch()) {
275 $values[$group->id] = $group->title;
276 }
277
278 return $values;
279 }
280
281 /**
282 * Get the list of groups for contact based on status of group membership.
283 *
284 * @param int $contactId
285 * Contact id.
286 * @param string $status
287 * State of membership.
288 * @param int $numGroupContact
289 * Number of groups for a contact that should be shown.
290 * @param bool $count
291 * True if we are interested only in the count.
292 * @param bool $ignorePermission
293 * True if we should ignore permissions for the current user.
294 * useful in profile where permissions are limited for the user. If left
295 * at false only groups viewable by the current user are returned
296 * @param bool $onlyPublicGroups
297 * True if we want to hide system groups.
298 *
299 * @param bool $excludeHidden
300 *
301 * @param int $groupId
302 *
303 * @param bool $includeSmartGroups
304 * Include or Exclude Smart Group(s)
305 * @param bool $public
306 * Are we returning groups for use on a public page.
307 *
308 * @return array|int
309 * the relevant data object values for the contact or the total count when $count is TRUE
310 */
311 public static function getContactGroup(
312 $contactId,
313 $status = NULL,
314 $numGroupContact = NULL,
315 $count = FALSE,
316 $ignorePermission = FALSE,
317 $onlyPublicGroups = FALSE,
318 $excludeHidden = TRUE,
319 $groupId = NULL,
320 $includeSmartGroups = FALSE,
321 $public = FALSE
322 ) {
323 if ($count) {
324 $select = 'SELECT count(DISTINCT civicrm_group_contact.id)';
325 }
326 else {
327 $select = 'SELECT
328 civicrm_group_contact.id as civicrm_group_contact_id,
329 civicrm_group.title as group_title,
330 civicrm_group.frontend_title as group_public_title,
331 civicrm_group.visibility as visibility,
332 civicrm_group_contact.status as status,
333 civicrm_group.id as group_id,
334 civicrm_group.is_hidden as is_hidden,
335 civicrm_subscription_history.date as date,
336 civicrm_subscription_history.method as method';
337 }
338
339 $where = " WHERE contact_a.id = %1 AND civicrm_group.is_active = 1";
340 if (!$includeSmartGroups) {
341 $where .= " AND saved_search_id IS NULL";
342 }
343 if ($excludeHidden) {
344 $where .= " AND civicrm_group.is_hidden = 0 ";
345 }
346 $params = [1 => [$contactId, 'Integer']];
347 if (!empty($status)) {
348 $where .= ' AND civicrm_group_contact.status = %2';
349 $params[2] = [$status, 'String'];
350 }
351 if (!empty($groupId)) {
352 $where .= " AND civicrm_group.id = %3 ";
353 $params[3] = [$groupId, 'Integer'];
354 }
355 $tables = [
356 'civicrm_group_contact' => 1,
357 'civicrm_group' => 1,
358 'civicrm_subscription_history' => 1,
359 ];
360 $whereTables = [];
361 if ($ignorePermission) {
362 $permission = ' ( 1 ) ';
363 }
364 else {
365 $permission = CRM_Core_Permission::getPermissionedStaticGroupClause(CRM_Core_Permission::VIEW, $tables, $whereTables);
366 }
367
368 $from = CRM_Contact_BAO_Query::fromClause($tables);
369
370 $where .= " AND $permission ";
371
372 if ($onlyPublicGroups) {
373 $where .= " AND civicrm_group.visibility != 'User and User Admin Only' ";
374 }
375
376 $order = $limit = '';
377 if (!$count) {
378 $order = ' ORDER BY civicrm_group.title, civicrm_subscription_history.date ASC';
379
380 if ($numGroupContact) {
381 $limit = " LIMIT 0, $numGroupContact";
382 }
383 }
384
385 $sql = $select . $from . $where . $order . $limit;
386
387 if ($count) {
388 $result = CRM_Core_DAO::singleValueQuery($sql, $params);
389 return $result;
390 }
391 else {
392 $dao = CRM_Core_DAO::executeQuery($sql, $params);
393 $values = [];
394 while ($dao->fetch()) {
395 $id = $dao->civicrm_group_contact_id;
396 $values[$id]['id'] = $id;
397 $values[$id]['group_id'] = $dao->group_id;
398 $values[$id]['title'] = ($public && !empty($group->group_public_title) ? $group->group_public_title : $dao->group_title);
399 $values[$id]['visibility'] = $dao->visibility;
400 $values[$id]['is_hidden'] = $dao->is_hidden;
401 switch ($dao->status) {
402 case 'Added':
403 $prefix = 'in_';
404 break;
405
406 case 'Removed':
407 $prefix = 'out_';
408 break;
409
410 default:
411 $prefix = 'pending_';
412 }
413 $values[$id][$prefix . 'date'] = $dao->date;
414 $values[$id][$prefix . 'method'] = $dao->method;
415 if ($status == 'Removed') {
416 $query = "SELECT `date` as `date_added` FROM civicrm_subscription_history WHERE id = (SELECT max(id) FROM civicrm_subscription_history WHERE contact_id = %1 AND status = \"Added\" AND group_id = $dao->group_id )";
417 $dateDAO = CRM_Core_DAO::executeQuery($query, $params);
418 if ($dateDAO->fetch()) {
419 $values[$id]['date_added'] = $dateDAO->date_added;
420 }
421 }
422 }
423 return $values;
424 }
425 }
426
427 /**
428 * Returns membership details of a contact for a group.
429 *
430 * @param int $contactId
431 * Id of the contact.
432 * @param int $groupID
433 * Id of a particular group.
434 * @param string $method
435 * If we want the subscription history details for a specific method.
436 *
437 * @return object
438 * of group contact
439 */
440 public static function getMembershipDetail($contactId, $groupID, $method = 'Email') {
441 $leftJoin = $where = $orderBy = NULL;
442
443 if ($method) {
444 //CRM-13341 add group_id clause
445 $leftJoin = "
446 LEFT JOIN civicrm_subscription_history
447 ON ( civicrm_group_contact.contact_id = civicrm_subscription_history.contact_id
448 AND civicrm_subscription_history.group_id = {$groupID} )";
449 $where = "AND civicrm_subscription_history.method ='Email'";
450 $orderBy = "ORDER BY civicrm_subscription_history.id DESC";
451 }
452 $query = "
453 SELECT *
454 FROM civicrm_group_contact
455 $leftJoin
456 WHERE civicrm_group_contact.contact_id = %1
457 AND civicrm_group_contact.group_id = %2
458 $where
459 $orderBy
460 ";
461
462 $params = [
463 1 => [$contactId, 'Integer'],
464 2 => [$groupID, 'Integer'],
465 ];
466 $dao = CRM_Core_DAO::executeQuery($query, $params);
467 $dao->fetch();
468 return $dao;
469 }
470
471 /**
472 * Method to get Group Id.
473 *
474 * @param int $groupContactID
475 * Id of a particular group.
476 *
477 *
478 * @return int groupID
479 */
480 public static function getGroupId($groupContactID) {
481 $dao = new CRM_Contact_DAO_GroupContact();
482 $dao->id = $groupContactID;
483 $dao->find(TRUE);
484 return $dao->group_id;
485 }
486
487 /**
488 * Deprecated create function.
489 *
490 * @deprecated
491 *
492 * @param array $params
493 *
494 * @return CRM_Contact_DAO_GroupContact
495 */
496 public static function create(array $params) {
497 // @fixme create was only called from CRM_Contact_BAO_Contact::createProfileContact
498 // As of Aug 2020 it's not called from anywhere so we can remove the below code after some time
499
500 CRM_Core_Error::deprecatedFunctionWarning('Use the GroupContact API');
501 return self::add($params);
502 }
503
504 /**
505 * @param int $contactID
506 * @param int $groupID
507 *
508 * @return bool
509 */
510 public static function isContactInGroup($contactID, $groupID) {
511 if (!CRM_Utils_Rule::positiveInteger($contactID) ||
512 !CRM_Utils_Rule::positiveInteger($groupID)
513 ) {
514 return FALSE;
515 }
516
517 $params = [
518 ['group', 'IN', [$groupID], 0, 0],
519 ['contact_id', '=', $contactID, 0, 0],
520 ];
521 [$contacts] = CRM_Contact_BAO_Query::apiQuery($params, ['contact_id']);
522
523 if (!empty($contacts)) {
524 return TRUE;
525 }
526 return FALSE;
527 }
528
529 /**
530 * Function merges the groups from otherContactID to mainContactID.
531 * along with subscription history
532 *
533 * @param int $mainContactId
534 * Contact id of main contact record.
535 * @param int $otherContactId
536 * Contact id of record which is going to merge.
537 *
538 * @see CRM_Dedupe_Merger::cpTables()
539 *
540 * TODO: use the 3rd $sqls param to append sql statements rather than executing them here
541 */
542 public static function mergeGroupContact($mainContactId, $otherContactId) {
543 $params = [
544 1 => [$mainContactId, 'Integer'],
545 2 => [$otherContactId, 'Integer'],
546 ];
547
548 // find all groups that are in otherContactID but not in mainContactID, copy them over
549 $sql = "
550 SELECT cOther.group_id
551 FROM civicrm_group_contact cOther
552 LEFT JOIN civicrm_group_contact cMain ON cOther.group_id = cMain.group_id AND cMain.contact_id = %1
553 WHERE cOther.contact_id = %2
554 AND cMain.contact_id IS NULL
555 ";
556 $dao = CRM_Core_DAO::executeQuery($sql, $params);
557
558 $otherGroupIDs = [];
559 while ($dao->fetch()) {
560 $otherGroupIDs[] = $dao->group_id;
561 }
562
563 if (!empty($otherGroupIDs)) {
564 $otherGroupIDString = implode(',', $otherGroupIDs);
565
566 $sql = "
567 UPDATE civicrm_group_contact
568 SET contact_id = %1
569 WHERE contact_id = %2
570 AND group_id IN ( $otherGroupIDString )
571 ";
572 CRM_Core_DAO::executeQuery($sql, $params);
573
574 $sql = "
575 UPDATE civicrm_subscription_history
576 SET contact_id = %1
577 WHERE contact_id = %2
578 AND group_id IN ( $otherGroupIDString )
579 ";
580 CRM_Core_DAO::executeQuery($sql, $params);
581 }
582
583 $sql = "
584 SELECT cOther.group_id as group_id,
585 cOther.status as group_status
586 FROM civicrm_group_contact cMain
587 INNER JOIN civicrm_group_contact cOther ON cMain.group_id = cOther.group_id
588 WHERE cMain.contact_id = %1
589 AND cOther.contact_id = %2
590 ";
591 $dao = CRM_Core_DAO::executeQuery($sql, $params);
592
593 $groupIDs = [];
594 while ($dao->fetch()) {
595 // only copy it over if it has added status and migrate the history
596 if ($dao->group_status == 'Added') {
597 $groupIDs[] = $dao->group_id;
598 }
599 }
600
601 if (!empty($groupIDs)) {
602 $groupIDString = implode(',', $groupIDs);
603
604 $sql = "
605 UPDATE civicrm_group_contact
606 SET status = 'Added'
607 WHERE contact_id = %1
608 AND group_id IN ( $groupIDString )
609 ";
610 CRM_Core_DAO::executeQuery($sql, $params);
611
612 $sql = "
613 UPDATE civicrm_subscription_history
614 SET contact_id = %1
615 WHERE contact_id = %2
616 AND group_id IN ( $groupIDString )
617 ";
618 CRM_Core_DAO::executeQuery($sql, $params);
619 }
620
621 // delete all the other group contacts
622 $sql = "
623 DELETE
624 FROM civicrm_group_contact
625 WHERE contact_id = %2
626 ";
627 CRM_Core_DAO::executeQuery($sql, $params);
628
629 $sql = "
630 DELETE
631 FROM civicrm_subscription_history
632 WHERE contact_id = %2
633 ";
634 CRM_Core_DAO::executeQuery($sql, $params);
635 }
636
637 /**
638 * Given an array of contact ids, add all the contacts to the group
639 *
640 * @param array $contactIDs
641 * The array of contact ids to be added.
642 * @param int $groupID
643 * The id of the group.
644 * @param string $method
645 * @param string $status
646 * @param string $tracking
647 *
648 * @return array
649 * (total, added, notAdded) count of contacts added to group
650 */
651 public static function bulkAddContactsToGroup(
652 $contactIDs,
653 $groupID,
654 $method = 'Admin',
655 $status = 'Added',
656 $tracking = NULL
657 ) {
658
659 $numContactsAdded = 0;
660 $numContactsNotAdded = 0;
661
662 $contactGroupSQL = "
663 REPLACE INTO civicrm_group_contact ( group_id, contact_id, status )
664 VALUES
665 ";
666 $subscriptioHistorySQL = "
667 INSERT INTO civicrm_subscription_history( group_id, contact_id, date, method, status, tracking )
668 VALUES
669 ";
670
671 $date = date('YmdHis');
672
673 // to avoid long strings, lets do BULK_INSERT_HIGH_COUNT values at a time
674 while (!empty($contactIDs)) {
675 $input = array_splice($contactIDs, 0, CRM_Core_DAO::BULK_INSERT_HIGH_COUNT);
676 $contactStr = implode(',', $input);
677
678 // lets check their current status
679 $sql = "
680 SELECT GROUP_CONCAT(contact_id) as contactStr
681 FROM civicrm_group_contact
682 WHERE group_id = %1
683 AND status = %2
684 AND contact_id IN ( $contactStr )
685 ";
686 $params = [
687 1 => [$groupID, 'Integer'],
688 2 => [$status, 'String'],
689 ];
690
691 $presentIDs = [];
692 $dao = CRM_Core_DAO::executeQuery($sql, $params);
693 if ($dao->fetch()) {
694 $presentIDs = explode(',', ($dao->contactStr ?? ''));
695 $presentIDs = array_flip($presentIDs);
696 }
697
698 $gcValues = $shValues = [];
699 foreach ($input as $cid) {
700 if (isset($presentIDs[$cid])) {
701 $numContactsNotAdded++;
702 continue;
703 }
704
705 $gcValues[] = "( $groupID, $cid, '$status' )";
706 $shValues[] = "( $groupID, $cid, '$date', '$method', '$status', '$tracking' )";
707 $numContactsAdded++;
708 }
709
710 if (!empty($gcValues)) {
711 $cgSQL = $contactGroupSQL . implode(",\n", $gcValues);
712 CRM_Core_DAO::executeQuery($cgSQL);
713
714 $shSQL = $subscriptioHistorySQL . implode(",\n", $shValues);
715 CRM_Core_DAO::executeQuery($shSQL);
716 }
717 }
718
719 return ['count_added' => $numContactsAdded, 'count_not_added' => $numContactsNotAdded];
720 }
721
722 /**
723 * Get options for a given field.
724 * @see CRM_Core_DAO::buildOptions
725 *
726 * @param string $fieldName
727 * @param string $context
728 * @see CRM_Core_DAO::buildOptionsContext
729 * @param array $props
730 * whatever is known about this dao object.
731 *
732 * @return array|bool
733 */
734 public static function buildOptions($fieldName, $context = NULL, $props = []) {
735 $options = CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, [], $context);
736
737 // Sort group list by hierarchy
738 // TODO: This will only work when api.entity is "group_contact". What about others?
739 if (($fieldName == 'group' || $fieldName == 'group_id') && ($context == 'search' || $context == 'create')) {
740 $options = CRM_Contact_BAO_Group::getGroupsHierarchy($options, NULL, '- ', TRUE);
741 }
742
743 return $options;
744 }
745
746 }