Completed
Branch BUG-10976-calculate-event-spac... (8081c7)
by
unknown
95:50 queued 85:12
created

Event::imagePostThumbnail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, EventEspresso\core\libra...t_api\calculations\Base.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
        if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID'])) {
44
            $event_obj = EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']);
45
        } else {
46
            $event_obj = null;
47
        }
48
        if ($event_obj instanceof EE_Event) {
49
            return $event_obj->total_available_spaces();
50
        } else {
51
            throw new EE_Error(
52
                sprintf(
53
                    __(
54
                        // @codingStandardsIgnoreStart
55
                        'Cannot calculate optimum_sales_at_start because the event with ID %1$s (from database row %2$s) was not found',
56
                        // @codingStandardsIgnoreEnd
57
                        'event_espresso'
58
                    ),
59
                    $wpdb_row['Event_CPT.ID'],
60
                    print_r($wpdb_row, true)
61
                )
62
            );
63
        }
64
    }
65
66
67
68
    /**
69
     * Calculates the total spaces on the event (ignoring all sales; so this is the optimum
70
     * sales that COULD have been achieved)
71
     * See EE_Event::total_available_spaces( true );
72
     *
73
     * @param array            $wpdb_row
74
     * @param \WP_REST_Request $request
75
     * @param Base             $controller
76
     * @return int
77
     * @throws EE_Error
78
     */
79 View Code Duplication
    public static function optimumSalesNow($wpdb_row, $request, $controller)
80
    {
81
        if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID'])) {
82
            $event_obj = EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']);
83
        } else {
84
            $event_obj = null;
85
        }
86
        if ($event_obj instanceof EE_Event) {
87
            return $event_obj->total_available_spaces(true);
88
        } else {
89
            throw new EE_Error(
90
                sprintf(
91
                    __(
92
                        // @codingStandardsIgnoreStart
93
                        'Cannot calculate optimum_sales_now because the event with ID %1$s (from database row %2$s) was not found',
94
                        // @codingStandardsIgnoreEnd
95
                        'event_espresso'
96
                    ),
97
                    $wpdb_row['Event_CPT.ID'],
98
                    print_r($wpdb_row, true)
99
                )
100
            );
101
        }
102
    }
103
104
105
106
    /**
107
     * Like optimum_sales_now, but minus total sales so far.
108
     * See EE_Event::spaces_remaining_for_sale( true );
109
     *
110
     * @param array            $wpdb_row
111
     * @param \WP_REST_Request $request
112
     * @param Base             $controller
113
     * @return int
114
     * @throws EE_Error
115
     */
116 View Code Duplication
    public static function spacesRemaining($wpdb_row, $request, $controller)
117
    {
118
        if (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID'])) {
119
            $event_obj = EEM_Event::instance()->get_one_by_ID($wpdb_row['Event_CPT.ID']);
120
        } else {
121
            $event_obj = null;
122
        }
123
        if ($event_obj instanceof EE_Event) {
124
            return $event_obj->spaces_remaining_for_sale();
125
        } else {
126
            throw new EE_Error(
127
                sprintf(
128
                    __(
129
                        // @codingStandardsIgnoreStart
130
                        'Cannot calculate spaces_remaining because the event with ID %1$s (from database row %2$s) was not found',
131
                        // @codingStandardsIgnoreEnd
132
                        'event_espresso'
133
                    ),
134
                    $wpdb_row['Event_CPT.ID'],
135
                    print_r($wpdb_row, true)
136
                )
137
            );
138
        }
139
    }
140
141
142
143
    /**
144
     * Counts the number of approved registrations for this event (regardless
145
     * of how many datetimes each registrations' ticket purchase is for)
146
     *
147
     * @param array            $wpdb_row
148
     * @param \WP_REST_Request $request
149
     * @param Base             $controller
150
     * @return int
151
     * @throws EE_Error
152
     */
153
    public static function spotsTaken($wpdb_row, $request, $controller)
154
    {
155
        if (! (is_array($wpdb_row) && isset($wpdb_row['Event_CPT.ID']) && absint($wpdb_row['Event_CPT.ID']))) {
156
            throw new EE_Error(
157
                sprintf(
158
                    __(
159
                        // @codingStandardsIgnoreStart
160
                        'Cannot calculate spots_taken because the database row %1$s does not have a valid entry for "Event_CPT.ID"',
161
                        // @codingStandardsIgnoreEnd
162
                        'event_espresso'
163
                    ),
164
                    print_r($wpdb_row, true)
165
                )
166
            );
167
        }
168
        return EEM_Registration::instance()->count(
169
            array(
170
                array(
171
                    'EVT_ID' => $wpdb_row['Event_CPT.ID'],
172
                    'STS_ID' => EEM_Registration::status_id_approved,
173
                ),
174
            ),
175
            'REG_ID',
176
            true
177
        );
178
    }
179
180
181
182
    /**
183
     * Counts the number of pending-payment registrations for this event (regardless
184
     * of how many datetimes each registrations' ticket purchase is for)
185
     *
186
     * @param array            $wpdb_row
187
     * @param \WP_REST_Request $request
188
     * @param Base             $controller
189
     * @return int
190
     * @throws EE_Error
191
     * @throws RestException
192
     */
193 View Code Duplication
    public static function spotsTakenPendingPayment($wpdb_row, $request, $controller)
194
    {
195
        if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) {
196
            throw new EE_Error(
197
                sprintf(
198
                    __(
199
                        // @codingStandardsIgnoreStart
200
                        'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Event_CPT.ID"',
201
                        // @codingStandardsIgnoreEnd
202
                        'event_espresso'
203
                    ),
204
                    print_r($wpdb_row, true)
205
                )
206
            );
207
        }
208
        self::verifyCurrentUserCan('ee_read_registrations', 'spots_taken_pending_payment');
209
        return EEM_Registration::instance()->count(
210
            array(
211
                array(
212
                    'EVT_ID' => $wpdb_row['Event_CPT.ID'],
213
                    'STS_ID' => EEM_Registration::status_id_pending_payment,
214
                ),
215
            ),
216
            'REG_ID',
217
            true
218
        );
219
    }
220
221
222
223
    /**
224
     * Counts all the registrations who have checked into one of this events' datetimes
225
     * See EE_Event::total_available_spaces( false );
226
     *
227
     * @param array            $wpdb_row
228
     * @param \WP_REST_Request $request
229
     * @param Base             $controller
230
     * @return int|null if permission denied
231
     * @throws EE_Error
232
     * @throws RestException
233
     */
234 View Code Duplication
    public static function registrationsCheckedInCount($wpdb_row, $request, $controller)
235
    {
236
        if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) {
237
            throw new EE_Error(
238
                sprintf(
239
                    __(
240
                        // @codingStandardsIgnoreStart
241
                        'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Event_CPT.ID"',
242
                        // @codingStandardsIgnoreEnd
243
                        'event_espresso'
244
                    ),
245
                    print_r($wpdb_row, true)
246
                )
247
            );
248
        }
249
        self::verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_in_count');
250
        return EEM_Registration::instance()->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], true);
251
    }
252
253
254
255
    /**
256
     * Counts all the registrations who have checked out of one of this events' datetimes
257
     * See EE_Event::total_available_spaces( false );
258
     *
259
     * @param array            $wpdb_row
260
     * @param \WP_REST_Request $request
261
     * @param Base             $controller
262
     * @return int
263
     * @throws EE_Error
264
     * @throws RestException
265
     */
266 View Code Duplication
    public static function registrationsCheckedOutCount($wpdb_row, $request, $controller)
267
    {
268
        if (! is_array($wpdb_row) || ! isset($wpdb_row['Event_CPT.ID'])) {
269
            throw new EE_Error(
270
                sprintf(
271
                    __(
272
                        // @codingStandardsIgnoreStart
273
                        'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Event_CPT.ID"',
274
                        // @codingStandardsIgnoreEnd
275
                        'event_espresso'
276
                    ),
277
                    print_r($wpdb_row, true)
278
                )
279
            );
280
        }
281
        self::verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_out_count');
282
        return EEM_Registration::instance()->count_registrations_checked_into_event($wpdb_row['Event_CPT.ID'], false);
283
    }
284
285
286
287
    /**
288
     * Gets the thumbnail image
289
     *
290
     * @param array            $wpdb_row
291
     * @param \WP_REST_Request $request
292
     * @param Base             $controller
293
     * @return array
294
     */
295
    public static function imageThumbnail($wpdb_row, $request, $controller)
296
    {
297
        return self::calculateImageData($wpdb_row['Event_CPT.ID'], 'thumbnail');
298
    }
299
300
301
302
    /**
303
     * Gets the medium image
304
     *
305
     * @param array            $wpdb_row
306
     * @param \WP_REST_Request $request
307
     * @param Base             $controller
308
     * @return array
309
     */
310
    public static function imageMedium($wpdb_row, $request, $controller)
311
    {
312
        return self::calculateImageData($wpdb_row['Event_CPT.ID'], 'medium');
313
    }
314
315
316
317
    /**
318
     * Gets the medium-large image
319
     *
320
     * @param array            $wpdb_row
321
     * @param \WP_REST_Request $request
322
     * @param Base             $controller
323
     * @return array
324
     */
325
    public static function imageMediumLarge($wpdb_row, $request, $controller)
326
    {
327
        return self::calculateImageData($wpdb_row['Event_CPT.ID'], 'medium_large');
328
    }
329
330
331
332
    /**
333
     * Gets the large image
334
     *
335
     * @param array            $wpdb_row
336
     * @param \WP_REST_Request $request
337
     * @param Base             $controller
338
     * @return array
339
     */
340
    public static function imageLarge($wpdb_row, $request, $controller)
341
    {
342
        return self::calculateImageData($wpdb_row['Event_CPT.ID'], 'large');
343
    }
344
345
346
347
    /**
348
     * Gets the post-thumbnail image
349
     *
350
     * @param array            $wpdb_row
351
     * @param \WP_REST_Request $request
352
     * @param Base             $controller
353
     * @return array
354
     */
355
    public static function imagePostThumbnail($wpdb_row, $request, $controller)
356
    {
357
        return self::calculateImageData($wpdb_row['Event_CPT.ID'], 'post-thumbnail');
358
    }
359
360
361
362
    /**
363
     * Gets the full size image
364
     *
365
     * @param array            $wpdb_row
366
     * @param \WP_REST_Request $request
367
     * @param Base             $controller
368
     * @return array
369
     */
370
    public static function imageFull($wpdb_row, $request, $controller)
371
    {
372
        return self::calculateImageData($wpdb_row['Event_CPT.ID'], '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 int    $EVT_ID
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
     */
385
    protected static function calculateImageData($EVT_ID, $image_size)
386
    {
387
        $attachment_id = get_post_thumbnail_id($EVT_ID);
388
        $data = wp_get_attachment_image_src($attachment_id, $image_size);
389
        if (! $data) {
390
            return null;
391
        }
392
        if (isset($data[3])) {
393
            $generated = $data[3];
394
        } else {
395
            $generated = true;
396
        }
397
        return array(
398
            'url'       => $data[0],
399
            'width'     => $data[1],
400
            'height'    => $data[2],
401
            'generated' => $generated,
402
        );
403
    }
404
}
405