Merge pull request #10988 from jmcclelland/CRM-21194
[civicrm-core.git] / api / v3 / ReportTemplate.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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', array('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'] = array('report_url');
84 $params['name']['api.aliases'] = array('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, array('report_id', 'instance_id'));
115 list($rows, $instance, $metadata) = _civicrm_api3_report_template_getrows($params);
116 return civicrm_api3_create_success($rows, $params, 'ReportTemplate', 'getrows', CRM_Core_DAO::$_nullObject, $metadata);
117 }
118
119 /**
120 * Get report template rows.
121 *
122 * @param array $params
123 *
124 * @return array
125 * @throws API_Exception
126 * @throws CiviCRM_API3_Exception
127 */
128 function _civicrm_api3_report_template_getrows($params) {
129 if (empty($params['report_id'])) {
130 $params['report_id'] = civicrm_api3('report_instance', 'getvalue', array('id' => $params['instance_id'], 'return' => 'report_id'));
131 }
132
133 $class = (string) civicrm_api3('option_value', 'getvalue', array(
134 'option_group_name' => 'report_template',
135 'return' => 'name',
136 'value' => $params['report_id'],
137 )
138 );
139
140 $reportInstance = new $class();
141 if (!empty($params['instance_id'])) {
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));
149 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'ReportTemplate', 'get');
150 $reportInstance->setLimitValue($options['limit']);
151 $reportInstance->setAddPaging(FALSE);
152 $reportInstance->setOffsetValue($options['offset']);
153 $reportInstance->beginPostProcessCommon();
154 $sql = $reportInstance->buildQuery();
155 $reportInstance->addToDeveloperTab($sql);
156 $rows = $metadata = $requiredMetadata = array();
157 $reportInstance->buildRows($sql, $rows);
158 $reportInstance->formatDisplay($rows);
159
160 if (isset($params['options']) && !empty($params['options']['metadata'])) {
161 $requiredMetadata = $params['options']['metadata'];
162 if (in_array('title', $requiredMetadata)) {
163 $metadata['metadata']['title'] = $reportInstance->getTitle();
164 }
165 if (in_array('labels', $requiredMetadata)) {
166 foreach ($reportInstance->_columnHeaders as $key => $header) {
167 // Would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty
168 //NB I think these are already translated
169 $metadata['metadata']['labels'][$key] = !empty($header['title']) ? $header['title'] : '';
170 }
171 }
172 if (in_array('sql', $requiredMetadata)) {
173 $metadata['metadata']['sql'] = $reportInstance->getReportSql();
174 }
175 }
176 return array($rows, $reportInstance, $metadata);
177 }
178
179 /**
180 * Get statistics from a given report.
181 *
182 * @param array $params
183 *
184 * @return array
185 * API result array
186 */
187 function civicrm_api3_report_template_getstatistics($params) {
188 list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params);
189 $stats = $reportInstance->statistics($rows);
190 return civicrm_api3_create_success($stats, $params, 'ReportTemplate', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata);
191 }
192 /**
193 * Adjust metadata for template getrows action.
194 *
195 * @param array $params
196 * Input parameters.
197 */
198 function _civicrm_api3_report_template_getrows_spec(&$params) {
199 $params['report_id'] = array(
200 'title' => 'Report ID - eg. member/lapse',
201 );
202 }
203
204 /* @codingStandardsIgnoreStart
205 function 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 ));
301 }
302 @codingStandardsIgnoreEnd */