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