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