Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | class CRM_Contact_BAO_GroupContact extends CRM_Contact_DAO_GroupContact { | |
34 | ||
35 | /** | |
fe482240 | 36 | * Class constructor. |
6a488035 | 37 | */ |
00be9182 | 38 | public function __construct() { |
6a488035 TO |
39 | parent::__construct(); |
40 | } | |
41 | ||
42 | /** | |
fe482240 | 43 | * Takes an associative array and creates a groupContact object. |
6a488035 TO |
44 | * |
45 | * the function extract all the params it needs to initialize the create a | |
46 | * group object. the params array could contain additional unused name/value | |
47 | * pairs | |
48 | * | |
77c5b619 TO |
49 | * @param array $params |
50 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 51 | * |
16b10e64 | 52 | * @return CRM_Contact_BAO_Group |
6a488035 | 53 | */ |
00be9182 | 54 | public static function add(&$params) { |
6a488035 TO |
55 | |
56 | $dataExists = self::dataExists($params); | |
57 | if (!$dataExists) { | |
58 | return NULL; | |
59 | } | |
60 | ||
61 | $groupContact = new CRM_Contact_BAO_GroupContact(); | |
62 | $groupContact->copyValues($params); | |
63 | CRM_Contact_BAO_SubscriptionHistory::create($params); | |
64 | $groupContact->save(); | |
65 | return $groupContact; | |
66 | } | |
67 | ||
68 | /** | |
fe482240 | 69 | * Check if there is data to create the object. |
6a488035 | 70 | * |
77c5b619 TO |
71 | * @param array $params |
72 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 73 | * |
608e6658 | 74 | * @return bool |
6a488035 | 75 | */ |
00be9182 | 76 | public static function dataExists(&$params) { |
6a488035 TO |
77 | // return if no data present |
78 | if ($params['group_id'] == 0) { | |
79 | return FALSE; | |
80 | } | |
81 | ||
82 | return TRUE; | |
83 | } | |
84 | ||
85 | /** | |
86 | * Given the list of params in the params array, fetch the object | |
87 | * and store the values in the values array | |
88 | * | |
77c5b619 TO |
89 | * @param array $params |
90 | * Input parameters to find object. | |
91 | * @param array $values | |
92 | * Output values of the object. | |
6c8f6e67 | 93 | * |
a6c01b45 CW |
94 | * @return array |
95 | * (reference) the values that could be potentially assigned to smarty | |
6a488035 | 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 | * | |
77c5b619 TO |
119 | * @param array $contactIds |
120 | * The array of contact ids to be added. | |
121 | * @param int $groupId | |
122 | * The id of the group. | |
c49a2977 CW |
123 | * @param string $method |
124 | * @param string $status | |
125 | * @param int $tracking | |
6a488035 | 126 | * |
a6c01b45 CW |
127 | * @return array |
128 | * (total, added, notAdded) count of contacts added to group | |
6a488035 | 129 | */ |
608e6658 | 130 | public static function addContactsToGroup( |
c49a2977 | 131 | $contactIds, |
6a488035 | 132 | $groupId, |
242bd179 TO |
133 | $method = 'Admin', |
134 | $status = 'Added', | |
6a488035 TO |
135 | $tracking = NULL |
136 | ) { | |
137 | ||
6a488035 TO |
138 | CRM_Utils_Hook::pre('create', 'GroupContact', $groupId, $contactIds); |
139 | ||
006389de TO |
140 | list($numContactsAdded, $numContactsNotAdded) |
141 | = self::bulkAddContactsToGroup($contactIds, $groupId, $method, $status, $tracking); | |
6a488035 TO |
142 | |
143 | // also reset the acl cache | |
144 | $config = CRM_Core_Config::singleton(); | |
145 | if (!$config->doNotResetCache) { | |
146 | CRM_ACL_BAO_Cache::resetCache(); | |
147 | } | |
148 | ||
149 | // reset the group contact cache for all group(s) | |
150 | // if this group is being used as a smart group | |
151 | CRM_Contact_BAO_GroupContactCache::remove(); | |
152 | ||
153 | CRM_Utils_Hook::post('create', 'GroupContact', $groupId, $contactIds); | |
154 | ||
155 | return array(count($contactIds), $numContactsAdded, $numContactsNotAdded); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Given an array of contact ids, remove all the contacts from the group | |
160 | * | |
77c5b619 TO |
161 | * @param array $contactIds |
162 | * (reference ) the array of contact ids to be removed. | |
163 | * @param int $groupId | |
164 | * The id of the group. | |
fd31fa4c EM |
165 | * |
166 | * @param string $method | |
167 | * @param string $status | |
e60f24eb | 168 | * @param NULL $tracking |
6a488035 | 169 | * |
a6c01b45 CW |
170 | * @return array |
171 | * (total, removed, notRemoved) count of contacts removed to group | |
6a488035 | 172 | */ |
608e6658 | 173 | public static function removeContactsFromGroup( |
6a488035 TO |
174 | &$contactIds, |
175 | $groupId, | |
242bd179 TO |
176 | $method = 'Admin', |
177 | $status = 'Removed', | |
6a488035 TO |
178 | $tracking = NULL |
179 | ) { | |
180 | if (!is_array($contactIds)) { | |
181 | return array(0, 0, 0); | |
182 | } | |
183 | ||
184 | if ($status == 'Removed' || $status == 'Deleted') { | |
185 | $op = 'delete'; | |
186 | } | |
187 | else { | |
188 | $op = 'edit'; | |
189 | } | |
190 | ||
191 | CRM_Utils_Hook::pre($op, 'GroupContact', $groupId, $contactIds); | |
192 | ||
193 | $date = date('YmdHis'); | |
194 | $numContactsRemoved = 0; | |
195 | $numContactsNotRemoved = 0; | |
196 | ||
197 | $group = new CRM_Contact_DAO_Group(); | |
198 | $group->id = $groupId; | |
199 | $group->find(TRUE); | |
200 | ||
201 | foreach ($contactIds as $contactId) { | |
202 | if ($status == 'Deleted') { | |
242bd179 TO |
203 | $query = "DELETE FROM civicrm_group_contact WHERE contact_id=$contactId AND group_id=$groupId"; |
204 | $dao = CRM_Core_DAO::executeQuery($query); | |
6a488035 TO |
205 | $historyParams = array( |
206 | 'group_id' => $groupId, | |
207 | 'contact_id' => $contactId, | |
208 | 'status' => $status, | |
209 | 'method' => $method, | |
210 | 'date' => $date, | |
211 | 'tracking' => $tracking, | |
212 | ); | |
213 | CRM_Contact_BAO_SubscriptionHistory::create($historyParams); | |
214 | } | |
215 | else { | |
216 | $groupContact = new CRM_Contact_DAO_GroupContact(); | |
217 | $groupContact->group_id = $groupId; | |
218 | $groupContact->contact_id = $contactId; | |
219 | // check if the selected contact id already a member, or if this is | |
220 | // an opt-out of a smart group. | |
221 | // if not a member remove to groupContact else keep the count of contacts that are not removed | |
222 | if ($groupContact->find(TRUE) || $group->saved_search_id) { | |
223 | // remove the contact from the group | |
224 | $numContactsRemoved++; | |
225 | } | |
226 | else { | |
227 | $numContactsNotRemoved++; | |
228 | } | |
229 | ||
230 | //now we grant the negative membership to contact if not member. CRM-3711 | |
231 | $historyParams = array( | |
232 | 'group_id' => $groupId, | |
233 | 'contact_id' => $contactId, | |
234 | 'status' => $status, | |
235 | 'method' => $method, | |
236 | 'date' => $date, | |
237 | 'tracking' => $tracking, | |
238 | ); | |
239 | CRM_Contact_BAO_SubscriptionHistory::create($historyParams); | |
240 | $groupContact->status = $status; | |
241 | $groupContact->save(); | |
242 | } | |
243 | } | |
244 | ||
245 | // also reset the acl cache | |
246 | $config = CRM_Core_Config::singleton(); | |
247 | if (!$config->doNotResetCache) { | |
248 | CRM_ACL_BAO_Cache::resetCache(); | |
249 | } | |
250 | ||
251 | // reset the group contact cache for all group(s) | |
252 | // if this group is being used as a smart group | |
253 | CRM_Contact_BAO_GroupContactCache::remove(); | |
254 | ||
255 | CRM_Utils_Hook::post($op, 'GroupContact', $groupId, $contactIds); | |
256 | ||
257 | return array(count($contactIds), $numContactsRemoved, $numContactsNotRemoved); | |
258 | } | |
259 | ||
260 | /** | |
fe482240 | 261 | * Get list of all the groups and groups for a contact. |
6a488035 | 262 | * |
77c5b619 TO |
263 | * @param int $contactId |
264 | * Contact id. | |
6a488035 | 265 | * |
da6b46f4 EM |
266 | * @param bool $visibility |
267 | * | |
6a488035 | 268 | * |
a6c01b45 CW |
269 | * @return array |
270 | * this array has key-> group id and value group title | |
6a488035 | 271 | */ |
00be9182 | 272 | public static function getGroupList($contactId = 0, $visibility = FALSE) { |
6a488035 TO |
273 | $group = new CRM_Contact_DAO_Group(); |
274 | ||
275 | $select = $from = $where = ''; | |
276 | ||
277 | $select = 'SELECT DISTINCT civicrm_group.id, civicrm_group.title '; | |
242bd179 TO |
278 | $from = ' FROM civicrm_group '; |
279 | $where = " WHERE civicrm_group.is_active = 1 "; | |
6a488035 TO |
280 | if ($contactId) { |
281 | $from .= ' , civicrm_group_contact '; | |
282 | $where .= " AND civicrm_group.id = civicrm_group_contact.group_id | |
283 | AND civicrm_group_contact.contact_id = " . CRM_Utils_Type::escape($contactId, 'Integer'); | |
284 | } | |
285 | ||
286 | if ($visibility) { | |
287 | $where .= " AND civicrm_group.visibility != 'User and User Admin Only'"; | |
288 | } | |
289 | ||
290 | $orderby = " ORDER BY civicrm_group.name"; | |
291 | $sql = $select . $from . $where . $orderby; | |
292 | ||
293 | $group->query($sql); | |
294 | ||
295 | $values = array(); | |
296 | while ($group->fetch()) { | |
297 | $values[$group->id] = $group->title; | |
298 | } | |
299 | ||
300 | return $values; | |
301 | } | |
302 | ||
303 | /** | |
fe482240 | 304 | * Get the list of groups for contact based on status of group membership. |
6a488035 | 305 | * |
77c5b619 TO |
306 | * @param int $contactId |
307 | * Contact id. | |
308 | * @param string $status | |
309 | * State of membership. | |
310 | * @param int $numGroupContact | |
311 | * Number of groups for a contact that should be shown. | |
312 | * @param bool $count | |
313 | * True if we are interested only in the count. | |
314 | * @param bool $ignorePermission | |
315 | * True if we should ignore permissions for the current user. | |
6a488035 TO |
316 | * useful in profile where permissions are limited for the user. If left |
317 | * at false only groups viewable by the current user are returned | |
77c5b619 TO |
318 | * @param bool $onlyPublicGroups |
319 | * True if we want to hide system groups. | |
fd31fa4c EM |
320 | * |
321 | * @param bool $excludeHidden | |
6a488035 | 322 | * |
608e6658 | 323 | * @return array (reference)|int $values |
324 | * the relevant data object values for the contact or | |
325 | * the total count when $count is TRUE | |
6a488035 | 326 | */ |
608e6658 | 327 | public static function &getContactGroup( |
6a488035 | 328 | $contactId, |
242bd179 TO |
329 | $status = NULL, |
330 | $numGroupContact = NULL, | |
331 | $count = FALSE, | |
6a488035 TO |
332 | $ignorePermission = FALSE, |
333 | $onlyPublicGroups = FALSE, | |
242bd179 TO |
334 | $excludeHidden = TRUE, |
335 | $groupId = NULL | |
6a488035 TO |
336 | ) { |
337 | if ($count) { | |
338 | $select = 'SELECT count(DISTINCT civicrm_group_contact.id)'; | |
339 | } | |
340 | else { | |
341 | $select = 'SELECT | |
342 | civicrm_group_contact.id as civicrm_group_contact_id, | |
343 | civicrm_group.title as group_title, | |
344 | civicrm_group.visibility as visibility, | |
345 | civicrm_group_contact.status as status, | |
346 | civicrm_group.id as group_id, | |
347 | civicrm_group.is_hidden as is_hidden, | |
348 | civicrm_subscription_history.date as date, | |
349 | civicrm_subscription_history.method as method'; | |
350 | } | |
351 | ||
f013e5da | 352 | $where = " WHERE contact_a.id = %1 AND civicrm_group.is_active = 1"; |
6a488035 TO |
353 | |
354 | if ($excludeHidden) { | |
355 | $where .= " AND civicrm_group.is_hidden = 0 "; | |
356 | } | |
357 | ||
358 | $params = array(1 => array($contactId, 'Integer')); | |
359 | if (!empty($status)) { | |
360 | $where .= ' AND civicrm_group_contact.status = %2'; | |
361 | $params[2] = array($status, 'String'); | |
362 | } | |
49fb4e06 RK |
363 | if (!empty($groupId)) { |
364 | $where .= " AND civicrm_group.id = %3 "; | |
365 | $params[3] = array($groupId, 'Integer'); | |
366 | } | |
6a488035 TO |
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 | ||
11ddfae4 | 382 | //CRM-16945: seems hackish but as per CRM-16483 of using group criteria for Search Builder it is mandatory |
383 | //to include group_contact_cache clause when group table is present, so following code remove duplicacy | |
f013e5da | 384 | $from = str_replace("OR civicrm_group.id = civicrm_group_contact_cache.group_id", 'AND civicrm_group.saved_search_id IS NULL', $from); |
11ddfae4 | 385 | |
6a488035 TO |
386 | $where .= " AND $permission "; |
387 | ||
388 | if ($onlyPublicGroups) { | |
389 | $where .= " AND civicrm_group.visibility != 'User and User Admin Only' "; | |
390 | } | |
391 | ||
392 | $order = $limit = ''; | |
393 | if (!$count) { | |
394 | $order = ' ORDER BY civicrm_group.title, civicrm_subscription_history.date ASC'; | |
395 | ||
396 | if ($numGroupContact) { | |
397 | $limit = " LIMIT 0, $numGroupContact"; | |
398 | } | |
399 | } | |
400 | ||
401 | $sql = $select . $from . $where . $order . $limit; | |
402 | ||
403 | if ($count) { | |
404 | $result = CRM_Core_DAO::singleValueQuery($sql, $params); | |
405 | return $result; | |
406 | } | |
407 | else { | |
408 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
409 | $values = array(); | |
410 | while ($dao->fetch()) { | |
411 | $id = $dao->civicrm_group_contact_id; | |
412 | $values[$id]['id'] = $id; | |
413 | $values[$id]['group_id'] = $dao->group_id; | |
414 | $values[$id]['title'] = $dao->group_title; | |
415 | $values[$id]['visibility'] = $dao->visibility; | |
416 | $values[$id]['is_hidden'] = $dao->is_hidden; | |
417 | switch ($dao->status) { | |
418 | case 'Added': | |
419 | $prefix = 'in_'; | |
420 | break; | |
421 | ||
422 | case 'Removed': | |
423 | $prefix = 'out_'; | |
424 | break; | |
425 | ||
426 | default: | |
427 | $prefix = 'pending_'; | |
428 | } | |
429 | $values[$id][$prefix . 'date'] = $dao->date; | |
430 | $values[$id][$prefix . 'method'] = $dao->method; | |
431 | if ($status == 'Removed') { | |
432 | $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 )"; | |
433 | $dateDAO = CRM_Core_DAO::executeQuery($query, $params); | |
434 | if ($dateDAO->fetch()) { | |
435 | $values[$id]['date_added'] = $dateDAO->date_added; | |
436 | } | |
437 | } | |
438 | } | |
439 | return $values; | |
440 | } | |
441 | } | |
442 | ||
443 | /** | |
fe482240 | 444 | * Returns membership details of a contact for a group. |
6a488035 | 445 | * |
77c5b619 TO |
446 | * @param int $contactId |
447 | * Id of the contact. | |
448 | * @param int $groupID | |
449 | * Id of a perticuler group. | |
450 | * @param string $method | |
451 | * If we want the subscription history details for a specific method. | |
6a488035 | 452 | * |
a6c01b45 CW |
453 | * @return object |
454 | * of group contact | |
6a488035 | 455 | */ |
00be9182 | 456 | public static function getMembershipDetail($contactId, $groupID, $method = 'Email') { |
e60f24eb | 457 | $leftJoin = $where = $orderBy = NULL; |
6a488035 TO |
458 | |
459 | if ($method) { | |
071c6f10 BS |
460 | //CRM-13341 add group_id clause |
461 | $leftJoin = " | |
462 | LEFT JOIN civicrm_subscription_history | |
463 | ON ( civicrm_group_contact.contact_id = civicrm_subscription_history.contact_id | |
464 | AND civicrm_subscription_history.group_id = {$groupID} )"; | |
6a488035 TO |
465 | $where = "AND civicrm_subscription_history.method ='Email'"; |
466 | $orderBy = "ORDER BY civicrm_subscription_history.id DESC"; | |
467 | } | |
468 | $query = " | |
469 | SELECT * | |
470 | FROM civicrm_group_contact | |
471 | $leftJoin | |
472 | WHERE civicrm_group_contact.contact_id = %1 | |
473 | AND civicrm_group_contact.group_id = %2 | |
474 | $where | |
475 | $orderBy | |
476 | "; | |
477 | ||
478 | $params = array( | |
479 | 1 => array($contactId, 'Integer'), | |
480 | 2 => array($groupID, 'Integer'), | |
481 | ); | |
482 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
483 | $dao->fetch(); | |
484 | return $dao; | |
485 | } | |
486 | ||
6a488035 | 487 | /** |
fe482240 | 488 | * Method to get Group Id. |
6a488035 | 489 | * |
77c5b619 TO |
490 | * @param int $groupContactID |
491 | * Id of a perticuler group. | |
6a488035 TO |
492 | * |
493 | * | |
494 | * @return groupID | |
6a488035 | 495 | */ |
00be9182 | 496 | public static function getGroupId($groupContactID) { |
6a488035 TO |
497 | $dao = new CRM_Contact_DAO_GroupContact(); |
498 | $dao->id = $groupContactID; | |
499 | $dao->find(TRUE); | |
500 | return $dao->group_id; | |
501 | } | |
502 | ||
503 | /** | |
100fef9d | 504 | * Takes an associative array and creates / removes |
6a488035 TO |
505 | * contacts from the groups |
506 | * | |
507 | * | |
77c5b619 TO |
508 | * @param array $params |
509 | * (reference ) an assoc array of name/value pairs. | |
510 | * @param array $contactId | |
511 | * Contact id. | |
2a6da8d7 EM |
512 | * |
513 | * @param bool $visibility | |
514 | * @param string $method | |
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 | /** | |
fe482240 | 590 | * Function merges the groups from otherContactID to mainContactID. |
6a488035 TO |
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 | |
6a488035 | 601 | */ |
00be9182 | 602 | public static function mergeGroupContact($mainContactId, $otherContactId) { |
6ea503d4 TO |
603 | $params = array( |
604 | 1 => array($mainContactId, 'Integer'), | |
6a488035 TO |
605 | 2 => array($otherContactId, 'Integer'), |
606 | ); | |
607 | ||
608 | // find all groups that are in otherContactID but not in mainContactID, copy them over | |
609 | $sql = " | |
610 | SELECT cOther.group_id | |
611 | FROM civicrm_group_contact cOther | |
612 | LEFT JOIN civicrm_group_contact cMain ON cOther.group_id = cMain.group_id AND cMain.contact_id = %1 | |
613 | WHERE cOther.contact_id = %2 | |
614 | AND cMain.contact_id IS NULL | |
615 | "; | |
616 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
617 | ||
618 | $otherGroupIDs = array(); | |
619 | while ($dao->fetch()) { | |
620 | $otherGroupIDs[] = $dao->group_id; | |
621 | } | |
622 | ||
623 | if (!empty($otherGroupIDs)) { | |
624 | $otherGroupIDString = implode(',', $otherGroupIDs); | |
625 | ||
626 | $sql = " | |
627 | UPDATE civicrm_group_contact | |
628 | SET contact_id = %1 | |
629 | WHERE contact_id = %2 | |
630 | AND group_id IN ( $otherGroupIDString ) | |
631 | "; | |
632 | CRM_Core_DAO::executeQuery($sql, $params); | |
633 | ||
634 | $sql = " | |
635 | UPDATE civicrm_subscription_history | |
636 | SET contact_id = %1 | |
637 | WHERE contact_id = %2 | |
638 | AND group_id IN ( $otherGroupIDString ) | |
639 | "; | |
640 | CRM_Core_DAO::executeQuery($sql, $params); | |
641 | } | |
642 | ||
643 | $sql = " | |
644 | SELECT cOther.group_id as group_id, | |
645 | cOther.status as group_status | |
646 | FROM civicrm_group_contact cMain | |
647 | INNER JOIN civicrm_group_contact cOther ON cMain.group_id = cOther.group_id | |
648 | WHERE cMain.contact_id = %1 | |
649 | AND cOther.contact_id = %2 | |
650 | "; | |
651 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
652 | ||
653 | $groupIDs = array(); | |
654 | while ($dao->fetch()) { | |
655 | // only copy it over if it has added status and migrate the history | |
656 | if ($dao->group_status == 'Added') { | |
657 | $groupIDs[] = $dao->group_id; | |
658 | } | |
659 | } | |
660 | ||
661 | if (!empty($groupIDs)) { | |
662 | $groupIDString = implode(',', $groupIDs); | |
663 | ||
664 | $sql = " | |
665 | UPDATE civicrm_group_contact | |
666 | SET status = 'Added' | |
667 | WHERE contact_id = %1 | |
668 | AND group_id IN ( $groupIDString ) | |
669 | "; | |
670 | CRM_Core_DAO::executeQuery($sql, $params); | |
671 | ||
672 | $sql = " | |
673 | UPDATE civicrm_subscription_history | |
674 | SET contact_id = %1 | |
675 | WHERE contact_id = %2 | |
676 | AND group_id IN ( $groupIDString ) | |
677 | "; | |
678 | CRM_Core_DAO::executeQuery($sql, $params); | |
679 | } | |
680 | ||
681 | // delete all the other group contacts | |
682 | $sql = " | |
683 | DELETE | |
684 | FROM civicrm_group_contact | |
685 | WHERE contact_id = %2 | |
686 | "; | |
687 | CRM_Core_DAO::executeQuery($sql, $params); | |
688 | ||
689 | $sql = " | |
690 | DELETE | |
691 | FROM civicrm_subscription_history | |
692 | WHERE contact_id = %2 | |
693 | "; | |
694 | CRM_Core_DAO::executeQuery($sql, $params); | |
695 | } | |
696 | ||
697 | /** | |
698 | * Given an array of contact ids, add all the contacts to the group | |
699 | * | |
77c5b619 TO |
700 | * @param array $contactIDs |
701 | * The array of contact ids to be added. | |
702 | * @param int $groupID | |
703 | * The id of the group. | |
2a6da8d7 EM |
704 | * @param string $method |
705 | * @param string $status | |
e60f24eb | 706 | * @param NULL $tracking |
6a488035 | 707 | * |
a6c01b45 CW |
708 | * @return array |
709 | * (total, added, notAdded) count of contacts added to group | |
6a488035 | 710 | */ |
608e6658 | 711 | public static function bulkAddContactsToGroup( |
6a488035 TO |
712 | $contactIDs, |
713 | $groupID, | |
242bd179 TO |
714 | $method = 'Admin', |
715 | $status = 'Added', | |
6a488035 TO |
716 | $tracking = NULL |
717 | ) { | |
718 | ||
719 | $numContactsAdded = 0; | |
720 | $numContactsNotAdded = 0; | |
721 | ||
722 | $contactGroupSQL = " | |
723 | REPLACE INTO civicrm_group_contact ( group_id, contact_id, status ) | |
724 | VALUES | |
725 | "; | |
726 | $subscriptioHistorySQL = " | |
727 | INSERT INTO civicrm_subscription_history( group_id, contact_id, date, method, status, tracking ) | |
728 | VALUES | |
729 | "; | |
730 | ||
731 | $date = date('YmdHis'); | |
732 | ||
733 | // to avoid long strings, lets do BULK_INSERT_HIGH_COUNT values at a time | |
734 | while (!empty($contactIDs)) { | |
735 | $input = array_splice($contactIDs, 0, CRM_Core_DAO::BULK_INSERT_HIGH_COUNT); | |
736 | $contactStr = implode(',', $input); | |
737 | ||
738 | // lets check their current status | |
739 | $sql = " | |
740 | SELECT GROUP_CONCAT(contact_id) as contactStr | |
741 | FROM civicrm_group_contact | |
742 | WHERE group_id = %1 | |
743 | AND status = %2 | |
744 | AND contact_id IN ( $contactStr ) | |
745 | "; | |
6ea503d4 TO |
746 | $params = array( |
747 | 1 => array($groupID, 'Integer'), | |
6a488035 TO |
748 | 2 => array($status, 'String'), |
749 | ); | |
750 | ||
751 | $presentIDs = array(); | |
752 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
753 | if ($dao->fetch()) { | |
754 | $presentIDs = explode(',', $dao->contactStr); | |
755 | $presentIDs = array_flip($presentIDs); | |
756 | } | |
757 | ||
758 | $gcValues = $shValues = array(); | |
759 | foreach ($input as $cid) { | |
760 | if (isset($presentIDs[$cid])) { | |
761 | $numContactsNotAdded++; | |
762 | continue; | |
763 | } | |
764 | ||
765 | $gcValues[] = "( $groupID, $cid, '$status' )"; | |
766 | $shValues[] = "( $groupID, $cid, '$date', '$method', '$status', '$tracking' )"; | |
767 | $numContactsAdded++; | |
768 | } | |
769 | ||
770 | if (!empty($gcValues)) { | |
771 | $cgSQL = $contactGroupSQL . implode(",\n", $gcValues); | |
772 | CRM_Core_DAO::executeQuery($cgSQL); | |
773 | ||
774 | $shSQL = $subscriptioHistorySQL . implode(",\n", $shValues); | |
775 | CRM_Core_DAO::executeQuery($shSQL); | |
776 | } | |
777 | } | |
778 | ||
779 | return array($numContactsAdded, $numContactsNotAdded); | |
780 | } | |
76773c5a CW |
781 | |
782 | /** | |
783 | * Get options for a given field. | |
784 | * @see CRM_Core_DAO::buildOptions | |
785 | * | |
77c5b619 TO |
786 | * @param string $fieldName |
787 | * @param string $context | |
608e6658 | 788 | * @see CRM_Core_DAO::buildOptionsContext |
77c5b619 | 789 | * @param array $props |
16b10e64 | 790 | * whatever is known about this dao object. |
76773c5a | 791 | * |
608e6658 | 792 | * @return array|bool |
76773c5a CW |
793 | */ |
794 | public static function buildOptions($fieldName, $context = NULL, $props = array()) { | |
795 | $params = array(); | |
796 | ||
797 | $options = CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); | |
798 | ||
76352fbc | 799 | // Sort group list by hierarchy |
800 | // TODO: This will only work when api.entity is "group_contact". What about others? | |
801 | if (($fieldName == 'group' || $fieldName == 'group_id') && ($context == 'search' || $context == 'create')) { | |
802 | $options = CRM_Contact_BAO_Group::getGroupsHierarchy($options, NULL, '- ', TRUE); | |
76773c5a CW |
803 | } |
804 | ||
805 | return $options; | |
806 | } | |
96025800 | 807 | |
6a488035 | 808 | } |