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