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