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