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']); | |
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 | */ | |
89 | function _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 | */ | |
102 | function _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 | */ | |
119 | function 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 |
178 | function _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 | */ | |
188 | function _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 | */ | |
209 | function 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 | */ | |
223 | function _civicrm_api3_event_getisfull(&$event, $event_id) { | |
6a488035 TO |
224 | $eventFullResult = CRM_Event_BAO_Participant::eventFull($event_id, 1); |
225 | if (!empty($eventFullResult) && is_int($eventFullResult)) { | |
226 | $event[$event_id]['available_places'] = $eventFullResult; | |
227 | } | |
228 | else { | |
229 | $event[$event_id]['available_places'] = 0; | |
230 | } | |
231 | $event[$event_id]['is_full'] = $event[$event_id]['available_places'] == 0 ? 1 : 0; | |
232 | } | |
233 | ||
9c7c6a00 CW |
234 | |
235 | /** | |
a6c6059d | 236 | * @see _civicrm_api3_generic_getlist_params. |
9c7c6a00 CW |
237 | * |
238 | * @param $request array | |
239 | */ | |
240 | function _civicrm_api3_event_getlist_params(&$request) { | |
8250601e CW |
241 | $fieldsToReturn = array('start_date', 'event_type_id', 'title', 'summary'); |
242 | $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); | |
9c7c6a00 CW |
243 | $request['params']['options']['sort'] = 'start_date DESC'; |
244 | $request['params'] += array( | |
245 | 'is_template' => 0, | |
246 | 'is_active' => 1, | |
247 | ); | |
248 | } | |
249 | ||
250 | /** | |
a6c6059d | 251 | * @see _civicrm_api3_generic_getlist_output |
9c7c6a00 CW |
252 | * |
253 | * @param $result array | |
254 | * @param $request array | |
255 | * | |
256 | * @return array | |
257 | */ | |
258 | function _civicrm_api3_event_getlist_output($result, $request) { | |
259 | $output = array(); | |
260 | if (!empty($result['values'])) { | |
261 | foreach ($result['values'] as $row) { | |
262 | $data = array( | |
263 | 'id' => $row[$request['id_field']], | |
264 | 'label' => $row[$request['label_field']], | |
88881f79 | 265 | 'description' => array(CRM_Core_Pseudoconstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $row['event_type_id'])), |
9c7c6a00 CW |
266 | ); |
267 | if (!empty($row['start_date'])) { | |
88881f79 | 268 | $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['start_date']); |
9c7c6a00 CW |
269 | } |
270 | if (!empty($row['summary'])) { | |
88881f79 | 271 | $data['description'][] = $row['summary']; |
9c7c6a00 | 272 | } |
8250601e CW |
273 | foreach ($request['extra'] as $field) { |
274 | $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; | |
275 | } | |
9c7c6a00 CW |
276 | $output[] = $data; |
277 | } | |
278 | } | |
279 | return $output; | |
280 | } |