Merge pull request #16882 from wmortada/wp#46
[civicrm-core.git] / CRM / Group / Page / AJAX.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 *
17 */
18
19 /**
20 * This class contains the functions that are called using AJAX (jQuery)
21 */
22 class CRM_Group_Page_AJAX {
23
24 /**
25 * Get list of groups.
26 */
27 public static function getGroupList() {
28 $params = $_GET;
29 if (isset($params['parent_id'])) {
30 // requesting child groups for a given parent
31 $params['page'] = 1;
32 $params['rp'] = 0;
33 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
34 }
35 else {
36 $requiredParams = [];
37 $optionalParams = [
38 'title' => 'String',
39 'created_by' => 'String',
40 'group_type' => 'String',
41 'visibility' => 'String',
42 'component_mode' => 'String',
43 'status' => 'Integer',
44 'parentsOnly' => 'Integer',
45 'showOrgInfo' => 'Boolean',
46 // Ignore 'parent_id' as that case is handled above
47 ];
48 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
49 $params += CRM_Core_Page_AJAX::validateParams($requiredParams, $optionalParams);
50
51 // get group list
52 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
53
54 // if no groups found with parent-child hierarchy and logged in user say can view child groups only (an ACL case),
55 // go ahead with flat hierarchy, CRM-12225
56 if (empty($groups)) {
57 $groupsAccessible = CRM_Core_PseudoConstant::group();
58 $parentsOnly = $params['parentsOnly'] ?? NULL;
59 if (!empty($groupsAccessible) && $parentsOnly) {
60 // recompute group list with flat hierarchy
61 $params['parentsOnly'] = 0;
62 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
63 }
64 }
65 }
66
67 if (!empty($_GET['is_unit_test'])) {
68 return $groups;
69 }
70
71 CRM_Utils_JSON::output($groups);
72 }
73
74 }