|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\modules\ticket_selector; |
|
4
|
|
|
|
|
5
|
|
|
use EE_Datetime; |
|
6
|
|
|
use EE_Error; |
|
7
|
|
|
use EE_Registry; |
|
8
|
|
|
use EE_Ticket; |
|
9
|
|
|
use EEH_Event_View; |
|
10
|
|
|
use EEH_URL; |
|
11
|
|
|
use EEM_Datetime; |
|
12
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
13
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
|
14
|
|
|
use EventEspresso\core\services\loaders\LoaderFactory; |
|
15
|
|
|
use InvalidArgumentException; |
|
16
|
|
|
|
|
17
|
|
|
if (! defined('EVENT_ESPRESSO_VERSION')) { |
|
18
|
|
|
exit('No direct script access allowed'); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class ProcessTicketSelector |
|
25
|
|
|
* Description |
|
26
|
|
|
* |
|
27
|
|
|
* @package Event Espresso |
|
28
|
|
|
* @subpackage core |
|
29
|
|
|
* @author Brent Christensen |
|
30
|
|
|
* @since 4.9.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
class ProcessTicketSelector |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* array of datetimes and the spaces available for them |
|
37
|
|
|
* |
|
38
|
|
|
* @var array[][] |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $_available_spaces = array(); |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* cancelTicketSelections |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
* @throws EE_Error |
|
48
|
|
|
* @throws InvalidArgumentException |
|
49
|
|
|
* @throws InvalidInterfaceException |
|
50
|
|
|
* @throws InvalidDataTypeException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function cancelTicketSelections() |
|
53
|
|
|
{ |
|
54
|
|
|
// check nonce |
|
55
|
|
|
if (! $this->processTicketSelectorNonce('cancel_ticket_selections')) { |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
59
|
|
|
if (EE_Registry::instance()->REQ->is_set('event_id')) { |
|
60
|
|
|
wp_safe_redirect( |
|
61
|
|
|
EEH_Event_View::event_link_url( |
|
62
|
|
|
EE_Registry::instance()->REQ->get('event_id') |
|
63
|
|
|
) |
|
64
|
|
|
); |
|
65
|
|
|
} else { |
|
66
|
|
|
wp_safe_redirect( |
|
67
|
|
|
site_url('/' . EE_Registry::instance()->CFG->core->event_cpt_slug . '/') |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
exit(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* processTicketSelectorNonce |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $nonce_name |
|
78
|
|
|
* @param string $id |
|
79
|
|
|
* @return bool |
|
80
|
|
|
* @throws InvalidArgumentException |
|
81
|
|
|
* @throws InvalidInterfaceException |
|
82
|
|
|
* @throws InvalidDataTypeException |
|
83
|
|
|
*/ |
|
84
|
|
|
private function processTicketSelectorNonce($nonce_name, $id = '') |
|
85
|
|
|
{ |
|
86
|
|
|
$nonce_name_with_id = ! empty($id) ? "{$nonce_name}_nonce_{$id}" : "{$nonce_name}_nonce"; |
|
87
|
|
|
if ( |
|
88
|
|
|
! is_admin() |
|
89
|
|
|
&& ( |
|
90
|
|
|
! EE_Registry::instance()->REQ->is_set($nonce_name_with_id) |
|
91
|
|
|
|| ! wp_verify_nonce( |
|
92
|
|
|
EE_Registry::instance()->REQ->get($nonce_name_with_id), |
|
93
|
|
|
$nonce_name |
|
94
|
|
|
) |
|
95
|
|
|
) |
|
96
|
|
|
) { |
|
97
|
|
|
EE_Error::add_error( |
|
98
|
|
|
sprintf( |
|
99
|
|
|
__( |
|
100
|
|
|
'We\'re sorry but your request failed to pass a security check.%sPlease click the back button on your browser and try again.', |
|
101
|
|
|
'event_espresso' |
|
102
|
|
|
), |
|
103
|
|
|
'<br/>' |
|
104
|
|
|
), |
|
105
|
|
|
__FILE__, |
|
106
|
|
|
__FUNCTION__, |
|
107
|
|
|
__LINE__ |
|
108
|
|
|
); |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* process_ticket_selections |
|
117
|
|
|
* |
|
118
|
|
|
* @return array|bool |
|
119
|
|
|
* @throws \ReflectionException |
|
120
|
|
|
* @throws InvalidArgumentException |
|
121
|
|
|
* @throws InvalidInterfaceException |
|
122
|
|
|
* @throws InvalidDataTypeException |
|
123
|
|
|
* @throws EE_Error |
|
124
|
|
|
*/ |
|
125
|
|
|
public function processTicketSelections() |
|
126
|
|
|
{ |
|
127
|
|
|
do_action('EED_Ticket_Selector__process_ticket_selections__before'); |
|
128
|
|
|
$request = LoaderFactory::getLoader()->getShared('EventEspresso\core\services\request\Request'); |
|
129
|
|
|
if($request->isBot()) { |
|
130
|
|
|
wp_safe_redirect( |
|
131
|
|
|
apply_filters( |
|
132
|
|
|
'FHEE__EE_Ticket_Selector__process_ticket_selections__bot_redirect_url', |
|
133
|
|
|
site_url() |
|
134
|
|
|
) |
|
135
|
|
|
); |
|
136
|
|
|
exit(); |
|
137
|
|
|
} |
|
138
|
|
|
// do we have an event id? |
|
139
|
|
View Code Duplication |
if (! EE_Registry::instance()->REQ->is_set('tkt-slctr-event-id')) { |
|
140
|
|
|
// $_POST['tkt-slctr-event-id'] was not set ?!?!?!? |
|
141
|
|
|
EE_Error::add_error( |
|
142
|
|
|
sprintf( |
|
143
|
|
|
__( |
|
144
|
|
|
'An event id was not provided or was not received.%sPlease click the back button on your browser and try again.', |
|
145
|
|
|
'event_espresso' |
|
146
|
|
|
), |
|
147
|
|
|
'<br/>' |
|
148
|
|
|
), |
|
149
|
|
|
__FILE__, |
|
150
|
|
|
__FUNCTION__, |
|
151
|
|
|
__LINE__ |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
//if event id is valid |
|
155
|
|
|
$id = absint(EE_Registry::instance()->REQ->get('tkt-slctr-event-id')); |
|
156
|
|
|
// d( \EE_Registry::instance()->REQ ); |
|
157
|
|
|
self::$_available_spaces = array( |
|
158
|
|
|
'tickets' => array(), |
|
159
|
|
|
'datetimes' => array(), |
|
160
|
|
|
); |
|
161
|
|
|
//we should really only have 1 registration in the works now (ie, no MER) so clear any previous items in the cart. |
|
162
|
|
|
// When MER happens this will probably need to be tweaked, possibly wrapped in a conditional checking for some constant defined in MER etc. |
|
163
|
|
|
EE_Registry::instance()->load_core('Session'); |
|
164
|
|
|
// unless otherwise requested, clear the session |
|
165
|
|
|
if (apply_filters('FHEE__EE_Ticket_Selector__process_ticket_selections__clear_session', true)) { |
|
166
|
|
|
EE_Registry::instance()->SSN->clear_session(__CLASS__, __FUNCTION__); |
|
167
|
|
|
} |
|
168
|
|
|
//d( \EE_Registry::instance()->SSN ); |
|
169
|
|
|
do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
170
|
|
|
// validate/sanitize data |
|
171
|
|
|
$valid = $this->validatePostData($id); |
|
172
|
|
|
//EEH_Debug_Tools::printr( $_REQUEST, '$_REQUEST', __FILE__, __LINE__ ); |
|
173
|
|
|
//EEH_Debug_Tools::printr( $valid, '$valid', __FILE__, __LINE__ ); |
|
174
|
|
|
//EEH_Debug_Tools::printr( $valid[ 'total_tickets' ], 'total_tickets', __FILE__, __LINE__ ); |
|
175
|
|
|
//EEH_Debug_Tools::printr( $valid[ 'max_atndz' ], 'max_atndz', __FILE__, __LINE__ ); |
|
176
|
|
|
//check total tickets ordered vs max number of attendees that can register |
|
177
|
|
|
if ($valid['total_tickets'] > $valid['max_atndz']) { |
|
178
|
|
|
// ordering too many tickets !!! |
|
179
|
|
|
$total_tickets_string = _n( |
|
180
|
|
|
'You have attempted to purchase %s ticket.', |
|
181
|
|
|
'You have attempted to purchase %s tickets.', |
|
182
|
|
|
$valid['total_tickets'], |
|
183
|
|
|
'event_espresso' |
|
184
|
|
|
); |
|
185
|
|
|
$limit_error_1 = sprintf($total_tickets_string, $valid['total_tickets']); |
|
186
|
|
|
// dev only message |
|
187
|
|
|
$max_atndz_string = _n( |
|
188
|
|
|
'The registration limit for this event is %s ticket per registration, therefore the total number of tickets you may purchase at a time can not exceed %s.', |
|
189
|
|
|
'The registration limit for this event is %s tickets per registration, therefore the total number of tickets you may purchase at a time can not exceed %s.', |
|
190
|
|
|
$valid['max_atndz'], |
|
191
|
|
|
'event_espresso' |
|
192
|
|
|
); |
|
193
|
|
|
$limit_error_2 = sprintf($max_atndz_string, $valid['max_atndz'], $valid['max_atndz']); |
|
194
|
|
|
EE_Error::add_error($limit_error_1 . '<br/>' . $limit_error_2, __FILE__, __FUNCTION__, __LINE__); |
|
195
|
|
|
} else { |
|
196
|
|
|
// all data appears to be valid |
|
197
|
|
|
$tckts_slctd = false; |
|
198
|
|
|
$tickets_added = 0; |
|
199
|
|
|
$valid = apply_filters('FHEE__EED_Ticket_Selector__process_ticket_selections__valid_post_data', |
|
200
|
|
|
$valid); |
|
201
|
|
|
if ($valid['total_tickets'] > 0) { |
|
202
|
|
|
// load cart |
|
203
|
|
|
EE_Registry::instance()->load_core('Cart'); |
|
204
|
|
|
// cycle thru the number of data rows sent from the event listing |
|
205
|
|
|
for ($x = 0; $x < $valid['rows']; $x++) { |
|
206
|
|
|
// does this row actually contain a ticket quantity? |
|
207
|
|
|
if (isset($valid['qty'][ $x ]) && $valid['qty'][ $x ] > 0) { |
|
208
|
|
|
// YES we have a ticket quantity |
|
209
|
|
|
$tckts_slctd = true; |
|
210
|
|
|
// d( $valid['ticket_obj'][$x] ); |
|
211
|
|
|
if ($valid['ticket_obj'][ $x ] instanceof EE_Ticket) { |
|
212
|
|
|
// then add ticket to cart |
|
213
|
|
|
$tickets_added += $this->addTicketToCart( |
|
214
|
|
|
$valid['ticket_obj'][ $x ], |
|
215
|
|
|
$valid['qty'][ $x ] |
|
216
|
|
|
); |
|
217
|
|
|
if (EE_Error::has_error()) { |
|
218
|
|
|
break; |
|
219
|
|
|
} |
|
220
|
|
|
} else { |
|
221
|
|
|
// nothing added to cart retrieved |
|
222
|
|
|
EE_Error::add_error( |
|
223
|
|
|
sprintf( |
|
224
|
|
|
__( |
|
225
|
|
|
'A valid ticket could not be retrieved for the event.%sPlease click the back button on your browser and try again.', |
|
226
|
|
|
'event_espresso' |
|
227
|
|
|
), |
|
228
|
|
|
'<br/>' |
|
229
|
|
|
), |
|
230
|
|
|
__FILE__, __FUNCTION__, __LINE__ |
|
231
|
|
|
); |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
do_action( |
|
237
|
|
|
'AHEE__EE_Ticket_Selector__process_ticket_selections__after_tickets_added_to_cart', |
|
238
|
|
|
EE_Registry::instance()->CART, |
|
239
|
|
|
$this |
|
240
|
|
|
); |
|
241
|
|
|
//d( \EE_Registry::instance()->CART ); |
|
242
|
|
|
//die(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< KILL REDIRECT HERE BEFORE CART UPDATE |
|
243
|
|
|
if (apply_filters('FHEE__EED_Ticket_Selector__process_ticket_selections__tckts_slctd', $tckts_slctd)) { |
|
244
|
|
|
if (apply_filters('FHEE__EED_Ticket_Selector__process_ticket_selections__success', $tickets_added)) { |
|
245
|
|
|
do_action( |
|
246
|
|
|
'FHEE__EE_Ticket_Selector__process_ticket_selections__before_redirecting_to_checkout', |
|
247
|
|
|
EE_Registry::instance()->CART, |
|
248
|
|
|
$this |
|
249
|
|
|
); |
|
250
|
|
|
EE_Registry::instance()->CART->recalculate_all_cart_totals(); |
|
251
|
|
|
EE_Registry::instance()->CART->save_cart(false); |
|
252
|
|
|
// exit('KILL REDIRECT AFTER CART UPDATE'); // <<<<<<<< OR HERE TO KILL REDIRECT AFTER CART UPDATE |
|
253
|
|
|
// just return TRUE for registrations being made from admin |
|
254
|
|
|
if (is_admin()) { |
|
255
|
|
|
return true; |
|
256
|
|
|
} |
|
257
|
|
|
EE_Error::get_notices(false, true); |
|
258
|
|
|
EEH_URL::safeRedirectAndExit( |
|
259
|
|
|
apply_filters( |
|
260
|
|
|
'FHEE__EE_Ticket_Selector__process_ticket_selections__success_redirect_url', |
|
261
|
|
|
EE_Registry::instance()->CFG->core->reg_page_url() |
|
262
|
|
|
) |
|
263
|
|
|
); |
|
264
|
|
|
} else { |
|
265
|
|
|
if (! EE_Error::has_error() && ! EE_Error::has_error(true, 'attention')) { |
|
266
|
|
|
// nothing added to cart |
|
267
|
|
|
EE_Error::add_attention(__('No tickets were added for the event', 'event_espresso'), |
|
268
|
|
|
__FILE__, __FUNCTION__, __LINE__); |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
} else { |
|
272
|
|
|
// no ticket quantities were selected |
|
273
|
|
|
EE_Error::add_error(__('You need to select a ticket quantity before you can proceed.', |
|
274
|
|
|
'event_espresso'), __FILE__, __FUNCTION__, __LINE__); |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
//die(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< KILL BEFORE REDIRECT |
|
278
|
|
|
// at this point, just return if registration is being made from admin |
|
279
|
|
|
if (is_admin()) { |
|
280
|
|
|
return false; |
|
281
|
|
|
} |
|
282
|
|
|
if ($valid['return_url']) { |
|
283
|
|
|
EEH_URL::safeRedirectAndExit($valid['return_url']); |
|
284
|
|
|
} |
|
285
|
|
|
if ($id) { |
|
286
|
|
|
EEH_URL::safeRedirectAndExit(get_permalink($id)); |
|
287
|
|
|
} |
|
288
|
|
|
echo EE_Error::get_notices(); |
|
289
|
|
|
return false; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* validate_post_data |
|
295
|
|
|
* |
|
296
|
|
|
* @param int $id |
|
297
|
|
|
* @return array|FALSE |
|
298
|
|
|
* @throws \ReflectionException |
|
299
|
|
|
* @throws InvalidArgumentException |
|
300
|
|
|
* @throws InvalidInterfaceException |
|
301
|
|
|
* @throws InvalidDataTypeException |
|
302
|
|
|
* @throws EE_Error |
|
303
|
|
|
*/ |
|
304
|
|
|
private function validatePostData($id = 0) |
|
305
|
|
|
{ |
|
306
|
|
|
do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
307
|
|
|
if (! $id) { |
|
308
|
|
|
EE_Error::add_error( |
|
309
|
|
|
__('The event id provided was not valid.', 'event_espresso'), |
|
310
|
|
|
__FILE__, |
|
311
|
|
|
__FUNCTION__, |
|
312
|
|
|
__LINE__ |
|
313
|
|
|
); |
|
314
|
|
|
return false; |
|
315
|
|
|
} |
|
316
|
|
|
// start with an empty array() |
|
317
|
|
|
$valid_data = array(); |
|
318
|
|
|
// grab valid id |
|
319
|
|
|
$valid_data['id'] = $id; |
|
320
|
|
|
// array of other form names |
|
321
|
|
|
$inputs_to_clean = array( |
|
322
|
|
|
'event_id' => 'tkt-slctr-event-id', |
|
323
|
|
|
'max_atndz' => 'tkt-slctr-max-atndz-', |
|
324
|
|
|
'rows' => 'tkt-slctr-rows-', |
|
325
|
|
|
'qty' => 'tkt-slctr-qty-', |
|
326
|
|
|
'ticket_id' => 'tkt-slctr-ticket-id-', |
|
327
|
|
|
'return_url' => 'tkt-slctr-return-url-', |
|
328
|
|
|
); |
|
329
|
|
|
// let's track the total number of tickets ordered.' |
|
330
|
|
|
$valid_data['total_tickets'] = 0; |
|
331
|
|
|
// cycle through $inputs_to_clean array |
|
332
|
|
|
foreach ($inputs_to_clean as $what => $input_to_clean) { |
|
333
|
|
|
// check for POST data |
|
334
|
|
|
if (EE_Registry::instance()->REQ->is_set($input_to_clean . $id)) { |
|
335
|
|
|
// grab value |
|
336
|
|
|
$input_value = EE_Registry::instance()->REQ->get($input_to_clean . $id); |
|
337
|
|
|
switch ($what) { |
|
338
|
|
|
// integers |
|
339
|
|
|
case 'event_id': |
|
340
|
|
|
$valid_data[ $what ] = absint($input_value); |
|
341
|
|
|
// get event via the event id we put in the form |
|
342
|
|
|
$valid_data['event'] = EE_Registry::instance() |
|
343
|
|
|
->load_model('Event') |
|
344
|
|
|
->get_one_by_ID($valid_data['event_id']); |
|
345
|
|
|
break; |
|
346
|
|
|
case 'rows': |
|
347
|
|
|
case 'max_atndz': |
|
348
|
|
|
$valid_data[ $what ] = absint($input_value); |
|
349
|
|
|
break; |
|
350
|
|
|
// arrays of integers |
|
351
|
|
|
case 'qty': |
|
352
|
|
|
/** @var array $row_qty */ |
|
353
|
|
|
$row_qty = $input_value; |
|
354
|
|
|
// if qty is coming from a radio button input, then we need to assemble an array of rows |
|
355
|
|
|
if (! is_array($row_qty)) { |
|
356
|
|
|
// get number of rows |
|
357
|
|
|
$rows = EE_Registry::instance()->REQ->is_set('tkt-slctr-rows-' . $id) |
|
358
|
|
|
? absint(EE_Registry::instance()->REQ->get('tkt-slctr-rows-' . $id)) |
|
359
|
|
|
: 1; |
|
360
|
|
|
// explode ints by the dash |
|
361
|
|
|
$row_qty = explode('-', $row_qty); |
|
362
|
|
|
$row = isset($row_qty[0]) ? absint($row_qty[0]) : 1; |
|
363
|
|
|
$qty = isset($row_qty[1]) ? absint($row_qty[1]) : 0; |
|
364
|
|
|
$row_qty = array($row => $qty); |
|
365
|
|
|
for ($x = 1; $x <= $rows; $x++) { |
|
366
|
|
|
if (! isset($row_qty[ $x ])) { |
|
367
|
|
|
$row_qty[ $x ] = 0; |
|
368
|
|
|
} |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
ksort($row_qty); |
|
372
|
|
|
// cycle thru values |
|
373
|
|
|
foreach ($row_qty as $qty) { |
|
374
|
|
|
$qty = absint($qty); |
|
375
|
|
|
// sanitize as integers |
|
376
|
|
|
$valid_data[ $what ][] = $qty; |
|
377
|
|
|
$valid_data['total_tickets'] += $qty; |
|
378
|
|
|
} |
|
379
|
|
|
break; |
|
380
|
|
|
// array of integers |
|
381
|
|
|
case 'ticket_id': |
|
382
|
|
|
$value_array = array(); |
|
383
|
|
|
// cycle thru values |
|
384
|
|
|
foreach ((array) $input_value as $key => $value) { |
|
385
|
|
|
// allow only numbers, letters, spaces, commas and dashes |
|
386
|
|
|
$value_array[ $key ] = wp_strip_all_tags($value); |
|
387
|
|
|
// get ticket via the ticket id we put in the form |
|
388
|
|
|
$ticket_obj = EE_Registry::instance() |
|
389
|
|
|
->load_model('Ticket') |
|
390
|
|
|
->get_one_by_ID($value); |
|
391
|
|
|
$valid_data['ticket_obj'][ $key ] = $ticket_obj; |
|
392
|
|
|
} |
|
393
|
|
|
$valid_data[ $what ] = $value_array; |
|
394
|
|
|
break; |
|
395
|
|
|
case 'return_url' : |
|
396
|
|
|
// grab and sanitize return-url |
|
397
|
|
|
$input_value = esc_url_raw($input_value); |
|
398
|
|
|
// was the request coming from an iframe ? if so, then: |
|
399
|
|
|
if (strpos($input_value, 'event_list=iframe')) { |
|
400
|
|
|
// get anchor fragment |
|
401
|
|
|
$input_value = explode('#', $input_value); |
|
402
|
|
|
$input_value = end($input_value); |
|
403
|
|
|
// use event list url instead, but append anchor |
|
404
|
|
|
$input_value = EEH_Event_View::event_archive_url() . '#' . $input_value; |
|
405
|
|
|
} |
|
406
|
|
|
$valid_data[ $what ] = $input_value; |
|
407
|
|
|
break; |
|
408
|
|
|
} // end switch $what |
|
409
|
|
|
} |
|
410
|
|
|
} // end foreach $inputs_to_clean |
|
411
|
|
|
return $valid_data; |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
|
|
415
|
|
|
/** |
|
416
|
|
|
* adds a ticket to the cart |
|
417
|
|
|
* |
|
418
|
|
|
* @param EE_Ticket $ticket |
|
419
|
|
|
* @param int $qty |
|
420
|
|
|
* @return TRUE on success, FALSE on fail |
|
421
|
|
|
* @throws InvalidArgumentException |
|
422
|
|
|
* @throws InvalidInterfaceException |
|
423
|
|
|
* @throws InvalidDataTypeException |
|
424
|
|
|
* @throws EE_Error |
|
425
|
|
|
*/ |
|
426
|
|
|
private function addTicketToCart(EE_Ticket $ticket = null, $qty = 1) |
|
427
|
|
|
{ |
|
428
|
|
|
do_action('AHEE_log', __FILE__, __FUNCTION__, ''); |
|
429
|
|
|
// get the number of spaces left for this datetime ticket |
|
430
|
|
|
$available_spaces = $this->ticketDatetimeAvailability($ticket); |
|
|
|
|
|
|
431
|
|
|
// compare available spaces against the number of tickets being purchased |
|
432
|
|
|
if ($available_spaces >= $qty) { |
|
433
|
|
|
// allow addons to prevent a ticket from being added to cart |
|
434
|
|
|
if ( |
|
435
|
|
|
! apply_filters( |
|
436
|
|
|
'FHEE__EE_Ticket_Selector___add_ticket_to_cart__allow_add_to_cart', |
|
437
|
|
|
true, |
|
438
|
|
|
$ticket, |
|
439
|
|
|
$qty, |
|
440
|
|
|
$available_spaces |
|
441
|
|
|
) |
|
442
|
|
|
) { |
|
443
|
|
|
return false; |
|
444
|
|
|
} |
|
445
|
|
|
$qty = absint(apply_filters('FHEE__EE_Ticket_Selector___add_ticket_to_cart__ticket_qty', $qty, $ticket)); |
|
446
|
|
|
// add event to cart |
|
447
|
|
|
if (EE_Registry::instance()->CART->add_ticket_to_cart($ticket, $qty)) { |
|
|
|
|
|
|
448
|
|
|
$this->recalculateTicketDatetimeAvailability($ticket, $qty); |
|
|
|
|
|
|
449
|
|
|
return true; |
|
450
|
|
|
} |
|
451
|
|
|
return false; |
|
452
|
|
|
} |
|
453
|
|
|
// tickets can not be purchased but let's find the exact number left |
|
454
|
|
|
// for the last ticket selected PRIOR to subtracting tickets |
|
455
|
|
|
$available_spaces = $this->ticketDatetimeAvailability($ticket, true); |
|
|
|
|
|
|
456
|
|
|
// greedy greedy greedy eh? |
|
457
|
|
|
if ($available_spaces > 0) { |
|
458
|
|
|
if ( |
|
459
|
|
|
apply_filters( |
|
460
|
|
|
'FHEE__EE_Ticket_Selector___add_ticket_to_cart__allow_display_availability_error', |
|
461
|
|
|
true, |
|
462
|
|
|
$ticket, |
|
463
|
|
|
$qty, |
|
464
|
|
|
$available_spaces |
|
465
|
|
|
) |
|
466
|
|
|
) { |
|
467
|
|
|
$this->displayAvailabilityError($available_spaces); |
|
468
|
|
|
} |
|
469
|
|
|
} else { |
|
470
|
|
|
EE_Error::add_error( |
|
471
|
|
|
__( |
|
472
|
|
|
'We\'re sorry, but there are no available spaces left for this event at this particular date and time.', |
|
473
|
|
|
'event_espresso' |
|
474
|
|
|
), |
|
475
|
|
|
__FILE__, __FUNCTION__, __LINE__ |
|
476
|
|
|
); |
|
477
|
|
|
} |
|
478
|
|
|
return false; |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
|
|
482
|
|
|
/** |
|
483
|
|
|
* @param int $available_spaces |
|
484
|
|
|
* @throws InvalidArgumentException |
|
485
|
|
|
* @throws InvalidInterfaceException |
|
486
|
|
|
* @throws InvalidDataTypeException |
|
487
|
|
|
* @throws EE_Error |
|
488
|
|
|
*/ |
|
489
|
|
|
private function displayAvailabilityError($available_spaces = 1) |
|
490
|
|
|
{ |
|
491
|
|
|
// add error messaging - we're using the _n function that will generate |
|
492
|
|
|
// the appropriate singular or plural message based on the number of $available_spaces |
|
493
|
|
|
if (EE_Registry::instance()->CART->all_ticket_quantity_count()) { |
|
494
|
|
|
$msg = sprintf( |
|
495
|
|
|
_n( |
|
496
|
|
|
'We\'re sorry, but there is only %1$s available space left for this event at this particular date and time. Please select a different number (or different combination) of tickets by cancelling the current selection and choosing again, or proceed to registration.', |
|
497
|
|
|
'We\'re sorry, but there are only %1$s available spaces left for this event at this particular date and time. Please select a different number (or different combination) of tickets by cancelling the current selection and choosing again, or proceed to registration.', |
|
498
|
|
|
$available_spaces, |
|
499
|
|
|
'event_espresso' |
|
500
|
|
|
), |
|
501
|
|
|
$available_spaces, |
|
502
|
|
|
'<br />' |
|
503
|
|
|
); |
|
504
|
|
|
} else { |
|
505
|
|
|
$msg = sprintf( |
|
506
|
|
|
_n( |
|
507
|
|
|
'We\'re sorry, but there is only %1$s available space left for this event at this particular date and time. Please select a different number (or different combination) of tickets.', |
|
508
|
|
|
'We\'re sorry, but there are only %1$s available spaces left for this event at this particular date and time. Please select a different number (or different combination) of tickets.', |
|
509
|
|
|
$available_spaces, |
|
510
|
|
|
'event_espresso' |
|
511
|
|
|
), |
|
512
|
|
|
$available_spaces, |
|
513
|
|
|
'<br />' |
|
514
|
|
|
); |
|
515
|
|
|
} |
|
516
|
|
|
EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
|
|
|
|
520
|
|
|
/** |
|
521
|
|
|
* ticketDatetimeAvailability |
|
522
|
|
|
* creates an array of tickets plus all of the datetimes available to each ticket |
|
523
|
|
|
* and tracks the spaces remaining for each of those datetimes |
|
524
|
|
|
* |
|
525
|
|
|
* @param EE_Ticket $ticket - selected ticket |
|
526
|
|
|
* @param bool $get_original_ticket_spaces |
|
527
|
|
|
* @return int |
|
528
|
|
|
* @throws InvalidArgumentException |
|
529
|
|
|
* @throws InvalidInterfaceException |
|
530
|
|
|
* @throws InvalidDataTypeException |
|
531
|
|
|
* @throws EE_Error |
|
532
|
|
|
*/ |
|
533
|
|
|
private function ticketDatetimeAvailability(EE_Ticket $ticket, $get_original_ticket_spaces = false) |
|
534
|
|
|
{ |
|
535
|
|
|
// if the $_available_spaces array has not been set up yet... |
|
536
|
|
|
if (! isset(self::$_available_spaces['tickets'][ $ticket->ID() ])) { |
|
537
|
|
|
$this->setInitialTicketDatetimeAvailability($ticket); |
|
538
|
|
|
} |
|
539
|
|
|
$available_spaces = $ticket->qty() - $ticket->sold(); |
|
540
|
|
|
if (isset(self::$_available_spaces['tickets'][ $ticket->ID() ])) { |
|
541
|
|
|
// loop thru tickets, which will ALSO include individual ticket records AND a total |
|
542
|
|
|
foreach (self::$_available_spaces['tickets'][ $ticket->ID() ] as $DTD_ID => $spaces) { |
|
543
|
|
|
// if we want the original datetime availability BEFORE we started subtracting tickets ? |
|
544
|
|
|
if ($get_original_ticket_spaces) { |
|
545
|
|
|
// then grab the available spaces from the "tickets" array |
|
546
|
|
|
// and compare with the above to get the lowest number |
|
547
|
|
|
$available_spaces = min( |
|
548
|
|
|
$available_spaces, |
|
549
|
|
|
self::$_available_spaces['tickets'][ $ticket->ID() ][ $DTD_ID ] |
|
550
|
|
|
); |
|
551
|
|
|
} else { |
|
552
|
|
|
// we want the updated ticket availability as stored in the "datetimes" array |
|
553
|
|
|
$available_spaces = min($available_spaces, self::$_available_spaces['datetimes'][ $DTD_ID ]); |
|
554
|
|
|
} |
|
555
|
|
|
} |
|
556
|
|
|
} |
|
557
|
|
|
return $available_spaces; |
|
558
|
|
|
} |
|
559
|
|
|
|
|
560
|
|
|
|
|
561
|
|
|
/** |
|
562
|
|
|
* @param EE_Ticket $ticket |
|
563
|
|
|
* @return void |
|
564
|
|
|
* @throws InvalidArgumentException |
|
565
|
|
|
* @throws InvalidInterfaceException |
|
566
|
|
|
* @throws InvalidDataTypeException |
|
567
|
|
|
* @throws EE_Error |
|
568
|
|
|
*/ |
|
569
|
|
|
private function setInitialTicketDatetimeAvailability(EE_Ticket $ticket) |
|
570
|
|
|
{ |
|
571
|
|
|
// first, get all of the datetimes that are available to this ticket |
|
572
|
|
|
$datetimes = $ticket->get_many_related( |
|
573
|
|
|
'Datetime', |
|
574
|
|
|
array( |
|
575
|
|
|
array( |
|
576
|
|
|
'DTT_EVT_end' => array( |
|
577
|
|
|
'>=', |
|
578
|
|
|
EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'), |
|
579
|
|
|
), |
|
580
|
|
|
), |
|
581
|
|
|
'order_by' => array('DTT_EVT_start' => 'ASC'), |
|
582
|
|
|
) |
|
583
|
|
|
); |
|
584
|
|
|
if (! empty($datetimes)) { |
|
585
|
|
|
// now loop thru all of the datetimes |
|
586
|
|
|
foreach ($datetimes as $datetime) { |
|
587
|
|
|
if ($datetime instanceof EE_Datetime) { |
|
588
|
|
|
// the number of spaces available for the datetime without considering individual ticket quantities |
|
589
|
|
|
$spaces_remaining = $datetime->spaces_remaining(); |
|
590
|
|
|
// save the total available spaces ( the lesser of the ticket qty minus the number of tickets sold |
|
591
|
|
|
// or the datetime spaces remaining) to this ticket using the datetime ID as the key |
|
592
|
|
|
self::$_available_spaces['tickets'][ $ticket->ID() ][ $datetime->ID() ] = min( |
|
593
|
|
|
$ticket->qty() - $ticket->sold(), |
|
594
|
|
|
$spaces_remaining |
|
595
|
|
|
); |
|
596
|
|
|
// if the remaining spaces for this datetime is already set, |
|
597
|
|
|
// then compare that against the datetime spaces remaining, and take the lowest number, |
|
598
|
|
|
// else just take the datetime spaces remaining, and assign to the datetimes array |
|
599
|
|
|
self::$_available_spaces['datetimes'][ $datetime->ID() ] = isset( |
|
600
|
|
|
self::$_available_spaces['datetimes'][ $datetime->ID() ] |
|
601
|
|
|
) |
|
602
|
|
|
? min(self::$_available_spaces['datetimes'][ $datetime->ID() ], $spaces_remaining) |
|
603
|
|
|
: $spaces_remaining; |
|
604
|
|
|
} |
|
605
|
|
|
} |
|
606
|
|
|
} |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
|
|
|
|
610
|
|
|
/** |
|
611
|
|
|
* @param EE_Ticket $ticket |
|
612
|
|
|
* @param int $qty |
|
613
|
|
|
* @return void |
|
614
|
|
|
* @throws EE_Error |
|
615
|
|
|
*/ |
|
616
|
|
|
private function recalculateTicketDatetimeAvailability(EE_Ticket $ticket, $qty = 0) |
|
617
|
|
|
{ |
|
618
|
|
|
if (isset(self::$_available_spaces['tickets'][ $ticket->ID() ])) { |
|
619
|
|
|
// loop thru tickets, which will ALSO include individual ticket records AND a total |
|
620
|
|
|
foreach (self::$_available_spaces['tickets'][ $ticket->ID() ] as $DTD_ID => $spaces) { |
|
621
|
|
|
// subtract the qty of selected tickets from each datetime's available spaces this ticket has access to, |
|
622
|
|
|
self::$_available_spaces['datetimes'][ $DTD_ID ] -= $qty; |
|
623
|
|
|
} |
|
624
|
|
|
} |
|
625
|
|
|
} |
|
626
|
|
|
} |
|
627
|
|
|
// End of file ProcessTicketSelector.php |
|
628
|
|
|
// Location: /ProcessTicketSelector.php |
|
629
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):