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