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