Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
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 | } | |
b2be28e3 | 101 | $values['group']['data'] = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], |
6a488035 TO |
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 | ) { | |
66fc0405 | 137 | if (empty($contactIds) || empty($groupId)) { |
138 | return array(); | |
139 | } | |
6a488035 | 140 | |
6a488035 TO |
141 | CRM_Utils_Hook::pre('create', 'GroupContact', $groupId, $contactIds); |
142 | ||
006389de TO |
143 | list($numContactsAdded, $numContactsNotAdded) |
144 | = self::bulkAddContactsToGroup($contactIds, $groupId, $method, $status, $tracking); | |
6a488035 TO |
145 | |
146 | // also reset the acl cache | |
147 | $config = CRM_Core_Config::singleton(); | |
148 | if (!$config->doNotResetCache) { | |
149 | CRM_ACL_BAO_Cache::resetCache(); | |
150 | } | |
151 | ||
152 | // reset the group contact cache for all group(s) | |
153 | // if this group is being used as a smart group | |
2b68a50c | 154 | CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); |
6a488035 TO |
155 | |
156 | CRM_Utils_Hook::post('create', 'GroupContact', $groupId, $contactIds); | |
157 | ||
158 | return array(count($contactIds), $numContactsAdded, $numContactsNotAdded); | |
159 | } | |
160 | ||
161 | /** | |
162 | * Given an array of contact ids, remove all the contacts from the group | |
163 | * | |
77c5b619 TO |
164 | * @param array $contactIds |
165 | * (reference ) the array of contact ids to be removed. | |
166 | * @param int $groupId | |
167 | * The id of the group. | |
fd31fa4c EM |
168 | * |
169 | * @param string $method | |
170 | * @param string $status | |
e60f24eb | 171 | * @param NULL $tracking |
6a488035 | 172 | * |
a6c01b45 CW |
173 | * @return array |
174 | * (total, removed, notRemoved) count of contacts removed to group | |
6a488035 | 175 | */ |
608e6658 | 176 | public static function removeContactsFromGroup( |
6a488035 TO |
177 | &$contactIds, |
178 | $groupId, | |
242bd179 TO |
179 | $method = 'Admin', |
180 | $status = 'Removed', | |
6a488035 TO |
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') { | |
242bd179 TO |
206 | $query = "DELETE FROM civicrm_group_contact WHERE contact_id=$contactId AND group_id=$groupId"; |
207 | $dao = CRM_Core_DAO::executeQuery($query); | |
6a488035 TO |
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 | |
cc7b1cd9 | 256 | // @todo consider what to do here - it feels like we should either |
257 | // 1) just invalidate the specific group's cache(& perhaps any parents) & let cron do it's thing or | |
2b68a50c | 258 | // possibly clear this specific groups cache, or just call opportunisticCacheFlush() - which would have the |
cc7b1cd9 | 259 | // same effect as the remove call. The reservation about that is that it is no more aggressive for the group that |
260 | // we know is altered than for all the others, or perhaps, more the point with it's parents & groups that use it in | |
261 | // their criteria. | |
6a488035 TO |
262 | CRM_Contact_BAO_GroupContactCache::remove(); |
263 | ||
264 | CRM_Utils_Hook::post($op, 'GroupContact', $groupId, $contactIds); | |
265 | ||
266 | return array(count($contactIds), $numContactsRemoved, $numContactsNotRemoved); | |
267 | } | |
268 | ||
269 | /** | |
fe482240 | 270 | * Get list of all the groups and groups for a contact. |
6a488035 | 271 | * |
77c5b619 TO |
272 | * @param int $contactId |
273 | * Contact id. | |
6a488035 | 274 | * |
da6b46f4 EM |
275 | * @param bool $visibility |
276 | * | |
6a488035 | 277 | * |
a6c01b45 CW |
278 | * @return array |
279 | * this array has key-> group id and value group title | |
6a488035 | 280 | */ |
00be9182 | 281 | public static function getGroupList($contactId = 0, $visibility = FALSE) { |
6a488035 TO |
282 | $group = new CRM_Contact_DAO_Group(); |
283 | ||
284 | $select = $from = $where = ''; | |
285 | ||
bad98dd5 | 286 | $select = 'SELECT civicrm_group.id, civicrm_group.title '; |
242bd179 TO |
287 | $from = ' FROM civicrm_group '; |
288 | $where = " WHERE civicrm_group.is_active = 1 "; | |
6a488035 TO |
289 | if ($contactId) { |
290 | $from .= ' , civicrm_group_contact '; | |
291 | $where .= " AND civicrm_group.id = civicrm_group_contact.group_id | |
292 | AND civicrm_group_contact.contact_id = " . CRM_Utils_Type::escape($contactId, 'Integer'); | |
293 | } | |
294 | ||
295 | if ($visibility) { | |
296 | $where .= " AND civicrm_group.visibility != 'User and User Admin Only'"; | |
297 | } | |
3636b520 | 298 | $groupBy = " GROUP BY civicrm_group.id"; |
6a488035 TO |
299 | |
300 | $orderby = " ORDER BY civicrm_group.name"; | |
bad98dd5 | 301 | $sql = $select . $from . $where . $groupBy . $orderby; |
6a488035 TO |
302 | |
303 | $group->query($sql); | |
304 | ||
305 | $values = array(); | |
306 | while ($group->fetch()) { | |
307 | $values[$group->id] = $group->title; | |
308 | } | |
309 | ||
310 | return $values; | |
311 | } | |
312 | ||
313 | /** | |
fe482240 | 314 | * Get the list of groups for contact based on status of group membership. |
6a488035 | 315 | * |
77c5b619 TO |
316 | * @param int $contactId |
317 | * Contact id. | |
318 | * @param string $status | |
319 | * State of membership. | |
320 | * @param int $numGroupContact | |
321 | * Number of groups for a contact that should be shown. | |
322 | * @param bool $count | |
323 | * True if we are interested only in the count. | |
324 | * @param bool $ignorePermission | |
325 | * True if we should ignore permissions for the current user. | |
6a488035 TO |
326 | * useful in profile where permissions are limited for the user. If left |
327 | * at false only groups viewable by the current user are returned | |
77c5b619 TO |
328 | * @param bool $onlyPublicGroups |
329 | * True if we want to hide system groups. | |
fd31fa4c EM |
330 | * |
331 | * @param bool $excludeHidden | |
6a488035 | 332 | * |
ea3ddccf | 333 | * @param int $groupId |
334 | * | |
f08cb0da | 335 | * @param bool $includeSmartGroups |
336 | * Include or Exclude Smart Group(s) | |
337 | * | |
ea3ddccf | 338 | * @return array|int $values |
339 | * the relevant data object values for the contact or the total count when $count is TRUE | |
6a488035 | 340 | */ |
3875e6b6 | 341 | public static function getContactGroup( |
6a488035 | 342 | $contactId, |
242bd179 TO |
343 | $status = NULL, |
344 | $numGroupContact = NULL, | |
345 | $count = FALSE, | |
6a488035 TO |
346 | $ignorePermission = FALSE, |
347 | $onlyPublicGroups = FALSE, | |
242bd179 | 348 | $excludeHidden = TRUE, |
b120248c | 349 | $groupId = NULL, |
965403bf | 350 | $includeSmartGroups = FALSE |
6a488035 TO |
351 | ) { |
352 | if ($count) { | |
353 | $select = 'SELECT count(DISTINCT civicrm_group_contact.id)'; | |
354 | } | |
355 | else { | |
356 | $select = 'SELECT | |
357 | civicrm_group_contact.id as civicrm_group_contact_id, | |
358 | civicrm_group.title as group_title, | |
359 | civicrm_group.visibility as visibility, | |
360 | civicrm_group_contact.status as status, | |
361 | civicrm_group.id as group_id, | |
362 | civicrm_group.is_hidden as is_hidden, | |
363 | civicrm_subscription_history.date as date, | |
364 | civicrm_subscription_history.method as method'; | |
365 | } | |
366 | ||
b120248c | 367 | $where = " WHERE contact_a.id = %1 AND civicrm_group.is_active = 1"; |
965403bf AJ |
368 | if (!$includeSmartGroups) { |
369 | $where .= " AND saved_search_id IS NULL"; | |
370 | } | |
6a488035 TO |
371 | if ($excludeHidden) { |
372 | $where .= " AND civicrm_group.is_hidden = 0 "; | |
373 | } | |
6a488035 TO |
374 | $params = array(1 => array($contactId, 'Integer')); |
375 | if (!empty($status)) { | |
376 | $where .= ' AND civicrm_group_contact.status = %2'; | |
377 | $params[2] = array($status, 'String'); | |
378 | } | |
49fb4e06 RK |
379 | if (!empty($groupId)) { |
380 | $where .= " AND civicrm_group.id = %3 "; | |
381 | $params[3] = array($groupId, 'Integer'); | |
382 | } | |
6a488035 TO |
383 | $tables = array( |
384 | 'civicrm_group_contact' => 1, | |
385 | 'civicrm_group' => 1, | |
386 | 'civicrm_subscription_history' => 1, | |
387 | ); | |
388 | $whereTables = array(); | |
389 | if ($ignorePermission) { | |
390 | $permission = ' ( 1 ) '; | |
391 | } | |
392 | else { | |
393 | $permission = CRM_Core_Permission::getPermissionedStaticGroupClause(CRM_Core_Permission::VIEW, $tables, $whereTables); | |
394 | } | |
395 | ||
396 | $from = CRM_Contact_BAO_Query::fromClause($tables); | |
397 | ||
398 | $where .= " AND $permission "; | |
399 | ||
400 | if ($onlyPublicGroups) { | |
401 | $where .= " AND civicrm_group.visibility != 'User and User Admin Only' "; | |
402 | } | |
403 | ||
404 | $order = $limit = ''; | |
405 | if (!$count) { | |
406 | $order = ' ORDER BY civicrm_group.title, civicrm_subscription_history.date ASC'; | |
407 | ||
408 | if ($numGroupContact) { | |
409 | $limit = " LIMIT 0, $numGroupContact"; | |
410 | } | |
411 | } | |
412 | ||
413 | $sql = $select . $from . $where . $order . $limit; | |
414 | ||
415 | if ($count) { | |
416 | $result = CRM_Core_DAO::singleValueQuery($sql, $params); | |
417 | return $result; | |
418 | } | |
419 | else { | |
420 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
421 | $values = array(); | |
422 | while ($dao->fetch()) { | |
423 | $id = $dao->civicrm_group_contact_id; | |
424 | $values[$id]['id'] = $id; | |
425 | $values[$id]['group_id'] = $dao->group_id; | |
426 | $values[$id]['title'] = $dao->group_title; | |
427 | $values[$id]['visibility'] = $dao->visibility; | |
428 | $values[$id]['is_hidden'] = $dao->is_hidden; | |
429 | switch ($dao->status) { | |
430 | case 'Added': | |
431 | $prefix = 'in_'; | |
432 | break; | |
433 | ||
434 | case 'Removed': | |
435 | $prefix = 'out_'; | |
436 | break; | |
437 | ||
438 | default: | |
439 | $prefix = 'pending_'; | |
440 | } | |
441 | $values[$id][$prefix . 'date'] = $dao->date; | |
442 | $values[$id][$prefix . 'method'] = $dao->method; | |
443 | if ($status == 'Removed') { | |
444 | $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 )"; | |
445 | $dateDAO = CRM_Core_DAO::executeQuery($query, $params); | |
446 | if ($dateDAO->fetch()) { | |
447 | $values[$id]['date_added'] = $dateDAO->date_added; | |
448 | } | |
449 | } | |
450 | } | |
451 | return $values; | |
452 | } | |
453 | } | |
454 | ||
455 | /** | |
fe482240 | 456 | * Returns membership details of a contact for a group. |
6a488035 | 457 | * |
77c5b619 TO |
458 | * @param int $contactId |
459 | * Id of the contact. | |
460 | * @param int $groupID | |
3b992cb6 | 461 | * Id of a particular group. |
77c5b619 TO |
462 | * @param string $method |
463 | * If we want the subscription history details for a specific method. | |
6a488035 | 464 | * |
a6c01b45 CW |
465 | * @return object |
466 | * of group contact | |
6a488035 | 467 | */ |
00be9182 | 468 | public static function getMembershipDetail($contactId, $groupID, $method = 'Email') { |
e60f24eb | 469 | $leftJoin = $where = $orderBy = NULL; |
6a488035 TO |
470 | |
471 | if ($method) { | |
071c6f10 BS |
472 | //CRM-13341 add group_id clause |
473 | $leftJoin = " | |
474 | LEFT JOIN civicrm_subscription_history | |
475 | ON ( civicrm_group_contact.contact_id = civicrm_subscription_history.contact_id | |
476 | AND civicrm_subscription_history.group_id = {$groupID} )"; | |
6a488035 TO |
477 | $where = "AND civicrm_subscription_history.method ='Email'"; |
478 | $orderBy = "ORDER BY civicrm_subscription_history.id DESC"; | |
479 | } | |
480 | $query = " | |
481 | SELECT * | |
482 | FROM civicrm_group_contact | |
483 | $leftJoin | |
484 | WHERE civicrm_group_contact.contact_id = %1 | |
485 | AND civicrm_group_contact.group_id = %2 | |
486 | $where | |
487 | $orderBy | |
488 | "; | |
489 | ||
490 | $params = array( | |
491 | 1 => array($contactId, 'Integer'), | |
492 | 2 => array($groupID, 'Integer'), | |
493 | ); | |
494 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
495 | $dao->fetch(); | |
496 | return $dao; | |
497 | } | |
498 | ||
6a488035 | 499 | /** |
fe482240 | 500 | * Method to get Group Id. |
6a488035 | 501 | * |
77c5b619 | 502 | * @param int $groupContactID |
3b992cb6 | 503 | * Id of a particular group. |
6a488035 TO |
504 | * |
505 | * | |
506 | * @return groupID | |
6a488035 | 507 | */ |
00be9182 | 508 | public static function getGroupId($groupContactID) { |
6a488035 TO |
509 | $dao = new CRM_Contact_DAO_GroupContact(); |
510 | $dao->id = $groupContactID; | |
511 | $dao->find(TRUE); | |
512 | return $dao->group_id; | |
513 | } | |
514 | ||
515 | /** | |
100fef9d | 516 | * Takes an associative array and creates / removes |
6a488035 TO |
517 | * contacts from the groups |
518 | * | |
519 | * | |
77c5b619 TO |
520 | * @param array $params |
521 | * (reference ) an assoc array of name/value pairs. | |
522 | * @param array $contactId | |
523 | * Contact id. | |
2a6da8d7 EM |
524 | * |
525 | * @param bool $visibility | |
526 | * @param string $method | |
6a488035 | 527 | */ |
00be9182 | 528 | public static function create(&$params, $contactId, $visibility = FALSE, $method = 'Admin') { |
6a488035 TO |
529 | $contactIds = array(); |
530 | $contactIds[] = $contactId; | |
531 | ||
532 | //if $visibility is true we are coming in via profile mean $method = 'Web' | |
533 | $ignorePermission = FALSE; | |
534 | if ($visibility) { | |
535 | $ignorePermission = TRUE; | |
536 | } | |
537 | ||
538 | if ($contactId) { | |
f078bc70 | 539 | $contactGroupList = CRM_Contact_BAO_GroupContact::getContactGroup($contactId, 'Added', |
6a488035 TO |
540 | NULL, FALSE, $ignorePermission |
541 | ); | |
542 | if (is_array($contactGroupList)) { | |
543 | foreach ($contactGroupList as $key) { | |
544 | $groupId = $key['group_id']; | |
545 | $contactGroup[$groupId] = $groupId; | |
546 | } | |
547 | } | |
548 | } | |
549 | ||
550 | // get the list of all the groups | |
551 | $allGroup = CRM_Contact_BAO_GroupContact::getGroupList(0, $visibility); | |
552 | ||
553 | // this fix is done to prevent warning generated by array_key_exits incase of empty array is given as input | |
554 | if (!is_array($params)) { | |
555 | $params = array(); | |
556 | } | |
557 | ||
558 | // this fix is done to prevent warning generated by array_key_exits incase of empty array is given as input | |
559 | if (!isset($contactGroup) || !is_array($contactGroup)) { | |
560 | $contactGroup = array(); | |
561 | } | |
562 | ||
563 | // check which values has to be add/remove contact from group | |
564 | foreach ($allGroup as $key => $varValue) { | |
a7488080 | 565 | if (!empty($params[$key]) && !array_key_exists($key, $contactGroup)) { |
6a488035 TO |
566 | // add contact to group |
567 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $key, $method); | |
568 | } | |
a7488080 | 569 | elseif (empty($params[$key]) && array_key_exists($key, $contactGroup)) { |
6a488035 TO |
570 | // remove contact from group |
571 | CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactIds, $key, $method); | |
572 | } | |
573 | } | |
574 | } | |
575 | ||
86538308 | 576 | /** |
100fef9d CW |
577 | * @param int $contactID |
578 | * @param int $groupID | |
86538308 EM |
579 | * |
580 | * @return bool | |
581 | */ | |
00be9182 | 582 | public static function isContactInGroup($contactID, $groupID) { |
6a488035 TO |
583 | if (!CRM_Utils_Rule::positiveInteger($contactID) || |
584 | !CRM_Utils_Rule::positiveInteger($groupID) | |
585 | ) { | |
586 | return FALSE; | |
587 | } | |
588 | ||
589 | $params = array( | |
3b992cb6 | 590 | array('group', 'IN', array($groupID), 0, 0), |
6a488035 TO |
591 | array('contact_id', '=', $contactID, 0, 0), |
592 | ); | |
593 | list($contacts, $_) = CRM_Contact_BAO_Query::apiQuery($params, array('contact_id')); | |
594 | ||
595 | if (!empty($contacts)) { | |
596 | return TRUE; | |
597 | } | |
598 | return FALSE; | |
599 | } | |
600 | ||
601 | /** | |
fe482240 | 602 | * Function merges the groups from otherContactID to mainContactID. |
6a488035 TO |
603 | * along with subscription history |
604 | * | |
77c5b619 TO |
605 | * @param int $mainContactId |
606 | * Contact id of main contact record. | |
607 | * @param int $otherContactId | |
608 | * Contact id of record which is going to merge. | |
6a488035 TO |
609 | * |
610 | * @see CRM_Dedupe_Merger::cpTables() | |
611 | * | |
612 | * TODO: use the 3rd $sqls param to append sql statements rather than executing them here | |
6a488035 | 613 | */ |
00be9182 | 614 | public static function mergeGroupContact($mainContactId, $otherContactId) { |
6ea503d4 TO |
615 | $params = array( |
616 | 1 => array($mainContactId, 'Integer'), | |
6a488035 TO |
617 | 2 => array($otherContactId, 'Integer'), |
618 | ); | |
619 | ||
620 | // find all groups that are in otherContactID but not in mainContactID, copy them over | |
621 | $sql = " | |
622 | SELECT cOther.group_id | |
623 | FROM civicrm_group_contact cOther | |
624 | LEFT JOIN civicrm_group_contact cMain ON cOther.group_id = cMain.group_id AND cMain.contact_id = %1 | |
625 | WHERE cOther.contact_id = %2 | |
626 | AND cMain.contact_id IS NULL | |
627 | "; | |
628 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
629 | ||
630 | $otherGroupIDs = array(); | |
631 | while ($dao->fetch()) { | |
632 | $otherGroupIDs[] = $dao->group_id; | |
633 | } | |
634 | ||
635 | if (!empty($otherGroupIDs)) { | |
636 | $otherGroupIDString = implode(',', $otherGroupIDs); | |
637 | ||
638 | $sql = " | |
639 | UPDATE civicrm_group_contact | |
640 | SET contact_id = %1 | |
641 | WHERE contact_id = %2 | |
642 | AND group_id IN ( $otherGroupIDString ) | |
643 | "; | |
644 | CRM_Core_DAO::executeQuery($sql, $params); | |
645 | ||
646 | $sql = " | |
647 | UPDATE civicrm_subscription_history | |
648 | SET contact_id = %1 | |
649 | WHERE contact_id = %2 | |
650 | AND group_id IN ( $otherGroupIDString ) | |
651 | "; | |
652 | CRM_Core_DAO::executeQuery($sql, $params); | |
653 | } | |
654 | ||
655 | $sql = " | |
656 | SELECT cOther.group_id as group_id, | |
657 | cOther.status as group_status | |
658 | FROM civicrm_group_contact cMain | |
659 | INNER JOIN civicrm_group_contact cOther ON cMain.group_id = cOther.group_id | |
660 | WHERE cMain.contact_id = %1 | |
661 | AND cOther.contact_id = %2 | |
662 | "; | |
663 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
664 | ||
665 | $groupIDs = array(); | |
666 | while ($dao->fetch()) { | |
667 | // only copy it over if it has added status and migrate the history | |
668 | if ($dao->group_status == 'Added') { | |
669 | $groupIDs[] = $dao->group_id; | |
670 | } | |
671 | } | |
672 | ||
673 | if (!empty($groupIDs)) { | |
674 | $groupIDString = implode(',', $groupIDs); | |
675 | ||
676 | $sql = " | |
677 | UPDATE civicrm_group_contact | |
678 | SET status = 'Added' | |
679 | WHERE contact_id = %1 | |
680 | AND group_id IN ( $groupIDString ) | |
681 | "; | |
682 | CRM_Core_DAO::executeQuery($sql, $params); | |
683 | ||
684 | $sql = " | |
685 | UPDATE civicrm_subscription_history | |
686 | SET contact_id = %1 | |
687 | WHERE contact_id = %2 | |
688 | AND group_id IN ( $groupIDString ) | |
689 | "; | |
690 | CRM_Core_DAO::executeQuery($sql, $params); | |
691 | } | |
692 | ||
693 | // delete all the other group contacts | |
694 | $sql = " | |
695 | DELETE | |
696 | FROM civicrm_group_contact | |
697 | WHERE contact_id = %2 | |
698 | "; | |
699 | CRM_Core_DAO::executeQuery($sql, $params); | |
700 | ||
701 | $sql = " | |
702 | DELETE | |
703 | FROM civicrm_subscription_history | |
704 | WHERE contact_id = %2 | |
705 | "; | |
706 | CRM_Core_DAO::executeQuery($sql, $params); | |
707 | } | |
708 | ||
709 | /** | |
710 | * Given an array of contact ids, add all the contacts to the group | |
711 | * | |
77c5b619 TO |
712 | * @param array $contactIDs |
713 | * The array of contact ids to be added. | |
714 | * @param int $groupID | |
715 | * The id of the group. | |
2a6da8d7 EM |
716 | * @param string $method |
717 | * @param string $status | |
e60f24eb | 718 | * @param NULL $tracking |
6a488035 | 719 | * |
a6c01b45 CW |
720 | * @return array |
721 | * (total, added, notAdded) count of contacts added to group | |
6a488035 | 722 | */ |
608e6658 | 723 | public static function bulkAddContactsToGroup( |
6a488035 TO |
724 | $contactIDs, |
725 | $groupID, | |
242bd179 TO |
726 | $method = 'Admin', |
727 | $status = 'Added', | |
6a488035 TO |
728 | $tracking = NULL |
729 | ) { | |
730 | ||
731 | $numContactsAdded = 0; | |
732 | $numContactsNotAdded = 0; | |
733 | ||
734 | $contactGroupSQL = " | |
735 | REPLACE INTO civicrm_group_contact ( group_id, contact_id, status ) | |
736 | VALUES | |
737 | "; | |
738 | $subscriptioHistorySQL = " | |
739 | INSERT INTO civicrm_subscription_history( group_id, contact_id, date, method, status, tracking ) | |
740 | VALUES | |
741 | "; | |
742 | ||
743 | $date = date('YmdHis'); | |
744 | ||
745 | // to avoid long strings, lets do BULK_INSERT_HIGH_COUNT values at a time | |
746 | while (!empty($contactIDs)) { | |
747 | $input = array_splice($contactIDs, 0, CRM_Core_DAO::BULK_INSERT_HIGH_COUNT); | |
748 | $contactStr = implode(',', $input); | |
749 | ||
750 | // lets check their current status | |
751 | $sql = " | |
752 | SELECT GROUP_CONCAT(contact_id) as contactStr | |
753 | FROM civicrm_group_contact | |
754 | WHERE group_id = %1 | |
755 | AND status = %2 | |
756 | AND contact_id IN ( $contactStr ) | |
757 | "; | |
6ea503d4 TO |
758 | $params = array( |
759 | 1 => array($groupID, 'Integer'), | |
6a488035 TO |
760 | 2 => array($status, 'String'), |
761 | ); | |
762 | ||
763 | $presentIDs = array(); | |
764 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
765 | if ($dao->fetch()) { | |
766 | $presentIDs = explode(',', $dao->contactStr); | |
767 | $presentIDs = array_flip($presentIDs); | |
768 | } | |
769 | ||
770 | $gcValues = $shValues = array(); | |
771 | foreach ($input as $cid) { | |
772 | if (isset($presentIDs[$cid])) { | |
773 | $numContactsNotAdded++; | |
774 | continue; | |
775 | } | |
776 | ||
777 | $gcValues[] = "( $groupID, $cid, '$status' )"; | |
778 | $shValues[] = "( $groupID, $cid, '$date', '$method', '$status', '$tracking' )"; | |
779 | $numContactsAdded++; | |
780 | } | |
781 | ||
782 | if (!empty($gcValues)) { | |
783 | $cgSQL = $contactGroupSQL . implode(",\n", $gcValues); | |
784 | CRM_Core_DAO::executeQuery($cgSQL); | |
785 | ||
786 | $shSQL = $subscriptioHistorySQL . implode(",\n", $shValues); | |
787 | CRM_Core_DAO::executeQuery($shSQL); | |
788 | } | |
789 | } | |
790 | ||
791 | return array($numContactsAdded, $numContactsNotAdded); | |
792 | } | |
76773c5a CW |
793 | |
794 | /** | |
795 | * Get options for a given field. | |
796 | * @see CRM_Core_DAO::buildOptions | |
797 | * | |
77c5b619 TO |
798 | * @param string $fieldName |
799 | * @param string $context | |
608e6658 | 800 | * @see CRM_Core_DAO::buildOptionsContext |
77c5b619 | 801 | * @param array $props |
16b10e64 | 802 | * whatever is known about this dao object. |
76773c5a | 803 | * |
608e6658 | 804 | * @return array|bool |
76773c5a CW |
805 | */ |
806 | public static function buildOptions($fieldName, $context = NULL, $props = array()) { | |
807 | $params = array(); | |
808 | ||
809 | $options = CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); | |
810 | ||
76352fbc | 811 | // Sort group list by hierarchy |
812 | // TODO: This will only work when api.entity is "group_contact". What about others? | |
813 | if (($fieldName == 'group' || $fieldName == 'group_id') && ($context == 'search' || $context == 'create')) { | |
814 | $options = CRM_Contact_BAO_Group::getGroupsHierarchy($options, NULL, '- ', TRUE); | |
76773c5a CW |
815 | } |
816 | ||
817 | return $options; | |
818 | } | |
96025800 | 819 | |
6a488035 | 820 | } |