Merge pull request #22601 from braders/set-print-functions-tidy
[civicrm-core.git] / CRM / Custom / Page / AJAX.php
CommitLineData
57c9c217 1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
57c9c217 5 | |
bc77d7c0
TO
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 |
57c9c217 9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
57c9c217 16 */
17
18/**
19 * This class contains the functions that are called using AJAX (jQuery)
20 */
21class CRM_Custom_Page_AJAX {
9d311c8f 22
57c9c217 23 /**
9d311c8f
CW
24 * This function uses the deprecated v1 datatable api and needs updating. See CRM-16353.
25 * @deprecated
57c9c217 26 */
27 public static function getOptionList() {
28 $params = $_REQUEST;
29
57c9c217 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;
57c9c217 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'];
be2fb01f 40 $selectorElements = [
57c9c217 41 'label',
42 'value',
4247b886 43 'description',
57c9c217 44 'is_default',
57c9c217 45 'is_active',
46 'links',
47 'class',
be2fb01f 48 ];
57c9c217 49
d42a224c 50 CRM_Utils_System::setHttpHeader('Content-Type', 'application/json');
57c9c217 51 echo CRM_Utils_JSON::encodeDataTableSelector($options, $sEcho, $iTotal, $iFilteredTotal, $selectorElements);
52 CRM_Utils_System::civiExit();
53 }
54
46227b8d 55 /**
56 * Fix Ordering of options
57 *
58 */
46227b8d 59 public static function fixOrdering() {
60 $params = $_REQUEST;
61
be2fb01f
CW
62 $queryParams = [
63 1 => [$params['start'], 'Integer'],
64 2 => [$params['end'], 'Integer'],
65 3 => [$params['gid'], 'Integer'],
66 ];
46227b8d 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);
c1cd77e2 90 CRM_Utils_JSON::output(TRUE);
46227b8d 91 }
92
5a2c7bb9 93 /**
94 * Get list of Multi Record Fields.
5a2c7bb9 95 */
96 public static function getMultiRecordFieldList() {
5a2c7bb9 97
9c3f979f
MM
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');
5a2c7bb9 101
317103ab
CW
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
5a2c7bb9 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;
c693f065 115 $obj->_DTparams['offset'] = ($params['page'] - 1) * $params['rp'];
116 $obj->_DTparams['rowCount'] = $params['rp'];
e525d6af 117 if (!empty($params['sortBy'])) {
f11b68e0 118 $obj->_DTparams['sort'] = $params['sortBy'];
c693f065 119 }
5a2c7bb9 120
c693f065 121 list($fields, $attributes) = $obj->browse();
5a2c7bb9 122
f5eda27f 123 // format params and add class attributes
be2fb01f 124 $fieldList = [];
c693f065 125 foreach ($fields as $id => $value) {
d51e02d3 126 foreach ($value as $fieldId => &$fieldName) {
127 if (!empty($attributes[$fieldId][$id]['class'])) {
be2fb01f 128 $fieldName = ['data' => $fieldName, 'cellClass' => $attributes[$fieldId][$id]['class']];
d51e02d3 129 }
c693f065 130 if (is_numeric($fieldId)) {
f11b68e0 131 $fName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $fieldId, 'column_name');
d51e02d3 132 CRM_Utils_Array::crmReplaceKey($value, $fieldId, $fName);
5a2c7bb9 133 }
134 }
317103ab 135 array_push($fieldList, $value);
5a2c7bb9 136 }
f5eda27f 137 $totalRecords = !empty($obj->_total) ? $obj->_total : 0;
5a2c7bb9 138
be2fb01f 139 $multiRecordFields = [];
c693f065 140 $multiRecordFields['data'] = $fieldList;
f5eda27f 141 $multiRecordFields['recordsTotal'] = $totalRecords;
142 $multiRecordFields['recordsFiltered'] = $totalRecords;
5a2c7bb9 143
c693f065 144 CRM_Utils_JSON::output($multiRecordFields);
5a2c7bb9 145 }
146
57c9c217 147}