Merge pull request #20885 from eileenmcnaughton/param_replace
[civicrm-core.git] / CRM / Custom / 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 * This class contains the functions that are called using AJAX (jQuery)
20 */
21 class CRM_Custom_Page_AJAX {
22
23 /**
24 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
25 * @deprecated
26 */
27 public static function getOptionList() {
28 $params = $_REQUEST;
29
30 $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer');
31 $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0;
32 $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25;
33
34 $params['page'] = ($offset / $rowCount) + 1;
35 $params['rp'] = $rowCount;
36
37 $options = CRM_Core_BAO_CustomOption::getOptionListSelector($params);
38
39 $iFilteredTotal = $iTotal = $params['total'];
40 $selectorElements = [
41 'label',
42 'value',
43 'description',
44 'is_default',
45 'is_active',
46 'links',
47 'class',
48 ];
49
50 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
51 echo CRM_Utils_JSON::encodeDataTableSelector($options, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
52 CRM_Utils_System::civiExit();
53 }
54
55 /**
56 * Fix Ordering of options
57 *
58 */
59 public static function fixOrdering() {
60 $params = $_REQUEST;
61
62 $queryParams = [
63 1 => [$params['start'], 'Integer'],
64 2 => [$params['end'], 'Integer'],
65 3 => [$params['gid'], 'Integer'],
66 ];
67 $dao = "SELECT id FROM civicrm_option_value WHERE weight = %1 AND option_group_id = %3";
68 $startid = CRM_Core_DAO::singleValueQuery($dao, $queryParams);
69
70 $dao2 = "SELECT id FROM civicrm_option_value WHERE weight = %2 AND option_group_id = %3";
71 $endid = CRM_Core_DAO::singleValueQuery($dao2, $queryParams);
72
73 $query = "UPDATE civicrm_option_value SET weight = %2 WHERE id = $startid";
74 CRM_Core_DAO::executeQuery($query, $queryParams);
75
76 // increment or decrement the rest by one
77 if ($params['start'] < $params['end']) {
78 $updateRows = "UPDATE civicrm_option_value
79 SET weight = weight - 1
80 WHERE weight > %1 AND weight < %2 AND option_group_id = %3
81 OR id = $endid";
82 }
83 else {
84 $updateRows = "UPDATE civicrm_option_value
85 SET weight = weight + 1
86 WHERE weight < %1 AND weight > %2 AND option_group_id = %3
87 OR id = $endid";
88 }
89 CRM_Core_DAO::executeQuery($updateRows, $queryParams);
90 CRM_Utils_JSON::output(TRUE);
91 }
92
93 /**
94 * Get list of Multi Record Fields.
95 */
96 public static function getMultiRecordFieldList() {
97
98 $params = CRM_Core_Page_AJAX::defaultSortAndPagerParams(0, 10);
99 $params['cid'] = CRM_Utils_Type::escape($_GET['cid'], 'Integer');
100 $params['cgid'] = CRM_Utils_Type::escape($_GET['cgid'], 'Integer');
101
102 if (!CRM_Core_BAO_CustomGroup::checkGroupAccess($params['cgid'], CRM_Core_Permission::VIEW) ||
103 !CRM_Contact_BAO_Contact_Permission::allow($params['cid'], CRM_Core_Permission::VIEW)
104 ) {
105 CRM_Utils_System::permissionDenied();
106 }
107
108 $contactType = CRM_Contact_BAO_Contact::getContactType($params['cid']);
109
110 $obj = new CRM_Profile_Page_MultipleRecordFieldsListing();
111 $obj->_pageViewType = 'customDataView';
112 $obj->_contactId = $params['cid'];
113 $obj->_customGroupId = $params['cgid'];
114 $obj->_contactType = $contactType;
115 $obj->_DTparams['offset'] = ($params['page'] - 1) * $params['rp'];
116 $obj->_DTparams['rowCount'] = $params['rp'];
117 if (!empty($params['sortBy'])) {
118 $obj->_DTparams['sort'] = $params['sortBy'];
119 }
120
121 list($fields, $attributes) = $obj->browse();
122
123 // format params and add class attributes
124 $fieldList = [];
125 foreach ($fields as $id => $value) {
126 foreach ($value as $fieldId => &$fieldName) {
127 if (!empty($attributes[$fieldId][$id]['class'])) {
128 $fieldName = ['data' => $fieldName, 'cellClass' => $attributes[$fieldId][$id]['class']];
129 }
130 if (is_numeric($fieldId)) {
131 $fName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $fieldId, 'column_name');
132 CRM_Utils_Array::crmReplaceKey($value, $fieldId, $fName);
133 }
134 }
135 array_push($fieldList, $value);
136 }
137 $totalRecords = !empty($obj->_total) ? $obj->_total : 0;
138
139 $multiRecordFields = [];
140 $multiRecordFields['data'] = $fieldList;
141 $multiRecordFields['recordsTotal'] = $totalRecords;
142 $multiRecordFields['recordsFiltered'] = $totalRecords;
143
144 CRM_Utils_JSON::output($multiRecordFields);
145 }
146
147 }