Merge pull request #14981 from eileenmcnaughton/load_extract
[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'],
7c31ae57 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);
7c31ae57 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 193}
7c31ae57 194
c58f66e0 195/**
dc64d047 196 * Adjust metadata for template getrows action.
c58f66e0 197 *
cf470720
TO
198 * @param array $params
199 * Input parameters.
c58f66e0
E
200 */
201function _civicrm_api3_report_template_getrows_spec(&$params) {
cf8f0fff 202 $params['report_id'] = [
c58f66e0 203 'title' => 'Report ID - eg. member/lapse',
cf8f0fff 204 ];
c58f66e0
E
205}
206
e70a7fc0 207/* @codingStandardsIgnoreStart
6a488035
TO
208function civicrm_api3_report_template_getfields($params) {
209 return civicrm_api3_create_success(array(
210 'id' => array(
211 'name' => 'id',
212 'type' => 1,
213 'required' => 1,
214 ),
215 'option_group_id' => array(
216 'name' => 'option_group_id',
217 'type' => 1,
218 'required' => 1,
219 'FKClassName' => 'CRM_Core_DAO_OptionGroup',
220 ),
221 'label' => array(
222 'name' => 'label',
223 'type' => 2,
224 'title' => 'Option Label',
225 'required' => 1,
226 'maxlength' => 255,
227 'size' => 45,
228 ),
229 'value' => array(
230 'name' => 'value',
231 'type' => 2,
232 'title' => 'Option Value',
233 'required' => 1,
234 'maxlength' => 512,
235 'size' => 45,
236 ),
237 'name' => array(
238 'name' => 'name',
239 'type' => 2,
240 'title' => 'Option Name',
241 'maxlength' => 255,
242 'size' => 45,
243 'import' => 1,
244 'where' => 'civicrm_option_value.name',
245 'export' => 1,
246 ),
247 'grouping' => array(
248 'name' => 'grouping',
249 'type' => 2,
250 'title' => 'Option Grouping Name',
251 'maxlength' => 255,
252 'size' => 45,
253 ),
254 'filter' => array(
255 'name' => 'filter',
256 'type' => 1,
257 'title' => 'Filter',
258 ),
259 'is_default' => array(
260 'name' => 'is_default',
261 'type' => 16,
262 ),
263 'weight' => array(
264 'name' => 'weight',
265 'type' => 1,
266 'title' => 'Weight',
267 'required' => 1,
268 ),
269 'description' => array(
270 'name' => 'description',
271 'type' => 32,
272 'title' => 'Description',
273 'rows' => 8,
274 'cols' => 60,
275 ),
276 'is_optgroup' => array(
277 'name' => 'is_optgroup',
278 'type' => 16,
279 ),
280 'is_reserved' => array(
281 'name' => 'is_reserved',
282 'type' => 16,
283 ),
284 'is_active' => array(
285 'name' => 'is_active',
286 'type' => 16,
287 ),
288 'component_id' => array(
289 'name' => 'component_id',
290 'type' => 1,
291 'FKClassName' => 'CRM_Core_DAO_Component',
292 ),
293 'domain_id' => array(
294 'name' => 'domain_id',
295 'type' => 1,
296 'FKClassName' => 'CRM_Core_DAO_Domain',
297 ),
298 'visibility_id' => array(
299 'name' => 'visibility_id',
300 'type' => 1,
301 'default' => 'UL',
302 ),
303 ));
e70a7fc0
TO
304}
305@codingStandardsIgnoreEnd */