3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * This api exposes CiviCRM report templates.
15 * @package CiviCRM_APIv3
19 * Retrieve a report template.
21 * @param array $params
26 function civicrm_api3_report_template_get($params) {
27 require_once 'api/v3/OptionValue.php';
28 $params['option_group_id'] = CRM_Core_DAO
::getFieldValue(
29 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
31 return civicrm_api3_option_value_get($params);
37 * OptionValues are used to classify CRM entities (including Contacts, Groups and Actions).
39 * @param array $params
44 * @throws \API_Exception
46 function civicrm_api3_report_template_create($params) {
47 require_once 'api/v3/OptionValue.php';
48 $params['option_group_id'] = CRM_Core_DAO
::getFieldValue(
49 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
51 if (!empty($params['component_id']) && !is_numeric($params['component_id'])) {
52 $components = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_OptionValue', 'component_id', ['onlyActive' => FALSE, 'labelColumn' => 'name']);
53 $params['component_id'] = array_search($params['component_id'], $components);
55 return civicrm_api3_option_value_create($params);
59 * Adjust Metadata for Create action.
61 * The metadata is used for setting defaults, documentation & validation.
63 * @param array $params
64 * Array of parameters determined by getfields.
66 function _civicrm_api3_report_template_create_spec(&$params) {
67 require_once 'api/v3/OptionValue.php';
68 _civicrm_api3_option_value_create_spec($params);
69 $params['value']['api.aliases'] = ['report_url'];
70 $params['name']['api.aliases'] = ['class_name'];
71 $params['option_group_id']['api.default'] = CRM_Core_DAO
::getFieldValue(
72 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
74 // $params['component']['api.required'] = TRUE;
78 * Deletes an existing ReportTemplate.
80 * @param array $params
85 * @throws \API_Exception
87 function civicrm_api3_report_template_delete($params) {
88 require_once 'api/v3/OptionValue.php';
89 return civicrm_api3_option_value_delete($params);
93 * Retrieve rows from a report template.
95 * @param array $params
101 * @throws \API_Exception
102 * @throws \CiviCRM_API3_Exception
104 function civicrm_api3_report_template_getrows($params) {
105 civicrm_api3_verify_one_mandatory($params, NULL, ['report_id', 'instance_id']);
106 list($rows, $instance, $metadata) = _civicrm_api3_report_template_getrows($params);
107 $instance->cleanUpTemporaryTables();
108 return civicrm_api3_create_success($rows, $params, 'ReportTemplate', 'getrows', CRM_Core_DAO
::$_nullObject, $metadata);
112 * Get report template rows.
114 * @param array $params
117 * @throws API_Exception
118 * @throws CiviCRM_API3_Exception
120 function _civicrm_api3_report_template_getrows($params) {
121 if (empty($params['report_id'])) {
122 $params['report_id'] = civicrm_api3('report_instance', 'getvalue', ['id' => $params['instance_id'], 'return' => 'report_id']);
125 $class = (string) civicrm_api3('option_value', 'getvalue', [
126 'option_group_name' => 'report_template',
128 'value' => $params['report_id'],
132 /* @var \CRM_Report_Form $reportInstance */
133 $reportInstance = new $class();
134 if (!empty($params['instance_id'])) {
135 $reportInstance->setID($params['instance_id']);
137 $reportInstance->setParams($params);
138 $reportInstance->noController
= TRUE;
139 $reportInstance->preProcess();
140 $reportInstance->setDefaultValues(FALSE);
141 $reportInstance->setParams(array_merge($reportInstance->getDefaultValues(), $params));
142 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'ReportTemplate', 'get');
143 $reportInstance->setLimitValue($options['limit']);
144 $reportInstance->setAddPaging(FALSE);
145 $reportInstance->setOffsetValue($options['offset']);
146 $reportInstance->beginPostProcessCommon();
147 $sql = $reportInstance->buildQuery();
148 $reportInstance->addToDeveloperTab($sql);
149 $rows = $metadata = $requiredMetadata = [];
150 $reportInstance->buildRows($sql, $rows);
151 $reportInstance->formatDisplay($rows);
153 if (isset($params['options']) && !empty($params['options']['metadata'])) {
154 $requiredMetadata = $params['options']['metadata'];
155 if (in_array('title', $requiredMetadata)) {
156 $metadata['metadata']['title'] = $reportInstance->getTitle();
158 if (in_array('labels', $requiredMetadata)) {
159 foreach ($reportInstance->_columnHeaders
as $key => $header) {
160 // Would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty
161 //NB I think these are already translated
162 $metadata['metadata']['labels'][$key] = !empty($header['title']) ?
$header['title'] : '';
165 if (in_array('sql', $requiredMetadata)) {
166 $metadata['metadata']['sql'] = $reportInstance->getReportSql();
169 return [$rows, $reportInstance, $metadata];
173 * Get statistics from a given report.
175 * @param array $params
180 * @throws \API_Exception
181 * @throws \CiviCRM_API3_Exception
183 function civicrm_api3_report_template_getstatistics($params) {
184 list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params);
185 $stats = $reportInstance->statistics($rows);
186 if (isset($metadata['metadata']['sql'])) {
187 // Update for stats queries.
188 $metadata['metadata']['sql'] = $reportInstance->getReportSql();
190 $reportInstance->cleanUpTemporaryTables();
191 return civicrm_api3_create_success($stats, $params, 'ReportTemplate', 'getstatistics', CRM_Core_DAO
::$_nullObject, $metadata);
195 * Adjust metadata for template getrows action.
197 * @param array $params
200 function _civicrm_api3_report_template_getrows_spec(&$params) {
201 $params['report_id'] = [
202 'title' => 'Report ID - eg. member/lapse',
206 /* @codingStandardsIgnoreStart
207 function civicrm_api3_report_template_getfields($params) {
208 return civicrm_api3_create_success(array(
214 'option_group_id' => array(
215 'name' => 'option_group_id',
218 'FKClassName' => 'CRM_Core_DAO_OptionGroup',
223 'title' => 'Option Label',
231 'title' => 'Option Value',
239 'title' => 'Option Name',
243 'where' => 'civicrm_option_value.name',
247 'name' => 'grouping',
249 'title' => 'Option Grouping Name',
258 'is_default' => array(
259 'name' => 'is_default',
268 'description' => array(
269 'name' => 'description',
271 'title' => 'Description',
275 'is_optgroup' => array(
276 'name' => 'is_optgroup',
279 'is_reserved' => array(
280 'name' => 'is_reserved',
283 'is_active' => array(
284 'name' => 'is_active',
287 'component_id' => array(
288 'name' => 'component_id',
290 'FKClassName' => 'CRM_Core_DAO_Component',
292 'domain_id' => array(
293 'name' => 'domain_id',
295 'FKClassName' => 'CRM_Core_DAO_Domain',
297 'visibility_id' => array(
298 'name' => 'visibility_id',
304 @codingStandardsIgnoreEnd */