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;
65 * Support for schema changes made in 4.2.
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.
71 * @param array $params
73 function _civicrm_api3_event_create_legacy_support_42(&$params) {
74 if (!empty($params['payment_processor_id'])) {
75 $params['payment_processor'] = CRM_Core_DAO
::VALUE_SEPARATOR
. $params['payment_processor_id'] . CRM_Core_DAO
::VALUE_SEPARATOR
;
82 * @param array $params
85 * Array of all found event property values.
87 function civicrm_api3_event_get($params) {
89 //legacy support for $params['return.sort']
90 if (!empty($params['return.sort'])) {
91 $params['options']['sort'] = $params['return.sort'];
92 unset($params['return.sort']);
95 //legacy support for $params['return.offset']
96 if (!empty($params['return.offset'])) {
97 $params['options']['offset'] = $params['return.offset'];
98 unset($params['return.offset']);
101 //legacy support for $params['return.max_results']
102 if (!empty($params['return.max_results'])) {
103 $params['options']['limit'] = $params['return.max_results'];
104 unset($params['return.max_results']);
107 $sql = CRM_Utils_SQL_Select
::fragment();
108 if (!empty($params['isCurrent'])) {
109 $sql->where('(start_date >= CURDATE() || end_date >= CURDATE())');
112 $events = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params, FALSE, 'Event', $sql, TRUE);
113 $options = _civicrm_api3_get_options_from_params($params, TRUE);
114 if ($options['is_count']) {
115 return civicrm_api3_create_success($events, $params, 'Event', 'get');
117 foreach ($events as $id => $event) {
118 if (!empty($options['return']['is_full'])) {
119 _civicrm_api3_event_getisfull($events, $id);
121 _civicrm_api3_event_get_legacy_support_42($events, $id);
122 if (!empty($options['return']['price_set_id'])) {
123 $events[$id]['price_set_id'] = CRM_Price_BAO_PriceSet
::getFor('civicrm_event', $id);
127 return civicrm_api3_create_success($events, $params, 'Event', 'get');
131 * Adjust Metadata for Get action.
133 * The metadata is used for setting defaults, documentation & validation.
135 * @param array $params
136 * Array of parameters determined by getfields.
138 function _civicrm_api3_event_get_spec(&$params) {
139 $params['financial_type_id']['api.aliases'] = ['contribution_type_id'];
143 * Support for schema changes made in 4.2.
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.
149 * @param array $event
150 * @param int $event_id
152 function _civicrm_api3_event_get_legacy_support_42(&$event, $event_id) {
153 if (!empty($event[$event_id]['payment_processor'])) {
154 $processors = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $event[$event_id]['payment_processor']);
155 if (count($processors) == 3) {
156 $event[$event_id]['payment_processor_id'] = $processors[1];
162 * Delete an existing Event.
164 * This API is used for deleting a event given its id.
166 * @param array $params
170 function civicrm_api3_event_delete($params) {
171 return CRM_Event_BAO_Event
::del($params['id']) ?
civicrm_api3_create_success() : civicrm_api3_create_error(ts('Error while deleting event'));
175 * Add 'is_full' & 'available_seats' to the return array.
177 * (this might be better in the BAO)
178 * Default BAO function returns a string if full rather than a Bool - which is more appropriate to a form
180 * @param array $event
181 * Return array of the event.
182 * @param int $event_id
183 * Id of the event to be updated.
185 function _civicrm_api3_event_getisfull(&$event, $event_id) {
186 $eventFullResult = CRM_Event_BAO_Participant
::eventFull($event_id, 1);
187 if (!empty($eventFullResult) && is_int($eventFullResult)) {
188 $event[$event_id]['available_places'] = $eventFullResult;
190 elseif (is_null($eventFullResult)) {
191 return $event[$event_id]['is_full'] = 0;
194 $event[$event_id]['available_places'] = 0;
196 $event[$event_id]['is_full'] = $event[$event_id]['available_places'] == 0 ?
1 : 0;
200 * Get event list parameters.
202 * @see _civicrm_api3_generic_getlist_params
204 * @param array $request
206 function _civicrm_api3_event_getlist_params(&$request) {
207 $fieldsToReturn = ['start_date', 'event_type_id', 'title', 'summary'];
208 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
209 $request['params']['options']['sort'] = 'start_date DESC';
210 if (empty($request['params']['id'])) {
211 $request['params'] +
= [
219 * Get event list output.
221 * @see _civicrm_api3_generic_getlist_output
223 * @param array $result
224 * @param array $request
228 function _civicrm_api3_event_getlist_output($result, $request) {
230 if (!empty($result['values'])) {
231 foreach ($result['values'] as $row) {
233 'id' => $row[$request['id_field']],
234 'label' => $row[$request['label_field']],
236 CRM_Core_PseudoConstant
::getLabel('CRM_Event_BAO_Event', 'event_type_id', $row['event_type_id']),
239 if (!empty($row['start_date'])) {
240 $data['description'][0] .= ': ' . CRM_Utils_Date
::customFormat($row['start_date']);
242 if (!empty($row['summary'])) {
243 $data['description'][] = $row['summary'];
245 // Add repeating info
246 $repeat = CRM_Core_BAO_RecurringEntity
::getPositionAndCount($row['id'], 'civicrm_event');
247 $data['extra']['is_recur'] = FALSE;
249 $data['suffix'] = ts('(%1 of %2)', [1 => $repeat[0], 2 => $repeat[1]]);
250 $data['extra']['is_recur'] = TRUE;