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