Merge pull request #11401 from JMAConsulting/CRM-21546
[civicrm-core.git] / api / v3 / ReportTemplate.php
CommitLineData
6a488035 1<?php
c28e1768
CW
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
c28e1768 5 +--------------------------------------------------------------------+
1f4ea726 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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'])) {
8f785c9e
AS
66 $components = CRM_Core_PseudoConstant::get('CRM_Core_DAO_OptionValue', 'component_id', array('onlyActive' => FALSE, 'labelColumn' => 'name'));
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);
6a488035
TO
83 $params['value']['api.aliases'] = array('report_url');
84 $params['name']['api.aliases'] = array('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) {
05274f12 114 civicrm_api3_verify_one_mandatory($params, NULL, array('report_id', 'instance_id'));
781d91c8 115 list($rows, $instance, $metadata) = _civicrm_api3_report_template_getrows($params);
244bbdd8 116 return civicrm_api3_create_success($rows, $params, 'ReportTemplate', 'getrows', CRM_Core_DAO::$_nullObject, $metadata);
c58f66e0
E
117}
118
aa1b1481 119/**
9d32e6f7
EM
120 * Get report template rows.
121 *
c490a46a 122 * @param array $params
aa1b1481
EM
123 *
124 * @return array
125 * @throws API_Exception
126 * @throws CiviCRM_API3_Exception
127 */
c58f66e0 128function _civicrm_api3_report_template_getrows($params) {
22e263ad 129 if (empty($params['report_id'])) {
05274f12
E
130 $params['report_id'] = civicrm_api3('report_instance', 'getvalue', array('id' => $params['instance_id'], 'return' => 'report_id'));
131 }
132
9d33294e 133 $class = (string) civicrm_api3('option_value', 'getvalue', array(
7b9f95c3 134 'option_group_name' => 'report_template',
6f900755
E
135 'return' => 'name',
136 'value' => $params['report_id'],
137 )
c58f66e0
E
138 );
139
140 $reportInstance = new $class();
22e263ad 141 if (!empty($params['instance_id'])) {
c58f66e0
E
142 $reportInstance->setID($params['instance_id']);
143 }
144 $reportInstance->setParams($params);
145 $reportInstance->noController = TRUE;
146 $reportInstance->preProcess();
147 $reportInstance->setDefaultValues(FALSE);
148 $reportInstance->setParams(array_merge($reportInstance->getDefaultValues(), $params));
244bbdd8 149 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'ReportTemplate', 'get');
6f900755 150 $reportInstance->setLimitValue($options['limit']);
182f5081 151 $reportInstance->setAddPaging(FALSE);
6f900755 152 $reportInstance->setOffsetValue($options['offset']);
c58f66e0
E
153 $reportInstance->beginPostProcessCommon();
154 $sql = $reportInstance->buildQuery();
781d91c8 155 $rows = $metadata = $requiredMetadata = array();
c58f66e0 156 $reportInstance->buildRows($sql, $rows);
55f71fa7 157 $reportInstance->formatDisplay($rows);
158
22e263ad 159 if (isset($params['options']) && !empty($params['options']['metadata'])) {
781d91c8 160 $requiredMetadata = $params['options']['metadata'];
22e263ad 161 if (in_array('title', $requiredMetadata)) {
781d91c8
EM
162 $metadata['metadata']['title'] = $reportInstance->getTitle();
163 }
22e263ad 164 if (in_array('labels', $requiredMetadata)) {
781d91c8 165 foreach ($reportInstance->_columnHeaders as $key => $header) {
55f71fa7 166 // Would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty
781d91c8
EM
167 //NB I think these are already translated
168 $metadata['metadata']['labels'][$key] = !empty($header['title']) ? $header['title'] : '';
169 }
170 }
55f71fa7 171 if (in_array('sql', $requiredMetadata)) {
172 $metadata['metadata']['sql'] = $reportInstance->getReportSql();
173 }
7cab1323
EM
174 }
175 return array($rows, $reportInstance, $metadata);
c58f66e0
E
176}
177
aa1b1481 178/**
9d32e6f7
EM
179 * Get statistics from a given report.
180 *
c490a46a 181 * @param array $params
aa1b1481
EM
182 *
183 * @return array
00f8641b 184 * API result array
aa1b1481 185 */
c58f66e0 186function civicrm_api3_report_template_getstatistics($params) {
781d91c8 187 list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params);
c58f66e0 188 $stats = $reportInstance->statistics($rows);
244bbdd8 189 return civicrm_api3_create_success($stats, $params, 'ReportTemplate', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata);
c58f66e0
E
190}
191/**
dc64d047 192 * Adjust metadata for template getrows action.
c58f66e0 193 *
cf470720
TO
194 * @param array $params
195 * Input parameters.
c58f66e0
E
196 */
197function _civicrm_api3_report_template_getrows_spec(&$params) {
198 $params['report_id'] = array(
c58f66e0
E
199 'title' => 'Report ID - eg. member/lapse',
200 );
201}
202
e70a7fc0 203/* @codingStandardsIgnoreStart
6a488035
TO
204function civicrm_api3_report_template_getfields($params) {
205 return civicrm_api3_create_success(array(
206 'id' => array(
207 'name' => 'id',
208 'type' => 1,
209 'required' => 1,
210 ),
211 'option_group_id' => array(
212 'name' => 'option_group_id',
213 'type' => 1,
214 'required' => 1,
215 'FKClassName' => 'CRM_Core_DAO_OptionGroup',
216 ),
217 'label' => array(
218 'name' => 'label',
219 'type' => 2,
220 'title' => 'Option Label',
221 'required' => 1,
222 'maxlength' => 255,
223 'size' => 45,
224 ),
225 'value' => array(
226 'name' => 'value',
227 'type' => 2,
228 'title' => 'Option Value',
229 'required' => 1,
230 'maxlength' => 512,
231 'size' => 45,
232 ),
233 'name' => array(
234 'name' => 'name',
235 'type' => 2,
236 'title' => 'Option Name',
237 'maxlength' => 255,
238 'size' => 45,
239 'import' => 1,
240 'where' => 'civicrm_option_value.name',
241 'export' => 1,
242 ),
243 'grouping' => array(
244 'name' => 'grouping',
245 'type' => 2,
246 'title' => 'Option Grouping Name',
247 'maxlength' => 255,
248 'size' => 45,
249 ),
250 'filter' => array(
251 'name' => 'filter',
252 'type' => 1,
253 'title' => 'Filter',
254 ),
255 'is_default' => array(
256 'name' => 'is_default',
257 'type' => 16,
258 ),
259 'weight' => array(
260 'name' => 'weight',
261 'type' => 1,
262 'title' => 'Weight',
263 'required' => 1,
264 ),
265 'description' => array(
266 'name' => 'description',
267 'type' => 32,
268 'title' => 'Description',
269 'rows' => 8,
270 'cols' => 60,
271 ),
272 'is_optgroup' => array(
273 'name' => 'is_optgroup',
274 'type' => 16,
275 ),
276 'is_reserved' => array(
277 'name' => 'is_reserved',
278 'type' => 16,
279 ),
280 'is_active' => array(
281 'name' => 'is_active',
282 'type' => 16,
283 ),
284 'component_id' => array(
285 'name' => 'component_id',
286 'type' => 1,
287 'FKClassName' => 'CRM_Core_DAO_Component',
288 ),
289 'domain_id' => array(
290 'name' => 'domain_id',
291 'type' => 1,
292 'FKClassName' => 'CRM_Core_DAO_Domain',
293 ),
294 'visibility_id' => array(
295 'name' => 'visibility_id',
296 'type' => 1,
297 'default' => 'UL',
298 ),
299 ));
e70a7fc0
TO
300}
301@codingStandardsIgnoreEnd */