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