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