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