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