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