1
|
|
|
<?php |
2
|
|
|
namespace EventEspresso\core\libraries\rest_api\calculations; |
3
|
|
|
|
4
|
|
|
use EventEspresso\core\libraries\rest_api\calculations\Base as Calculations_Base; |
5
|
|
|
use EventEspresso\core\libraries\rest_api\controllers\model\Base; |
|
|
|
|
6
|
|
|
use EventEspresso\core\libraries\rest_api\RestException; |
7
|
|
|
use EEM_Event; |
8
|
|
|
use EE_Event; |
9
|
|
|
use EE_Error; |
10
|
|
|
use EEM_Registration; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Event_Calculations |
14
|
|
|
* Description here |
15
|
|
|
* |
16
|
|
|
* @package Event Espresso |
17
|
|
|
* @subpackage |
18
|
|
|
* @author Mike Nelson |
19
|
|
|
* @since $VID:$ |
20
|
|
|
*/ |
21
|
|
|
if (! defined('EVENT_ESPRESSO_VERSION')) { |
22
|
|
|
exit('No direct script access allowed'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class Event extends Calculations_Base |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Calculates the total spaces on the event (not subtracting sales, but taking |
32
|
|
|
* sales into account; so this is the optimum sales that CAN still be achieved) |
33
|
|
|
* See EE_Event::total_available_spaces( true ); |
34
|
|
|
* |
35
|
|
|
* @param array $wpdb_row |
36
|
|
|
* @param \WP_REST_Request $request |
37
|
|
|
* @param Base $controller |
38
|
|
|
* @return int |
39
|
|
|
* @throws EE_Error |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public static function optimumSalesAtStart($wpdb_row, $request, $controller) |
42
|
|
|
{ |
43
|
|
|
$event_obj = null; |
44
|
|
|
if (Event::wpdbRowHasEventId($wpdb_row)) { |
45
|
|
|
$event_obj = EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
46
|
|
|
} |
47
|
|
|
if ($event_obj instanceof EE_Event) { |
48
|
|
|
return $event_obj->total_available_spaces(); |
49
|
|
|
} |
50
|
|
|
throw new EE_Error( |
51
|
|
|
sprintf( |
52
|
|
|
__( |
53
|
|
|
// @codingStandardsIgnoreStart |
54
|
|
|
'Cannot calculate optimum_sales_at_start because the event with ID %1$s (from database row %2$s) was not found', |
55
|
|
|
// @codingStandardsIgnoreEnd |
56
|
|
|
'event_espresso' |
57
|
|
|
), |
58
|
|
|
$wpdb_row['Event_CPT.ID'], |
59
|
|
|
print_r($wpdb_row, true) |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Calculates the total spaces on the event (ignoring all sales; so this is the optimum |
68
|
|
|
* sales that COULD have been achieved) |
69
|
|
|
* See EE_Event::total_available_spaces( true ); |
70
|
|
|
* |
71
|
|
|
* @param array $wpdb_row |
72
|
|
|
* @param \WP_REST_Request $request |
73
|
|
|
* @param Base $controller |
74
|
|
|
* @return int |
75
|
|
|
* @throws EE_Error |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public static function optimumSalesNow($wpdb_row, $request, $controller) |
78
|
|
|
{ |
79
|
|
|
$event_obj = null; |
80
|
|
|
if (Event::wpdbRowHasEventId($wpdb_row)) { |
81
|
|
|
$event_obj = EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
82
|
|
|
} |
83
|
|
|
if ($event_obj instanceof EE_Event) { |
84
|
|
|
return $event_obj->total_available_spaces(true); |
85
|
|
|
} |
86
|
|
|
throw new EE_Error( |
87
|
|
|
sprintf( |
88
|
|
|
__( |
89
|
|
|
// @codingStandardsIgnoreStart |
90
|
|
|
'Cannot calculate optimum_sales_now because the event with ID %1$s (from database row %2$s) was not found', |
91
|
|
|
// @codingStandardsIgnoreEnd |
92
|
|
|
'event_espresso' |
93
|
|
|
), |
94
|
|
|
$wpdb_row['Event_CPT.ID'], |
95
|
|
|
print_r($wpdb_row, true) |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Like optimum_sales_now, but minus total sales so far. |
104
|
|
|
* See EE_Event::spaces_remaining_for_sale( true ); |
105
|
|
|
* |
106
|
|
|
* @param array $wpdb_row |
107
|
|
|
* @param \WP_REST_Request $request |
108
|
|
|
* @param Base $controller |
109
|
|
|
* @return int |
110
|
|
|
* @throws EE_Error |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
public static function spacesRemaining($wpdb_row, $request, $controller) |
113
|
|
|
{ |
114
|
|
|
$event_obj = null; |
115
|
|
|
if (Event::wpdbRowHasEventId($wpdb_row)) { |
116
|
|
|
$event_obj = EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']); |
117
|
|
|
} |
118
|
|
|
if ($event_obj instanceof EE_Event) { |
119
|
|
|
return $event_obj->spaces_remaining_for_sale(); |
120
|
|
|
} |
121
|
|
|
throw new EE_Error( |
122
|
|
|
sprintf( |
123
|
|
|
__( |
124
|
|
|
// @codingStandardsIgnoreStart |
125
|
|
|
'Cannot calculate spaces_remaining because the event with ID %1$s (from database row %2$s) was not found', |
126
|
|
|
// @codingStandardsIgnoreEnd |
127
|
|
|
'event_espresso' |
128
|
|
|
), |
129
|
|
|
$wpdb_row['Event_CPT.ID'], |
130
|
|
|
print_r($wpdb_row, true) |
131
|
|
|
) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Counts the number of approved registrations for this event (regardless |
139
|
|
|
* of how many datetimes each registrations' ticket purchase is for) |
140
|
|
|
* |
141
|
|
|
* @param array $wpdb_row |
142
|
|
|
* @param \WP_REST_Request $request |
143
|
|
|
* @param Base $controller |
144
|
|
|
* @return int |
145
|
|
|
* @throws EE_Error |
146
|
|
|
*/ |
147
|
|
View Code Duplication |
public static function spotsTaken($wpdb_row, $request, $controller) |
148
|
|
|
{ |
149
|
|
|
if (! Event::wpdbRowHasEventId($wpdb_row)) { |
150
|
|
|
throw new EE_Error( |
151
|
|
|
sprintf( |
152
|
|
|
__( |
153
|
|
|
// @codingStandardsIgnoreStart |
154
|
|
|
'Cannot calculate spots_taken because the database row %1$s does not have a valid entry for "Event_CPT.ID"', |
155
|
|
|
// @codingStandardsIgnoreEnd |
156
|
|
|
'event_espresso' |
157
|
|
|
), |
158
|
|
|
print_r($wpdb_row, true) |
159
|
|
|
) |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
return EEM_Registration::instance()->count( |
163
|
|
|
array( |
164
|
|
|
array( |
165
|
|
|
'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
166
|
|
|
'STS_ID' => EEM_Registration::status_id_approved, |
167
|
|
|
), |
168
|
|
|
), |
169
|
|
|
'REG_ID', |
170
|
|
|
true |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Counts the number of pending-payment registrations for this event (regardless |
178
|
|
|
* of how many datetimes each registrations' ticket purchase is for) |
179
|
|
|
* |
180
|
|
|
* @param array $wpdb_row |
181
|
|
|
* @param \WP_REST_Request $request |
182
|
|
|
* @param Base $controller |
183
|
|
|
* @return int |
184
|
|
|
* @throws EE_Error |
185
|
|
|
* @throws RestException |
186
|
|
|
*/ |
187
|
|
View Code Duplication |
public static function spotsTakenPendingPayment($wpdb_row, $request, $controller) |
188
|
|
|
{ |
189
|
|
|
if (! Event::wpdbRowHasEventId($wpdb_row)) { |
190
|
|
|
throw new EE_Error( |
191
|
|
|
sprintf( |
192
|
|
|
__( |
193
|
|
|
// @codingStandardsIgnoreStart |
194
|
|
|
'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"', |
195
|
|
|
// @codingStandardsIgnoreEnd |
196
|
|
|
'event_espresso' |
197
|
|
|
), |
198
|
|
|
print_r($wpdb_row, true) |
199
|
|
|
) |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
self::verifyCurrentUserCan('ee_read_registrations', 'spots_taken_pending_payment'); |
203
|
|
|
return EEM_Registration::instance()->count( |
204
|
|
|
array( |
205
|
|
|
array( |
206
|
|
|
'EVT_ID' => $wpdb_row['Event_CPT.ID'], |
207
|
|
|
'STS_ID' => EEM_Registration::status_id_pending_payment, |
208
|
|
|
), |
209
|
|
|
), |
210
|
|
|
'REG_ID', |
211
|
|
|
true |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Counts all the registrations who have checked into one of this events' datetimes |
219
|
|
|
* See EE_Event::total_available_spaces( false ); |
220
|
|
|
* |
221
|
|
|
* @param array $wpdb_row |
222
|
|
|
* @param \WP_REST_Request $request |
223
|
|
|
* @param Base $controller |
224
|
|
|
* @return int|null if permission denied |
225
|
|
|
* @throws EE_Error |
226
|
|
|
* @throws RestException |
227
|
|
|
*/ |
228
|
|
View Code Duplication |
public static function registrationsCheckedInCount($wpdb_row, $request, $controller) |
229
|
|
|
{ |
230
|
|
|
if (! Event::wpdbRowHasEventId($wpdb_row)) { |
231
|
|
|
throw new EE_Error( |
232
|
|
|
sprintf( |
233
|
|
|
__( |
234
|
|
|
// @codingStandardsIgnoreStart |
235
|
|
|
'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
236
|
|
|
// @codingStandardsIgnoreEnd |
237
|
|
|
'event_espresso' |
238
|
|
|
), |
239
|
|
|
print_r($wpdb_row, true) |
240
|
|
|
) |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
self::verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_in_count'); |
244
|
|
|
return EEM_Registration::instance()->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], true); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Counts all the registrations who have checked out of one of this events' datetimes |
251
|
|
|
* See EE_Event::total_available_spaces( false ); |
252
|
|
|
* |
253
|
|
|
* @param array $wpdb_row |
254
|
|
|
* @param \WP_REST_Request $request |
255
|
|
|
* @param Base $controller |
256
|
|
|
* @return int |
257
|
|
|
* @throws EE_Error |
258
|
|
|
* @throws RestException |
259
|
|
|
*/ |
260
|
|
View Code Duplication |
public static function registrationsCheckedOutCount($wpdb_row, $request, $controller) |
261
|
|
|
{ |
262
|
|
|
if (! Event::wpdbRowHasEventId($wpdb_row)) { |
263
|
|
|
throw new EE_Error( |
264
|
|
|
sprintf( |
265
|
|
|
__( |
266
|
|
|
// @codingStandardsIgnoreStart |
267
|
|
|
'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"', |
268
|
|
|
// @codingStandardsIgnoreEnd |
269
|
|
|
'event_espresso' |
270
|
|
|
), |
271
|
|
|
print_r($wpdb_row, true) |
272
|
|
|
) |
273
|
|
|
); |
274
|
|
|
} |
275
|
|
|
self::verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_out_count'); |
276
|
|
|
return EEM_Registration::instance()->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], false); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Gets the thumbnail image |
283
|
|
|
* |
284
|
|
|
* @param array $wpdb_row |
285
|
|
|
* @param \WP_REST_Request $request |
286
|
|
|
* @param Base $controller |
287
|
|
|
* @return array |
288
|
|
|
* @throws EE_Error |
289
|
|
|
*/ |
290
|
|
|
public static function imageThumbnail($wpdb_row, $request, $controller) |
291
|
|
|
{ |
292
|
|
|
return self::calculateImageData($wpdb_row, 'thumbnail'); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
|
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Gets the medium image |
299
|
|
|
* |
300
|
|
|
* @param array $wpdb_row |
301
|
|
|
* @param \WP_REST_Request $request |
302
|
|
|
* @param Base $controller |
303
|
|
|
* @return array |
304
|
|
|
* @throws EE_Error |
305
|
|
|
*/ |
306
|
|
|
public static function imageMedium($wpdb_row, $request, $controller) |
307
|
|
|
{ |
308
|
|
|
return self::calculateImageData($wpdb_row, 'medium'); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
|
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Gets the medium-large image |
315
|
|
|
* |
316
|
|
|
* @param array $wpdb_row |
317
|
|
|
* @param \WP_REST_Request $request |
318
|
|
|
* @param Base $controller |
319
|
|
|
* @return array |
320
|
|
|
* @throws EE_Error |
321
|
|
|
*/ |
322
|
|
|
public static function imageMediumLarge($wpdb_row, $request, $controller) |
323
|
|
|
{ |
324
|
|
|
return self::calculateImageData($wpdb_row, 'medium_large'); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
|
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Gets the large image |
331
|
|
|
* |
332
|
|
|
* @param array $wpdb_row |
333
|
|
|
* @param \WP_REST_Request $request |
334
|
|
|
* @param Base $controller |
335
|
|
|
* @return array |
336
|
|
|
* @throws EE_Error |
337
|
|
|
*/ |
338
|
|
|
public static function imageLarge($wpdb_row, $request, $controller) |
339
|
|
|
{ |
340
|
|
|
return self::calculateImageData($wpdb_row, 'large'); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
|
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Gets the post-thumbnail image |
347
|
|
|
* |
348
|
|
|
* @param array $wpdb_row |
349
|
|
|
* @param \WP_REST_Request $request |
350
|
|
|
* @param Base $controller |
351
|
|
|
* @return array |
352
|
|
|
* @throws EE_Error |
353
|
|
|
*/ |
354
|
|
|
public static function imagePostThumbnail($wpdb_row, $request, $controller) |
355
|
|
|
{ |
356
|
|
|
return self::calculateImageData($wpdb_row, 'post-thumbnail'); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
|
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Gets the full size image |
363
|
|
|
* |
364
|
|
|
* @param array $wpdb_row |
365
|
|
|
* @param \WP_REST_Request $request |
366
|
|
|
* @param Base $controller |
367
|
|
|
* @return array |
368
|
|
|
* @throws EE_Error |
369
|
|
|
*/ |
370
|
|
|
public static function imageFull($wpdb_row, $request, $controller) |
371
|
|
|
{ |
372
|
|
|
return self::calculateImageData($wpdb_row, 'full'); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
|
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Gets image specs and formats them for the display in the API, |
379
|
|
|
* according to the image size requested |
380
|
|
|
* |
381
|
|
|
* @param array $wpdb_row |
382
|
|
|
* @param string $image_size one of these: thumbnail, medium, medium_large, large, post-thumbnail, full |
383
|
|
|
* @return array|false if no such image exists. If array it will have keys 'url', 'width', 'height' and 'original' |
384
|
|
|
* @throws EE_Error |
385
|
|
|
*/ |
386
|
|
|
protected static function calculateImageData($wpdb_row, $image_size) |
387
|
|
|
{ |
388
|
|
|
if (! Event::wpdbRowHasEventId($wpdb_row)) { |
389
|
|
|
throw new EE_Error( |
390
|
|
|
sprintf( |
391
|
|
|
__( |
392
|
|
|
// @codingStandardsIgnoreStart |
393
|
|
|
'Cannot calculate image because the database row %1$s does not have an entry for "Event_CPT.ID"', |
394
|
|
|
// @codingStandardsIgnoreEnd |
395
|
|
|
'event_espresso' |
396
|
|
|
), |
397
|
|
|
print_r($wpdb_row, true) |
398
|
|
|
) |
399
|
|
|
); |
400
|
|
|
} |
401
|
|
|
$EVT_ID = $wpdb_row['Event_CPT.ID']; |
402
|
|
|
$attachment_id = get_post_thumbnail_id($EVT_ID); |
403
|
|
|
$data = wp_get_attachment_image_src($attachment_id, $image_size); |
404
|
|
|
if (! $data) { |
405
|
|
|
return null; |
406
|
|
|
} |
407
|
|
|
$generated = true; |
408
|
|
|
if (isset($data[3])) { |
409
|
|
|
$generated = $data[3]; |
410
|
|
|
} |
411
|
|
|
return array( |
412
|
|
|
'url' => $data[0], |
413
|
|
|
'width' => $data[1], |
414
|
|
|
'height' => $data[2], |
415
|
|
|
'generated' => $generated, |
416
|
|
|
); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
|
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Returns true if the array of data contains 'Event_CPT.ID'. False otherwise |
423
|
|
|
* @param array $wpdb_row |
424
|
|
|
* @return bool |
425
|
|
|
*/ |
426
|
|
|
protected static function wpdbRowHasEventId($wpdb_row) |
427
|
|
|
{ |
428
|
|
|
return (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID'])); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: