remove some @access from phpdoc
[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 * @param array $params
55 * Array or parameters determined by getfields.
56 */
57 function _civicrm_api3_report_template_create_spec(&$params) {
58 require_once 'api/v3/OptionValue.php';
59 _civicrm_api3_option_value_create_spec($params);
60 $params['value']['api.aliases'] = array('report_url');
61 $params['name']['api.aliases'] = array('class_name');
62 $params['option_group_id']['api.default'] = CRM_Core_DAO::getFieldValue(
63 'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
64 );
65 // $params['component']['api.required'] = TRUE;
66 }
67
68 /**
69 * Deletes an existing ReportTemplate
70 *
71 * @param array $params
72 *
73 * {@example ReportTemplateDelete.php 0}
74 *
75 * @return array
76 * Api result
77 * {@getfields ReportTemplate_create}
78 */
79 function civicrm_api3_report_template_delete($params) {
80 require_once 'api/v3/OptionValue.php';
81 return civicrm_api3_option_value_delete($params);
82 }
83
84 /**
85 * Retrieve rows from a report template
86 *
87 * @param array $params
88 * Input parameters.
89 *
90 * @return array
91 * details of found instances
92 */
93 function civicrm_api3_report_template_getrows($params) {
94 civicrm_api3_verify_one_mandatory($params, NULL, array('report_id', 'instance_id'));
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);
97 }
98
99 /**
100 * @param array $params
101 *
102 * @return array
103 * @throws API_Exception
104 * @throws CiviCRM_API3_Exception
105 */
106 function _civicrm_api3_report_template_getrows($params) {
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
111 $class = civicrm_api3('option_value', 'getvalue', array(
112 'option_group_name' => 'report_template',
113 'return' => 'name',
114 'value' => $params['report_id'],
115 )
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));
127 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'report_template', 'get');
128 $reportInstance->setLimitValue($options['limit']);
129 $reportInstance->setOffsetValue($options['offset']);
130 $reportInstance->beginPostProcessCommon();
131 $sql = $reportInstance->buildQuery();
132 $rows = $metadata = $requiredMetadata = array();
133 $reportInstance->buildRows($sql, $rows);
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 }
147 }
148 return array($rows, $reportInstance, $metadata);
149 }
150
151 /**
152 * @param array $params
153 *
154 * @return array
155 */
156 function civicrm_api3_report_template_getstatistics($params) {
157 list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params);
158 $stats = $reportInstance->statistics($rows);
159 return civicrm_api3_create_success($stats, $params, 'report_template', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata);
160 }
161 /**
162 * Retrieve rows from a report template
163 *
164 * @param array $params
165 * Input parameters.
166 *
167 * @return array
168 * details of found instances
169 */
170 function _civicrm_api3_report_template_getrows_spec(&$params) {
171 $params['report_id'] = array(
172 'title' => 'Report ID - eg. member/lapse',
173 );
174 }
175
176 /*
177 function 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 }*/