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