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