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