Merge pull request #686 from totten/mktest-mail
[civicrm-core.git] / CRM / Group / Page / AJAX.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 *
33 */
34
35 /**
36 * This class contains the functions that are called using AJAX (jQuery)
37 */
38 class CRM_Group_Page_AJAX {
39 static function getGroupList() {
40 $params = $_REQUEST;
41
42 if ( isset($params['parent_id']) ) {
43 // requesting child groups for a given parent
44 $params['page'] = 1;
45 $params['rp'] = 25;
46 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
47
48 echo json_encode($groups);
49 CRM_Utils_System::civiExit();
50 } else {
51 $sortMapper = array(
52 0 => 'groups.title', 1 => 'groups.id', 2 => 'createdBy.sort_name', 3 => '',
53 4 => 'groups.group_type', 5 => 'groups.visibility',
54 );
55
56 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
57 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
58 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
59 $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL;
60 $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc';
61
62 if ($sort && $sortOrder) {
63 $params['sortBy'] = $sort . ' ' . $sortOrder;
64 }
65
66 $params['page'] = ($offset / $rowCount) + 1;
67 $params['rp'] = $rowCount;
68
69 // get group list
70 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
71
72 // if no groups found with parent-child hierarchy and logged in user say can view child groups only (an ACL case),
73 // go ahead with flat hierarchy, CRM-12225
74 if (empty($groups)) {
75 $groupsAccessible = CRM_Core_PseudoConstant::group();
76 $parentsOnly = CRM_Utils_Array::value('parentsOnly', $params);
77 if (!empty($groupsAccessible) && $parentsOnly) {
78 // recompute group list with flat hierarchy
79 $params['parentsOnly'] = 0;
80 $groups = CRM_Contact_BAO_Group::getGroupListSelector($params);
81 }
82 }
83
84 $iFilteredTotal = $iTotal = $params['total'];
85 $selectorElements = array(
86 'group_name', 'group_id', 'created_by', 'group_description',
87 'group_type', 'visibility', 'org_info', 'links', 'class',
88 );
89
90 if (!CRM_Utils_Array::value('showOrgInfo', $params)) {
91 unset($selectorElements[6]);
92 }
93
94 echo CRM_Utils_JSON::encodeDataTableSelector($groups, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
95 CRM_Utils_System::civiExit();
96 }
97 }
98 }
99