Completed
Branch FET/ticket-visibility (37a2a1)
by
unknown
64:29 queued 51:59
created

EEM_Ticket::ticketVisibility()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Ticket Model
5
 *
6
 * @package            Event Espresso
7
 * @subpackage         includes/models/EEM_Ticket.model.php
8
 * @author             Darren Ethier
9
 */
10
class EEM_Ticket extends EEM_Soft_Delete_Base
11
{
12
13
    /**
14
     * the following constants define where tickets can be viewed throughout the UI
15
     *
16
     *  TICKET_VISIBILITY_PUBLIC        - displayed basically anywhere
17
     *  TICKET_VISIBILITY_MEMBERS_ONLY  - displayed to any logged in user
18
     *  TICKET_VISIBILITY_ADMINS_ONLY   - displayed to any logged in user that is an admin
19
     *  TICKET_VISIBILITY_ADMIN_UI_ONLY - only displayed in the admin, never publicly
20
     *  TICKET_VISIBILITY_NONE          - will not be displayed anywhere
21
     */
22
    public const TICKET_VISIBILITY_PUBLIC        = 100;
23
24
    public const TICKET_VISIBILITY_MEMBERS_ONLY  = 200;
25
26
    public const TICKET_VISIBILITY_ADMINS_ONLY   = 300;
27
28
    public const TICKET_VISIBILITY_ADMIN_UI_ONLY = 400;
29
30
    public const TICKET_VISIBILITY_NONE          = 500;
31
32
33
    /**
34
     * defines where tickets can be viewed throughout the UI
35
     *
36
     * @var array
37
     */
38
    private $ticket_visibility;
39
40
    /**
41
     * private instance of the EEM_Ticket object
42
     *
43
     * @var EEM_Ticket $_instance
44
     */
45
    protected static $_instance;
46
47
48
    /**
49
     * private constructor to prevent direct creation
50
     *
51
     * @Constructor
52
     * @access private
53
     * @param string $timezone string representing the timezone we want to set for returned Date Time Strings
54
     *                         (and any incoming timezone data that gets saved).
55
     *                         Note this just sends the timezone info to the date time model field objects.
56
     *                         Default is NULL
57
     *                         (and will be assumed using the set timezone in the 'timezone_string' wp option)
58
     * @throws EE_Error
59
     */
60
    protected function __construct($timezone)
61
    {
62
        $this->singular_item = esc_html__('Ticket', 'event_espresso');
63
        $this->plural_item = esc_html__('Tickets', 'event_espresso');
64
        $this->_tables = array(
65
            'Ticket' => new EE_Primary_Table('esp_ticket', 'TKT_ID'),
66
        );
67
        $this->ticket_visibility = (array) apply_filters(
68
            'FHEE__EEM_Ticket__construct__ticket_visibility',
69
            [
70
                EEM_Ticket::TICKET_VISIBILITY_PUBLIC => esc_html__('Public', 'event_espresso'),
71
                EEM_Ticket::TICKET_VISIBILITY_MEMBERS_ONLY => esc_html__('Members only', 'event_espresso'),
72
                EEM_Ticket::TICKET_VISIBILITY_ADMINS_ONLY => esc_html__('Admins only', 'event_espresso'),
73
                EEM_Ticket::TICKET_VISIBILITY_ADMIN_UI_ONLY => esc_html__('Admin UI only', 'event_espresso'),
74
                EEM_Ticket::TICKET_VISIBILITY_NONE => esc_html__('None', 'event_espresso'),
75
            ]
76
        );
77
        $this->_fields = array(
78
            'Ticket' => array(
79
                'TKT_ID'          => new EE_Primary_Key_Int_Field(
80
                    'TKT_ID',
81
                    esc_html__('Ticket ID', 'event_espresso')
82
                ),
83
                'TTM_ID'          => new EE_Foreign_Key_Int_Field(
84
                    'TTM_ID',
85
                    esc_html__('Ticket Template ID', 'event_espresso'),
86
                    false,
87
                    0,
88
                    'Ticket_Template'
89
                ),
90
                'TKT_name'        => new EE_Plain_Text_Field(
91
                    'TKT_name',
92
                    esc_html__('Ticket Name', 'event_espresso'),
93
                    false,
94
                    ''
95
                ),
96
                'TKT_description' => new EE_Post_Content_Field(
97
                    'TKT_description',
98
                    esc_html__('Description of Ticket', 'event_espresso'),
99
                    false,
100
                    ''
101
                ),
102
                'TKT_start_date'  => new EE_Datetime_Field(
103
                    'TKT_start_date',
104
                    esc_html__('Start time/date of Ticket', 'event_espresso'),
105
                    false,
106
                    EE_Datetime_Field::now,
107
                    $timezone
108
                ),
109
                'TKT_end_date'    => new EE_Datetime_Field(
110
                    'TKT_end_date',
111
                    esc_html__('End time/date of Ticket', 'event_espresso'),
112
                    false,
113
                    EE_Datetime_Field::now,
114
                    $timezone
115
                ),
116
                'TKT_min'         => new EE_Integer_Field(
117
                    'TKT_min',
118
                    esc_html__('Minimum quantity of this ticket that must be purchased', 'event_espresso'),
119
                    false,
120
                    0
121
                ),
122
                'TKT_max'         => new EE_Infinite_Integer_Field(
123
                    'TKT_max',
124
                    esc_html__(
125
                        'Maximum quantity of this ticket that can be purchased in one transaction',
126
                        'event_espresso'
127
                    ),
128
                    false,
129
                    EE_INF
130
                ),
131
                'TKT_price'       => new EE_Money_Field(
132
                    'TKT_price',
133
                    esc_html__('Final calculated price for ticket', 'event_espresso'),
134
                    false,
135
                    0
136
                ),
137
                'TKT_sold'        => new EE_Integer_Field(
138
                    'TKT_sold',
139
                    esc_html__('Number of this ticket sold', 'event_espresso'),
140
                    false,
141
                    0
142
                ),
143
                'TKT_qty'         => new EE_Infinite_Integer_Field(
144
                    'TKT_qty',
145
                    esc_html__('Quantity of this ticket that is available', 'event_espresso'),
146
                    false,
147
                    EE_INF
148
                ),
149
                'TKT_reserved'    => new EE_Integer_Field(
150
                    'TKT_reserved',
151
                    esc_html__(
152
                        'Quantity of this ticket that is reserved, but not yet fully purchased',
153
                        'event_espresso'
154
                    ),
155
                    false,
156
                    0
157
                ),
158
                'TKT_uses'        => new EE_Infinite_Integer_Field(
159
                    'TKT_uses',
160
                    esc_html__('Number of datetimes this ticket can be used at', 'event_espresso'),
161
                    false,
162
                    EE_INF
163
                ),
164
                'TKT_required'    => new EE_Boolean_Field(
165
                    'TKT_required',
166
                    esc_html__(
167
                        'Flag indicating whether this ticket must be purchased with a transaction',
168
                        'event_espresso'
169
                    ),
170
                    false,
171
                    false
172
                ),
173
                'TKT_taxable'     => new EE_Boolean_Field(
174
                    'TKT_taxable',
175
                    esc_html__(
176
                        'Flag indicating whether there is tax applied on this ticket',
177
                        'event_espresso'
178
                    ),
179
                    false,
180
                    false
181
                ),
182
                'TKT_is_default'  => new EE_Boolean_Field(
183
                    'TKT_is_default',
184
                    esc_html__('Flag indicating that this ticket is a default ticket', 'event_espresso'),
185
                    false,
186
                    false
187
                ),
188
                'TKT_order'       => new EE_Integer_Field(
189
                    'TKT_order',
190
                    esc_html__(
191
                        'The order in which the Ticket is displayed in the editor (used for autosaves when the form doesn\'t have the ticket ID yet)',
192
                        'event_espresso'
193
                    ),
194
                    false,
195
                    0
196
                ),
197
                'TKT_row'         => new EE_Integer_Field(
198
                    'TKT_row',
199
                    esc_html__('How tickets are displayed in the ui', 'event_espresso'),
200
                    false,
201
                    0
202
                ),
203
                'TKT_deleted'     => new EE_Trashed_Flag_Field(
204
                    'TKT_deleted',
205
                    esc_html__('Flag indicating if this has been archived or not', 'event_espresso'),
206
                    false,
207
                    false
208
                ),
209
                'TKT_wp_user'     => new EE_WP_User_Field(
210
                    'TKT_wp_user',
211
                    esc_html__('Ticket Creator ID', 'event_espresso'),
212
                    false
213
                ),
214
                'TKT_parent'      => new EE_Integer_Field(
215
                    'TKT_parent',
216
                    esc_html__(
217
                        'Indicates what TKT_ID is the parent of this TKT_ID (used in autosaves/revisions)',
218
                        'event_espresso'
219
                    ),
220
                    true,
221
                    0
222
                ),
223
                'TKT_reverse_calculate' => new EE_Boolean_Field(
224
                    'TKT_reverse_calculate',
225
                    esc_html__(
226
                        'Flag indicating whether ticket calculations should run in reverse and calculate the base ticket price from the provided ticket total.',
227
                        'event_espresso'
228
                    ),
229
                    false,
230
                    false
231
                ),
232
                'TKT_visibility' => new EE_Enum_Integer_Field(
233
                    'TKT_visibility',
234
                    esc_html__('Defines where the ticket can be viewed throughout the UI.', 'event_espresso'),
235
                    false,
236
                    EEM_Ticket::TICKET_VISIBILITY_PUBLIC,
237
                    $this->ticket_visibility
238
                ),
239
            ),
240
        );
241
        $this->_model_relations = array(
242
            'Datetime'        => new EE_HABTM_Relation('Datetime_Ticket'),
243
            'Datetime_Ticket' => new EE_Has_Many_Relation(),
244
            'Price'           => new EE_HABTM_Relation('Ticket_Price'),
245
            'Ticket_Template' => new EE_Belongs_To_Relation(),
246
            'Registration'    => new EE_Has_Many_Relation(),
247
            'WP_User'         => new EE_Belongs_To_Relation(),
248
        );
249
        // this model is generally available for reading
250
        $path_to_event = 'Datetime.Event';
251
        $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Default_Public(
252
            'TKT_is_default',
253
            $path_to_event
254
        );
255
        // account for default tickets in the caps
256
        $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Default_Protected(
257
            'TKT_is_default',
258
            $path_to_event
259
        );
260
        $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Default_Protected(
261
            'TKT_is_default',
262
            $path_to_event
263
        );
264
        $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Default_Protected(
265
            'TKT_is_default',
266
            $path_to_event
267
        );
268
        $this->model_chain_to_password = $path_to_event;
269
        parent::__construct($timezone);
270
    }
271
272
273
    /**
274
     * This returns all tickets that are defaults from the db
275
     *
276
     * @return EE_Ticket[]
277
     * @throws EE_Error
278
     */
279
    public function get_all_default_tickets()
280
    {
281
        /** @type EE_Ticket[] $tickets */
282
        $tickets = $this->get_all(array(array('TKT_is_default' => 1), 'order_by' => array('TKT_ID' => 'ASC')));
283
        // we need to set the start date and end date to today's date and the start of the default dtt
284
        return $this->_set_default_dates($tickets);
285
    }
286
287
288
    /**
289
     * sets up relevant start and end date for EE_Ticket (s)
290
     *
291
     * @param EE_Ticket[] $tickets
292
     * @return EE_Ticket[]
293
     * @throws EE_Error
294
     */
295
    private function _set_default_dates($tickets)
296
    {
297
        foreach ($tickets as $ticket) {
298
            $ticket->set(
299
                'TKT_start_date',
300
                (int) $this->current_time_for_query('TKT_start_date', true)
301
            );
302
            $ticket->set(
303
                'TKT_end_date',
304
                (int) $this->current_time_for_query('TKT_end_date', true) + MONTH_IN_SECONDS
305
            );
306
            $ticket->set_end_time(
307
                $this->convert_datetime_for_query(
308
                    'TKT_end_date',
309
                    '11:59 pm',
310
                    'g:i a',
311
                    $this->_timezone
312
                )
313
            );
314
        }
315
        return $tickets;
316
    }
317
318
319
    /**
320
     * Gets the total number of tickets available at a particular datetime (does
321
     * NOT take int account the datetime's spaces available)
322
     *
323
     * @param int   $DTT_ID
324
     * @param array $query_params
325
     * @return int
326
     */
327
    public function sum_tickets_currently_available_at_datetime($DTT_ID, $query_params = array())
328
    {
329
        return EEM_Datetime::instance()->sum_tickets_currently_available_at_datetime($DTT_ID, $query_params);
330
    }
331
332
333
    /**
334
     * Updates the TKT_sold quantity on all the tickets matching $query_params
335
     *
336
     * @param EE_Ticket[] $tickets
337
     * @return void
338
     * @throws EE_Error
339
     */
340
    public function update_tickets_sold($tickets)
341
    {
342
        foreach ($tickets as $ticket) {
343
            /* @var  $ticket EE_Ticket */
344
            $ticket->update_tickets_sold();
345
        }
346
    }
347
348
349
    /**
350
     * returns an array of EE_Ticket objects with a non-zero value for TKT_reserved
351
     *
352
     * @return EE_Base_Class[]|EE_Ticket[]
353
     * @throws EE_Error
354
     */
355
    public function get_tickets_with_reservations()
356
    {
357
        return $this->get_all(
358
            array(
359
                array(
360
                    'TKT_reserved' => array('>', 0),
361
                ),
362
            )
363
        );
364
    }
365
366
367
    /**
368
     * returns an array of EE_Ticket objects matching the supplied list of IDs
369
     *
370
     * @param array $ticket_IDs
371
     * @return EE_Base_Class[]|EE_Ticket[]
372
     * @throws EE_Error
373
     */
374
    public function get_tickets_with_IDs(array $ticket_IDs)
375
    {
376
        return $this->get_all(
377
            array(
378
                array(
379
                    'TKT_ID' => array('IN', $ticket_IDs),
380
                ),
381
            )
382
        );
383
    }
384
385
386
    /**
387
     * @return array
388
     */
389
    public function ticketVisibility(): array
390
    {
391
        return $this->ticket_visibility;
392
    }
393
}
394