Merge pull request #18266 from sunilpawar/dev_912
[civicrm-core.git] / api / v3 / ReportTemplate.php
CommitLineData
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 */
26function 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 */
46function 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 */
66function _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 */
87function 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
c86f74b1 100 *
101 * @throws \API_Exception
102 * @throws \CiviCRM_API3_Exception
c58f66e0
E
103 */
104function civicrm_api3_report_template_getrows($params) {
cf8f0fff 105 civicrm_api3_verify_one_mandatory($params, NULL, ['report_id', 'instance_id']);
781d91c8 106 list($rows, $instance, $metadata) = _civicrm_api3_report_template_getrows($params);
e6bab5ea 107 $instance->cleanUpTemporaryTables();
244bbdd8 108 return civicrm_api3_create_success($rows, $params, 'ReportTemplate', 'getrows', CRM_Core_DAO::$_nullObject, $metadata);
c58f66e0
E
109}
110
aa1b1481 111/**
9d32e6f7
EM
112 * Get report template rows.
113 *
c490a46a 114 * @param array $params
aa1b1481
EM
115 *
116 * @return array
117 * @throws API_Exception
118 * @throws CiviCRM_API3_Exception
119 */
c58f66e0 120function _civicrm_api3_report_template_getrows($params) {
22e263ad 121 if (empty($params['report_id'])) {
cf8f0fff 122 $params['report_id'] = civicrm_api3('report_instance', 'getvalue', ['id' => $params['instance_id'], 'return' => 'report_id']);
05274f12
E
123 }
124
cf8f0fff 125 $class = (string) civicrm_api3('option_value', 'getvalue', [
7b9f95c3 126 'option_group_name' => 'report_template',
6f900755
E
127 'return' => 'name',
128 'value' => $params['report_id'],
7c31ae57 129 ]
c58f66e0
E
130 );
131
26e4ac38 132 /* @var \CRM_Report_Form $reportInstance */
c58f66e0 133 $reportInstance = new $class();
22e263ad 134 if (!empty($params['instance_id'])) {
c58f66e0
E
135 $reportInstance->setID($params['instance_id']);
136 }
137 $reportInstance->setParams($params);
138 $reportInstance->noController = TRUE;
139 $reportInstance->preProcess();
140 $reportInstance->setDefaultValues(FALSE);
141 $reportInstance->setParams(array_merge($reportInstance->getDefaultValues(), $params));
244bbdd8 142 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'ReportTemplate', 'get');
6f900755 143 $reportInstance->setLimitValue($options['limit']);
182f5081 144 $reportInstance->setAddPaging(FALSE);
6f900755 145 $reportInstance->setOffsetValue($options['offset']);
c58f66e0
E
146 $reportInstance->beginPostProcessCommon();
147 $sql = $reportInstance->buildQuery();
15d9e604 148 $reportInstance->addToDeveloperTab($sql);
7c31ae57 149 $rows = $metadata = $requiredMetadata = [];
c58f66e0 150 $reportInstance->buildRows($sql, $rows);
55f71fa7 151 $reportInstance->formatDisplay($rows);
152
22e263ad 153 if (isset($params['options']) && !empty($params['options']['metadata'])) {
781d91c8 154 $requiredMetadata = $params['options']['metadata'];
22e263ad 155 if (in_array('title', $requiredMetadata)) {
781d91c8
EM
156 $metadata['metadata']['title'] = $reportInstance->getTitle();
157 }
22e263ad 158 if (in_array('labels', $requiredMetadata)) {
781d91c8 159 foreach ($reportInstance->_columnHeaders as $key => $header) {
55f71fa7 160 // Would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty
781d91c8
EM
161 //NB I think these are already translated
162 $metadata['metadata']['labels'][$key] = !empty($header['title']) ? $header['title'] : '';
163 }
164 }
55f71fa7 165 if (in_array('sql', $requiredMetadata)) {
166 $metadata['metadata']['sql'] = $reportInstance->getReportSql();
167 }
7cab1323 168 }
cf8f0fff 169 return [$rows, $reportInstance, $metadata];
c58f66e0
E
170}
171
aa1b1481 172/**
9d32e6f7
EM
173 * Get statistics from a given report.
174 *
c490a46a 175 * @param array $params
aa1b1481
EM
176 *
177 * @return array
00f8641b 178 * API result array
26e4ac38 179 *
180 * @throws \API_Exception
181 * @throws \CiviCRM_API3_Exception
aa1b1481 182 */
c58f66e0 183function civicrm_api3_report_template_getstatistics($params) {
781d91c8 184 list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params);
c58f66e0 185 $stats = $reportInstance->statistics($rows);
bd2913d3 186 if (isset($metadata['metadata']['sql'])) {
187 // Update for stats queries.
188 $metadata['metadata']['sql'] = $reportInstance->getReportSql();
189 }
e6bab5ea 190 $reportInstance->cleanUpTemporaryTables();
244bbdd8 191 return civicrm_api3_create_success($stats, $params, 'ReportTemplate', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata);
c58f66e0 192}
7c31ae57 193
c58f66e0 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 */