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