Merge pull request #9475 from jitendrapurohit/CRM-19549
[civicrm-core.git] / CRM / Contact / BAO / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2016
32 */
33 class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group {
34
35 /**
36 * Class constructor.
37 */
38 public function __construct() {
39 parent::__construct();
40 }
41
42 /**
43 * Retrieve DB object based on input parameters.
44 *
45 * It also stores all the retrieved values in the default array.
46 *
47 * @param array $params
48 * (reference ) an assoc array of name/value pairs.
49 * @param array $defaults
50 * (reference ) an assoc array to hold the flattened values.
51 *
52 * @return CRM_Contact_BAO_Group
53 */
54 public static function retrieve(&$params, &$defaults) {
55 $group = new CRM_Contact_DAO_Group();
56 $group->copyValues($params);
57 if ($group->find(TRUE)) {
58 CRM_Core_DAO::storeValues($group, $defaults);
59 return $group;
60 }
61 }
62
63 /**
64 * Delete the group and all the object that connect to this group.
65 *
66 * Incredibly destructive.
67 *
68 * @param int $id Group id.
69 */
70 public static function discard($id) {
71 CRM_Utils_Hook::pre('delete', 'Group', $id, CRM_Core_DAO::$_nullArray);
72
73 $transaction = new CRM_Core_Transaction();
74
75 // added for CRM-1631 and CRM-1794
76 // delete all subscribed mails with the selected group id
77 $subscribe = new CRM_Mailing_Event_DAO_Subscribe();
78 $subscribe->group_id = $id;
79 $subscribe->delete();
80
81 // delete all Subscription records with the selected group id
82 $subHistory = new CRM_Contact_DAO_SubscriptionHistory();
83 $subHistory->group_id = $id;
84 $subHistory->delete();
85
86 // delete all crm_group_contact records with the selected group id
87 $groupContact = new CRM_Contact_DAO_GroupContact();
88 $groupContact->group_id = $id;
89 $groupContact->delete();
90
91 // make all the 'add_to_group_id' field of 'civicrm_uf_group table', pointing to this group, as null
92 $params = array(1 => array($id, 'Integer'));
93 $query = "UPDATE civicrm_uf_group SET `add_to_group_id`= NULL WHERE `add_to_group_id` = %1";
94 CRM_Core_DAO::executeQuery($query, $params);
95
96 $query = "UPDATE civicrm_uf_group SET `limit_listings_group_id`= NULL WHERE `limit_listings_group_id` = %1";
97 CRM_Core_DAO::executeQuery($query, $params);
98
99 // make sure u delete all the entries from civicrm_mailing_group and civicrm_campaign_group
100 // CRM-6186
101 $query = "DELETE FROM civicrm_mailing_group where entity_table = 'civicrm_group' AND entity_id = %1";
102 CRM_Core_DAO::executeQuery($query, $params);
103
104 $query = "DELETE FROM civicrm_campaign_group where entity_table = 'civicrm_group' AND entity_id = %1";
105 CRM_Core_DAO::executeQuery($query, $params);
106
107 $query = "DELETE FROM civicrm_acl_entity_role where entity_table = 'civicrm_group' AND entity_id = %1";
108 CRM_Core_DAO::executeQuery($query, $params);
109
110 // delete from group table
111 $group = new CRM_Contact_DAO_Group();
112 $group->id = $id;
113 $group->delete();
114
115 $transaction->commit();
116
117 CRM_Utils_Hook::post('delete', 'Group', $id, $group);
118
119 // delete the recently created Group
120 $groupRecent = array(
121 'id' => $id,
122 'type' => 'Group',
123 );
124 CRM_Utils_Recent::del($groupRecent);
125 }
126
127 /**
128 * Returns an array of the contacts in the given group.
129 *
130 * @param int $id
131 */
132 public static function getGroupContacts($id) {
133 $params = array(array('group', 'IN', array(1 => $id), 0, 0));
134 list($contacts, $_) = CRM_Contact_BAO_Query::apiQuery($params, array('contact_id'));
135 return $contacts;
136 }
137
138 /**
139 * Get the count of a members in a group with the specific status.
140 *
141 * @param int $id
142 * Group id.
143 * @param string $status
144 * status of members in group
145 * @param bool $countChildGroups
146 *
147 * @return int
148 * count of members in the group with above status
149 */
150 public static function memberCount($id, $status = 'Added', $countChildGroups = FALSE) {
151 $groupContact = new CRM_Contact_DAO_GroupContact();
152 $groupIds = array($id);
153 if ($countChildGroups) {
154 $groupIds = CRM_Contact_BAO_GroupNesting::getDescendentGroupIds($groupIds);
155 }
156 $count = 0;
157
158 $contacts = self::getGroupContacts($id);
159
160 foreach ($groupIds as $groupId) {
161
162 $groupContacts = self::getGroupContacts($groupId);
163 foreach ($groupContacts as $gcontact) {
164 if ($groupId != $id) {
165 // Loop through main group's contacts
166 // and subtract from the count for each contact which
167 // matches one in the present group, if it is not the
168 // main group
169 foreach ($contacts as $contact) {
170 if ($contact['contact_id'] == $gcontact['contact_id']) {
171 $count--;
172 }
173 }
174 }
175 }
176 $groupContact->group_id = $groupId;
177 if (isset($status)) {
178 $groupContact->status = $status;
179 }
180 $groupContact->_query['condition'] = 'WHERE contact_id NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1)';
181 $count += $groupContact->count();
182 }
183 return $count;
184 }
185
186 /**
187 * Get the list of member for a group id.
188 *
189 * @param int $groupID
190 * @param bool $useCache
191 * @param int $limit
192 * Number to limit to (or 0 for unlimited).
193 *
194 * @return array
195 * this array contains the list of members for this group id
196 */
197 public static function getMember($groupID, $useCache = TRUE, $limit = 0) {
198 $params = array(array('group', '=', $groupID, 0, 0));
199 $returnProperties = array('contact_id');
200 list($contacts) = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, $limit, $useCache);
201
202 $aMembers = array();
203 foreach ($contacts as $contact) {
204 $aMembers[$contact['contact_id']] = 1;
205 }
206
207 return $aMembers;
208 }
209
210 /**
211 * Returns array of group object(s) matching a set of one or Group properties.
212 *
213 * @param array $params
214 * Limits the set of groups returned.
215 * @param array $returnProperties
216 * Which properties should be included in the returned group objects.
217 * (member_count should be last element.)
218 * @param string $sort
219 * @param int $offset
220 * @param int $rowCount
221 *
222 * @return array
223 * Array of group objects.
224 *
225 *
226 * @todo other BAO functions that use returnProperties (e.g. Query Objects) receive the array flipped & filled with 1s and
227 * add in essential fields (e.g. id). This should follow a regular pattern like the others
228 */
229 public static function getGroups(
230 $params = NULL,
231 $returnProperties = NULL,
232 $sort = NULL,
233 $offset = NULL,
234 $rowCount = NULL
235 ) {
236 $dao = new CRM_Contact_DAO_Group();
237 if (!isset($params['is_active'])) {
238 $dao->is_active = 1;
239 }
240 if ($params) {
241 foreach ($params as $k => $v) {
242 if ($k == 'name' || $k == 'title') {
243 $dao->whereAdd($k . ' LIKE "' . CRM_Core_DAO::escapeString($v) . '"');
244 }
245 elseif ($k == 'group_type') {
246 foreach ((array) $v as $type) {
247 $dao->whereAdd($k . " LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . (int) $type . CRM_Core_DAO::VALUE_SEPARATOR . "%'");
248 }
249 }
250 elseif (is_array($v)) {
251 foreach ($v as &$num) {
252 $num = (int) $num;
253 }
254 $dao->whereAdd($k . ' IN (' . implode(',', $v) . ')');
255 }
256 else {
257 $dao->$k = $v;
258 }
259 }
260 }
261
262 if ($offset || $rowCount) {
263 $offset = ($offset > 0) ? $offset : 0;
264 $rowCount = ($rowCount > 0) ? $rowCount : 25;
265 $dao->limit($offset, $rowCount);
266 }
267
268 if ($sort) {
269 $dao->orderBy($sort);
270 }
271
272 // return only specific fields if returnproperties are sent
273 if (!empty($returnProperties)) {
274 $dao->selectAdd();
275 $dao->selectAdd(implode(',', $returnProperties));
276 }
277 $dao->find();
278
279 $flag = $returnProperties && in_array('member_count', $returnProperties) ? 1 : 0;
280
281 $groups = array();
282 while ($dao->fetch()) {
283 $group = new CRM_Contact_DAO_Group();
284 if ($flag) {
285 $dao->member_count = CRM_Contact_BAO_Group::memberCount($dao->id);
286 }
287 $groups[] = clone($dao);
288 }
289 return $groups;
290 }
291
292 /**
293 * Make sure that the user has permission to access this group.
294 *
295 * @param int $id
296 * The id of the object.
297 * @param bool $excludeHidden
298 * Should hidden groups be excluded.
299 * Logically this is the wrong place to filter hidden groups out as that is
300 * not a permission issue. However, as other functions may rely on that defaulting to
301 * FALSE for now & only the api call is calling with true.
302 *
303 * @return array
304 * The permission that the user has (or NULL)
305 */
306 public static function checkPermission($id, $excludeHidden = FALSE) {
307 $allGroups = CRM_Core_PseudoConstant::allGroup(NULL, $excludeHidden);
308
309 $permissions = NULL;
310 if (CRM_Core_Permission::check('edit all contacts') ||
311 CRM_ACL_API::groupPermission(CRM_ACL_API::EDIT, $id, NULL,
312 'civicrm_saved_search', $allGroups
313 )
314 ) {
315 $permissions[] = CRM_Core_Permission::EDIT;
316 }
317
318 if (CRM_Core_Permission::check('view all contacts') ||
319 CRM_ACL_API::groupPermission(CRM_ACL_API::VIEW, $id, NULL,
320 'civicrm_saved_search', $allGroups
321 )
322 ) {
323 $permissions[] = CRM_Core_Permission::VIEW;
324 }
325
326 if (!empty($permissions) && CRM_Core_Permission::check('delete contacts')) {
327 // Note: using !empty() in if condition, restricts the scope of delete
328 // permission to groups/contacts that are editable/viewable.
329 // We can remove this !empty condition once we have ACL support for delete functionality.
330 $permissions[] = CRM_Core_Permission::DELETE;
331 }
332
333 return $permissions;
334 }
335
336 /**
337 * Create a new group.
338 *
339 * @param array $params
340 *
341 * @return CRM_Contact_BAO_Group|NULL
342 * The new group BAO (if created)
343 */
344 public static function create(&$params) {
345
346 if (!empty($params['id'])) {
347 CRM_Utils_Hook::pre('edit', 'Group', $params['id'], $params);
348 }
349 else {
350 CRM_Utils_Hook::pre('create', 'Group', NULL, $params);
351 }
352
353 // form the name only if missing: CRM-627
354 $nameParam = CRM_Utils_Array::value('name', $params, NULL);
355 if (!$nameParam && empty($params['id'])) {
356 $params['name'] = CRM_Utils_String::titleToVar($params['title']);
357 }
358
359 // convert params if array type
360 if (isset($params['group_type'])) {
361 if (is_array($params['group_type'])) {
362 $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
363 $params['group_type']
364 ) . CRM_Core_DAO::VALUE_SEPARATOR;
365 }
366 else {
367 $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['group_type'] . CRM_Core_DAO::VALUE_SEPARATOR;
368 }
369 }
370 else {
371 $params['group_type'] = NULL;
372 }
373
374 $session = CRM_Core_Session::singleton();
375 $cid = $session->get('userID');
376 // this action is add
377 if ($cid && empty($params['id'])) {
378 $params['created_id'] = $cid;
379 }
380 // this action is update
381 if ($cid && !empty($params['id'])) {
382 $params['modified_id'] = $cid;
383 }
384
385 // CRM-19068.
386 // Validate parents parameter when creating group.
387 if (!empty($params['parents'])) {
388 $parents = is_array($params['parents']) ? array_keys($params['parents']) : (array) $params['parents'];
389 foreach ($parents as $parent) {
390 CRM_Utils_Type::validate($parent, 'Integer');
391 }
392 }
393 $group = new CRM_Contact_BAO_Group();
394 $group->copyValues($params);
395 //@todo very hacky fix for the fact this function wants to receive 'parents' as an array further down but
396 // needs it as a separated string for the DB. Preferred approaches are having the copyParams or save fn
397 // use metadata to translate the array to the appropriate DB type or altering the param in the api layer,
398 // or at least altering the param in same section as 'group_type' rather than repeating here. However, further down
399 // we need the $params one to be in it's original form & we are not sure what test coverage we have on that
400 if (isset($group->parents) && is_array($group->parents)) {
401 $group->parents = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
402 array_keys($group->parents)
403 ) . CRM_Core_DAO::VALUE_SEPARATOR;
404 }
405 if (empty($params['id']) &&
406 !$nameParam
407 ) {
408 $group->name .= "_tmp";
409 }
410 $group->save();
411
412 if (!$group->id) {
413 return NULL;
414 }
415
416 if (empty($params['id']) &&
417 !$nameParam
418 ) {
419 $group->name = substr($group->name, 0, -4) . "_{$group->id}";
420 }
421
422 $group->buildClause();
423 $group->save();
424
425 // add custom field values
426 if (!empty($params['custom'])) {
427 CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_group', $group->id);
428 }
429
430 // make the group, child of domain/site group by default.
431 $domainGroupID = CRM_Core_BAO_Domain::getGroupId();
432 if (CRM_Utils_Array::value('no_parent', $params) !== 1) {
433 if (empty($params['parents']) &&
434 $domainGroupID != $group->id &&
435 Civi::settings()->get('is_enabled') &&
436 !CRM_Contact_BAO_GroupNesting::hasParentGroups($group->id)
437 ) {
438 // if no parent present and the group doesn't already have any parents,
439 // make sure site group goes as parent
440 $params['parents'] = array($domainGroupID => 1);
441 }
442 elseif (array_key_exists('parents', $params) && !is_array($params['parents'])) {
443 $params['parents'] = array($params['parents'] => 1);
444 }
445
446 if (!empty($params['parents'])) {
447 foreach ($params['parents'] as $parentId => $dnc) {
448 if ($parentId && !CRM_Contact_BAO_GroupNesting::isParentChild($parentId, $group->id)) {
449 CRM_Contact_BAO_GroupNesting::add($parentId, $group->id);
450 }
451 }
452 }
453
454 // this is always required, since we don't know when a
455 // parent group is removed
456 CRM_Contact_BAO_GroupNestingCache::update();
457
458 // update group contact cache for all parent groups
459 $parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($group->id);
460 foreach ($parentIds as $parentId) {
461 CRM_Contact_BAO_GroupContactCache::add($parentId);
462 }
463 }
464
465 if (!empty($params['organization_id'])) {
466 $groupOrg = $params;
467 $groupOrg['group_id'] = $group->id;
468 CRM_Contact_BAO_GroupOrganization::add($groupOrg);
469 }
470
471 CRM_Utils_System::flushCache();
472 CRM_Contact_BAO_GroupContactCache::add($group->id);
473
474 if (!empty($params['id'])) {
475 CRM_Utils_Hook::post('edit', 'Group', $group->id, $group);
476 }
477 else {
478 CRM_Utils_Hook::post('create', 'Group', $group->id, $group);
479 }
480
481 $recentOther = array();
482 if (CRM_Core_Permission::check('edit groups')) {
483 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=update&id=' . $group->id);
484 // currently same permission we are using for delete a group
485 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=delete&id=' . $group->id);
486 }
487
488 // add the recently added group (unless hidden: CRM-6432)
489 if (!$group->is_hidden) {
490 CRM_Utils_Recent::add($group->title,
491 CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id),
492 $group->id,
493 'Group',
494 NULL,
495 NULL,
496 $recentOther
497 );
498 }
499 return $group;
500 }
501
502 /**
503 * Given a saved search compute the clause and the tables
504 * and store it for future use
505 */
506 public function buildClause() {
507 $params = array(array('group', 'IN', array($this->id), 0, 0));
508
509 if (!empty($params)) {
510 $tables = $whereTables = array();
511 $this->where_clause = CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables);
512 if (!empty($tables)) {
513 $this->select_tables = serialize($tables);
514 }
515 if (!empty($whereTables)) {
516 $this->where_tables = serialize($whereTables);
517 }
518 }
519 }
520
521 /**
522 * Defines a new smart group.
523 *
524 * @param array $params
525 * Associative array of parameters.
526 *
527 * @return CRM_Contact_BAO_Group|NULL
528 * The new group BAO (if created)
529 */
530 public static function createSmartGroup(&$params) {
531 if (!empty($params['formValues'])) {
532 $ssParams = $params;
533 unset($ssParams['id']);
534 if (isset($ssParams['saved_search_id'])) {
535 $ssParams['id'] = $ssParams['saved_search_id'];
536 }
537
538 $savedSearch = CRM_Contact_BAO_SavedSearch::create($params);
539
540 $params['saved_search_id'] = $savedSearch->id;
541 }
542 else {
543 return NULL;
544 }
545
546 return self::create($params);
547 }
548
549 /**
550 * Update the is_active flag in the db.
551 *
552 * @param int $id
553 * Id of the database record.
554 * @param bool $isActive
555 * Value we want to set the is_active field.
556 *
557 * @return CRM_Core_DAO|null
558 * DAO object on success, NULL otherwise
559 */
560 public static function setIsActive($id, $isActive) {
561 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Group', $id, 'is_active', $isActive);
562 }
563
564 /**
565 * Build the condition to retrieve groups.
566 *
567 * @param string $groupType
568 * Type of group(Access/Mailing) OR the key of the group.
569 * @param bool $excludeHidden exclude hidden groups.
570 *
571 * @return string
572 */
573 public static function groupTypeCondition($groupType = NULL, $excludeHidden = TRUE) {
574 $value = NULL;
575 if ($groupType == 'Mailing') {
576 $value = CRM_Core_DAO::VALUE_SEPARATOR . '2' . CRM_Core_DAO::VALUE_SEPARATOR;
577 }
578 elseif ($groupType == 'Access') {
579 $value = CRM_Core_DAO::VALUE_SEPARATOR . '1' . CRM_Core_DAO::VALUE_SEPARATOR;
580 }
581 elseif (!empty($groupType)) {
582 // ie we have been given the group key
583 $value = CRM_Core_DAO::VALUE_SEPARATOR . $groupType . CRM_Core_DAO::VALUE_SEPARATOR;
584 }
585
586 $condition = NULL;
587 if ($excludeHidden) {
588 $condition = "is_hidden = 0";
589 }
590
591 if ($value) {
592 if ($condition) {
593 $condition .= " AND group_type LIKE '%$value%'";
594 }
595 else {
596 $condition = "group_type LIKE '%$value%'";
597 }
598 }
599
600 return $condition;
601 }
602
603 /**
604 * Get permission relevant clauses.
605 * CRM-12209
606 *
607 * @param bool $force
608 *
609 * @return array
610 */
611 public static function getPermissionClause($force = FALSE) {
612 static $clause = 1;
613 static $retrieved = FALSE;
614 if (!$retrieved || $force) {
615 if (CRM_Core_Permission::check('view all contacts') || CRM_Core_Permission::check('edit all contacts')) {
616 $clause = 1;
617 }
618 else {
619 //get the allowed groups for the current user
620 $groups = CRM_ACL_API::group(CRM_ACL_API::VIEW);
621 if (!empty($groups)) {
622 $groupList = implode(', ', array_values($groups));
623 $clause = "groups.id IN ( $groupList ) ";
624 }
625 else {
626 $clause = '1 = 0';
627 }
628 }
629 }
630 $retrieved = TRUE;
631 return $clause;
632 }
633
634 /**
635 * @return string
636 */
637 public function __toString() {
638 return $this->title;
639 }
640
641 /**
642 * This function create the hidden smart group when user perform
643 * contact search and want to send mailing to search contacts.
644 *
645 * @param array $params
646 * ( reference ) an assoc array of name/value pairs.
647 *
648 * @return array
649 * ( smartGroupId, ssId ) smart group id and saved search id
650 */
651 public static function createHiddenSmartGroup($params) {
652 $ssId = CRM_Utils_Array::value('saved_search_id', $params);
653
654 //add mapping record only for search builder saved search
655 $mappingId = NULL;
656 if ($params['search_context'] == 'builder') {
657 //save the mapping for search builder
658 if (!$ssId) {
659 //save record in mapping table
660 $mappingParams = array(
661 'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type',
662 'Search Builder',
663 'name'
664 ),
665 );
666 $mapping = CRM_Core_BAO_Mapping::add($mappingParams);
667 $mappingId = $mapping->id;
668 }
669 else {
670 //get the mapping id from saved search
671 $savedSearch = new CRM_Contact_BAO_SavedSearch();
672 $savedSearch->id = $ssId;
673 $savedSearch->find(TRUE);
674 $mappingId = $savedSearch->mapping_id;
675 }
676
677 //save mapping fields
678 CRM_Core_BAO_Mapping::saveMappingFields($params['form_values'], $mappingId);
679 }
680
681 //create/update saved search record.
682 $savedSearch = new CRM_Contact_BAO_SavedSearch();
683 $savedSearch->id = $ssId;
684 $savedSearch->form_values = serialize($params['form_values']);
685 $savedSearch->mapping_id = $mappingId;
686 $savedSearch->search_custom_id = CRM_Utils_Array::value('search_custom_id', $params);
687 $savedSearch->save();
688
689 $ssId = $savedSearch->id;
690 if (!$ssId) {
691 return NULL;
692 }
693
694 $smartGroupId = NULL;
695 if (!empty($params['saved_search_id'])) {
696 $smartGroupId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $ssId, 'id', 'saved_search_id');
697 }
698 else {
699 //create group only when new saved search.
700 $groupParams = array(
701 'title' => "Hidden Smart Group {$ssId}",
702 'is_active' => CRM_Utils_Array::value('is_active', $params, 1),
703 'is_hidden' => CRM_Utils_Array::value('is_hidden', $params, 1),
704 'group_type' => CRM_Utils_Array::value('group_type', $params),
705 'visibility' => CRM_Utils_Array::value('visibility', $params),
706 'saved_search_id' => $ssId,
707 );
708
709 $smartGroup = self::create($groupParams);
710 $smartGroupId = $smartGroup->id;
711 }
712
713 // Update mapping with the name and description of the hidden smart group.
714 if ($mappingId) {
715 $mappingParams = array(
716 'id' => $mappingId,
717 'name' => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $smartGroupId, 'name', 'id'),
718 'description' => CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $smartGroupId, 'description', 'id'),
719 'mapping_type_id' => CRM_Core_OptionGroup::getValue('mapping_type',
720 'Search Builder',
721 'name'
722 ),
723 );
724 CRM_Core_BAO_Mapping::add($mappingParams);
725 }
726
727 return array($smartGroupId, $ssId);
728 }
729
730 /**
731 * wrapper for ajax group selector.
732 *
733 * @param array $params
734 * Associated array for params record id.
735 *
736 * @return array
737 * associated array of group list
738 * -rp = rowcount
739 * -page= offset
740 * @todo there seems little reason for the small number of functions that call this to pass in
741 * params that then need to be translated in this function since they are coding them when calling
742 */
743 static public function getGroupListSelector(&$params) {
744 // format the params
745 $params['offset'] = ($params['page'] - 1) * $params['rp'];
746 $params['rowCount'] = $params['rp'];
747 $params['sort'] = CRM_Utils_Array::value('sortBy', $params);
748
749 // get groups
750 $groups = CRM_Contact_BAO_Group::getGroupList($params);
751
752 //skip total if we are making call to show only children
753 if (empty($params['parent_id'])) {
754 // add total
755 $params['total'] = CRM_Contact_BAO_Group::getGroupCount($params);
756
757 // get all the groups
758 $allGroups = CRM_Core_PseudoConstant::allGroup();
759 }
760
761 // format params and add links
762 $groupList = array();
763 foreach ($groups as $id => $value) {
764 $group = array();
765 $group['group_id'] = $value['id'];
766 $group['count'] = $value['count'];
767 $group['title'] = $value['title'];
768
769 // append parent names if in search mode
770 if (empty($params['parent_id']) && !empty($value['parents'])) {
771 $group['parent_id'] = $value['parents'];
772 $groupIds = explode(',', $value['parents']);
773 $title = array();
774 foreach ($groupIds as $gId) {
775 $title[] = $allGroups[$gId];
776 }
777 $group['title'] .= '<div class="crm-row-parent-name"><em>' . ts('Child of') . '</em>: ' . implode(', ', $title) . '</div>';
778 $value['class'] = array_diff($value['class'], array('crm-row-parent'));
779 }
780 $group['DT_RowId'] = 'row_' . $value['id'];
781 if (empty($params['parentsOnly'])) {
782 foreach ($value['class'] as $id => $class) {
783 if ($class == 'crm-group-parent') {
784 unset($value['class'][$id]);
785 }
786 }
787 }
788 $group['DT_RowClass'] = 'crm-entity ' . implode(' ', $value['class']);
789 $group['DT_RowAttr'] = array();
790 $group['DT_RowAttr']['data-id'] = $value['id'];
791 $group['DT_RowAttr']['data-entity'] = 'group';
792
793 $group['description'] = CRM_Utils_Array::value('description', $value);
794
795 if (!empty($value['group_type'])) {
796 $group['group_type'] = $value['group_type'];
797 }
798 else {
799 $group['group_type'] = '';
800 }
801
802 $group['visibility'] = $value['visibility'];
803 $group['links'] = $value['action'];
804 $group['org_info'] = CRM_Utils_Array::value('org_info', $value);
805 $group['created_by'] = CRM_Utils_Array::value('created_by', $value);
806
807 $group['is_parent'] = $value['is_parent'];
808
809 array_push($groupList, $group);
810 }
811
812 $groupsDT = array();
813 $groupsDT['data'] = $groupList;
814 $groupsDT['recordsTotal'] = !empty($params['total']) ? $params['total'] : NULL;
815 $groupsDT['recordsFiltered'] = !empty($params['total']) ? $params['total'] : NULL;
816
817 return $groupsDT;
818 }
819
820 /**
821 * This function to get list of groups.
822 *
823 * @param array $params
824 * Associated array for params.
825 *
826 * @return array
827 */
828 public static function getGroupList(&$params) {
829 $config = CRM_Core_Config::singleton();
830
831 $whereClause = self::whereClause($params, FALSE);
832
833 //$this->pagerAToZ( $whereClause, $params );
834
835 $limit = "";
836 if (!empty($params['rowCount']) &&
837 $params['rowCount'] > 0
838 ) {
839 $limit = " LIMIT {$params['offset']}, {$params['rowCount']} ";
840 }
841
842 $orderBy = ' ORDER BY groups.title asc';
843 if (!empty($params['sort'])) {
844 $orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($params['sort'], 'String');
845
846 // CRM-16905 - Sort by count cannot be done with sql
847 if (strpos($params['sort'], 'count') === 0) {
848 $orderBy = $limit = '';
849 }
850 }
851
852 $select = $from = $where = "";
853 $groupOrg = FALSE;
854 if (CRM_Core_Permission::check('administer Multiple Organizations') &&
855 CRM_Core_Permission::isMultisiteEnabled()
856 ) {
857 $select = ", contact.display_name as org_name, contact.id as org_id";
858 $from = " LEFT JOIN civicrm_group_organization gOrg
859 ON gOrg.group_id = groups.id
860 LEFT JOIN civicrm_contact contact
861 ON contact.id = gOrg.organization_id ";
862
863 //get the Organization ID
864 $orgID = CRM_Utils_Request::retrieve('oid', 'Positive');
865 if ($orgID) {
866 $where = " AND gOrg.organization_id = {$orgID}";
867 }
868
869 $groupOrg = TRUE;
870 }
871
872 $query = "
873 SELECT groups.*, createdBy.sort_name as created_by {$select}
874 FROM civicrm_group groups
875 LEFT JOIN civicrm_contact createdBy
876 ON createdBy.id = groups.created_id
877 {$from}
878 WHERE $whereClause {$where}
879 {$orderBy}
880 {$limit}";
881
882 $object = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contact_DAO_Group');
883
884 //FIXME CRM-4418, now we are handling delete separately
885 //if we introduce 'delete for group' make sure to handle here.
886 $groupPermissions = array(CRM_Core_Permission::VIEW);
887 if (CRM_Core_Permission::check('edit groups')) {
888 $groupPermissions[] = CRM_Core_Permission::EDIT;
889 $groupPermissions[] = CRM_Core_Permission::DELETE;
890 }
891
892 // CRM-9936
893 $reservedPermission = CRM_Core_Permission::check('administer reserved groups');
894
895 $links = self::actionLinks();
896
897 $allTypes = CRM_Core_OptionGroup::values('group_type');
898 $values = $groupsToCount = array();
899
900 $visibility = CRM_Core_SelectValues::ufVisibility();
901
902 while ($object->fetch()) {
903 $permission = CRM_Contact_BAO_Group::checkPermission($object->id, $object->title);
904 //@todo CRM-12209 introduced an ACL check in the whereClause function
905 // it may be that this checking is now obsolete - or that what remains
906 // should be removed to the whereClause (which is also accessed by getCount)
907
908 if ($permission) {
909 $newLinks = $links;
910 $values[$object->id] = array(
911 'class' => array(),
912 'count' => '0',
913 );
914 CRM_Core_DAO::storeValues($object, $values[$object->id]);
915
916 if ($object->saved_search_id) {
917 $values[$object->id]['title'] .= ' (' . ts('Smart Group') . ')';
918 // check if custom search, if so fix view link
919 $customSearchID = CRM_Core_DAO::getFieldValue(
920 'CRM_Contact_DAO_SavedSearch',
921 $object->saved_search_id,
922 'search_custom_id'
923 );
924
925 if ($customSearchID) {
926 $newLinks[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/search/custom';
927 $newLinks[CRM_Core_Action::VIEW]['qs'] = "reset=1&force=1&ssID={$object->saved_search_id}";
928 }
929 }
930
931 $action = array_sum(array_keys($newLinks));
932
933 // CRM-9936
934 if (array_key_exists('is_reserved', $object)) {
935 //if group is reserved and I don't have reserved permission, suppress delete/edit
936 if ($object->is_reserved && !$reservedPermission) {
937 $action -= CRM_Core_Action::DELETE;
938 $action -= CRM_Core_Action::UPDATE;
939 $action -= CRM_Core_Action::DISABLE;
940 }
941 }
942
943 if (array_key_exists('is_active', $object)) {
944 if ($object->is_active) {
945 $action -= CRM_Core_Action::ENABLE;
946 }
947 else {
948 $values[$object->id]['class'][] = 'disabled';
949 $action -= CRM_Core_Action::VIEW;
950 $action -= CRM_Core_Action::DISABLE;
951 }
952 }
953
954 $action = $action & CRM_Core_Action::mask($groupPermissions);
955
956 $values[$object->id]['visibility'] = $visibility[$values[$object->id]['visibility']];
957
958 $groupsToCount[$object->saved_search_id ? 'civicrm_group_contact_cache' : 'civicrm_group_contact'][] = $object->id;
959
960 if (isset($values[$object->id]['group_type'])) {
961 $groupTypes = explode(CRM_Core_DAO::VALUE_SEPARATOR,
962 substr($values[$object->id]['group_type'], 1, -1)
963 );
964 $types = array();
965 foreach ($groupTypes as $type) {
966 $types[] = CRM_Utils_Array::value($type, $allTypes);
967 }
968 $values[$object->id]['group_type'] = implode(', ', $types);
969 }
970 $values[$object->id]['action'] = CRM_Core_Action::formLink($newLinks,
971 $action,
972 array(
973 'id' => $object->id,
974 'ssid' => $object->saved_search_id,
975 ),
976 ts('more'),
977 FALSE,
978 'group.selector.row',
979 'Group',
980 $object->id
981 );
982
983 // If group has children, add class for link to view children
984 $values[$object->id]['is_parent'] = FALSE;
985 if (array_key_exists('children', $values[$object->id])) {
986 $values[$object->id]['class'][] = "crm-group-parent";
987 $values[$object->id]['is_parent'] = TRUE;
988 }
989
990 // If group is a child, add child class
991 if (array_key_exists('parents', $values[$object->id])) {
992 $values[$object->id]['class'][] = "crm-group-child";
993 }
994
995 if ($groupOrg) {
996 if ($object->org_id) {
997 $contactUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$object->org_id}");
998 $values[$object->id]['org_info'] = "<a href='{$contactUrl}'>{$object->org_name}</a>";
999 }
1000 else {
1001 $values[$object->id]['org_info'] = ''; // Empty cell
1002 }
1003 }
1004 else {
1005 $values[$object->id]['org_info'] = NULL; // Collapsed column if all cells are NULL
1006 }
1007 if ($object->created_id) {
1008 $contactUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$object->created_id}");
1009 $values[$object->id]['created_by'] = "<a href='{$contactUrl}'>{$object->created_by}</a>";
1010 }
1011 }
1012 }
1013
1014 // Get group counts - executes one query for regular groups and another for smart groups
1015 foreach ($groupsToCount as $table => $groups) {
1016 $where = "g.group_id IN (" . implode(',', $groups) . ")";
1017 if ($table == 'civicrm_group_contact') {
1018 $where .= " AND g.status = 'Added'";
1019 }
1020 // Exclude deleted contacts
1021 $where .= " and c.id = g.contact_id AND c.is_deleted = 0";
1022 $dao = CRM_Core_DAO::executeQuery("SELECT g.group_id, COUNT(g.id) as `count` FROM $table g, civicrm_contact c WHERE $where GROUP BY g.group_id");
1023 while ($dao->fetch()) {
1024 $values[$dao->group_id]['count'] = $dao->count;
1025 }
1026 }
1027
1028 // CRM-16905 - Sort by count cannot be done with sql
1029 if (!empty($params['sort']) && strpos($params['sort'], 'count') === 0) {
1030 usort($values, function($a, $b) {
1031 return $a['count'] - $b['count'];
1032 });
1033 if (strpos($params['sort'], 'desc')) {
1034 $values = array_reverse($values, TRUE);
1035 }
1036 return array_slice($values, $params['offset'], $params['rowCount']);
1037 }
1038
1039 return $values;
1040 }
1041
1042 /**
1043 * This function to get hierarchical list of groups (parent followed by children)
1044 *
1045 * @param array $groupIDs
1046 * Array of group ids.
1047 *
1048 * @param NULL $parents
1049 * @param string $spacer
1050 * @param bool $titleOnly
1051 *
1052 * @return array
1053 */
1054 public static function getGroupsHierarchy(
1055 $groupIDs,
1056 $parents = NULL,
1057 $spacer = '<span class="child-indent"></span>',
1058 $titleOnly = FALSE
1059 ) {
1060 if (empty($groupIDs)) {
1061 return array();
1062 }
1063
1064 $groupIdString = '(' . implode(',', array_keys($groupIDs)) . ')';
1065 // <span class="child-icon"></span>
1066 // need to return id, title (w/ spacer), description, visibility
1067
1068 // We need to build a list of tags ordered by hierarchy and sorted by
1069 // name. The hierarchy will be communicated by an accumulation of
1070 // separators in front of the name to give it a visual offset.
1071 // Instead of recursively making mysql queries, we'll make one big
1072 // query and build the hierarchy with the algorithm below.
1073 $groups = array();
1074 $args = array(1 => array($groupIdString, 'String'));
1075 $query = "
1076 SELECT id, title, description, visibility, parents
1077 FROM civicrm_group
1078 WHERE id IN $groupIdString
1079 ";
1080 if ($parents) {
1081 // group can have > 1 parent so parents may be comma separated list (eg. '1,2,5'). We just grab and match on 1st parent.
1082 $parentArray = explode(',', $parents);
1083 $parent = $parentArray[0];
1084 $args[2] = array($parent, 'Integer');
1085 $query .= " AND SUBSTRING_INDEX(parents, ',', 1) = %2";
1086 }
1087 $query .= " ORDER BY title";
1088 $dao = CRM_Core_DAO::executeQuery($query, $args);
1089
1090 // Sort the groups into the correct storage by the parent
1091 // $roots represent the current leaf nodes that need to be checked for
1092 // children. $rows represent the unplaced nodes
1093 $roots = $rows = $allGroups = array();
1094 while ($dao->fetch()) {
1095 $allGroups[$dao->id] = array(
1096 'title' => $dao->title,
1097 'visibility' => $dao->visibility,
1098 'description' => $dao->description,
1099 );
1100
1101 if ($dao->parents == $parents) {
1102 $roots[] = array(
1103 'id' => $dao->id,
1104 'prefix' => '',
1105 'title' => $dao->title,
1106 );
1107 }
1108 else {
1109 // group can have > 1 parent so $dao->parents may be comma separated list (eg. '1,2,5'). Grab and match on 1st parent.
1110 $parentArray = explode(',', $dao->parents);
1111 $parent = $parentArray[0];
1112 $rows[] = array(
1113 'id' => $dao->id,
1114 'prefix' => '',
1115 'title' => $dao->title,
1116 'parents' => $parent,
1117 );
1118 }
1119 }
1120 $dao->free();
1121 // While we have nodes left to build, shift the first (alphabetically)
1122 // node of the list, place it in our groups list and loop through the
1123 // list of unplaced nodes to find its children. We make a copy to
1124 // iterate through because we must modify the unplaced nodes list
1125 // during the loop.
1126 while (count($roots)) {
1127 $new_roots = array();
1128 $current_rows = $rows;
1129 $root = array_shift($roots);
1130 $groups[$root['id']] = array($root['prefix'], $root['title']);
1131
1132 // As you find the children, append them to the end of the new set
1133 // of roots (maintain alphabetical ordering). Also remove the node
1134 // from the set of unplaced nodes.
1135 if (is_array($current_rows)) {
1136 foreach ($current_rows as $key => $row) {
1137 if ($row['parents'] == $root['id']) {
1138 $new_roots[] = array(
1139 'id' => $row['id'],
1140 'prefix' => $groups[$root['id']][0] . $spacer,
1141 'title' => $row['title'],
1142 );
1143 unset($rows[$key]);
1144 }
1145 }
1146 }
1147
1148 //As a group, insert the new roots into the beginning of the roots
1149 //list. This maintains the hierarchical ordering of the tags.
1150 $roots = array_merge($new_roots, $roots);
1151 }
1152
1153 // below is the redundant looping to ensure child groups are populated in the case where user does not have
1154 // access to parent groups ( esp. using ACL permissions and logged in user can assess only child groups )
1155 foreach ($rows as $value) {
1156 $groups[$value['id']] = array($value['prefix'], $value['title']);
1157 }
1158 // Prefix titles with the calcuated spacing to give the visual
1159 // appearance of ordering when transformed into HTML in the form layer. Add description and visibility.
1160 $groupsReturn = array();
1161 foreach ($groups as $key => $value) {
1162 if ($titleOnly) {
1163 $groupsReturn[$key] = $value[0] . $value[1];
1164 }
1165 else {
1166 $groupsReturn[$key] = array(
1167 'title' => $value[0] . $value[1],
1168 'description' => $allGroups[$key]['description'],
1169 'visibility' => $allGroups[$key]['visibility'],
1170 );
1171 }
1172 }
1173
1174 return $groupsReturn;
1175 }
1176
1177 /**
1178 * @param array $params
1179 *
1180 * @return NULL|string
1181 */
1182 public static function getGroupCount(&$params) {
1183 $whereClause = self::whereClause($params, FALSE);
1184 $query = "SELECT COUNT(*) FROM civicrm_group groups";
1185
1186 if (!empty($params['created_by'])) {
1187 $query .= "
1188 INNER JOIN civicrm_contact createdBy
1189 ON createdBy.id = groups.created_id";
1190 }
1191 $query .= "
1192 WHERE {$whereClause}";
1193 return CRM_Core_DAO::singleValueQuery($query, $params);
1194 }
1195
1196 /**
1197 * Generate permissioned where clause for group search.
1198 * @param array $params
1199 * @param bool $sortBy
1200 * @param bool $excludeHidden
1201 *
1202 * @return string
1203 */
1204 public static function whereClause(&$params, $sortBy = TRUE, $excludeHidden = TRUE) {
1205 $values = array();
1206 $title = CRM_Utils_Array::value('title', $params);
1207 if ($title) {
1208 $clauses[] = "groups.title LIKE %1";
1209 if (strpos($title, '%') !== FALSE) {
1210 $params[1] = array($title, 'String', FALSE);
1211 }
1212 else {
1213 $params[1] = array($title, 'String', TRUE);
1214 }
1215 }
1216
1217 $groupType = CRM_Utils_Array::value('group_type', $params);
1218 if ($groupType) {
1219 $types = explode(',', $groupType);
1220 if (!empty($types)) {
1221 $clauses[] = 'groups.group_type LIKE %2';
1222 $typeString = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $types) . CRM_Core_DAO::VALUE_SEPARATOR;
1223 $params[2] = array($typeString, 'String', TRUE);
1224 }
1225 }
1226
1227 $visibility = CRM_Utils_Array::value('visibility', $params);
1228 if ($visibility) {
1229 $clauses[] = 'groups.visibility = %3';
1230 $params[3] = array($visibility, 'String');
1231 }
1232
1233 $groupStatus = CRM_Utils_Array::value('status', $params);
1234 if ($groupStatus) {
1235 switch ($groupStatus) {
1236 case 1:
1237 $clauses[] = 'groups.is_active = 1';
1238 $params[4] = array($groupStatus, 'Integer');
1239 break;
1240
1241 case 2:
1242 $clauses[] = 'groups.is_active = 0';
1243 $params[4] = array($groupStatus, 'Integer');
1244 break;
1245
1246 case 3:
1247 $clauses[] = '(groups.is_active = 0 OR groups.is_active = 1 )';
1248 break;
1249 }
1250 }
1251
1252 $parentsOnly = CRM_Utils_Array::value('parentsOnly', $params);
1253 if ($parentsOnly) {
1254 $clauses[] = 'groups.parents IS NULL';
1255 }
1256
1257 // only show child groups of a specific parent group
1258 $parent_id = CRM_Utils_Array::value('parent_id', $params);
1259 if ($parent_id) {
1260 $clauses[] = 'groups.id IN (SELECT child_group_id FROM civicrm_group_nesting WHERE parent_group_id = %5)';
1261 $params[5] = array($parent_id, 'Integer');
1262 }
1263
1264 if ($createdBy = CRM_Utils_Array::value('created_by', $params)) {
1265 $clauses[] = "createdBy.sort_name LIKE %6";
1266 if (strpos($createdBy, '%') !== FALSE) {
1267 $params[6] = array($createdBy, 'String', FALSE);
1268 }
1269 else {
1270 $params[6] = array($createdBy, 'String', TRUE);
1271 }
1272 }
1273
1274 if (empty($clauses)) {
1275 $clauses[] = 'groups.is_active = 1';
1276 }
1277
1278 if ($excludeHidden) {
1279 $clauses[] = 'groups.is_hidden = 0';
1280 }
1281
1282 $clauses[] = self::getPermissionClause();
1283
1284 return implode(' AND ', $clauses);
1285 }
1286
1287 /**
1288 * Define action links.
1289 *
1290 * @return array
1291 * array of action links
1292 */
1293 public static function actionLinks() {
1294 $links = array(
1295 CRM_Core_Action::VIEW => array(
1296 'name' => ts('Contacts'),
1297 'url' => 'civicrm/group/search',
1298 'qs' => 'reset=1&force=1&context=smog&gid=%%id%%',
1299 'title' => ts('Group Contacts'),
1300 ),
1301 CRM_Core_Action::UPDATE => array(
1302 'name' => ts('Settings'),
1303 'url' => 'civicrm/group',
1304 'qs' => 'reset=1&action=update&id=%%id%%',
1305 'title' => ts('Edit Group'),
1306 ),
1307 CRM_Core_Action::DISABLE => array(
1308 'name' => ts('Disable'),
1309 'ref' => 'crm-enable-disable',
1310 'title' => ts('Disable Group'),
1311 ),
1312 CRM_Core_Action::ENABLE => array(
1313 'name' => ts('Enable'),
1314 'ref' => 'crm-enable-disable',
1315 'title' => ts('Enable Group'),
1316 ),
1317 CRM_Core_Action::DELETE => array(
1318 'name' => ts('Delete'),
1319 'url' => 'civicrm/group',
1320 'qs' => 'reset=1&action=delete&id=%%id%%',
1321 'title' => ts('Delete Group'),
1322 ),
1323 );
1324
1325 return $links;
1326 }
1327
1328 /**
1329 * @param $whereClause
1330 * @param array $whereParams
1331 *
1332 * @return string
1333 */
1334 public function pagerAtoZ($whereClause, $whereParams) {
1335 $query = "
1336 SELECT DISTINCT UPPER(LEFT(groups.title, 1)) as sort_name
1337 FROM civicrm_group groups
1338 WHERE $whereClause
1339 ORDER BY LEFT(groups.title, 1)
1340 ";
1341 $dao = CRM_Core_DAO::executeQuery($query, $whereParams);
1342
1343 return CRM_Utils_PagerAToZ::getAToZBar($dao, $this->_sortByCharacter, TRUE);
1344 }
1345
1346 /**
1347 * Assign Test Value.
1348 *
1349 * @param string $fieldName
1350 * @param array $fieldDef
1351 * @param int $counter
1352 */
1353 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
1354 if ($fieldName == 'children' || $fieldName == 'parents') {
1355 $this->{$fieldName} = "NULL";
1356 }
1357 else {
1358 parent::assignTestValues($fieldName, $fieldDef, $counter);
1359 }
1360 }
1361
1362 /**
1363 * Get child group ids
1364 *
1365 * @param array $ids
1366 * Parent Group IDs
1367 *
1368 * @return array
1369 */
1370 public static function getChildGroupIds($ids) {
1371 $notFound = FALSE;
1372 $childGroupIds = array();
1373 foreach ($ids as $id) {
1374 $childId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $id, 'children');
1375 while (!empty($childId)) {
1376 $childGroupIds[] = $childId;
1377 $childId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $childId, 'children');
1378 }
1379 }
1380
1381 return $childGroupIds;
1382 }
1383
1384 }