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