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