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