Merge pull request #14981 from eileenmcnaughton/load_extract
[civicrm-core.git] / api / v3 / ReportTemplate.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 function 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 );
65 if (!empty($params['component_id']) && !is_numeric($params['component_id'])) {
66 $components = CRM_Core_PseudoConstant::get('CRM_Core_DAO_OptionValue', 'component_id', ['onlyActive' => FALSE, 'labelColumn' => 'name']);
67 $params['component_id'] = array_search($params['component_id'], $components);
68 }
69 return civicrm_api3_option_value_create($params);
70 }
71
72 /**
73 * Adjust Metadata for Create action.
74 *
75 * The metadata is used for setting defaults, documentation & validation.
76 *
77 * @param array $params
78 * Array of parameters determined by getfields.
79 */
80 function _civicrm_api3_report_template_create_spec(&$params) {
81 require_once 'api/v3/OptionValue.php';
82 _civicrm_api3_option_value_create_spec($params);
83 $params['value']['api.aliases'] = ['report_url'];
84 $params['name']['api.aliases'] = ['class_name'];
85 $params['option_group_id']['api.default'] = CRM_Core_DAO::getFieldValue(
86 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
87 );
88 // $params['component']['api.required'] = TRUE;
89 }
90
91 /**
92 * Deletes an existing ReportTemplate.
93 *
94 * @param array $params
95 *
96 * @return array
97 * API result array
98 */
99 function civicrm_api3_report_template_delete($params) {
100 require_once 'api/v3/OptionValue.php';
101 return civicrm_api3_option_value_delete($params);
102 }
103
104 /**
105 * Retrieve rows from a report template.
106 *
107 * @param array $params
108 * Input parameters.
109 *
110 * @return array
111 * API result array
112 */
113 function civicrm_api3_report_template_getrows($params) {
114 civicrm_api3_verify_one_mandatory($params, NULL, ['report_id', 'instance_id']);
115 list($rows, $instance, $metadata) = _civicrm_api3_report_template_getrows($params);
116 $instance->cleanUpTemporaryTables();
117 return civicrm_api3_create_success($rows, $params, 'ReportTemplate', 'getrows', CRM_Core_DAO::$_nullObject, $metadata);
118 }
119
120 /**
121 * Get report template rows.
122 *
123 * @param array $params
124 *
125 * @return array
126 * @throws API_Exception
127 * @throws CiviCRM_API3_Exception
128 */
129 function _civicrm_api3_report_template_getrows($params) {
130 if (empty($params['report_id'])) {
131 $params['report_id'] = civicrm_api3('report_instance', 'getvalue', ['id' => $params['instance_id'], 'return' => 'report_id']);
132 }
133
134 $class = (string) civicrm_api3('option_value', 'getvalue', [
135 'option_group_name' => 'report_template',
136 'return' => 'name',
137 'value' => $params['report_id'],
138 ]
139 );
140
141 $reportInstance = new $class();
142 if (!empty($params['instance_id'])) {
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));
150 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'ReportTemplate', 'get');
151 $reportInstance->setLimitValue($options['limit']);
152 $reportInstance->setAddPaging(FALSE);
153 $reportInstance->setOffsetValue($options['offset']);
154 $reportInstance->beginPostProcessCommon();
155 $sql = $reportInstance->buildQuery();
156 $reportInstance->addToDeveloperTab($sql);
157 $rows = $metadata = $requiredMetadata = [];
158 $reportInstance->buildRows($sql, $rows);
159 $reportInstance->formatDisplay($rows);
160
161 if (isset($params['options']) && !empty($params['options']['metadata'])) {
162 $requiredMetadata = $params['options']['metadata'];
163 if (in_array('title', $requiredMetadata)) {
164 $metadata['metadata']['title'] = $reportInstance->getTitle();
165 }
166 if (in_array('labels', $requiredMetadata)) {
167 foreach ($reportInstance->_columnHeaders as $key => $header) {
168 // Would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty
169 //NB I think these are already translated
170 $metadata['metadata']['labels'][$key] = !empty($header['title']) ? $header['title'] : '';
171 }
172 }
173 if (in_array('sql', $requiredMetadata)) {
174 $metadata['metadata']['sql'] = $reportInstance->getReportSql();
175 }
176 }
177 return [$rows, $reportInstance, $metadata];
178 }
179
180 /**
181 * Get statistics from a given report.
182 *
183 * @param array $params
184 *
185 * @return array
186 * API result array
187 */
188 function civicrm_api3_report_template_getstatistics($params) {
189 list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params);
190 $stats = $reportInstance->statistics($rows);
191 $reportInstance->cleanUpTemporaryTables();
192 return civicrm_api3_create_success($stats, $params, 'ReportTemplate', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata);
193 }
194
195 /**
196 * Adjust metadata for template getrows action.
197 *
198 * @param array $params
199 * Input parameters.
200 */
201 function _civicrm_api3_report_template_getrows_spec(&$params) {
202 $params['report_id'] = [
203 'title' => 'Report ID - eg. member/lapse',
204 ];
205 }
206
207 /* @codingStandardsIgnoreStart
208 function 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 ));
304 }
305 @codingStandardsIgnoreEnd */