Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
c28e1768 CW |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
c28e1768 | 5 | | | |
a30c801b 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 | | |
c28e1768 CW |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * This api exposes CiviCRM report templates. | |
14 | * | |
b081365f | 15 | * @package CiviCRM_APIv3 |
c28e1768 | 16 | */ |
6a488035 TO |
17 | |
18 | /** | |
9d32e6f7 | 19 | * Retrieve a report template. |
6a488035 | 20 | * |
c490a46a | 21 | * @param array $params |
6a488035 | 22 | * |
a6c01b45 | 23 | * @return array |
00f8641b | 24 | * API result array |
6a488035 TO |
25 | */ |
26 | function civicrm_api3_report_template_get($params) { | |
27 | require_once 'api/v3/OptionValue.php'; | |
28 | $params['option_group_id'] = CRM_Core_DAO::getFieldValue( | |
29 | 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name' | |
30 | ); | |
31 | return civicrm_api3_option_value_get($params); | |
32 | } | |
33 | ||
34 | /** | |
9d32e6f7 | 35 | * Add an OptionValue. |
6a488035 | 36 | * |
9d32e6f7 | 37 | * OptionValues are used to classify CRM entities (including Contacts, Groups and Actions). |
6a488035 | 38 | * |
c490a46a | 39 | * @param array $params |
77b97be7 | 40 | * |
a6c01b45 | 41 | * @return array |
00f8641b | 42 | * API result array |
26e4ac38 | 43 | * |
44 | * @throws \API_Exception | |
6a488035 TO |
45 | */ |
46 | function civicrm_api3_report_template_create($params) { | |
47 | require_once 'api/v3/OptionValue.php'; | |
48 | $params['option_group_id'] = CRM_Core_DAO::getFieldValue( | |
49 | 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name' | |
50 | ); | |
1cbea43e | 51 | if (!empty($params['component_id']) && !is_numeric($params['component_id'])) { |
cf8f0fff | 52 | $components = CRM_Core_PseudoConstant::get('CRM_Core_DAO_OptionValue', 'component_id', ['onlyActive' => FALSE, 'labelColumn' => 'name']); |
8f785c9e | 53 | $params['component_id'] = array_search($params['component_id'], $components); |
1cbea43e | 54 | } |
6a488035 TO |
55 | return civicrm_api3_option_value_create($params); |
56 | } | |
57 | ||
11e09c59 | 58 | /** |
0aa0303c EM |
59 | * Adjust Metadata for Create action. |
60 | * | |
61 | * The metadata is used for setting defaults, documentation & validation. | |
6a488035 | 62 | * |
cf470720 | 63 | * @param array $params |
b081365f | 64 | * Array of parameters determined by getfields. |
6a488035 TO |
65 | */ |
66 | function _civicrm_api3_report_template_create_spec(&$params) { | |
67 | require_once 'api/v3/OptionValue.php'; | |
68 | _civicrm_api3_option_value_create_spec($params); | |
cf8f0fff CW |
69 | $params['value']['api.aliases'] = ['report_url']; |
70 | $params['name']['api.aliases'] = ['class_name']; | |
1cbea43e C |
71 | $params['option_group_id']['api.default'] = CRM_Core_DAO::getFieldValue( |
72 | 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name' | |
73 | ); | |
6a488035 TO |
74 | // $params['component']['api.required'] = TRUE; |
75 | } | |
76 | ||
77 | /** | |
9d32e6f7 | 78 | * Deletes an existing ReportTemplate. |
6a488035 | 79 | * |
cf470720 | 80 | * @param array $params |
6a488035 | 81 | * |
a6c01b45 | 82 | * @return array |
00f8641b | 83 | * API result array |
26e4ac38 | 84 | * |
85 | * @throws \API_Exception | |
6a488035 TO |
86 | */ |
87 | function civicrm_api3_report_template_delete($params) { | |
88 | require_once 'api/v3/OptionValue.php'; | |
89 | return civicrm_api3_option_value_delete($params); | |
90 | } | |
91 | ||
c58f66e0 | 92 | /** |
9d32e6f7 | 93 | * Retrieve rows from a report template. |
c58f66e0 | 94 | * |
cf470720 TO |
95 | * @param array $params |
96 | * Input parameters. | |
c58f66e0 | 97 | * |
a6c01b45 | 98 | * @return array |
00f8641b | 99 | * API result array |
c58f66e0 E |
100 | */ |
101 | function civicrm_api3_report_template_getrows($params) { | |
cf8f0fff | 102 | civicrm_api3_verify_one_mandatory($params, NULL, ['report_id', 'instance_id']); |
781d91c8 | 103 | list($rows, $instance, $metadata) = _civicrm_api3_report_template_getrows($params); |
e6bab5ea | 104 | $instance->cleanUpTemporaryTables(); |
244bbdd8 | 105 | return civicrm_api3_create_success($rows, $params, 'ReportTemplate', 'getrows', CRM_Core_DAO::$_nullObject, $metadata); |
c58f66e0 E |
106 | } |
107 | ||
aa1b1481 | 108 | /** |
9d32e6f7 EM |
109 | * Get report template rows. |
110 | * | |
c490a46a | 111 | * @param array $params |
aa1b1481 EM |
112 | * |
113 | * @return array | |
114 | * @throws API_Exception | |
115 | * @throws CiviCRM_API3_Exception | |
116 | */ | |
c58f66e0 | 117 | function _civicrm_api3_report_template_getrows($params) { |
22e263ad | 118 | if (empty($params['report_id'])) { |
cf8f0fff | 119 | $params['report_id'] = civicrm_api3('report_instance', 'getvalue', ['id' => $params['instance_id'], 'return' => 'report_id']); |
05274f12 E |
120 | } |
121 | ||
cf8f0fff | 122 | $class = (string) civicrm_api3('option_value', 'getvalue', [ |
7b9f95c3 | 123 | 'option_group_name' => 'report_template', |
6f900755 E |
124 | 'return' => 'name', |
125 | 'value' => $params['report_id'], | |
7c31ae57 | 126 | ] |
c58f66e0 E |
127 | ); |
128 | ||
26e4ac38 | 129 | /* @var \CRM_Report_Form $reportInstance */ |
c58f66e0 | 130 | $reportInstance = new $class(); |
22e263ad | 131 | if (!empty($params['instance_id'])) { |
c58f66e0 E |
132 | $reportInstance->setID($params['instance_id']); |
133 | } | |
134 | $reportInstance->setParams($params); | |
135 | $reportInstance->noController = TRUE; | |
136 | $reportInstance->preProcess(); | |
137 | $reportInstance->setDefaultValues(FALSE); | |
138 | $reportInstance->setParams(array_merge($reportInstance->getDefaultValues(), $params)); | |
244bbdd8 | 139 | $options = _civicrm_api3_get_options_from_params($params, TRUE, 'ReportTemplate', 'get'); |
6f900755 | 140 | $reportInstance->setLimitValue($options['limit']); |
182f5081 | 141 | $reportInstance->setAddPaging(FALSE); |
6f900755 | 142 | $reportInstance->setOffsetValue($options['offset']); |
c58f66e0 E |
143 | $reportInstance->beginPostProcessCommon(); |
144 | $sql = $reportInstance->buildQuery(); | |
15d9e604 | 145 | $reportInstance->addToDeveloperTab($sql); |
7c31ae57 | 146 | $rows = $metadata = $requiredMetadata = []; |
c58f66e0 | 147 | $reportInstance->buildRows($sql, $rows); |
55f71fa7 | 148 | $reportInstance->formatDisplay($rows); |
149 | ||
22e263ad | 150 | if (isset($params['options']) && !empty($params['options']['metadata'])) { |
781d91c8 | 151 | $requiredMetadata = $params['options']['metadata']; |
22e263ad | 152 | if (in_array('title', $requiredMetadata)) { |
781d91c8 EM |
153 | $metadata['metadata']['title'] = $reportInstance->getTitle(); |
154 | } | |
22e263ad | 155 | if (in_array('labels', $requiredMetadata)) { |
781d91c8 | 156 | foreach ($reportInstance->_columnHeaders as $key => $header) { |
55f71fa7 | 157 | // Would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty |
781d91c8 EM |
158 | //NB I think these are already translated |
159 | $metadata['metadata']['labels'][$key] = !empty($header['title']) ? $header['title'] : ''; | |
160 | } | |
161 | } | |
55f71fa7 | 162 | if (in_array('sql', $requiredMetadata)) { |
163 | $metadata['metadata']['sql'] = $reportInstance->getReportSql(); | |
164 | } | |
7cab1323 | 165 | } |
cf8f0fff | 166 | return [$rows, $reportInstance, $metadata]; |
c58f66e0 E |
167 | } |
168 | ||
aa1b1481 | 169 | /** |
9d32e6f7 EM |
170 | * Get statistics from a given report. |
171 | * | |
c490a46a | 172 | * @param array $params |
aa1b1481 EM |
173 | * |
174 | * @return array | |
00f8641b | 175 | * API result array |
26e4ac38 | 176 | * |
177 | * @throws \API_Exception | |
178 | * @throws \CiviCRM_API3_Exception | |
aa1b1481 | 179 | */ |
c58f66e0 | 180 | function civicrm_api3_report_template_getstatistics($params) { |
781d91c8 | 181 | list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params); |
c58f66e0 | 182 | $stats = $reportInstance->statistics($rows); |
e6bab5ea | 183 | $reportInstance->cleanUpTemporaryTables(); |
244bbdd8 | 184 | return civicrm_api3_create_success($stats, $params, 'ReportTemplate', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata); |
c58f66e0 | 185 | } |
7c31ae57 | 186 | |
c58f66e0 | 187 | /** |
dc64d047 | 188 | * Adjust metadata for template getrows action. |
c58f66e0 | 189 | * |
cf470720 TO |
190 | * @param array $params |
191 | * Input parameters. | |
c58f66e0 E |
192 | */ |
193 | function _civicrm_api3_report_template_getrows_spec(&$params) { | |
cf8f0fff | 194 | $params['report_id'] = [ |
c58f66e0 | 195 | 'title' => 'Report ID - eg. member/lapse', |
cf8f0fff | 196 | ]; |
c58f66e0 E |
197 | } |
198 | ||
e70a7fc0 | 199 | /* @codingStandardsIgnoreStart |
6a488035 TO |
200 | function civicrm_api3_report_template_getfields($params) { |
201 | return civicrm_api3_create_success(array( | |
202 | 'id' => array( | |
203 | 'name' => 'id', | |
204 | 'type' => 1, | |
205 | 'required' => 1, | |
206 | ), | |
207 | 'option_group_id' => array( | |
208 | 'name' => 'option_group_id', | |
209 | 'type' => 1, | |
210 | 'required' => 1, | |
211 | 'FKClassName' => 'CRM_Core_DAO_OptionGroup', | |
212 | ), | |
213 | 'label' => array( | |
214 | 'name' => 'label', | |
215 | 'type' => 2, | |
216 | 'title' => 'Option Label', | |
217 | 'required' => 1, | |
218 | 'maxlength' => 255, | |
219 | 'size' => 45, | |
220 | ), | |
221 | 'value' => array( | |
222 | 'name' => 'value', | |
223 | 'type' => 2, | |
224 | 'title' => 'Option Value', | |
225 | 'required' => 1, | |
226 | 'maxlength' => 512, | |
227 | 'size' => 45, | |
228 | ), | |
229 | 'name' => array( | |
230 | 'name' => 'name', | |
231 | 'type' => 2, | |
232 | 'title' => 'Option Name', | |
233 | 'maxlength' => 255, | |
234 | 'size' => 45, | |
235 | 'import' => 1, | |
236 | 'where' => 'civicrm_option_value.name', | |
237 | 'export' => 1, | |
238 | ), | |
239 | 'grouping' => array( | |
240 | 'name' => 'grouping', | |
241 | 'type' => 2, | |
242 | 'title' => 'Option Grouping Name', | |
243 | 'maxlength' => 255, | |
244 | 'size' => 45, | |
245 | ), | |
246 | 'filter' => array( | |
247 | 'name' => 'filter', | |
248 | 'type' => 1, | |
249 | 'title' => 'Filter', | |
250 | ), | |
251 | 'is_default' => array( | |
252 | 'name' => 'is_default', | |
253 | 'type' => 16, | |
254 | ), | |
255 | 'weight' => array( | |
256 | 'name' => 'weight', | |
257 | 'type' => 1, | |
258 | 'title' => 'Weight', | |
259 | 'required' => 1, | |
260 | ), | |
261 | 'description' => array( | |
262 | 'name' => 'description', | |
263 | 'type' => 32, | |
264 | 'title' => 'Description', | |
265 | 'rows' => 8, | |
266 | 'cols' => 60, | |
267 | ), | |
268 | 'is_optgroup' => array( | |
269 | 'name' => 'is_optgroup', | |
270 | 'type' => 16, | |
271 | ), | |
272 | 'is_reserved' => array( | |
273 | 'name' => 'is_reserved', | |
274 | 'type' => 16, | |
275 | ), | |
276 | 'is_active' => array( | |
277 | 'name' => 'is_active', | |
278 | 'type' => 16, | |
279 | ), | |
280 | 'component_id' => array( | |
281 | 'name' => 'component_id', | |
282 | 'type' => 1, | |
283 | 'FKClassName' => 'CRM_Core_DAO_Component', | |
284 | ), | |
285 | 'domain_id' => array( | |
286 | 'name' => 'domain_id', | |
287 | 'type' => 1, | |
288 | 'FKClassName' => 'CRM_Core_DAO_Domain', | |
289 | ), | |
290 | 'visibility_id' => array( | |
291 | 'name' => 'visibility_id', | |
292 | 'type' => 1, | |
293 | 'default' => 'UL', | |
294 | ), | |
295 | )); | |
e70a7fc0 TO |
296 | } |
297 | @codingStandardsIgnoreEnd */ |