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