Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-09-14-48-51
[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 *
13 * @return array details of found Option Values
14 * {@getfields OptionValue_get}
15 * @access public
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 *
6a488035
TO
34 * @return array of newly created option_value property values.
35 * {@getfields OptionValue_create}
36 * @access public
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
54 * @param array $params array or parameters determined by getfields
55 */
56function _civicrm_api3_report_template_create_spec(&$params) {
57 require_once 'api/v3/OptionValue.php';
58 _civicrm_api3_option_value_create_spec($params);
6a488035
TO
59 $params['value']['api.aliases'] = array('report_url');
60 $params['name']['api.aliases'] = array('class_name');
1cbea43e
C
61 $params['option_group_id']['api.default'] = CRM_Core_DAO::getFieldValue(
62 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
63 );
6a488035
TO
64 // $params['component']['api.required'] = TRUE;
65}
66
67/**
68 * Deletes an existing ReportTemplate
69 *
70 * @param array $params
71 *
72 * {@example ReportTemplateDelete.php 0}
73 *
74 * @return array Api result
75 * {@getfields ReportTemplate_create}
76 * @access public
77 */
78function civicrm_api3_report_template_delete($params) {
79 require_once 'api/v3/OptionValue.php';
80 return civicrm_api3_option_value_delete($params);
81}
82
c58f66e0
E
83/**
84 * Retrieve rows from a report template
85 *
86 * @param array $params input parameters
87 *
88 * @return array details of found instances
89 * @access public
90 */
91function civicrm_api3_report_template_getrows($params) {
05274f12 92 civicrm_api3_verify_one_mandatory($params, NULL, array('report_id', 'instance_id'));
781d91c8
EM
93 list($rows, $instance, $metadata) = _civicrm_api3_report_template_getrows($params);
94 return civicrm_api3_create_success($rows, $params, 'report_template', 'getrows', CRM_Core_DAO::$_nullObject, $metadata);
c58f66e0
E
95}
96
aa1b1481 97/**
c490a46a 98 * @param array $params
aa1b1481
EM
99 *
100 * @return array
101 * @throws API_Exception
102 * @throws CiviCRM_API3_Exception
103 */
c58f66e0 104function _civicrm_api3_report_template_getrows($params) {
05274f12
E
105 if(empty($params['report_id'])) {
106 $params['report_id'] = civicrm_api3('report_instance', 'getvalue', array('id' => $params['instance_id'], 'return' => 'report_id'));
107 }
108
c58f66e0 109 $class = civicrm_api3('option_value', 'getvalue', array(
6f900755
E
110 'option_group_id' => 'report_template',
111 'return' => 'name',
112 'value' => $params['report_id'],
113 )
c58f66e0
E
114 );
115
116 $reportInstance = new $class();
117 if(!empty($params['instance_id'])) {
118 $reportInstance->setID($params['instance_id']);
119 }
120 $reportInstance->setParams($params);
121 $reportInstance->noController = TRUE;
122 $reportInstance->preProcess();
123 $reportInstance->setDefaultValues(FALSE);
124 $reportInstance->setParams(array_merge($reportInstance->getDefaultValues(), $params));
6f900755
E
125 $options = _civicrm_api3_get_options_from_params($params, TRUE,'report_template','get');
126 $reportInstance->setLimitValue($options['limit']);
127 $reportInstance->setOffsetValue($options['offset']);
c58f66e0
E
128 $reportInstance->beginPostProcessCommon();
129 $sql = $reportInstance->buildQuery();
781d91c8 130 $rows = $metadata = $requiredMetadata = array();
c58f66e0 131 $reportInstance->buildRows($sql, $rows);
781d91c8
EM
132 $requiredMetadata = array();
133 if(isset($params['options']) && !empty($params['options']['metadata'])) {
134 $requiredMetadata = $params['options']['metadata'];
135 if(in_array('title', $requiredMetadata)) {
136 $metadata['metadata']['title'] = $reportInstance->getTitle();
137 }
138 if(in_array('labels', $requiredMetadata)) {
139 foreach ($reportInstance->_columnHeaders as $key => $header) {
140 //would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty
141 //NB I think these are already translated
142 $metadata['metadata']['labels'][$key] = !empty($header['title']) ? $header['title'] : '';
143 }
144 }
7cab1323
EM
145 }
146 return array($rows, $reportInstance, $metadata);
c58f66e0
E
147}
148
aa1b1481 149/**
c490a46a 150 * @param array $params
aa1b1481
EM
151 *
152 * @return array
153 */
c58f66e0 154function civicrm_api3_report_template_getstatistics($params) {
781d91c8 155 list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params);
c58f66e0 156 $stats = $reportInstance->statistics($rows);
781d91c8 157 return civicrm_api3_create_success($stats, $params, 'report_template', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata);
c58f66e0
E
158}
159/**
160 * Retrieve rows from a report template
161 *
162 * @param array $params input parameters
163 *
164 * @return array details of found instances
165 * @access public
166 */
167function _civicrm_api3_report_template_getrows_spec(&$params) {
168 $params['report_id'] = array(
c58f66e0
E
169 'title' => 'Report ID - eg. member/lapse',
170 );
171}
172
6a488035
TO
173/*
174function civicrm_api3_report_template_getfields($params) {
175 return civicrm_api3_create_success(array(
176 'id' => array(
177 'name' => 'id',
178 'type' => 1,
179 'required' => 1,
180 ),
181 'option_group_id' => array(
182 'name' => 'option_group_id',
183 'type' => 1,
184 'required' => 1,
185 'FKClassName' => 'CRM_Core_DAO_OptionGroup',
186 ),
187 'label' => array(
188 'name' => 'label',
189 'type' => 2,
190 'title' => 'Option Label',
191 'required' => 1,
192 'maxlength' => 255,
193 'size' => 45,
194 ),
195 'value' => array(
196 'name' => 'value',
197 'type' => 2,
198 'title' => 'Option Value',
199 'required' => 1,
200 'maxlength' => 512,
201 'size' => 45,
202 ),
203 'name' => array(
204 'name' => 'name',
205 'type' => 2,
206 'title' => 'Option Name',
207 'maxlength' => 255,
208 'size' => 45,
209 'import' => 1,
210 'where' => 'civicrm_option_value.name',
211 'export' => 1,
212 ),
213 'grouping' => array(
214 'name' => 'grouping',
215 'type' => 2,
216 'title' => 'Option Grouping Name',
217 'maxlength' => 255,
218 'size' => 45,
219 ),
220 'filter' => array(
221 'name' => 'filter',
222 'type' => 1,
223 'title' => 'Filter',
224 ),
225 'is_default' => array(
226 'name' => 'is_default',
227 'type' => 16,
228 ),
229 'weight' => array(
230 'name' => 'weight',
231 'type' => 1,
232 'title' => 'Weight',
233 'required' => 1,
234 ),
235 'description' => array(
236 'name' => 'description',
237 'type' => 32,
238 'title' => 'Description',
239 'rows' => 8,
240 'cols' => 60,
241 ),
242 'is_optgroup' => array(
243 'name' => 'is_optgroup',
244 'type' => 16,
245 ),
246 'is_reserved' => array(
247 'name' => 'is_reserved',
248 'type' => 16,
249 ),
250 'is_active' => array(
251 'name' => 'is_active',
252 'type' => 16,
253 ),
254 'component_id' => array(
255 'name' => 'component_id',
256 'type' => 1,
257 'FKClassName' => 'CRM_Core_DAO_Component',
258 ),
259 'domain_id' => array(
260 'name' => 'domain_id',
261 'type' => 1,
262 'FKClassName' => 'CRM_Core_DAO_Domain',
263 ),
264 'visibility_id' => array(
265 'name' => 'visibility_id',
266 'type' => 1,
267 'default' => 'UL',
268 ),
269 ));
270}*/