Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
731a0992 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * File for the CiviCRM APIv3 event functions | |
31 | * | |
32 | * @package CiviCRM_APIv3 | |
33 | * @subpackage API_Event | |
34 | * | |
731a0992 | 35 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
36 | * @version $Id: Event.php 30964 2010-11-29 09:41:54Z shot $ |
37 | * | |
38 | */ | |
39 | ||
6a488035 TO |
40 | /** |
41 | * Create a Event | |
42 | * | |
43 | * This API is used for creating a Event | |
44 | * | |
cf470720 TO |
45 | * @param array $params |
46 | * Input parameters. | |
16b10e64 | 47 | * Allowed @params array keys are: |
6a488035 TO |
48 | * {@getfields event_create} |
49 | * | |
a6c01b45 | 50 | * @return array |
72b3a70c | 51 | * API result Array. |
6a488035 TO |
52 | * @access public |
53 | */ | |
54 | function civicrm_api3_event_create($params) { | |
d8df9906 CW |
55 | civicrm_api3_verify_one_mandatory($params, NULL, array('event_type_id', 'template_id')); |
56 | ||
57 | // Clone event from template | |
58 | if (!empty($params['template_id']) && empty($params['id'])) { | |
59 | $copy = CRM_Event_BAO_Event::copy($params['template_id']); | |
60 | $params['id'] = $copy->id; | |
61 | unset($params['template_id']); | |
d8df9906 | 62 | } |
6a488035 TO |
63 | |
64 | _civicrm_api3_event_create_legacy_support_42($params); | |
b2cdd843 | 65 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Event'); |
6a488035 | 66 | } |
11e09c59 TO |
67 | |
68 | /** | |
6a488035 TO |
69 | * Adjust Metadata for Create action |
70 | * | |
71 | * The metadata is used for setting defaults, documentation & validation | |
cf470720 TO |
72 | * @param array $params |
73 | * Array or parameters determined by getfields. | |
6a488035 TO |
74 | */ |
75 | function _civicrm_api3_event_create_spec(&$params) { | |
6a488035 TO |
76 | $params['start_date']['api.required'] = 1; |
77 | $params['title']['api.required'] = 1; | |
78 | $params['is_active']['api.default'] = 1; | |
79 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id'); | |
633430e6 | 80 | $params['is_template']['api.default'] = 0; |
6a488035 | 81 | } |
11e09c59 TO |
82 | |
83 | /** | |
6a488035 TO |
84 | * Support for schema changes made in 4.2 |
85 | * The main purpose of the API is to provide integrators a level of stability not provided by | |
86 | * the core code or schema - this means we have to provide support for api calls (where possible) | |
87 | * across schema changes. | |
88 | */ | |
9b873358 TO |
89 | function _civicrm_api3_event_create_legacy_support_42(&$params) { |
90 | if (!empty($params['payment_processor_id'])) { | |
6a488035 TO |
91 | $params['payment_processor'] = CRM_Core_DAO::VALUE_SEPARATOR . $params['payment_processor_id'] . CRM_Core_DAO::VALUE_SEPARATOR; |
92 | } | |
93 | } | |
94 | ||
95 | /** | |
96 | * Get Event record. | |
97 | * | |
98 | * | |
cf470720 TO |
99 | * @param array $params |
100 | * An associative array of name/value property values of civicrm_event. | |
6a488035 TO |
101 | * {@getfields event_get} |
102 | * | |
16b10e64 CW |
103 | * @return array |
104 | * Array of all found event property values. | |
6a488035 TO |
105 | * @access public |
106 | * | |
107 | */ | |
108 | function civicrm_api3_event_get($params) { | |
109 | ||
110 | //legacy support for $params['return.sort'] | |
a7488080 | 111 | if (!empty($params['return.sort'])) { |
6a488035 TO |
112 | $params['options']['sort'] = $params['return.sort']; |
113 | unset($params['return.sort']); | |
114 | } | |
115 | ||
d8df9906 | 116 | //legacy support for $params['return.offset'] |
a7488080 | 117 | if (!empty($params['return.offset'])) { |
6a488035 TO |
118 | $params['options']['offset'] = $params['return.offset']; |
119 | unset($params['return.offset']); | |
120 | } | |
121 | ||
122 | //legacy support for $params['return.max_results'] | |
a7488080 | 123 | if (!empty($params['return.max_results'])) { |
6a488035 TO |
124 | $params['options']['limit'] = $params['return.max_results']; |
125 | unset($params['return.max_results']); | |
126 | } | |
127 | ||
128 | $eventDAO = new CRM_Event_BAO_Event(); | |
129 | _civicrm_api3_dao_set_filter($eventDAO, $params, TRUE, 'Event'); | |
130 | ||
a7488080 | 131 | if (!empty($params['isCurrent'])) { |
6a488035 TO |
132 | $eventDAO->whereAdd('(start_date >= CURDATE() || end_date >= CURDATE())'); |
133 | } | |
134 | ||
135 | // @todo should replace all this with _civicrm_api3_dao_to_array($bao, $params, FALSE, $entity) - but we still have | |
136 | // the return.is_full to deal with. | |
137 | // NB the std dao_to_array function should only return custom if required. | |
138 | $event = array(); | |
b2cdd843 EM |
139 | $options = _civicrm_api3_get_options_from_params($params); |
140 | ||
6a488035 TO |
141 | $eventDAO->find(); |
142 | while ($eventDAO->fetch()) { | |
143 | $event[$eventDAO->id] = array(); | |
144 | CRM_Core_DAO::storeValues($eventDAO, $event[$eventDAO->id]); | |
a7488080 | 145 | if (!empty($params['return.is_full'])) { |
6a488035 TO |
146 | _civicrm_api3_event_getisfull($event, $eventDAO->id); |
147 | } | |
148 | _civicrm_api3_event_get_legacy_support_42($event, $eventDAO->id); | |
149 | _civicrm_api3_custom_data_get($event[$eventDAO->id], 'Event', $eventDAO->id, NULL, $eventDAO->event_type_id); | |
22e263ad | 150 | if (!empty($options['return'])) { |
b2cdd843 EM |
151 | $event[$eventDAO->id]['price_set_id'] = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $eventDAO->id); |
152 | } | |
6a488035 | 153 | } |
6a488035 TO |
154 | |
155 | return civicrm_api3_create_success($event, $params, 'event', 'get', $eventDAO); | |
156 | } | |
11e09c59 TO |
157 | |
158 | /** | |
6a488035 | 159 | * Adjust Metadata for Get action |
11e09c59 TO |
160 | * |
161 | * The metadata is used for setting defaults, documentation & validation | |
cf470720 TO |
162 | * @param array $params |
163 | * Array or parameters determined by getfields. | |
11e09c59 | 164 | */ |
6a488035 TO |
165 | function _civicrm_api3_event_get_spec(&$params) { |
166 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id'); | |
167 | } | |
11e09c59 TO |
168 | |
169 | /** | |
6a488035 TO |
170 | * Support for schema changes made in 4.2 |
171 | * The main purpose of the API is to provide integrators a level of stability not provided by | |
172 | * the core code or schema - this means we have to provide support for api calls (where possible) | |
173 | * across schema changes. | |
174 | */ | |
9b873358 TO |
175 | function _civicrm_api3_event_get_legacy_support_42(&$event, $event_id) { |
176 | if (!empty($event[$event_id]['payment_processor'])) { | |
35671d00 | 177 | $processors = explode(CRM_Core_DAO::VALUE_SEPARATOR, $event[$event_id]['payment_processor']); |
481a74f4 | 178 | if (count($processors) == 3) { |
6a488035 TO |
179 | $event[$event_id]['payment_processor_id'] = $processors[1]; |
180 | } | |
181 | } | |
182 | } | |
183 | ||
184 | /** | |
185 | * Deletes an existing event | |
186 | * | |
187 | * This API is used for deleting a event | |
188 | * | |
35671d00 | 189 | * @param array $paramsArray containing event_id to be deleted. |
cf470720 | 190 | * Array containing event_id to be deleted. |
6a488035 | 191 | * |
a6c01b45 | 192 | * @return boolean |
72b3a70c | 193 | * true if success, error otherwise |
6a488035 TO |
194 | * @access public |
195 | * note API has legacy support for 'event_id' | |
196 | * {@getfields event_delete} | |
197 | */ | |
198 | function civicrm_api3_event_delete($params) { | |
199 | ||
200 | return CRM_Event_BAO_Event::del($params['id']) ? civicrm_api3_create_success() : civicrm_api3_create_error(ts('Error while deleting event')); | |
201 | } | |
202 | /* | |
203 | ||
11e09c59 | 204 | /** |
c490a46a | 205 | * add 'is_full' & 'available_seats' to the return array. (this might be better in the BAO) |
6a488035 TO |
206 | * Default BAO function returns a string if full rather than a Bool - which is more appropriate to a form |
207 | * | |
cf470720 TO |
208 | * @param array $event |
209 | * Return array of the event. | |
210 | * @param int $event_id | |
211 | * Id of the event to be updated. | |
6a488035 TO |
212 | * |
213 | */ | |
aa1b1481 EM |
214 | /** |
215 | * @param $event | |
100fef9d | 216 | * @param int $event_id |
aa1b1481 | 217 | */ |
6a488035 | 218 | function _civicrm_api3_event_getisfull(&$event, $event_id) { |
6a488035 TO |
219 | $eventFullResult = CRM_Event_BAO_Participant::eventFull($event_id, 1); |
220 | if (!empty($eventFullResult) && is_int($eventFullResult)) { | |
221 | $event[$event_id]['available_places'] = $eventFullResult; | |
222 | } | |
223 | else { | |
224 | $event[$event_id]['available_places'] = 0; | |
225 | } | |
226 | $event[$event_id]['is_full'] = $event[$event_id]['available_places'] == 0 ? 1 : 0; | |
227 | } | |
228 | ||
9c7c6a00 CW |
229 | |
230 | /** | |
37fa58b0 | 231 | * @see _civicrm_api3_generic_getlist_params |
9c7c6a00 | 232 | * |
cf470720 TO |
233 | * @param $request |
234 | * Array. | |
9c7c6a00 CW |
235 | */ |
236 | function _civicrm_api3_event_getlist_params(&$request) { | |
8250601e CW |
237 | $fieldsToReturn = array('start_date', 'event_type_id', 'title', 'summary'); |
238 | $request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra'])); | |
9c7c6a00 CW |
239 | $request['params']['options']['sort'] = 'start_date DESC'; |
240 | $request['params'] += array( | |
241 | 'is_template' => 0, | |
242 | 'is_active' => 1, | |
243 | ); | |
244 | } | |
245 | ||
246 | /** | |
a6c6059d | 247 | * @see _civicrm_api3_generic_getlist_output |
9c7c6a00 | 248 | * |
cf470720 TO |
249 | * @param $result |
250 | * Array. | |
251 | * @param $request | |
252 | * Array. | |
9c7c6a00 CW |
253 | * |
254 | * @return array | |
255 | */ | |
256 | function _civicrm_api3_event_getlist_output($result, $request) { | |
257 | $output = array(); | |
258 | if (!empty($result['values'])) { | |
259 | foreach ($result['values'] as $row) { | |
260 | $data = array( | |
261 | 'id' => $row[$request['id_field']], | |
262 | 'label' => $row[$request['label_field']], | |
88881f79 | 263 | 'description' => array(CRM_Core_Pseudoconstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $row['event_type_id'])), |
9c7c6a00 CW |
264 | ); |
265 | if (!empty($row['start_date'])) { | |
88881f79 | 266 | $data['description'][0] .= ': ' . CRM_Utils_Date::customFormat($row['start_date']); |
9c7c6a00 CW |
267 | } |
268 | if (!empty($row['summary'])) { | |
88881f79 | 269 | $data['description'][] = $row['summary']; |
9c7c6a00 | 270 | } |
8250601e CW |
271 | foreach ($request['extra'] as $field) { |
272 | $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; | |
273 | } | |
9c7c6a00 CW |
274 | $output[] = $data; |
275 | } | |
276 | } | |
277 | return $output; | |
278 | } |