2976162973770cadaabf9c7943b22f647e2b23a9
[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 $params
9 *
10 * @internal param $array $ params input parameters
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 */
19 function 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 *
34 * @param $params
35 *
36 * @return array 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 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 Api result
77 * {@getfields ReportTemplate_create}
78 * @access public
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 input parameters
89 *
90 * @return array details of found instances
91 * @access public
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 function _civicrm_api3_report_template_getrows($params) {
100 if(empty($params['report_id'])) {
101 $params['report_id'] = civicrm_api3('report_instance', 'getvalue', array('id' => $params['instance_id'], 'return' => 'report_id'));
102 }
103
104 $class = civicrm_api3('option_value', 'getvalue', array(
105 'option_group_id' => 'report_template',
106 'return' => 'name',
107 'value' => $params['report_id'],
108 )
109 );
110
111 $reportInstance = new $class();
112 if(!empty($params['instance_id'])) {
113 $reportInstance->setID($params['instance_id']);
114 }
115 $reportInstance->setParams($params);
116 $reportInstance->noController = TRUE;
117 $reportInstance->preProcess();
118 $reportInstance->setDefaultValues(FALSE);
119 $reportInstance->setParams(array_merge($reportInstance->getDefaultValues(), $params));
120 $options = _civicrm_api3_get_options_from_params($params, TRUE,'report_template','get');
121 $reportInstance->setLimitValue($options['limit']);
122 $reportInstance->setOffsetValue($options['offset']);
123 $reportInstance->beginPostProcessCommon();
124 $sql = $reportInstance->buildQuery();
125 $rows = $metadata = $requiredMetadata = array();
126 $reportInstance->buildRows($sql, $rows);
127 $requiredMetadata = array();
128 if(isset($params['options']) && !empty($params['options']['metadata'])) {
129 $requiredMetadata = $params['options']['metadata'];
130 if(in_array('title', $requiredMetadata)) {
131 $metadata['metadata']['title'] = $reportInstance->getTitle();
132 }
133 if(in_array('labels', $requiredMetadata)) {
134 foreach ($reportInstance->_columnHeaders as $key => $header) {
135 //would be better just to expect reports to provide titles but reports are not consistent so we anticipate empty
136 //NB I think these are already translated
137 $metadata['metadata']['labels'][$key] = !empty($header['title']) ? $header['title'] : '';
138 }
139 }
140 }
141 return array($rows, $reportInstance, $metadata);
142 }
143
144 function civicrm_api3_report_template_getstatistics($params) {
145 list($rows, $reportInstance, $metadata) = _civicrm_api3_report_template_getrows($params);
146 $stats = $reportInstance->statistics($rows);
147 return civicrm_api3_create_success($stats, $params, 'report_template', 'getstatistics', CRM_Core_DAO::$_nullObject, $metadata);
148 }
149 /**
150 * Retrieve rows from a report template
151 *
152 * @param array $params input parameters
153 *
154 * @return array details of found instances
155 * @access public
156 */
157 function _civicrm_api3_report_template_getrows_spec(&$params) {
158 $params['report_id'] = array(
159 'title' => 'Report ID - eg. member/lapse',
160 );
161 }
162
163 /*
164 function civicrm_api3_report_template_getfields($params) {
165 return civicrm_api3_create_success(array(
166 'id' => array(
167 'name' => 'id',
168 'type' => 1,
169 'required' => 1,
170 ),
171 'option_group_id' => array(
172 'name' => 'option_group_id',
173 'type' => 1,
174 'required' => 1,
175 'FKClassName' => 'CRM_Core_DAO_OptionGroup',
176 ),
177 'label' => array(
178 'name' => 'label',
179 'type' => 2,
180 'title' => 'Option Label',
181 'required' => 1,
182 'maxlength' => 255,
183 'size' => 45,
184 ),
185 'value' => array(
186 'name' => 'value',
187 'type' => 2,
188 'title' => 'Option Value',
189 'required' => 1,
190 'maxlength' => 512,
191 'size' => 45,
192 ),
193 'name' => array(
194 'name' => 'name',
195 'type' => 2,
196 'title' => 'Option Name',
197 'maxlength' => 255,
198 'size' => 45,
199 'import' => 1,
200 'where' => 'civicrm_option_value.name',
201 'export' => 1,
202 ),
203 'grouping' => array(
204 'name' => 'grouping',
205 'type' => 2,
206 'title' => 'Option Grouping Name',
207 'maxlength' => 255,
208 'size' => 45,
209 ),
210 'filter' => array(
211 'name' => 'filter',
212 'type' => 1,
213 'title' => 'Filter',
214 ),
215 'is_default' => array(
216 'name' => 'is_default',
217 'type' => 16,
218 ),
219 'weight' => array(
220 'name' => 'weight',
221 'type' => 1,
222 'title' => 'Weight',
223 'required' => 1,
224 ),
225 'description' => array(
226 'name' => 'description',
227 'type' => 32,
228 'title' => 'Description',
229 'rows' => 8,
230 'cols' => 60,
231 ),
232 'is_optgroup' => array(
233 'name' => 'is_optgroup',
234 'type' => 16,
235 ),
236 'is_reserved' => array(
237 'name' => 'is_reserved',
238 'type' => 16,
239 ),
240 'is_active' => array(
241 'name' => 'is_active',
242 'type' => 16,
243 ),
244 'component_id' => array(
245 'name' => 'component_id',
246 'type' => 1,
247 'FKClassName' => 'CRM_Core_DAO_Component',
248 ),
249 'domain_id' => array(
250 'name' => 'domain_id',
251 'type' => 1,
252 'FKClassName' => 'CRM_Core_DAO_Domain',
253 ),
254 'visibility_id' => array(
255 'name' => 'visibility_id',
256 'type' => 1,
257 'default' => 'UL',
258 ),
259 ));
260 }*/