Merge pull request #3679 from yashodha/CRM-14951
[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 *
77b97be7
EM
8 * @param $params
9 *
10 * @internal param $array $ params input parameters
6a488035
TO
11 *
12 * {@example OptionValueGet.php 0}
13 * @example OptionValueGet.php
14 *
15 * @return array details of found Option Values
16 * {@getfields OptionValue_get}
17 * @access public
18 */
19function civicrm_api3_report_template_get($params) {
20 require_once 'api/v3/OptionValue.php';
21 $params['option_group_id'] = CRM_Core_DAO::getFieldValue(
22 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
23 );
24 return civicrm_api3_option_value_get($params);
25}
26
27/**
28 * Add a OptionValue. OptionValues are used to classify CRM entities (including Contacts, Groups and Actions).
29 *
30 * Allowed @params array keys are:
31 *
32 * {@example OptionValueCreate.php}
33 *
77b97be7
EM
34 * @param $params
35 *
6a488035
TO
36 * @return array of newly created option_value property values.
37 * {@getfields OptionValue_create}
38 * @access public
39 */
40function civicrm_api3_report_template_create($params) {
41 require_once 'api/v3/OptionValue.php';
42 $params['option_group_id'] = CRM_Core_DAO::getFieldValue(
43 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
44 );
1cbea43e 45 if (!empty($params['component_id']) && !is_numeric($params['component_id'])) {
8f785c9e
AS
46 $components = CRM_Core_PseudoConstant::get('CRM_Core_DAO_OptionValue', 'component_id', array('onlyActive' => FALSE, 'labelColumn' => 'name'));
47 $params['component_id'] = array_search($params['component_id'], $components);
1cbea43e 48 }
6a488035
TO
49 return civicrm_api3_option_value_create($params);
50}
51
11e09c59 52/**
6a488035
TO
53 * Adjust Metadata for Create action
54 *
55 * The metadata is used for setting defaults, documentation & validation
56 * @param array $params array or parameters determined by getfields
57 */
58function _civicrm_api3_report_template_create_spec(&$params) {
59 require_once 'api/v3/OptionValue.php';
60 _civicrm_api3_option_value_create_spec($params);
6a488035
TO
61 $params['value']['api.aliases'] = array('report_url');
62 $params['name']['api.aliases'] = array('class_name');
1cbea43e
C
63 $params['option_group_id']['api.default'] = CRM_Core_DAO::getFieldValue(
64 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
65 );
6a488035
TO
66 // $params['component']['api.required'] = TRUE;
67}
68
69/**
70 * Deletes an existing ReportTemplate
71 *
72 * @param array $params
73 *
74 * {@example ReportTemplateDelete.php 0}
75 *
76 * @return array Api result
77 * {@getfields ReportTemplate_create}
78 * @access public
79 */
80function civicrm_api3_report_template_delete($params) {
81 require_once 'api/v3/OptionValue.php';
82 return civicrm_api3_option_value_delete($params);
83}
84
c58f66e0
E
85/**
86 * Retrieve rows from a report template
87 *
88 * @param array $params input parameters
89 *
90 * @return array details of found instances
91 * @access public
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
EM
99/**
100 * @param $params
101 *
102 * @return array
103 * @throws API_Exception
104 * @throws CiviCRM_API3_Exception
105 */
c58f66e0 106function _civicrm_api3_report_template_getrows($params) {
05274f12
E
107 if(empty($params['report_id'])) {
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(
6f900755
E
112 'option_group_id' => 'report_template',
113 'return' => 'name',
114 'value' => $params['report_id'],
115 )
c58f66e0
E
116 );
117
118 $reportInstance = new $class();
119 if(!empty($params['instance_id'])) {
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));
6f900755
E
127 $options = _civicrm_api3_get_options_from_params($params, TRUE,'report_template','get');
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
EM
134 $requiredMetadata = array();
135 if(isset($params['options']) && !empty($params['options']['metadata'])) {
136 $requiredMetadata = $params['options']['metadata'];
137 if(in_array('title', $requiredMetadata)) {
138 $metadata['metadata']['title'] = $reportInstance->getTitle();
139 }
140 if(in_array('labels', $requiredMetadata)) {
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
EM
151/**
152 * @param $params
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 *
164 * @param array $params input parameters
165 *
166 * @return array details of found instances
167 * @access public
168 */
169function _civicrm_api3_report_template_getrows_spec(&$params) {
170 $params['report_id'] = array(
c58f66e0
E
171 'title' => 'Report ID - eg. member/lapse',
172 );
173}
174
6a488035
TO
175/*
176function civicrm_api3_report_template_getfields($params) {
177 return civicrm_api3_create_success(array(
178 'id' => array(
179 'name' => 'id',
180 'type' => 1,
181 'required' => 1,
182 ),
183 'option_group_id' => array(
184 'name' => 'option_group_id',
185 'type' => 1,
186 'required' => 1,
187 'FKClassName' => 'CRM_Core_DAO_OptionGroup',
188 ),
189 'label' => array(
190 'name' => 'label',
191 'type' => 2,
192 'title' => 'Option Label',
193 'required' => 1,
194 'maxlength' => 255,
195 'size' => 45,
196 ),
197 'value' => array(
198 'name' => 'value',
199 'type' => 2,
200 'title' => 'Option Value',
201 'required' => 1,
202 'maxlength' => 512,
203 'size' => 45,
204 ),
205 'name' => array(
206 'name' => 'name',
207 'type' => 2,
208 'title' => 'Option Name',
209 'maxlength' => 255,
210 'size' => 45,
211 'import' => 1,
212 'where' => 'civicrm_option_value.name',
213 'export' => 1,
214 ),
215 'grouping' => array(
216 'name' => 'grouping',
217 'type' => 2,
218 'title' => 'Option Grouping Name',
219 'maxlength' => 255,
220 'size' => 45,
221 ),
222 'filter' => array(
223 'name' => 'filter',
224 'type' => 1,
225 'title' => 'Filter',
226 ),
227 'is_default' => array(
228 'name' => 'is_default',
229 'type' => 16,
230 ),
231 'weight' => array(
232 'name' => 'weight',
233 'type' => 1,
234 'title' => 'Weight',
235 'required' => 1,
236 ),
237 'description' => array(
238 'name' => 'description',
239 'type' => 32,
240 'title' => 'Description',
241 'rows' => 8,
242 'cols' => 60,
243 ),
244 'is_optgroup' => array(
245 'name' => 'is_optgroup',
246 'type' => 16,
247 ),
248 'is_reserved' => array(
249 'name' => 'is_reserved',
250 'type' => 16,
251 ),
252 'is_active' => array(
253 'name' => 'is_active',
254 'type' => 16,
255 ),
256 'component_id' => array(
257 'name' => 'component_id',
258 'type' => 1,
259 'FKClassName' => 'CRM_Core_DAO_Component',
260 ),
261 'domain_id' => array(
262 'name' => 'domain_id',
263 'type' => 1,
264 'FKClassName' => 'CRM_Core_DAO_Domain',
265 ),
266 'visibility_id' => array(
267 'name' => 'visibility_id',
268 'type' => 1,
269 'default' => 'UL',
270 ),
271 ));
272}*/