3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
14 * This api exposes CiviCRM Event.
16 * @package CiviCRM_APIv3
22 * @param array $params
27 * @throws \CRM_Core_Exception
28 * @throws \API_Exception
30 function civicrm_api3_event_create($params) {
31 // Required fields for creating an event
32 if (empty($params['id']) && empty($params['is_template'])) {
33 civicrm_api3_verify_mandatory($params, NULL, [
36 ['event_type_id', 'template_id'],
39 // Required fields for creating an event template
40 elseif (empty($params['id']) && !empty($params['is_template'])) {
41 civicrm_api3_verify_mandatory($params, NULL, [
46 _civicrm_api3_event_create_legacy_support_42($params);
47 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params, 'Event');
51 * Adjust Metadata for Create action.
53 * The metadata is used for setting defaults, documentation & validation.
55 * @param array $params
56 * Array of parameters determined by getfields.
58 function _civicrm_api3_event_create_spec(&$params) {
59 $params['is_active']['api.default'] = 1;
60 $params['financial_type_id']['api.aliases'] = ['contribution_type_id'];
61 $params['is_template']['api.default'] = 0;
62 $params['event_tz']['api.default'] = CRM_Core_Config
::singleton()->userSystem
->getTimeZoneString();
66 * Support for schema changes made in 4.2.
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.
72 * @param array $params
74 function _civicrm_api3_event_create_legacy_support_42(&$params) {
75 if (!empty($params['payment_processor_id'])) {
76 $params['payment_processor'] = CRM_Core_DAO
::VALUE_SEPARATOR
. $params['payment_processor_id'] . CRM_Core_DAO
::VALUE_SEPARATOR
;
83 * @param array $params
86 * Array of all found event property values.
88 function civicrm_api3_event_get($params) {
90 //legacy support for $params['return.sort']
91 if (!empty($params['return.sort'])) {
92 $params['options']['sort'] = $params['return.sort'];
93 unset($params['return.sort']);
96 //legacy support for $params['return.offset']
97 if (!empty($params['return.offset'])) {
98 $params['options']['offset'] = $params['return.offset'];
99 unset($params['return.offset']);
102 //legacy support for $params['return.max_results']
103 if (!empty($params['return.max_results'])) {
104 $params['options']['limit'] = $params['return.max_results'];
105 unset($params['return.max_results']);
108 $sql = CRM_Utils_SQL_Select
::fragment();
109 if (!empty($params['isCurrent'])) {
110 $sql->where('(start_date >= CURDATE() || end_date >= CURDATE())');
113 $events = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE, 'Event', $sql, TRUE);
114 $options = _civicrm_api3_get_options_from_params($params, TRUE);
115 if ($options['is_count']) {
116 return civicrm_api3_create_success($events, $params, 'Event', 'get');
118 foreach ($events as $id => $event) {
119 if (!empty($options['return']['is_full'])) {
120 _civicrm_api3_event_getisfull($events, $id);
122 _civicrm_api3_event_get_legacy_support_42($events, $id);
123 if (!empty($options['return']['price_set_id'])) {
124 $events[$id]['price_set_id'] = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $id);
128 return civicrm_api3_create_success($events, $params, 'Event', 'get');
132 * Adjust Metadata for Get action.
134 * The metadata is used for setting defaults, documentation & validation.
136 * @param array $params
137 * Array of parameters determined by getfields.
139 function _civicrm_api3_event_get_spec(&$params) {
140 $params['financial_type_id']['api.aliases'] = ['contribution_type_id'];
144 * Support for schema changes made in 4.2.
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.
150 * @param array $event
151 * @param int $event_id
153 function _civicrm_api3_event_get_legacy_support_42(&$event, $event_id) {
154 if (!empty($event[$event_id]['payment_processor'])) {
155 $processors = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $event[$event_id]['payment_processor']);
156 if (count($processors) == 3) {
157 $event[$event_id]['payment_processor_id'] = $processors[1];
163 * Delete an existing Event.
165 * This API is used for deleting a event given its id.
167 * @param array $params
171 function civicrm_api3_event_delete($params) {
172 return CRM_Event_BAO_Event
::del($params['id']) ?
civicrm_api3_create_success() : civicrm_api3_create_error(ts('Error while deleting event'));
176 * Add 'is_full' & 'available_seats' to the return array.
178 * (this might be better in the BAO)
179 * Default BAO function returns a string if full rather than a Bool - which is more appropriate to a form
181 * @param array $event
182 * Return array of the event.
183 * @param int $event_id
184 * Id of the event to be updated.
186 function _civicrm_api3_event_getisfull(&$event, $event_id) {
187 $eventFullResult = CRM_Event_BAO_Participant
::eventFull($event_id, 1);
188 if (!empty($eventFullResult) && is_int($eventFullResult)) {
189 $event[$event_id]['available_places'] = $eventFullResult;
191 elseif (is_null($eventFullResult)) {
192 return $event[$event_id]['is_full'] = 0;
195 $event[$event_id]['available_places'] = 0;
197 $event[$event_id]['is_full'] = $event[$event_id]['available_places'] == 0 ?
1 : 0;
201 * Get event list parameters.
203 * @see _civicrm_api3_generic_getlist_params
205 * @param array $request
207 function _civicrm_api3_event_getlist_params(&$request) {
213 $request['id_field'],
214 $request['label_field'],
216 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
217 $request['params']['options']['sort'] = 'start_date DESC';
218 if (empty($request['params']['id'])) {
219 $request['params'] +
= [
227 * Get event list output.
229 * @see _civicrm_api3_generic_getlist_output
231 * @param array $result
232 * @param array $request
236 function _civicrm_api3_event_getlist_output($result, $request) {
238 if (!empty($result['values'])) {
239 foreach ($result['values'] as $row) {
241 'id' => $row[$request['id_field']],
242 'label' => $row[$request['label_field']],
244 CRM_Core_PseudoConstant
::getLabel('CRM_Event_BAO_Event', 'event_type_id', $row['event_type_id']),
247 if (!empty($row['start_date'])) {
248 $data['description'][0] .= ': ' . CRM_Utils_Date
::customFormat($row['start_date']);
250 if (!empty($row['summary'])) {
251 $data['description'][] = $row['summary'];
253 // Add repeating info
254 $repeat = CRM_Core_BAO_RecurringEntity
::getPositionAndCount($row['id'], 'civicrm_event');
255 $data['extra']['is_recur'] = FALSE;
257 $data['suffix'] = ts('(%1 of %2)', [1 => $repeat[0], 2 => $repeat[1]]);
258 $data['extra']['is_recur'] = TRUE;
267 * Add missing timezones to all events.
271 function civicrm_api3_event_addmissingtimezones($params) {
272 $defaultTZ = CRM_Core_Config
::singleton()->userSystem
->getTimeZoneString();
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');