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