Commit | Line | Data |
---|---|---|
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 | */ | |
57 | function 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']); | |
d8df9906 | 65 | } |
6a488035 TO |
66 | |
67 | _civicrm_api3_event_create_legacy_support_42($params); | |
b2cdd843 | 68 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Event'); |
6a488035 | 69 | } |
11e09c59 TO |
70 | |
71 | /** | |
6a488035 TO |
72 | * Adjust Metadata for Create action |
73 | * | |
74 | * The metadata is used for setting defaults, documentation & validation | |
75 | * @param array $params array or parameters determined by getfields | |
76 | */ | |
77 | function _civicrm_api3_event_create_spec(&$params) { | |
6a488035 TO |
78 | $params['start_date']['api.required'] = 1; |
79 | $params['title']['api.required'] = 1; | |
80 | $params['is_active']['api.default'] = 1; | |
81 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id'); | |
82 | } | |
11e09c59 TO |
83 | |
84 | /** | |
6a488035 TO |
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 an associative array of name/value property values of civicrm_event | |
101 | * {@getfields event_get} | |
102 | * | |
103 | * @return Array of all found event property values. | |
104 | * @access public | |
105 | * | |
106 | */ | |
107 | function civicrm_api3_event_get($params) { | |
108 | ||
109 | //legacy support for $params['return.sort'] | |
a7488080 | 110 | if (!empty($params['return.sort'])) { |
6a488035 TO |
111 | $params['options']['sort'] = $params['return.sort']; |
112 | unset($params['return.sort']); | |
113 | } | |
114 | ||
d8df9906 | 115 | //legacy support for $params['return.offset'] |
a7488080 | 116 | if (!empty($params['return.offset'])) { |
6a488035 TO |
117 | $params['options']['offset'] = $params['return.offset']; |
118 | unset($params['return.offset']); | |
119 | } | |
120 | ||
121 | //legacy support for $params['return.max_results'] | |
a7488080 | 122 | if (!empty($params['return.max_results'])) { |
6a488035 TO |
123 | $params['options']['limit'] = $params['return.max_results']; |
124 | unset($params['return.max_results']); | |
125 | } | |
126 | ||
127 | $eventDAO = new CRM_Event_BAO_Event(); | |
128 | _civicrm_api3_dao_set_filter($eventDAO, $params, TRUE, 'Event'); | |
129 | ||
a7488080 | 130 | if (!empty($params['is_template'])) { |
6a488035 TO |
131 | $eventDAO->whereAdd( '( is_template = 1 )' ); |
132 | } | |
7467081f | 133 | elseif(empty($eventDAO->id)){ |
afef19e4 | 134 | $eventDAO->whereAdd('( is_template IS NULL ) OR ( is_template = 0 )'); |
6a488035 TO |
135 | } |
136 | ||
a7488080 | 137 | if (!empty($params['isCurrent'])) { |
6a488035 TO |
138 | $eventDAO->whereAdd('(start_date >= CURDATE() || end_date >= CURDATE())'); |
139 | } | |
140 | ||
141 | // @todo should replace all this with _civicrm_api3_dao_to_array($bao, $params, FALSE, $entity) - but we still have | |
142 | // the return.is_full to deal with. | |
143 | // NB the std dao_to_array function should only return custom if required. | |
144 | $event = array(); | |
b2cdd843 EM |
145 | $options = _civicrm_api3_get_options_from_params($params); |
146 | ||
6a488035 TO |
147 | $eventDAO->find(); |
148 | while ($eventDAO->fetch()) { | |
149 | $event[$eventDAO->id] = array(); | |
150 | CRM_Core_DAO::storeValues($eventDAO, $event[$eventDAO->id]); | |
a7488080 | 151 | if (!empty($params['return.is_full'])) { |
6a488035 TO |
152 | _civicrm_api3_event_getisfull($event, $eventDAO->id); |
153 | } | |
154 | _civicrm_api3_event_get_legacy_support_42($event, $eventDAO->id); | |
155 | _civicrm_api3_custom_data_get($event[$eventDAO->id], 'Event', $eventDAO->id, NULL, $eventDAO->event_type_id); | |
b2cdd843 EM |
156 | if(!empty($options['return'])) { |
157 | $event[$eventDAO->id]['price_set_id'] = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventDAO->id); | |
158 | } | |
6a488035 TO |
159 | } |
160 | //end of the loop | |
161 | ||
162 | return civicrm_api3_create_success($event, $params, 'event', 'get', $eventDAO); | |
163 | } | |
11e09c59 TO |
164 | |
165 | /** | |
6a488035 | 166 | * Adjust Metadata for Get action |
11e09c59 TO |
167 | * |
168 | * The metadata is used for setting defaults, documentation & validation | |
169 | * @param array $params array or parameters determined by getfields | |
170 | */ | |
6a488035 TO |
171 | function _civicrm_api3_event_get_spec(&$params) { |
172 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id'); | |
173 | } | |
11e09c59 TO |
174 | |
175 | /** | |
6a488035 TO |
176 | * Support for schema changes made in 4.2 |
177 | * The main purpose of the API is to provide integrators a level of stability not provided by | |
178 | * the core code or schema - this means we have to provide support for api calls (where possible) | |
179 | * across schema changes. | |
180 | */ | |
181 | function _civicrm_api3_event_get_legacy_support_42(&$event, $event_id){ | |
182 | if(!empty($event[$event_id]['payment_processor'])){ | |
183 | $processors = explode(CRM_Core_DAO::VALUE_SEPARATOR,$event[$event_id]['payment_processor']); | |
184 | if(count($processors) == 3 ){ | |
185 | $event[$event_id]['payment_processor_id'] = $processors[1]; | |
186 | } | |
187 | } | |
188 | } | |
189 | ||
190 | /** | |
191 | * Deletes an existing event | |
192 | * | |
193 | * This API is used for deleting a event | |
194 | * | |
195 | * @param Array $params array containing event_id to be deleted | |
196 | * | |
197 | * @return boolean true if success, error otherwise | |
198 | * @access public | |
199 | * note API has legacy support for 'event_id' | |
200 | * {@getfields event_delete} | |
201 | */ | |
202 | function civicrm_api3_event_delete($params) { | |
203 | ||
204 | return CRM_Event_BAO_Event::del($params['id']) ? civicrm_api3_create_success() : civicrm_api3_create_error(ts('Error while deleting event')); | |
205 | } | |
206 | /* | |
207 | ||
11e09c59 | 208 | /** |
6a488035 TO |
209 | * Function to add 'is_full' & 'available_seats' to the return array. (this might be better in the BAO) |
210 | * Default BAO function returns a string if full rather than a Bool - which is more appropriate to a form | |
211 | * | |
212 | * @param array $event return array of the event | |
213 | * @param int $event_id Id of the event to be updated | |
214 | * | |
215 | */ | |
aa1b1481 EM |
216 | /** |
217 | * @param $event | |
218 | * @param $event_id | |
219 | */ | |
6a488035 | 220 | function _civicrm_api3_event_getisfull(&$event, $event_id) { |
6a488035 TO |
221 | $eventFullResult = CRM_Event_BAO_Participant::eventFull($event_id, 1); |
222 | if (!empty($eventFullResult) && is_int($eventFullResult)) { | |
223 | $event[$event_id]['available_places'] = $eventFullResult; | |
224 | } | |
225 | else { | |
226 | $event[$event_id]['available_places'] = 0; | |
227 | } | |
228 | $event[$event_id]['is_full'] = $event[$event_id]['available_places'] == 0 ? 1 : 0; | |
229 | } | |
230 | ||
9c7c6a00 CW |
231 | |
232 | /** | |
a6c6059d | 233 | * @see _civicrm_api3_generic_getlist_params. |
9c7c6a00 CW |
234 | * |
235 | * @param $request array | |
236 | */ | |
237 | function _civicrm_api3_event_getlist_params(&$request) { | |
8250601e CW |
238 | $fieldsToReturn = array('start_date', 'event_type_id', 'title', 'summary'); |
239 | $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); | |
9c7c6a00 CW |
240 | $request['params']['options']['sort'] = 'start_date DESC'; |
241 | $request['params'] += array( | |
242 | 'is_template' => 0, | |
243 | 'is_active' => 1, | |
244 | ); | |
245 | } | |
246 | ||
247 | /** | |
a6c6059d | 248 | * @see _civicrm_api3_generic_getlist_output |
9c7c6a00 CW |
249 | * |
250 | * @param $result array | |
251 | * @param $request array | |
252 | * | |
253 | * @return array | |
254 | */ | |
255 | function _civicrm_api3_event_getlist_output($result, $request) { | |
256 | $output = array(); | |
257 | if (!empty($result['values'])) { | |
258 | foreach ($result['values'] as $row) { | |
259 | $data = array( | |
260 | 'id' => $row[$request['id_field']], | |
261 | 'label' => $row[$request['label_field']], | |
88881f79 | 262 | 'description' => array(CRM_Core_Pseudoconstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $row['event_type_id'])), |
9c7c6a00 CW |
263 | ); |
264 | if (!empty($row['start_date'])) { | |
88881f79 | 265 | $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['start_date']); |
9c7c6a00 CW |
266 | } |
267 | if (!empty($row['summary'])) { | |
88881f79 | 268 | $data['description'][] = $row['summary']; |
9c7c6a00 | 269 | } |
8250601e CW |
270 | foreach ($request['extra'] as $field) { |
271 | $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; | |
272 | } | |
9c7c6a00 CW |
273 | $output[] = $data; |
274 | } | |
275 | } | |
276 | return $output; | |
277 | } |