Code style
[civicrm-core.git] / api / v3 / Event.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
1f4ea726 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
TO
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 *
244bbdd8 30 * This api exposes CiviCRM Event.
6a488035
TO
31 *
32 * @package CiviCRM_APIv3
6a488035
TO
33 */
34
6a488035 35/**
35823763 36 * Create a Event.
6a488035 37 *
cf470720
TO
38 * @param array $params
39 * Input parameters.
6a488035 40 *
a6c01b45 41 * @return array
72b3a70c 42 * API result Array.
6a488035
TO
43 */
44function civicrm_api3_event_create($params) {
48ae8c9d
CW
45 // Required fields for creating an event
46 if (empty($params['id']) && empty($params['is_template'])) {
47 civicrm_api3_verify_mandatory($params, NULL, array(
48 'start_date',
49 'title',
50 array('event_type_id', 'template_id'),
51 ));
52 }
53 // Required fields for creating an event template
54 elseif (empty($params['id']) && !empty($params['is_template'])) {
55 civicrm_api3_verify_mandatory($params, NULL, array(
56 'template_title',
57 ));
58 }
d8df9906
CW
59
60 // Clone event from template
61 if (!empty($params['template_id']) && empty($params['id'])) {
62 $copy = CRM_Event_BAO_Event::copy($params['template_id']);
63 $params['id'] = $copy->id;
64 unset($params['template_id']);
d8df9906 65 }
6a488035
TO
66
67 _civicrm_api3_event_create_legacy_support_42($params);
b2cdd843 68 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Event');
6a488035 69}
11e09c59
TO
70
71/**
0aa0303c
EM
72 * Adjust Metadata for Create action.
73 *
74 * The metadata is used for setting defaults, documentation & validation.
6a488035 75 *
cf470720 76 * @param array $params
b081365f 77 * Array of parameters determined by getfields.
6a488035
TO
78 */
79function _civicrm_api3_event_create_spec(&$params) {
6a488035
TO
80 $params['is_active']['api.default'] = 1;
81 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
633430e6 82 $params['is_template']['api.default'] = 0;
6a488035 83}
11e09c59
TO
84
85/**
35823763
EM
86 * Support for schema changes made in 4.2.
87 *
6a488035
TO
88 * The main purpose of the API is to provide integrators a level of stability not provided by
89 * the core code or schema - this means we have to provide support for api calls (where possible)
90 * across schema changes.
35823763 91 *
d0997921 92 * @param array $params
6a488035 93 */
9b873358
TO
94function _civicrm_api3_event_create_legacy_support_42(&$params) {
95 if (!empty($params['payment_processor_id'])) {
6a488035
TO
96 $params['payment_processor'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['payment_processor_id'] . CRM_Core_DAO::VALUE_SEPARATOR;
97 }
98}
99
100/**
101 * Get Event record.
102 *
cf470720 103 * @param array $params
6a488035 104 *
16b10e64
CW
105 * @return array
106 * Array of all found event property values.
6a488035
TO
107 */
108function civicrm_api3_event_get($params) {
109
110 //legacy support for $params['return.sort']
a7488080 111 if (!empty($params['return.sort'])) {
6a488035
TO
112 $params['options']['sort'] = $params['return.sort'];
113 unset($params['return.sort']);
114 }
115
d8df9906 116 //legacy support for $params['return.offset']
a7488080 117 if (!empty($params['return.offset'])) {
6a488035
TO
118 $params['options']['offset'] = $params['return.offset'];
119 unset($params['return.offset']);
120 }
121
122 //legacy support for $params['return.max_results']
a7488080 123 if (!empty($params['return.max_results'])) {
6a488035
TO
124 $params['options']['limit'] = $params['return.max_results'];
125 unset($params['return.max_results']);
126 }
127
01c8287d 128 $sql = CRM_Utils_SQL_Select::fragment();
a7488080 129 if (!empty($params['isCurrent'])) {
01c8287d 130 $sql->where('(start_date >= CURDATE() || end_date >= CURDATE())');
6a488035
TO
131 }
132
01c8287d 133 $events = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Event', $sql, TRUE);
b2cdd843 134 $options = _civicrm_api3_get_options_from_params($params);
0298287b 135 if ($options['is_count']) {
136 return civicrm_api3_create_success($events, $params, 'Event', 'get');
137 }
0f3699bf 138 foreach ($events as $id => $event) {
a7488080 139 if (!empty($params['return.is_full'])) {
0f3699bf 140 _civicrm_api3_event_getisfull($events, $id);
6a488035 141 }
0f3699bf 142 _civicrm_api3_event_get_legacy_support_42($events, $id);
22e263ad 143 if (!empty($options['return'])) {
0f3699bf 144 $events[$id]['price_set_id'] = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $id);
b2cdd843 145 }
6a488035 146 }
6a488035 147
0f3699bf 148 return civicrm_api3_create_success($events, $params, 'Event', 'get');
6a488035 149}
11e09c59
TO
150
151/**
35823763 152 * Adjust Metadata for Get action.
11e09c59 153 *
0aa0303c
EM
154 * The metadata is used for setting defaults, documentation & validation.
155 *
cf470720 156 * @param array $params
b081365f 157 * Array of parameters determined by getfields.
11e09c59 158 */
6a488035
TO
159function _civicrm_api3_event_get_spec(&$params) {
160 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
161}
11e09c59
TO
162
163/**
35823763
EM
164 * Support for schema changes made in 4.2.
165 *
6a488035
TO
166 * The main purpose of the API is to provide integrators a level of stability not provided by
167 * the core code or schema - this means we have to provide support for api calls (where possible)
168 * across schema changes.
35823763
EM
169 *
170 * @param array $event
171 * @param int $event_id
6a488035 172 */
9b873358
TO
173function _civicrm_api3_event_get_legacy_support_42(&$event, $event_id) {
174 if (!empty($event[$event_id]['payment_processor'])) {
35671d00 175 $processors = explode(CRM_Core_DAO::VALUE_SEPARATOR, $event[$event_id]['payment_processor']);
481a74f4 176 if (count($processors) == 3) {
6a488035
TO
177 $event[$event_id]['payment_processor_id'] = $processors[1];
178 }
179 }
180}
181
182/**
244bbdd8 183 * Delete an existing Event.
6a488035 184 *
244bbdd8 185 * This API is used for deleting a event given its id.
6a488035 186 *
d0997921 187 * @param array $params
35823763 188 *
645ee340 189 * @return array
6a488035
TO
190 */
191function civicrm_api3_event_delete($params) {
6a488035
TO
192 return CRM_Event_BAO_Event::del($params['id']) ? civicrm_api3_create_success() : civicrm_api3_create_error(ts('Error while deleting event'));
193}
6a488035 194
11e09c59 195/**
35823763
EM
196 * Add 'is_full' & 'available_seats' to the return array.
197 *
198 * (this might be better in the BAO)
6a488035
TO
199 * Default BAO function returns a string if full rather than a Bool - which is more appropriate to a form
200 *
cf470720
TO
201 * @param array $event
202 * Return array of the event.
203 * @param int $event_id
204 * Id of the event to be updated.
aa1b1481 205 */
6a488035 206function _civicrm_api3_event_getisfull(&$event, $event_id) {
6a488035
TO
207 $eventFullResult = CRM_Event_BAO_Participant::eventFull($event_id, 1);
208 if (!empty($eventFullResult) && is_int($eventFullResult)) {
209 $event[$event_id]['available_places'] = $eventFullResult;
210 }
862352a9 211 elseif (is_null($eventFullResult)) {
2d81659f 212 return $event[$event_id]['is_full'] = 0;
213 }
6a488035
TO
214 else {
215 $event[$event_id]['available_places'] = 0;
216 }
217 $event[$event_id]['is_full'] = $event[$event_id]['available_places'] == 0 ? 1 : 0;
218}
219
9c7c6a00
CW
220
221/**
35823763
EM
222 * Get event list parameters.
223 *
37fa58b0 224 * @see _civicrm_api3_generic_getlist_params
9c7c6a00 225 *
8c6b335b 226 * @param array $request
9c7c6a00
CW
227 */
228function _civicrm_api3_event_getlist_params(&$request) {
8250601e
CW
229 $fieldsToReturn = array('start_date', 'event_type_id', 'title', 'summary');
230 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
9c7c6a00 231 $request['params']['options']['sort'] = 'start_date DESC';
38a14841
CW
232 if (empty($request['params']['id'])) {
233 $request['params'] += array(
234 'is_template' => 0,
235 'is_active' => 1,
236 );
237 }
9c7c6a00
CW
238}
239
240/**
35823763
EM
241 * Get event list output.
242 *
a6c6059d 243 * @see _civicrm_api3_generic_getlist_output
9c7c6a00 244 *
8c6b335b
CW
245 * @param array $result
246 * @param array $request
9c7c6a00
CW
247 *
248 * @return array
249 */
250function _civicrm_api3_event_getlist_output($result, $request) {
251 $output = array();
252 if (!empty($result['values'])) {
253 foreach ($result['values'] as $row) {
254 $data = array(
255 'id' => $row[$request['id_field']],
256 'label' => $row[$request['label_field']],
35823763
EM
257 'description' => array(
258 CRM_Core_Pseudoconstant::getLabel(
259 'CRM_Event_BAO_Event',
260 'event_type_id',
261 $row['event_type_id']
262 ),
263 ),
9c7c6a00
CW
264 );
265 if (!empty($row['start_date'])) {
88881f79 266 $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['start_date']);
9c7c6a00
CW
267 }
268 if (!empty($row['summary'])) {
88881f79 269 $data['description'][] = $row['summary'];
9c7c6a00 270 }
8a938c69
CW
271 // Add repeating info
272 $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($row['id'], 'civicrm_event');
273 $data['extra']['is_recur'] = FALSE;
274 if ($repeat) {
275 $data['suffix'] = ts('(%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1]));
276 $data['extra']['is_recur'] = TRUE;
277 }
9c7c6a00
CW
278 $output[] = $data;
279 }
280 }
281 return $output;
282}