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