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