3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 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'
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);
69 return civicrm_api3_option_value_create($params);
73 * Adjust Metadata for Create action.
75 * The metadata is used for setting defaults, documentation & validation.
77 * @param array $params
78 * Array of parameters determined by getfields.
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'
88 // $params['component']['api.required'] = TRUE;
92 * Deletes an existing ReportTemplate.
94 * @param array $params
99 function civicrm_api3_report_template_delete($params) {
100 require_once 'api/v3/OptionValue.php';
101 return civicrm_api3_option_value_delete($params);
105 * Retrieve rows from a report template.
107 * @param array $params
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);
120 * Get report template rows.
122 * @param array $params
125 * @throws API_Exception
126 * @throws CiviCRM_API3_Exception
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'));
133 $class = (string) civicrm_api3('option_value', 'getvalue', array(
134 'option_group_name' => 'report_template',
136 'value' => $params['report_id'],
140 $reportInstance = new $class();
141 if (!empty($params['instance_id'])) {
142 $reportInstance->setID($params['instance_id']);
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 $rows = $metadata = $requiredMetadata = array();
156 $reportInstance->buildRows($sql, $rows);
157 $reportInstance->formatDisplay($rows);
159 if (isset($params['options']) && !empty($params['options']['metadata'])) {
160 $requiredMetadata = $params['options']['metadata'];
161 if (in_array('title', $requiredMetadata)) {
162 $metadata['metadata']['title'] = $reportInstance->getTitle();
164 if (in_array('labels', $requiredMetadata)) {
165 foreach ($reportInstance->_columnHeaders
as $key => $header) {
166 // Would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty
167 //NB I think these are already translated
168 $metadata['metadata']['labels'][$key] = !empty($header['title']) ?
$header['title'] : '';
171 if (in_array('sql', $requiredMetadata)) {
172 $metadata['metadata']['sql'] = $reportInstance->getReportSql();
175 return array($rows, $reportInstance, $metadata);
179 * Get statistics from a given report.
181 * @param array $params
186 function civicrm_api3_report_template_getstatistics($params) {
187 list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params);
188 $stats = $reportInstance->statistics($rows);
189 return civicrm_api3_create_success($stats, $params, 'ReportTemplate', 'getstatistics', CRM_Core_DAO
::$_nullObject, $metadata);
192 * Adjust metadata for template getrows action.
194 * @param array $params
197 function _civicrm_api3_report_template_getrows_spec(&$params) {
198 $params['report_id'] = array(
199 'title' => 'Report ID - eg. member/lapse',
203 /* @codingStandardsIgnoreStart
204 function civicrm_api3_report_template_getfields($params) {
205 return civicrm_api3_create_success(array(
211 'option_group_id' => array(
212 'name' => 'option_group_id',
215 'FKClassName' => 'CRM_Core_DAO_OptionGroup',
220 'title' => 'Option Label',
228 'title' => 'Option Value',
236 'title' => 'Option Name',
240 'where' => 'civicrm_option_value.name',
244 'name' => 'grouping',
246 'title' => 'Option Grouping Name',
255 'is_default' => array(
256 'name' => 'is_default',
265 'description' => array(
266 'name' => 'description',
268 'title' => 'Description',
272 'is_optgroup' => array(
273 'name' => 'is_optgroup',
276 'is_reserved' => array(
277 'name' => 'is_reserved',
280 'is_active' => array(
281 'name' => 'is_active',
284 'component_id' => array(
285 'name' => 'component_id',
287 'FKClassName' => 'CRM_Core_DAO_Component',
289 'domain_id' => array(
290 'name' => 'domain_id',
292 'FKClassName' => 'CRM_Core_DAO_Domain',
294 'visibility_id' => array(
295 'name' => 'visibility_id',
301 @codingStandardsIgnoreEnd */