CRM-15988 - Cleanup internal use of entity names
[civicrm-core.git] / api / v3 / ReportTemplate.php
CommitLineData
6a488035 1<?php
c28e1768
CW
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 *
b081365f 31 * @package CiviCRM_APIv3
c28e1768 32 */
6a488035
TO
33
34/**
9d32e6f7 35 * Retrieve a report template.
6a488035
TO
36 *
37 * FIXME This is a bare-minimum placeholder
38 *
c490a46a 39 * @param array $params
6a488035 40 *
a6c01b45 41 * @return array
72b3a70c 42 * details of found Option Values
6a488035
TO
43 */
44function civicrm_api3_report_template_get($params) {
45 require_once 'api/v3/OptionValue.php';
46 $params['option_group_id'] = CRM_Core_DAO::getFieldValue(
47 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
48 );
49 return civicrm_api3_option_value_get($params);
50}
51
52/**
9d32e6f7 53 * Add an OptionValue.
6a488035 54 *
9d32e6f7 55 * OptionValues are used to classify CRM entities (including Contacts, Groups and Actions).
6a488035 56 *
c490a46a 57 * @param array $params
77b97be7 58 *
a6c01b45 59 * @return array
16b10e64 60 * Array of newly created option_value property values.
6a488035
TO
61 */
62function 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'
66 );
1cbea43e 67 if (!empty($params['component_id']) && !is_numeric($params['component_id'])) {
8f785c9e
AS
68 $components = CRM_Core_PseudoConstant::get('CRM_Core_DAO_OptionValue', 'component_id', array('onlyActive' => FALSE, 'labelColumn' => 'name'));
69 $params['component_id'] = array_search($params['component_id'], $components);
1cbea43e 70 }
6a488035
TO
71 return civicrm_api3_option_value_create($params);
72}
73
11e09c59 74/**
0aa0303c
EM
75 * Adjust Metadata for Create action.
76 *
77 * The metadata is used for setting defaults, documentation & validation.
6a488035 78 *
cf470720 79 * @param array $params
b081365f 80 * Array of parameters determined by getfields.
6a488035
TO
81 */
82function _civicrm_api3_report_template_create_spec(&$params) {
83 require_once 'api/v3/OptionValue.php';
84 _civicrm_api3_option_value_create_spec($params);
6a488035
TO
85 $params['value']['api.aliases'] = array('report_url');
86 $params['name']['api.aliases'] = array('class_name');
1cbea43e
C
87 $params['option_group_id']['api.default'] = CRM_Core_DAO::getFieldValue(
88 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
89 );
6a488035
TO
90 // $params['component']['api.required'] = TRUE;
91}
92
93/**
9d32e6f7 94 * Deletes an existing ReportTemplate.
6a488035 95 *
cf470720 96 * @param array $params
6a488035 97 *
a6c01b45 98 * @return array
72b3a70c 99 * Api result
6a488035
TO
100 */
101function civicrm_api3_report_template_delete($params) {
102 require_once 'api/v3/OptionValue.php';
103 return civicrm_api3_option_value_delete($params);
104}
105
c58f66e0 106/**
9d32e6f7 107 * Retrieve rows from a report template.
c58f66e0 108 *
cf470720
TO
109 * @param array $params
110 * Input parameters.
c58f66e0 111 *
a6c01b45 112 * @return array
72b3a70c 113 * details of found instances
c58f66e0
E
114 */
115function civicrm_api3_report_template_getrows($params) {
05274f12 116 civicrm_api3_verify_one_mandatory($params, NULL, array('report_id', 'instance_id'));
781d91c8
EM
117 list($rows, $instance, $metadata) = _civicrm_api3_report_template_getrows($params);
118 return civicrm_api3_create_success($rows, $params, 'report_template', 'getrows', CRM_Core_DAO::$_nullObject, $metadata);
c58f66e0
E
119}
120
aa1b1481 121/**
9d32e6f7
EM
122 * Get report template rows.
123 *
c490a46a 124 * @param array $params
aa1b1481
EM
125 *
126 * @return array
127 * @throws API_Exception
128 * @throws CiviCRM_API3_Exception
129 */
c58f66e0 130function _civicrm_api3_report_template_getrows($params) {
22e263ad 131 if (empty($params['report_id'])) {
05274f12
E
132 $params['report_id'] = civicrm_api3('report_instance', 'getvalue', array('id' => $params['instance_id'], 'return' => 'report_id'));
133 }
134
c58f66e0 135 $class = civicrm_api3('option_value', 'getvalue', array(
7b9f95c3 136 'option_group_name' => 'report_template',
6f900755
E
137 'return' => 'name',
138 'value' => $params['report_id'],
139 )
c58f66e0
E
140 );
141
142 $reportInstance = new $class();
22e263ad 143 if (!empty($params['instance_id'])) {
c58f66e0
E
144 $reportInstance->setID($params['instance_id']);
145 }
146 $reportInstance->setParams($params);
147 $reportInstance->noController = TRUE;
148 $reportInstance->preProcess();
149 $reportInstance->setDefaultValues(FALSE);
150 $reportInstance->setParams(array_merge($reportInstance->getDefaultValues(), $params));
35671d00 151 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'report_template', 'get');
6f900755
E
152 $reportInstance->setLimitValue($options['limit']);
153 $reportInstance->setOffsetValue($options['offset']);
c58f66e0
E
154 $reportInstance->beginPostProcessCommon();
155 $sql = $reportInstance->buildQuery();
781d91c8 156 $rows = $metadata = $requiredMetadata = array();
c58f66e0 157 $reportInstance->buildRows($sql, $rows);
781d91c8 158 $requiredMetadata = array();
22e263ad 159 if (isset($params['options']) && !empty($params['options']['metadata'])) {
781d91c8 160 $requiredMetadata = $params['options']['metadata'];
22e263ad 161 if (in_array('title', $requiredMetadata)) {
781d91c8
EM
162 $metadata['metadata']['title'] = $reportInstance->getTitle();
163 }
22e263ad 164 if (in_array('labels', $requiredMetadata)) {
781d91c8
EM
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'] : '';
169 }
170 }
7cab1323
EM
171 }
172 return array($rows, $reportInstance, $metadata);
c58f66e0
E
173}
174
aa1b1481 175/**
9d32e6f7
EM
176 * Get statistics from a given report.
177 *
c490a46a 178 * @param array $params
aa1b1481
EM
179 *
180 * @return array
181 */
c58f66e0 182function civicrm_api3_report_template_getstatistics($params) {
781d91c8 183 list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params);
c58f66e0 184 $stats = $reportInstance->statistics($rows);
781d91c8 185 return civicrm_api3_create_success($stats, $params, 'report_template', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata);
c58f66e0
E
186}
187/**
dc64d047 188 * Adjust metadata for template getrows action.
c58f66e0 189 *
cf470720
TO
190 * @param array $params
191 * Input parameters.
c58f66e0
E
192 */
193function _civicrm_api3_report_template_getrows_spec(&$params) {
194 $params['report_id'] = array(
c58f66e0
E
195 'title' => 'Report ID - eg. member/lapse',
196 );
197}
198
e70a7fc0 199/* @codingStandardsIgnoreStart
6a488035
TO
200function civicrm_api3_report_template_getfields($params) {
201 return civicrm_api3_create_success(array(
202 'id' => array(
203 'name' => 'id',
204 'type' => 1,
205 'required' => 1,
206 ),
207 'option_group_id' => array(
208 'name' => 'option_group_id',
209 'type' => 1,
210 'required' => 1,
211 'FKClassName' => 'CRM_Core_DAO_OptionGroup',
212 ),
213 'label' => array(
214 'name' => 'label',
215 'type' => 2,
216 'title' => 'Option Label',
217 'required' => 1,
218 'maxlength' => 255,
219 'size' => 45,
220 ),
221 'value' => array(
222 'name' => 'value',
223 'type' => 2,
224 'title' => 'Option Value',
225 'required' => 1,
226 'maxlength' => 512,
227 'size' => 45,
228 ),
229 'name' => array(
230 'name' => 'name',
231 'type' => 2,
232 'title' => 'Option Name',
233 'maxlength' => 255,
234 'size' => 45,
235 'import' => 1,
236 'where' => 'civicrm_option_value.name',
237 'export' => 1,
238 ),
239 'grouping' => array(
240 'name' => 'grouping',
241 'type' => 2,
242 'title' => 'Option Grouping Name',
243 'maxlength' => 255,
244 'size' => 45,
245 ),
246 'filter' => array(
247 'name' => 'filter',
248 'type' => 1,
249 'title' => 'Filter',
250 ),
251 'is_default' => array(
252 'name' => 'is_default',
253 'type' => 16,
254 ),
255 'weight' => array(
256 'name' => 'weight',
257 'type' => 1,
258 'title' => 'Weight',
259 'required' => 1,
260 ),
261 'description' => array(
262 'name' => 'description',
263 'type' => 32,
264 'title' => 'Description',
265 'rows' => 8,
266 'cols' => 60,
267 ),
268 'is_optgroup' => array(
269 'name' => 'is_optgroup',
270 'type' => 16,
271 ),
272 'is_reserved' => array(
273 'name' => 'is_reserved',
274 'type' => 16,
275 ),
276 'is_active' => array(
277 'name' => 'is_active',
278 'type' => 16,
279 ),
280 'component_id' => array(
281 'name' => 'component_id',
282 'type' => 1,
283 'FKClassName' => 'CRM_Core_DAO_Component',
284 ),
285 'domain_id' => array(
286 'name' => 'domain_id',
287 'type' => 1,
288 'FKClassName' => 'CRM_Core_DAO_Domain',
289 ),
290 'visibility_id' => array(
291 'name' => 'visibility_id',
292 'type' => 1,
293 'default' => 'UL',
294 ),
295 ));
e70a7fc0
TO
296}
297@codingStandardsIgnoreEnd */