c6ea19926c4bf6d9d7100bfe0c6ff80700f1a4e4
3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This api exposes CiviCRM report templates.
31 * @package CiviCRM_APIv3
35 * Retrieve a report template.
37 * @param array $params
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'
47 return civicrm_api3_option_value_get($params);
53 * OptionValues are used to classify CRM entities (including Contacts, Groups and Actions).
55 * @param array $params
60 * @throws \API_Exception
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'
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);
71 return civicrm_api3_option_value_create($params);
75 * Adjust Metadata for Create action.
77 * The metadata is used for setting defaults, documentation & validation.
79 * @param array $params
80 * Array of parameters determined by getfields.
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'
90 // $params['component']['api.required'] = TRUE;
94 * Deletes an existing ReportTemplate.
96 * @param array $params
101 * @throws \API_Exception
103 function civicrm_api3_report_template_delete($params) {
104 require_once 'api/v3/OptionValue.php';
105 return civicrm_api3_option_value_delete($params);
109 * Retrieve rows from a report template.
111 * @param array $params
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);
125 * Get report template rows.
127 * @param array $params
130 * @throws API_Exception
131 * @throws CiviCRM_API3_Exception
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']);
138 $class = (string) civicrm_api3('option_value', 'getvalue', [
139 'option_group_name' => 'report_template',
141 'value' => $params['report_id'],
145 /* @var \CRM_Report_Form $reportInstance */
146 $reportInstance = new $class();
147 if (!empty($params['instance_id'])) {
148 $reportInstance->setID($params['instance_id']);
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);
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();
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'] : '';
178 if (in_array('sql', $requiredMetadata)) {
179 $metadata['metadata']['sql'] = $reportInstance->getReportSql();
182 return [$rows, $reportInstance, $metadata];
186 * Get statistics from a given report.
188 * @param array $params
193 * @throws \API_Exception
194 * @throws \CiviCRM_API3_Exception
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);
204 * Adjust metadata for template getrows action.
206 * @param array $params
209 function _civicrm_api3_report_template_getrows_spec(&$params) {
210 $params['report_id'] = [
211 'title' => 'Report ID - eg. member/lapse',
215 /* @codingStandardsIgnoreStart
216 function civicrm_api3_report_template_getfields($params) {
217 return civicrm_api3_create_success(array(
223 'option_group_id' => array(
224 'name' => 'option_group_id',
227 'FKClassName' => 'CRM_Core_DAO_OptionGroup',
232 'title' => 'Option Label',
240 'title' => 'Option Value',
248 'title' => 'Option Name',
252 'where' => 'civicrm_option_value.name',
256 'name' => 'grouping',
258 'title' => 'Option Grouping Name',
267 'is_default' => array(
268 'name' => 'is_default',
277 'description' => array(
278 'name' => 'description',
280 'title' => 'Description',
284 'is_optgroup' => array(
285 'name' => 'is_optgroup',
288 'is_reserved' => array(
289 'name' => 'is_reserved',
292 'is_active' => array(
293 'name' => 'is_active',
296 'component_id' => array(
297 'name' => 'component_id',
299 'FKClassName' => 'CRM_Core_DAO_Component',
301 'domain_id' => array(
302 'name' => 'domain_id',
304 'FKClassName' => 'CRM_Core_DAO_Domain',
306 'visibility_id' => array(
307 'name' => 'visibility_id',
313 @codingStandardsIgnoreEnd */