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