Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
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 | */ |
35cb4b48 CW |
54 | public static function add($params) { |
55 | $hook = empty($params['id']) ? 'create' : 'edit'; | |
56 | CRM_Utils_Hook::pre($hook, 'GroupContact', CRM_Utils_Array::value('id', $params), $params); | |
6a488035 | 57 | |
35cb4b48 | 58 | if (!self::dataExists($params)) { |
6a488035 TO |
59 | return NULL; |
60 | } | |
61 | ||
62 | $groupContact = new CRM_Contact_BAO_GroupContact(); | |
63 | $groupContact->copyValues($params); | |
6a488035 | 64 | $groupContact->save(); |
35cb4b48 CW |
65 | |
66 | // Lookup existing info for the sake of subscription history | |
67 | if (!empty($params['id'])) { | |
68 | $groupContact->find(TRUE); | |
69 | $params = $groupContact->toArray(); | |
70 | } | |
71 | CRM_Contact_BAO_SubscriptionHistory::create($params); | |
72 | ||
73 | CRM_Utils_Hook::post($hook, 'GroupContact', $groupContact->id, $groupContact); | |
74 | ||
6a488035 TO |
75 | return $groupContact; |
76 | } | |
77 | ||
78 | /** | |
fe482240 | 79 | * Check if there is data to create the object. |
6a488035 | 80 | * |
77c5b619 TO |
81 | * @param array $params |
82 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 83 | * |
608e6658 | 84 | * @return bool |
6a488035 | 85 | */ |
00be9182 | 86 | public static function dataExists(&$params) { |
35cb4b48 | 87 | return (!empty($params['id']) || (!empty($params['group_id']) && !empty($params['contact_id']))); |
6a488035 TO |
88 | } |
89 | ||
90 | /** | |
91 | * Given the list of params in the params array, fetch the object | |
92 | * and store the values in the values array | |
93 | * | |
77c5b619 TO |
94 | * @param array $params |
95 | * Input parameters to find object. | |
96 | * @param array $values | |
97 | * Output values of the object. | |
6c8f6e67 | 98 | * |
a6c01b45 CW |
99 | * @return array |
100 | * (reference) the values that could be potentially assigned to smarty | |
6a488035 | 101 | */ |
00be9182 | 102 | public static function getValues(&$params, &$values) { |
6a488035 TO |
103 | if (empty($params)) { |
104 | return NULL; | |
105 | } | |
b2be28e3 | 106 | $values['group']['data'] = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], |
6a488035 TO |
107 | 'Added', |
108 | 3 | |
109 | ); | |
110 | ||
111 | // get the total count of groups | |
112 | $values['group']['totalCount'] = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], | |
113 | 'Added', | |
114 | NULL, | |
115 | TRUE | |
116 | ); | |
117 | ||
118 | return NULL; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Given an array of contact ids, add all the contacts to the group | |
123 | * | |
77c5b619 TO |
124 | * @param array $contactIds |
125 | * The array of contact ids to be added. | |
126 | * @param int $groupId | |
127 | * The id of the group. | |
c49a2977 CW |
128 | * @param string $method |
129 | * @param string $status | |
130 | * @param int $tracking | |
6a488035 | 131 | * |
a6c01b45 CW |
132 | * @return array |
133 | * (total, added, notAdded) count of contacts added to group | |
6a488035 | 134 | */ |
608e6658 | 135 | public static function addContactsToGroup( |
c49a2977 | 136 | $contactIds, |
6a488035 | 137 | $groupId, |
242bd179 TO |
138 | $method = 'Admin', |
139 | $status = 'Added', | |
6a488035 TO |
140 | $tracking = NULL |
141 | ) { | |
66fc0405 | 142 | if (empty($contactIds) || empty($groupId)) { |
143 | return array(); | |
144 | } | |
6a488035 | 145 | |
6a488035 TO |
146 | CRM_Utils_Hook::pre('create', 'GroupContact', $groupId, $contactIds); |
147 | ||
006389de TO |
148 | list($numContactsAdded, $numContactsNotAdded) |
149 | = self::bulkAddContactsToGroup($contactIds, $groupId, $method, $status, $tracking); | |
6a488035 | 150 | |
0626851e | 151 | CRM_Contact_BAO_Contact_Utils::clearContactCaches(); |
6a488035 TO |
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 | ||
0626851e | 245 | CRM_Contact_BAO_Contact_Utils::clearContactCaches(); |
6a488035 TO |
246 | |
247 | CRM_Utils_Hook::post($op, 'GroupContact', $groupId, $contactIds); | |
248 | ||
249 | return array(count($contactIds), $numContactsRemoved, $numContactsNotRemoved); | |
250 | } | |
251 | ||
252 | /** | |
fe482240 | 253 | * Get list of all the groups and groups for a contact. |
6a488035 | 254 | * |
77c5b619 TO |
255 | * @param int $contactId |
256 | * Contact id. | |
6a488035 | 257 | * |
da6b46f4 EM |
258 | * @param bool $visibility |
259 | * | |
6a488035 | 260 | * |
a6c01b45 CW |
261 | * @return array |
262 | * this array has key-> group id and value group title | |
6a488035 | 263 | */ |
00be9182 | 264 | public static function getGroupList($contactId = 0, $visibility = FALSE) { |
6a488035 TO |
265 | $group = new CRM_Contact_DAO_Group(); |
266 | ||
267 | $select = $from = $where = ''; | |
268 | ||
bad98dd5 | 269 | $select = 'SELECT civicrm_group.id, civicrm_group.title '; |
242bd179 TO |
270 | $from = ' FROM civicrm_group '; |
271 | $where = " WHERE civicrm_group.is_active = 1 "; | |
6a488035 TO |
272 | if ($contactId) { |
273 | $from .= ' , civicrm_group_contact '; | |
274 | $where .= " AND civicrm_group.id = civicrm_group_contact.group_id | |
275 | AND civicrm_group_contact.contact_id = " . CRM_Utils_Type::escape($contactId, 'Integer'); | |
276 | } | |
277 | ||
278 | if ($visibility) { | |
279 | $where .= " AND civicrm_group.visibility != 'User and User Admin Only'"; | |
280 | } | |
3636b520 | 281 | $groupBy = " GROUP BY civicrm_group.id"; |
6a488035 TO |
282 | |
283 | $orderby = " ORDER BY civicrm_group.name"; | |
bad98dd5 | 284 | $sql = $select . $from . $where . $groupBy . $orderby; |
6a488035 TO |
285 | |
286 | $group->query($sql); | |
287 | ||
288 | $values = array(); | |
289 | while ($group->fetch()) { | |
290 | $values[$group->id] = $group->title; | |
291 | } | |
292 | ||
293 | return $values; | |
294 | } | |
295 | ||
296 | /** | |
fe482240 | 297 | * Get the list of groups for contact based on status of group membership. |
6a488035 | 298 | * |
77c5b619 TO |
299 | * @param int $contactId |
300 | * Contact id. | |
301 | * @param string $status | |
302 | * State of membership. | |
303 | * @param int $numGroupContact | |
304 | * Number of groups for a contact that should be shown. | |
305 | * @param bool $count | |
306 | * True if we are interested only in the count. | |
307 | * @param bool $ignorePermission | |
308 | * True if we should ignore permissions for the current user. | |
6a488035 TO |
309 | * useful in profile where permissions are limited for the user. If left |
310 | * at false only groups viewable by the current user are returned | |
77c5b619 TO |
311 | * @param bool $onlyPublicGroups |
312 | * True if we want to hide system groups. | |
fd31fa4c EM |
313 | * |
314 | * @param bool $excludeHidden | |
6a488035 | 315 | * |
ea3ddccf | 316 | * @param int $groupId |
317 | * | |
f08cb0da | 318 | * @param bool $includeSmartGroups |
319 | * Include or Exclude Smart Group(s) | |
320 | * | |
ea3ddccf | 321 | * @return array|int $values |
322 | * the relevant data object values for the contact or the total count when $count is TRUE | |
6a488035 | 323 | */ |
3875e6b6 | 324 | public static function getContactGroup( |
6a488035 | 325 | $contactId, |
242bd179 TO |
326 | $status = NULL, |
327 | $numGroupContact = NULL, | |
328 | $count = FALSE, | |
6a488035 TO |
329 | $ignorePermission = FALSE, |
330 | $onlyPublicGroups = FALSE, | |
242bd179 | 331 | $excludeHidden = TRUE, |
b120248c | 332 | $groupId = NULL, |
965403bf | 333 | $includeSmartGroups = FALSE |
6a488035 TO |
334 | ) { |
335 | if ($count) { | |
336 | $select = 'SELECT count(DISTINCT civicrm_group_contact.id)'; | |
337 | } | |
338 | else { | |
339 | $select = 'SELECT | |
340 | civicrm_group_contact.id as civicrm_group_contact_id, | |
341 | civicrm_group.title as group_title, | |
342 | civicrm_group.visibility as visibility, | |
343 | civicrm_group_contact.status as status, | |
344 | civicrm_group.id as group_id, | |
345 | civicrm_group.is_hidden as is_hidden, | |
346 | civicrm_subscription_history.date as date, | |
347 | civicrm_subscription_history.method as method'; | |
348 | } | |
349 | ||
b120248c | 350 | $where = " WHERE contact_a.id = %1 AND civicrm_group.is_active = 1"; |
965403bf AJ |
351 | if (!$includeSmartGroups) { |
352 | $where .= " AND saved_search_id IS NULL"; | |
353 | } | |
6a488035 TO |
354 | if ($excludeHidden) { |
355 | $where .= " AND civicrm_group.is_hidden = 0 "; | |
356 | } | |
6a488035 TO |
357 | $params = array(1 => array($contactId, 'Integer')); |
358 | if (!empty($status)) { | |
359 | $where .= ' AND civicrm_group_contact.status = %2'; | |
360 | $params[2] = array($status, 'String'); | |
361 | } | |
49fb4e06 RK |
362 | if (!empty($groupId)) { |
363 | $where .= " AND civicrm_group.id = %3 "; | |
364 | $params[3] = array($groupId, 'Integer'); | |
365 | } | |
6a488035 TO |
366 | $tables = array( |
367 | 'civicrm_group_contact' => 1, | |
368 | 'civicrm_group' => 1, | |
369 | 'civicrm_subscription_history' => 1, | |
370 | ); | |
371 | $whereTables = array(); | |
372 | if ($ignorePermission) { | |
373 | $permission = ' ( 1 ) '; | |
374 | } | |
375 | else { | |
376 | $permission = CRM_Core_Permission::getPermissionedStaticGroupClause(CRM_Core_Permission::VIEW, $tables, $whereTables); | |
377 | } | |
378 | ||
379 | $from = CRM_Contact_BAO_Query::fromClause($tables); | |
380 | ||
381 | $where .= " AND $permission "; | |
382 | ||
383 | if ($onlyPublicGroups) { | |
384 | $where .= " AND civicrm_group.visibility != 'User and User Admin Only' "; | |
385 | } | |
386 | ||
387 | $order = $limit = ''; | |
388 | if (!$count) { | |
389 | $order = ' ORDER BY civicrm_group.title, civicrm_subscription_history.date ASC'; | |
390 | ||
391 | if ($numGroupContact) { | |
392 | $limit = " LIMIT 0, $numGroupContact"; | |
393 | } | |
394 | } | |
395 | ||
396 | $sql = $select . $from . $where . $order . $limit; | |
397 | ||
398 | if ($count) { | |
399 | $result = CRM_Core_DAO::singleValueQuery($sql, $params); | |
400 | return $result; | |
401 | } | |
402 | else { | |
403 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
404 | $values = array(); | |
405 | while ($dao->fetch()) { | |
406 | $id = $dao->civicrm_group_contact_id; | |
407 | $values[$id]['id'] = $id; | |
408 | $values[$id]['group_id'] = $dao->group_id; | |
409 | $values[$id]['title'] = $dao->group_title; | |
410 | $values[$id]['visibility'] = $dao->visibility; | |
411 | $values[$id]['is_hidden'] = $dao->is_hidden; | |
412 | switch ($dao->status) { | |
413 | case 'Added': | |
414 | $prefix = 'in_'; | |
415 | break; | |
416 | ||
417 | case 'Removed': | |
418 | $prefix = 'out_'; | |
419 | break; | |
420 | ||
421 | default: | |
422 | $prefix = 'pending_'; | |
423 | } | |
424 | $values[$id][$prefix . 'date'] = $dao->date; | |
425 | $values[$id][$prefix . 'method'] = $dao->method; | |
426 | if ($status == 'Removed') { | |
427 | $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 )"; | |
428 | $dateDAO = CRM_Core_DAO::executeQuery($query, $params); | |
429 | if ($dateDAO->fetch()) { | |
430 | $values[$id]['date_added'] = $dateDAO->date_added; | |
431 | } | |
432 | } | |
433 | } | |
434 | return $values; | |
435 | } | |
436 | } | |
437 | ||
438 | /** | |
fe482240 | 439 | * Returns membership details of a contact for a group. |
6a488035 | 440 | * |
77c5b619 TO |
441 | * @param int $contactId |
442 | * Id of the contact. | |
443 | * @param int $groupID | |
3b992cb6 | 444 | * Id of a particular group. |
77c5b619 TO |
445 | * @param string $method |
446 | * If we want the subscription history details for a specific method. | |
6a488035 | 447 | * |
a6c01b45 CW |
448 | * @return object |
449 | * of group contact | |
6a488035 | 450 | */ |
00be9182 | 451 | public static function getMembershipDetail($contactId, $groupID, $method = 'Email') { |
e60f24eb | 452 | $leftJoin = $where = $orderBy = NULL; |
6a488035 TO |
453 | |
454 | if ($method) { | |
071c6f10 BS |
455 | //CRM-13341 add group_id clause |
456 | $leftJoin = " | |
457 | LEFT JOIN civicrm_subscription_history | |
458 | ON ( civicrm_group_contact.contact_id = civicrm_subscription_history.contact_id | |
459 | AND civicrm_subscription_history.group_id = {$groupID} )"; | |
6a488035 TO |
460 | $where = "AND civicrm_subscription_history.method ='Email'"; |
461 | $orderBy = "ORDER BY civicrm_subscription_history.id DESC"; | |
462 | } | |
463 | $query = " | |
464 | SELECT * | |
465 | FROM civicrm_group_contact | |
466 | $leftJoin | |
467 | WHERE civicrm_group_contact.contact_id = %1 | |
468 | AND civicrm_group_contact.group_id = %2 | |
469 | $where | |
470 | $orderBy | |
471 | "; | |
472 | ||
473 | $params = array( | |
474 | 1 => array($contactId, 'Integer'), | |
475 | 2 => array($groupID, 'Integer'), | |
476 | ); | |
477 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
478 | $dao->fetch(); | |
479 | return $dao; | |
480 | } | |
481 | ||
6a488035 | 482 | /** |
fe482240 | 483 | * Method to get Group Id. |
6a488035 | 484 | * |
77c5b619 | 485 | * @param int $groupContactID |
3b992cb6 | 486 | * Id of a particular group. |
6a488035 TO |
487 | * |
488 | * | |
489 | * @return groupID | |
6a488035 | 490 | */ |
00be9182 | 491 | public static function getGroupId($groupContactID) { |
6a488035 TO |
492 | $dao = new CRM_Contact_DAO_GroupContact(); |
493 | $dao->id = $groupContactID; | |
494 | $dao->find(TRUE); | |
495 | return $dao->group_id; | |
496 | } | |
497 | ||
498 | /** | |
100fef9d | 499 | * Takes an associative array and creates / removes |
6a488035 TO |
500 | * contacts from the groups |
501 | * | |
502 | * | |
77c5b619 TO |
503 | * @param array $params |
504 | * (reference ) an assoc array of name/value pairs. | |
505 | * @param array $contactId | |
506 | * Contact id. | |
2a6da8d7 EM |
507 | * |
508 | * @param bool $visibility | |
509 | * @param string $method | |
6a488035 | 510 | */ |
00be9182 | 511 | public static function create(&$params, $contactId, $visibility = FALSE, $method = 'Admin') { |
6a488035 TO |
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) { | |
f078bc70 | 522 | $contactGroupList = CRM_Contact_BAO_GroupContact::getContactGroup($contactId, 'Added', |
6a488035 TO |
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 | */ | |
00be9182 | 565 | public static function isContactInGroup($contactID, $groupID) { |
6a488035 TO |
566 | if (!CRM_Utils_Rule::positiveInteger($contactID) || |
567 | !CRM_Utils_Rule::positiveInteger($groupID) | |
568 | ) { | |
569 | return FALSE; | |
570 | } | |
571 | ||
572 | $params = array( | |
3b992cb6 | 573 | array('group', 'IN', array($groupID), 0, 0), |
6a488035 TO |
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 | /** | |
fe482240 | 585 | * Function merges the groups from otherContactID to mainContactID. |
6a488035 TO |
586 | * along with subscription history |
587 | * | |
77c5b619 TO |
588 | * @param int $mainContactId |
589 | * Contact id of main contact record. | |
590 | * @param int $otherContactId | |
591 | * Contact id of record which is going to merge. | |
6a488035 TO |
592 | * |
593 | * @see CRM_Dedupe_Merger::cpTables() | |
594 | * | |
595 | * TODO: use the 3rd $sqls param to append sql statements rather than executing them here | |
6a488035 | 596 | */ |
00be9182 | 597 | public static function mergeGroupContact($mainContactId, $otherContactId) { |
6ea503d4 TO |
598 | $params = array( |
599 | 1 => array($mainContactId, 'Integer'), | |
6a488035 TO |
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 | * | |
77c5b619 TO |
695 | * @param array $contactIDs |
696 | * The array of contact ids to be added. | |
697 | * @param int $groupID | |
698 | * The id of the group. | |
2a6da8d7 EM |
699 | * @param string $method |
700 | * @param string $status | |
e60f24eb | 701 | * @param NULL $tracking |
6a488035 | 702 | * |
a6c01b45 CW |
703 | * @return array |
704 | * (total, added, notAdded) count of contacts added to group | |
6a488035 | 705 | */ |
608e6658 | 706 | public static function bulkAddContactsToGroup( |
6a488035 TO |
707 | $contactIDs, |
708 | $groupID, | |
242bd179 TO |
709 | $method = 'Admin', |
710 | $status = 'Added', | |
6a488035 TO |
711 | $tracking = NULL |
712 | ) { | |
713 | ||
714 | $numContactsAdded = 0; | |
715 | $numContactsNotAdded = 0; | |
716 | ||
717 | $contactGroupSQL = " | |
718 | REPLACE INTO civicrm_group_contact ( group_id, contact_id, status ) | |
719 | VALUES | |
720 | "; | |
721 | $subscriptioHistorySQL = " | |
722 | INSERT INTO civicrm_subscription_history( group_id, contact_id, date, method, status, tracking ) | |
723 | VALUES | |
724 | "; | |
725 | ||
726 | $date = date('YmdHis'); | |
727 | ||
728 | // to avoid long strings, lets do BULK_INSERT_HIGH_COUNT values at a time | |
729 | while (!empty($contactIDs)) { | |
730 | $input = array_splice($contactIDs, 0, CRM_Core_DAO::BULK_INSERT_HIGH_COUNT); | |
731 | $contactStr = implode(',', $input); | |
732 | ||
733 | // lets check their current status | |
734 | $sql = " | |
735 | SELECT GROUP_CONCAT(contact_id) as contactStr | |
736 | FROM civicrm_group_contact | |
737 | WHERE group_id = %1 | |
738 | AND status = %2 | |
739 | AND contact_id IN ( $contactStr ) | |
740 | "; | |
6ea503d4 TO |
741 | $params = array( |
742 | 1 => array($groupID, 'Integer'), | |
6a488035 TO |
743 | 2 => array($status, 'String'), |
744 | ); | |
745 | ||
746 | $presentIDs = array(); | |
747 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
748 | if ($dao->fetch()) { | |
749 | $presentIDs = explode(',', $dao->contactStr); | |
750 | $presentIDs = array_flip($presentIDs); | |
751 | } | |
752 | ||
753 | $gcValues = $shValues = array(); | |
754 | foreach ($input as $cid) { | |
755 | if (isset($presentIDs[$cid])) { | |
756 | $numContactsNotAdded++; | |
757 | continue; | |
758 | } | |
759 | ||
760 | $gcValues[] = "( $groupID, $cid, '$status' )"; | |
761 | $shValues[] = "( $groupID, $cid, '$date', '$method', '$status', '$tracking' )"; | |
762 | $numContactsAdded++; | |
763 | } | |
764 | ||
765 | if (!empty($gcValues)) { | |
766 | $cgSQL = $contactGroupSQL . implode(",\n", $gcValues); | |
767 | CRM_Core_DAO::executeQuery($cgSQL); | |
768 | ||
769 | $shSQL = $subscriptioHistorySQL . implode(",\n", $shValues); | |
770 | CRM_Core_DAO::executeQuery($shSQL); | |
771 | } | |
772 | } | |
773 | ||
774 | return array($numContactsAdded, $numContactsNotAdded); | |
775 | } | |
76773c5a CW |
776 | |
777 | /** | |
778 | * Get options for a given field. | |
779 | * @see CRM_Core_DAO::buildOptions | |
780 | * | |
77c5b619 TO |
781 | * @param string $fieldName |
782 | * @param string $context | |
608e6658 | 783 | * @see CRM_Core_DAO::buildOptionsContext |
77c5b619 | 784 | * @param array $props |
16b10e64 | 785 | * whatever is known about this dao object. |
76773c5a | 786 | * |
608e6658 | 787 | * @return array|bool |
76773c5a CW |
788 | */ |
789 | public static function buildOptions($fieldName, $context = NULL, $props = array()) { | |
790 | $params = array(); | |
791 | ||
792 | $options = CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); | |
793 | ||
76352fbc | 794 | // Sort group list by hierarchy |
795 | // TODO: This will only work when api.entity is "group_contact". What about others? | |
796 | if (($fieldName == 'group' || $fieldName == 'group_id') && ($context == 'search' || $context == 'create')) { | |
797 | $options = CRM_Contact_BAO_Group::getGroupsHierarchy($options, NULL, '- ', TRUE); | |
76773c5a CW |
798 | } |
799 | ||
800 | return $options; | |
801 | } | |
96025800 | 802 | |
6a488035 | 803 | } |