Merge pull request #11987 from civicrm/5.1
[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();
15d9e604 155 $reportInstance->addToDeveloperTab($sql);
781d91c8 156 $rows = $metadata = $requiredMetadata = array();
c58f66e0 157 $reportInstance->buildRows($sql, $rows);
55f71fa7 158 $reportInstance->formatDisplay($rows);
159
22e263ad 160 if (isset($params['options']) && !empty($params['options']['metadata'])) {
781d91c8 161 $requiredMetadata = $params['options']['metadata'];
22e263ad 162 if (in_array('title', $requiredMetadata)) {
781d91c8
EM
163 $metadata['metadata']['title'] = $reportInstance->getTitle();
164 }
22e263ad 165 if (in_array('labels', $requiredMetadata)) {
781d91c8 166 foreach ($reportInstance->_columnHeaders as $key => $header) {
55f71fa7 167 // Would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty
781d91c8
EM
168 //NB I think these are already translated
169 $metadata['metadata']['labels'][$key] = !empty($header['title']) ? $header['title'] : '';
170 }
171 }
55f71fa7 172 if (in_array('sql', $requiredMetadata)) {
173 $metadata['metadata']['sql'] = $reportInstance->getReportSql();
174 }
7cab1323
EM
175 }
176 return array($rows, $reportInstance, $metadata);
c58f66e0
E
177}
178
aa1b1481 179/**
9d32e6f7
EM
180 * Get statistics from a given report.
181 *
c490a46a 182 * @param array $params
aa1b1481
EM
183 *
184 * @return array
00f8641b 185 * API result array
aa1b1481 186 */
c58f66e0 187function civicrm_api3_report_template_getstatistics($params) {
781d91c8 188 list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params);
c58f66e0 189 $stats = $reportInstance->statistics($rows);
244bbdd8 190 return civicrm_api3_create_success($stats, $params, 'ReportTemplate', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata);
c58f66e0
E
191}
192/**
dc64d047 193 * Adjust metadata for template getrows action.
c58f66e0 194 *
cf470720
TO
195 * @param array $params
196 * Input parameters.
c58f66e0
E
197 */
198function _civicrm_api3_report_template_getrows_spec(&$params) {
199 $params['report_id'] = array(
c58f66e0
E
200 'title' => 'Report ID - eg. member/lapse',
201 );
202}
203
e70a7fc0 204/* @codingStandardsIgnoreStart
6a488035
TO
205function civicrm_api3_report_template_getfields($params) {
206 return civicrm_api3_create_success(array(
207 'id' => array(
208 'name' => 'id',
209 'type' => 1,
210 'required' => 1,
211 ),
212 'option_group_id' => array(
213 'name' => 'option_group_id',
214 'type' => 1,
215 'required' => 1,
216 'FKClassName' => 'CRM_Core_DAO_OptionGroup',
217 ),
218 'label' => array(
219 'name' => 'label',
220 'type' => 2,
221 'title' => 'Option Label',
222 'required' => 1,
223 'maxlength' => 255,
224 'size' => 45,
225 ),
226 'value' => array(
227 'name' => 'value',
228 'type' => 2,
229 'title' => 'Option Value',
230 'required' => 1,
231 'maxlength' => 512,
232 'size' => 45,
233 ),
234 'name' => array(
235 'name' => 'name',
236 'type' => 2,
237 'title' => 'Option Name',
238 'maxlength' => 255,
239 'size' => 45,
240 'import' => 1,
241 'where' => 'civicrm_option_value.name',
242 'export' => 1,
243 ),
244 'grouping' => array(
245 'name' => 'grouping',
246 'type' => 2,
247 'title' => 'Option Grouping Name',
248 'maxlength' => 255,
249 'size' => 45,
250 ),
251 'filter' => array(
252 'name' => 'filter',
253 'type' => 1,
254 'title' => 'Filter',
255 ),
256 'is_default' => array(
257 'name' => 'is_default',
258 'type' => 16,
259 ),
260 'weight' => array(
261 'name' => 'weight',
262 'type' => 1,
263 'title' => 'Weight',
264 'required' => 1,
265 ),
266 'description' => array(
267 'name' => 'description',
268 'type' => 32,
269 'title' => 'Description',
270 'rows' => 8,
271 'cols' => 60,
272 ),
273 'is_optgroup' => array(
274 'name' => 'is_optgroup',
275 'type' => 16,
276 ),
277 'is_reserved' => array(
278 'name' => 'is_reserved',
279 'type' => 16,
280 ),
281 'is_active' => array(
282 'name' => 'is_active',
283 'type' => 16,
284 ),
285 'component_id' => array(
286 'name' => 'component_id',
287 'type' => 1,
288 'FKClassName' => 'CRM_Core_DAO_Component',
289 ),
290 'domain_id' => array(
291 'name' => 'domain_id',
292 'type' => 1,
293 'FKClassName' => 'CRM_Core_DAO_Domain',
294 ),
295 'visibility_id' => array(
296 'name' => 'visibility_id',
297 'type' => 1,
298 'default' => 'UL',
299 ),
300 ));
e70a7fc0
TO
301}
302@codingStandardsIgnoreEnd */