Merge branch 'MiscFixes' of github.com:dlobo/civicrm-core into dlobo-MiscFixes
[civicrm-core.git] / api / v3 / Event.php
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 */
45 require_once 'CRM/Event/BAO/Event.php';
46
47 /**
48 * Create a Event
49 *
50 * This API is used for creating a Event
51 *
52 * @param array $params input parameters
53 * Allowed @params array keys are:
54 * {@getfields event_create}
55 *
56 * @return array API result Array.
57 * @access public
58 */
59 function civicrm_api3_event_create($params) {
60
61 _civicrm_api3_event_create_legacy_support_42($params);
62 //format custom fields so they can be added
63 $value = array();
64 _civicrm_api3_custom_format_params($params, $values, 'Event');
65 $params = array_merge($values, $params);
66 require_once 'CRM/Event/BAO/Event.php';
67
68 $eventBAO = CRM_Event_BAO_Event::create($params);
69 $event = array();
70 _civicrm_api3_object_to_array($eventBAO, $event[$eventBAO->id]);
71 return civicrm_api3_create_success($event, $params);
72 }
73
74 /**
75 * Adjust Metadata for Create action
76 *
77 * The metadata is used for setting defaults, documentation & validation
78 * @param array $params array or parameters determined by getfields
79 */
80 function _civicrm_api3_event_create_spec(&$params) {
81 $params['event_type_id']['api.required'] = 1;;
82 $params['start_date']['api.required'] = 1;
83 $params['title']['api.required'] = 1;
84 $params['is_active']['api.default'] = 1;
85 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
86 }
87
88 /**
89 * Support for schema changes made in 4.2
90 * The main purpose of the API is to provide integrators a level of stability not provided by
91 * the core code or schema - this means we have to provide support for api calls (where possible)
92 * across schema changes.
93 */
94 function _civicrm_api3_event_create_legacy_support_42(&$params){
95 if(!empty($params['payment_processor_id'])){
96 $params['payment_processor'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['payment_processor_id'] . CRM_Core_DAO::VALUE_SEPARATOR;
97 }
98 }
99
100 /**
101 * Get Event record.
102 *
103 *
104 * @param array $params an associative array of name/value property values of civicrm_event
105 * {@getfields event_get}
106 *
107 * @return Array of all found event property values.
108 * @access public
109 *
110 */
111 function civicrm_api3_event_get($params) {
112
113 //legacy support for $params['return.sort']
114 if (CRM_Utils_Array::value('return.sort', $params)) {
115 $params['options']['sort'] = $params['return.sort'];
116 unset($params['return.sort']);
117 }
118
119 //legacy support for $params['return.sort']
120 if (CRM_Utils_Array::value('return.offset', $params)) {
121 $params['options']['offset'] = $params['return.offset'];
122 unset($params['return.offset']);
123 }
124
125 //legacy support for $params['return.max_results']
126 if (CRM_Utils_Array::value('return.max_results', $params)) {
127 $params['options']['limit'] = $params['return.max_results'];
128 unset($params['return.max_results']);
129 }
130
131 $eventDAO = new CRM_Event_BAO_Event();
132 _civicrm_api3_dao_set_filter($eventDAO, $params, TRUE, 'Event');
133
134 if (CRM_Utils_Array::value('is_template', $params)) {
135 $eventDAO->whereAdd( '( is_template = 1 )' );
136 }
137 else {
138 $eventDAO->whereAdd('( is_template IS NULL ) OR ( is_template = 0 )');
139 }
140
141 if (CRM_Utils_Array::value('isCurrent', $params)) {
142 $eventDAO->whereAdd('(start_date >= CURDATE() || end_date >= CURDATE())');
143 }
144
145 // @todo should replace all this with _civicrm_api3_dao_to_array($bao, $params, FALSE, $entity) - but we still have
146 // the return.is_full to deal with.
147 // NB the std dao_to_array function should only return custom if required.
148 $event = array();
149 $eventDAO->find();
150 while ($eventDAO->fetch()) {
151 $event[$eventDAO->id] = array();
152 CRM_Core_DAO::storeValues($eventDAO, $event[$eventDAO->id]);
153 if (CRM_Utils_Array::value('return.is_full', $params)) {
154 _civicrm_api3_event_getisfull($event, $eventDAO->id);
155 }
156 _civicrm_api3_event_get_legacy_support_42($event, $eventDAO->id);
157 _civicrm_api3_custom_data_get($event[$eventDAO->id], 'Event', $eventDAO->id, NULL, $eventDAO->event_type_id);
158 }
159 //end of the loop
160
161 return civicrm_api3_create_success($event, $params, 'event', 'get', $eventDAO);
162 }
163
164 /**
165 * Adjust Metadata for Get action
166 *
167 * The metadata is used for setting defaults, documentation & validation
168 * @param array $params array or parameters determined by getfields
169 */
170 function _civicrm_api3_event_get_spec(&$params) {
171 $params['financial_type_id']['api.aliases'] = array('contribution_type_id');
172 }
173
174 /**
175 * Support for schema changes made in 4.2
176 * The main purpose of the API is to provide integrators a level of stability not provided by
177 * the core code or schema - this means we have to provide support for api calls (where possible)
178 * across schema changes.
179 */
180 function _civicrm_api3_event_get_legacy_support_42(&$event, $event_id){
181 if(!empty($event[$event_id]['payment_processor'])){
182 $processors = explode(CRM_Core_DAO::VALUE_SEPARATOR,$event[$event_id]['payment_processor']);
183 if(count($processors) == 3 ){
184 $event[$event_id]['payment_processor_id'] = $processors[1];
185 }
186 }
187 }
188
189 /**
190 * Deletes an existing event
191 *
192 * This API is used for deleting a event
193 *
194 * @param Array $params array containing event_id to be deleted
195 *
196 * @return boolean true if success, error otherwise
197 * @access public
198 * note API has legacy support for 'event_id'
199 * {@getfields event_delete}
200 */
201 function civicrm_api3_event_delete($params) {
202
203 return CRM_Event_BAO_Event::del($params['id']) ? civicrm_api3_create_success() : civicrm_api3_create_error(ts('Error while deleting event'));
204 }
205 /*
206
207 /**
208 * Function to add 'is_full' & 'available_seats' to the return array. (this might be better in the BAO)
209 * Default BAO function returns a string if full rather than a Bool - which is more appropriate to a form
210 *
211 * @param array $event return array of the event
212 * @param int $event_id Id of the event to be updated
213 *
214 */
215 function _civicrm_api3_event_getisfull(&$event, $event_id) {
216 require_once 'CRM/Event/BAO/Participant.php';
217 $eventFullResult = CRM_Event_BAO_Participant::eventFull($event_id, 1);
218 if (!empty($eventFullResult) && is_int($eventFullResult)) {
219 $event[$event_id]['available_places'] = $eventFullResult;
220 }
221 else {
222 $event[$event_id]['available_places'] = 0;
223 }
224 $event[$event_id]['is_full'] = $event[$event_id]['available_places'] == 0 ? 1 : 0;
225 }
226