Cleanup api3 docblocks
[civicrm-core.git] / api / v3 / Event.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * This api exposes CiviCRM event.
31 *
32 * @package CiviCRM_APIv3
33 * @subpackage API_Event
34 *
35 */
36
37 /**
38 * Create a Event.
39 *
40 * @param array $params
41 * Input parameters.
42 *
43 * @return array
44 * API result Array.
45 */
46 function civicrm_api3_event_create($params) {
47 civicrm_api3_verify_one_mandatory($params, NULL, array('event_type_id', 'template_id'));
48
49 // Clone event from template
50 if (!empty($params['template_id']) && empty($params['id'])) {
51 $copy = CRM_Event_BAO_Event::copy($params['template_id']);
52 $params['id'] = $copy->id;
53 unset($params['template_id']);
54 }
55
56 _civicrm_api3_event_create_legacy_support_42($params);
57 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Event');
58 }
59
60 /**
61 * Adjust Metadata for Create action.
62 *
63 * The metadata is used for setting defaults, documentation & validation.
64 *
65 * @param array $params
66 * Array or parameters determined by getfields.
67 */
68 function _civicrm_api3_event_create_spec(&$params) {
69 $params['start_date']['api.required'] = 1;
70 $params['title']['api.required'] = 1;
71 $params['is_active']['api.default'] = 1;
72 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
73 $params['is_template']['api.default'] = 0;
74 }
75
76 /**
77 * Support for schema changes made in 4.2.
78 *
79 * The main purpose of the API is to provide integrators a level of stability not provided by
80 * the core code or schema - this means we have to provide support for api calls (where possible)
81 * across schema changes.
82 *
83 * @param array $params
84 */
85 function _civicrm_api3_event_create_legacy_support_42(&$params) {
86 if (!empty($params['payment_processor_id'])) {
87 $params['payment_processor'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['payment_processor_id'] . CRM_Core_DAO::VALUE_SEPARATOR;
88 }
89 }
90
91 /**
92 * Get Event record.
93 *
94 * @param array $params
95 *
96 * @return array
97 * Array of all found event property values.
98 */
99 function civicrm_api3_event_get($params) {
100
101 //legacy support for $params['return.sort']
102 if (!empty($params['return.sort'])) {
103 $params['options']['sort'] = $params['return.sort'];
104 unset($params['return.sort']);
105 }
106
107 //legacy support for $params['return.offset']
108 if (!empty($params['return.offset'])) {
109 $params['options']['offset'] = $params['return.offset'];
110 unset($params['return.offset']);
111 }
112
113 //legacy support for $params['return.max_results']
114 if (!empty($params['return.max_results'])) {
115 $params['options']['limit'] = $params['return.max_results'];
116 unset($params['return.max_results']);
117 }
118
119 $eventDAO = new CRM_Event_BAO_Event();
120 _civicrm_api3_dao_set_filter($eventDAO, $params, TRUE, 'Event');
121
122 if (!empty($params['isCurrent'])) {
123 $eventDAO->whereAdd('(start_date >= CURDATE() || end_date >= CURDATE())');
124 }
125
126 // @todo should replace all this with _civicrm_api3_dao_to_array($bao, $params, FALSE, $entity) - but we still have
127 // the return.is_full to deal with.
128 // NB the std dao_to_array function should only return custom if required.
129 $event = array();
130 $options = _civicrm_api3_get_options_from_params($params);
131
132 $eventDAO->find();
133 while ($eventDAO->fetch()) {
134 $event[$eventDAO->id] = array();
135 CRM_Core_DAO::storeValues($eventDAO, $event[$eventDAO->id]);
136 if (!empty($params['return.is_full'])) {
137 _civicrm_api3_event_getisfull($event, $eventDAO->id);
138 }
139 _civicrm_api3_event_get_legacy_support_42($event, $eventDAO->id);
140 _civicrm_api3_custom_data_get($event[$eventDAO->id], 'Event', $eventDAO->id, NULL, $eventDAO->event_type_id);
141 if (!empty($options['return'])) {
142 $event[$eventDAO->id]['price_set_id'] = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventDAO->id);
143 }
144 }
145
146 return civicrm_api3_create_success($event, $params, 'event', 'get', $eventDAO);
147 }
148
149 /**
150 * Adjust Metadata for Get action.
151 *
152 * The metadata is used for setting defaults, documentation & validation.
153 *
154 * @param array $params
155 * Array or parameters determined by getfields.
156 */
157 function _civicrm_api3_event_get_spec(&$params) {
158 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
159 }
160
161 /**
162 * Support for schema changes made in 4.2.
163 *
164 * The main purpose of the API is to provide integrators a level of stability not provided by
165 * the core code or schema - this means we have to provide support for api calls (where possible)
166 * across schema changes.
167 *
168 * @param array $event
169 * @param int $event_id
170 */
171 function _civicrm_api3_event_get_legacy_support_42(&$event, $event_id) {
172 if (!empty($event[$event_id]['payment_processor'])) {
173 $processors = explode(CRM_Core_DAO::VALUE_SEPARATOR, $event[$event_id]['payment_processor']);
174 if (count($processors) == 3) {
175 $event[$event_id]['payment_processor_id'] = $processors[1];
176 }
177 }
178 }
179
180 /**
181 * Delete an existing event.
182 *
183 * This API is used for deleting a event.
184 *
185 * @param array $params
186 *
187 * @return array
188 */
189 function civicrm_api3_event_delete($params) {
190 return CRM_Event_BAO_Event::del($params['id']) ? civicrm_api3_create_success() : civicrm_api3_create_error(ts('Error while deleting event'));
191 }
192
193 /**
194 * Add 'is_full' & 'available_seats' to the return array.
195 *
196 * (this might be better in the BAO)
197 * Default BAO function returns a string if full rather than a Bool - which is more appropriate to a form
198 *
199 * @param array $event
200 * Return array of the event.
201 * @param int $event_id
202 * Id of the event to be updated.
203 */
204 function _civicrm_api3_event_getisfull(&$event, $event_id) {
205 $eventFullResult = CRM_Event_BAO_Participant::eventFull($event_id, 1);
206 if (!empty($eventFullResult) && is_int($eventFullResult)) {
207 $event[$event_id]['available_places'] = $eventFullResult;
208 }
209 else {
210 $event[$event_id]['available_places'] = 0;
211 }
212 $event[$event_id]['is_full'] = $event[$event_id]['available_places'] == 0 ? 1 : 0;
213 }
214
215
216 /**
217 * Get event list parameters.
218 *
219 * @see _civicrm_api3_generic_getlist_params
220 *
221 * @param array $request
222 */
223 function _civicrm_api3_event_getlist_params(&$request) {
224 $fieldsToReturn = array('start_date', 'event_type_id', 'title', 'summary');
225 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
226 $request['params']['options']['sort'] = 'start_date DESC';
227 $request['params'] += array(
228 'is_template' => 0,
229 'is_active' => 1,
230 );
231 }
232
233 /**
234 * Get event list output.
235 *
236 * @see _civicrm_api3_generic_getlist_output
237 *
238 * @param array $result
239 * @param array $request
240 *
241 * @return array
242 */
243 function _civicrm_api3_event_getlist_output($result, $request) {
244 $output = array();
245 if (!empty($result['values'])) {
246 foreach ($result['values'] as $row) {
247 $data = array(
248 'id' => $row[$request['id_field']],
249 'label' => $row[$request['label_field']],
250 'description' => array(
251 CRM_Core_Pseudoconstant::getLabel(
252 'CRM_Event_BAO_Event',
253 'event_type_id',
254 $row['event_type_id']
255 ),
256 ),
257 );
258 if (!empty($row['start_date'])) {
259 $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['start_date']);
260 }
261 if (!empty($row['summary'])) {
262 $data['description'][] = $row['summary'];
263 }
264 foreach ($request['extra'] as $field) {
265 $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL;
266 }
267 $output[] = $data;
268 }
269 }
270 return $output;
271 }