Fixed Code clean up for batch 15
[civicrm-core.git] / api / v3 / ReportTemplate.php
CommitLineData
6a488035
TO
1<?php
2
3/**
4 * Retrieve a report template
5 *
6 * FIXME This is a bare-minimum placeholder
7 *
c490a46a 8 * @param array $params
6a488035
TO
9 *
10 * {@example OptionValueGet.php 0}
11 * @example OptionValueGet.php
12 *
a6c01b45 13 * @return array
72b3a70c 14 * details of found Option Values
408b79bf 15 * {@getfields OptionValue_get}
6a488035
TO
16 */
17function civicrm_api3_report_template_get($params) {
18 require_once 'api/v3/OptionValue.php';
19 $params['option_group_id'] = CRM_Core_DAO::getFieldValue(
20 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
21 );
22 return civicrm_api3_option_value_get($params);
23}
24
25/**
26 * Add a OptionValue. OptionValues are used to classify CRM entities (including Contacts, Groups and Actions).
27 *
28 * Allowed @params array keys are:
29 *
30 * {@example OptionValueCreate.php}
31 *
c490a46a 32 * @param array $params
77b97be7 33 *
a6c01b45 34 * @return array
16b10e64 35 * Array of newly created option_value property values.
408b79bf 36 * {@getfields OptionValue_create}
6a488035
TO
37 */
38function civicrm_api3_report_template_create($params) {
39 require_once 'api/v3/OptionValue.php';
40 $params['option_group_id'] = CRM_Core_DAO::getFieldValue(
41 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
42 );
1cbea43e 43 if (!empty($params['component_id']) && !is_numeric($params['component_id'])) {
8f785c9e
AS
44 $components = CRM_Core_PseudoConstant::get('CRM_Core_DAO_OptionValue', 'component_id', array('onlyActive' => FALSE, 'labelColumn' => 'name'));
45 $params['component_id'] = array_search($params['component_id'], $components);
1cbea43e 46 }
6a488035
TO
47 return civicrm_api3_option_value_create($params);
48}
49
11e09c59 50/**
6a488035
TO
51 * Adjust Metadata for Create action
52 *
53 * The metadata is used for setting defaults, documentation & validation
cf470720
TO
54 * @param array $params
55 * Array or parameters determined by getfields.
6a488035
TO
56 */
57function _civicrm_api3_report_template_create_spec(&$params) {
58 require_once 'api/v3/OptionValue.php';
59 _civicrm_api3_option_value_create_spec($params);
6a488035
TO
60 $params['value']['api.aliases'] = array('report_url');
61 $params['name']['api.aliases'] = array('class_name');
1cbea43e
C
62 $params['option_group_id']['api.default'] = CRM_Core_DAO::getFieldValue(
63 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
64 );
6a488035
TO
65 // $params['component']['api.required'] = TRUE;
66}
67
68/**
69 * Deletes an existing ReportTemplate
70 *
cf470720 71 * @param array $params
6a488035
TO
72 *
73 * {@example ReportTemplateDelete.php 0}
74 *
a6c01b45 75 * @return array
72b3a70c 76 * Api result
408b79bf 77 * {@getfields ReportTemplate_create}
6a488035
TO
78 */
79function civicrm_api3_report_template_delete($params) {
80 require_once 'api/v3/OptionValue.php';
81 return civicrm_api3_option_value_delete($params);
82}
83
c58f66e0
E
84/**
85 * Retrieve rows from a report template
86 *
cf470720
TO
87 * @param array $params
88 * Input parameters.
c58f66e0 89 *
a6c01b45 90 * @return array
72b3a70c 91 * details of found instances
c58f66e0
E
92 */
93function civicrm_api3_report_template_getrows($params) {
05274f12 94 civicrm_api3_verify_one_mandatory($params, NULL, array('report_id', 'instance_id'));
781d91c8
EM
95 list($rows, $instance, $metadata) = _civicrm_api3_report_template_getrows($params);
96 return civicrm_api3_create_success($rows, $params, 'report_template', 'getrows', CRM_Core_DAO::$_nullObject, $metadata);
c58f66e0
E
97}
98
aa1b1481 99/**
c490a46a 100 * @param array $params
aa1b1481
EM
101 *
102 * @return array
103 * @throws API_Exception
104 * @throws CiviCRM_API3_Exception
105 */
c58f66e0 106function _civicrm_api3_report_template_getrows($params) {
22e263ad 107 if (empty($params['report_id'])) {
05274f12
E
108 $params['report_id'] = civicrm_api3('report_instance', 'getvalue', array('id' => $params['instance_id'], 'return' => 'report_id'));
109 }
110
c58f66e0 111 $class = civicrm_api3('option_value', 'getvalue', array(
7b9f95c3 112 'option_group_name' => 'report_template',
6f900755
E
113 'return' => 'name',
114 'value' => $params['report_id'],
115 )
c58f66e0
E
116 );
117
118 $reportInstance = new $class();
22e263ad 119 if (!empty($params['instance_id'])) {
c58f66e0
E
120 $reportInstance->setID($params['instance_id']);
121 }
122 $reportInstance->setParams($params);
123 $reportInstance->noController = TRUE;
124 $reportInstance->preProcess();
125 $reportInstance->setDefaultValues(FALSE);
126 $reportInstance->setParams(array_merge($reportInstance->getDefaultValues(), $params));
35671d00 127 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'report_template', 'get');
6f900755
E
128 $reportInstance->setLimitValue($options['limit']);
129 $reportInstance->setOffsetValue($options['offset']);
c58f66e0
E
130 $reportInstance->beginPostProcessCommon();
131 $sql = $reportInstance->buildQuery();
781d91c8 132 $rows = $metadata = $requiredMetadata = array();
c58f66e0 133 $reportInstance->buildRows($sql, $rows);
781d91c8 134 $requiredMetadata = array();
22e263ad 135 if (isset($params['options']) && !empty($params['options']['metadata'])) {
781d91c8 136 $requiredMetadata = $params['options']['metadata'];
22e263ad 137 if (in_array('title', $requiredMetadata)) {
781d91c8
EM
138 $metadata['metadata']['title'] = $reportInstance->getTitle();
139 }
22e263ad 140 if (in_array('labels', $requiredMetadata)) {
781d91c8
EM
141 foreach ($reportInstance->_columnHeaders as $key => $header) {
142 //would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty
143 //NB I think these are already translated
144 $metadata['metadata']['labels'][$key] = !empty($header['title']) ? $header['title'] : '';
145 }
146 }
7cab1323
EM
147 }
148 return array($rows, $reportInstance, $metadata);
c58f66e0
E
149}
150
aa1b1481 151/**
c490a46a 152 * @param array $params
aa1b1481
EM
153 *
154 * @return array
155 */
c58f66e0 156function civicrm_api3_report_template_getstatistics($params) {
781d91c8 157 list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params);
c58f66e0 158 $stats = $reportInstance->statistics($rows);
781d91c8 159 return civicrm_api3_create_success($stats, $params, 'report_template', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata);
c58f66e0
E
160}
161/**
162 * Retrieve rows from a report template
163 *
cf470720
TO
164 * @param array $params
165 * Input parameters.
c58f66e0 166 *
408b79bf 167 * @return;
72b3a70c 168 * details of found instances
c58f66e0
E
169 */
170function _civicrm_api3_report_template_getrows_spec(&$params) {
171 $params['report_id'] = array(
c58f66e0
E
172 'title' => 'Report ID - eg. member/lapse',
173 );
174}
175
6a488035
TO
176/*
177function civicrm_api3_report_template_getfields($params) {
178 return civicrm_api3_create_success(array(
179 'id' => array(
180 'name' => 'id',
181 'type' => 1,
182 'required' => 1,
183 ),
184 'option_group_id' => array(
185 'name' => 'option_group_id',
186 'type' => 1,
187 'required' => 1,
188 'FKClassName' => 'CRM_Core_DAO_OptionGroup',
189 ),
190 'label' => array(
191 'name' => 'label',
192 'type' => 2,
193 'title' => 'Option Label',
194 'required' => 1,
195 'maxlength' => 255,
196 'size' => 45,
197 ),
198 'value' => array(
199 'name' => 'value',
200 'type' => 2,
201 'title' => 'Option Value',
202 'required' => 1,
203 'maxlength' => 512,
204 'size' => 45,
205 ),
206 'name' => array(
207 'name' => 'name',
208 'type' => 2,
209 'title' => 'Option Name',
210 'maxlength' => 255,
211 'size' => 45,
212 'import' => 1,
213 'where' => 'civicrm_option_value.name',
214 'export' => 1,
215 ),
216 'grouping' => array(
217 'name' => 'grouping',
218 'type' => 2,
219 'title' => 'Option Grouping Name',
220 'maxlength' => 255,
221 'size' => 45,
222 ),
223 'filter' => array(
224 'name' => 'filter',
225 'type' => 1,
226 'title' => 'Filter',
227 ),
228 'is_default' => array(
229 'name' => 'is_default',
230 'type' => 16,
231 ),
232 'weight' => array(
233 'name' => 'weight',
234 'type' => 1,
235 'title' => 'Weight',
236 'required' => 1,
237 ),
238 'description' => array(
239 'name' => 'description',
240 'type' => 32,
241 'title' => 'Description',
242 'rows' => 8,
243 'cols' => 60,
244 ),
245 'is_optgroup' => array(
246 'name' => 'is_optgroup',
247 'type' => 16,
248 ),
249 'is_reserved' => array(
250 'name' => 'is_reserved',
251 'type' => 16,
252 ),
253 'is_active' => array(
254 'name' => 'is_active',
255 'type' => 16,
256 ),
257 'component_id' => array(
258 'name' => 'component_id',
259 'type' => 1,
260 'FKClassName' => 'CRM_Core_DAO_Component',
261 ),
262 'domain_id' => array(
263 'name' => 'domain_id',
264 'type' => 1,
265 'FKClassName' => 'CRM_Core_DAO_Domain',
266 ),
267 'visibility_id' => array(
268 'name' => 'visibility_id',
269 'type' => 1,
270 'default' => 'UL',
271 ),
272 ));
273}*/