Merge pull request #4887 from pratikshad/broken-webtest
[civicrm-core.git] / api / v3 / Event.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * File for the CiviCRM APIv3 event functions
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_Event
35 *
36 * @copyright CiviCRM LLC (c) 2004-2014
37 * @version $Id: Event.php 30964 2010-11-29 09:41:54Z shot $
38 *
39 */
40
41 /**
42 * Create a Event
43 *
44 * This API is used for creating a Event
45 *
46 * @param array $params
47 * Input parameters.
48 * Allowed @params array keys are:
49 * {@getfields event_create}
50 *
51 * @return array
52 * API result Array.
53 * @access public
54 */
55 function civicrm_api3_event_create($params) {
56 civicrm_api3_verify_one_mandatory($params, NULL, array('event_type_id', 'template_id'));
57
58 // Clone event from template
59 if (!empty($params['template_id']) && empty($params['id'])) {
60 $copy = CRM_Event_BAO_Event::copy($params['template_id']);
61 $params['id'] = $copy->id;
62 unset($params['template_id']);
63 }
64
65 _civicrm_api3_event_create_legacy_support_42($params);
66 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Event');
67 }
68
69 /**
70 * Adjust Metadata for Create action
71 *
72 * The metadata is used for setting defaults, documentation & validation
73 * @param array $params
74 * Array or parameters determined by getfields.
75 */
76 function _civicrm_api3_event_create_spec(&$params) {
77 $params['start_date']['api.required'] = 1;
78 $params['title']['api.required'] = 1;
79 $params['is_active']['api.default'] = 1;
80 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
81 $params['is_template']['api.default'] = 0;
82 }
83
84 /**
85 * Support for schema changes made in 4.2
86 * The main purpose of the API is to provide integrators a level of stability not provided by
87 * the core code or schema - this means we have to provide support for api calls (where possible)
88 * across schema changes.
89 */
90 function _civicrm_api3_event_create_legacy_support_42(&$params) {
91 if (!empty($params['payment_processor_id'])) {
92 $params['payment_processor'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['payment_processor_id'] . CRM_Core_DAO::VALUE_SEPARATOR;
93 }
94 }
95
96 /**
97 * Get Event record.
98 *
99 *
100 * @param array $params
101 * An associative array of name/value property values of civicrm_event.
102 * {@getfields event_get}
103 *
104 * @return array
105 * Array of all found event property values.
106 * @access public
107 *
108 */
109 function civicrm_api3_event_get($params) {
110
111 //legacy support for $params['return.sort']
112 if (!empty($params['return.sort'])) {
113 $params['options']['sort'] = $params['return.sort'];
114 unset($params['return.sort']);
115 }
116
117 //legacy support for $params['return.offset']
118 if (!empty($params['return.offset'])) {
119 $params['options']['offset'] = $params['return.offset'];
120 unset($params['return.offset']);
121 }
122
123 //legacy support for $params['return.max_results']
124 if (!empty($params['return.max_results'])) {
125 $params['options']['limit'] = $params['return.max_results'];
126 unset($params['return.max_results']);
127 }
128
129 $eventDAO = new CRM_Event_BAO_Event();
130 _civicrm_api3_dao_set_filter($eventDAO, $params, TRUE, 'Event');
131
132 if (!empty($params['isCurrent'])) {
133 $eventDAO->whereAdd('(start_date >= CURDATE() || end_date >= CURDATE())');
134 }
135
136 // @todo should replace all this with _civicrm_api3_dao_to_array($bao, $params, FALSE, $entity) - but we still have
137 // the return.is_full to deal with.
138 // NB the std dao_to_array function should only return custom if required.
139 $event = array();
140 $options = _civicrm_api3_get_options_from_params($params);
141
142 $eventDAO->find();
143 while ($eventDAO->fetch()) {
144 $event[$eventDAO->id] = array();
145 CRM_Core_DAO::storeValues($eventDAO, $event[$eventDAO->id]);
146 if (!empty($params['return.is_full'])) {
147 _civicrm_api3_event_getisfull($event, $eventDAO->id);
148 }
149 _civicrm_api3_event_get_legacy_support_42($event, $eventDAO->id);
150 _civicrm_api3_custom_data_get($event[$eventDAO->id], 'Event', $eventDAO->id, NULL, $eventDAO->event_type_id);
151 if (!empty($options['return'])) {
152 $event[$eventDAO->id]['price_set_id'] = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventDAO->id);
153 }
154 }
155
156 return civicrm_api3_create_success($event, $params, 'event', 'get', $eventDAO);
157 }
158
159 /**
160 * Adjust Metadata for Get action
161 *
162 * The metadata is used for setting defaults, documentation & validation
163 * @param array $params
164 * Array or parameters determined by getfields.
165 */
166 function _civicrm_api3_event_get_spec(&$params) {
167 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
168 }
169
170 /**
171 * Support for schema changes made in 4.2
172 * The main purpose of the API is to provide integrators a level of stability not provided by
173 * the core code or schema - this means we have to provide support for api calls (where possible)
174 * across schema changes.
175 */
176 function _civicrm_api3_event_get_legacy_support_42(&$event, $event_id) {
177 if (!empty($event[$event_id]['payment_processor'])) {
178 $processors = explode(CRM_Core_DAO::VALUE_SEPARATOR, $event[$event_id]['payment_processor']);
179 if (count($processors) == 3) {
180 $event[$event_id]['payment_processor_id'] = $processors[1];
181 }
182 }
183 }
184
185 /**
186 * Deletes an existing event
187 *
188 * This API is used for deleting a event
189 *
190 * @param array $paramsArray containing event_id to be deleted.
191 * Array containing event_id to be deleted.
192 *
193 * @return boolean
194 * true if success, error otherwise
195 * @access public
196 * note API has legacy support for 'event_id'
197 * {@getfields event_delete}
198 */
199 function civicrm_api3_event_delete($params) {
200
201 return CRM_Event_BAO_Event::del($params['id']) ? civicrm_api3_create_success() : civicrm_api3_create_error(ts('Error while deleting event'));
202 }
203 /*
204
205 /**
206 * add 'is_full' & 'available_seats' to the return array. (this might be better in the BAO)
207 * Default BAO function returns a string if full rather than a Bool - which is more appropriate to a form
208 *
209 * @param array $event
210 * Return array of the event.
211 * @param int $event_id
212 * Id of the event to be updated.
213 *
214 */
215 /**
216 * @param $event
217 * @param int $event_id
218 */
219 function _civicrm_api3_event_getisfull(&$event, $event_id) {
220 $eventFullResult = CRM_Event_BAO_Participant::eventFull($event_id, 1);
221 if (!empty($eventFullResult) && is_int($eventFullResult)) {
222 $event[$event_id]['available_places'] = $eventFullResult;
223 }
224 else {
225 $event[$event_id]['available_places'] = 0;
226 }
227 $event[$event_id]['is_full'] = $event[$event_id]['available_places'] == 0 ? 1 : 0;
228 }
229
230
231 /**
232 * @see _civicrm_api3_generic_getlist_params
233 *
234 * @param $request
235 * Array.
236 */
237 function _civicrm_api3_event_getlist_params(&$request) {
238 $fieldsToReturn = array('start_date', 'event_type_id', 'title', 'summary');
239 $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
240 $request['params']['options']['sort'] = 'start_date DESC';
241 $request['params'] += array(
242 'is_template' => 0,
243 'is_active' => 1,
244 );
245 }
246
247 /**
248 * @see _civicrm_api3_generic_getlist_output
249 *
250 * @param $result
251 * Array.
252 * @param $request
253 * Array.
254 *
255 * @return array
256 */
257 function _civicrm_api3_event_getlist_output($result, $request) {
258 $output = array();
259 if (!empty($result['values'])) {
260 foreach ($result['values'] as $row) {
261 $data = array(
262 'id' => $row[$request['id_field']],
263 'label' => $row[$request['label_field']],
264 'description' => array(CRM_Core_Pseudoconstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $row['event_type_id'])),
265 );
266 if (!empty($row['start_date'])) {
267 $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['start_date']);
268 }
269 if (!empty($row['summary'])) {
270 $data['description'][] = $row['summary'];
271 }
272 foreach ($request['extra'] as $field) {
273 $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL;
274 }
275 $output[] = $data;
276 }
277 }
278 return $output;
279 }