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