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