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