Completed
Branch FET-10723-event-editor-unit-te... (c4854a)
by
unknown
55:52 queued 43:56
created

EEM_Ticket   C

Complexity

Total Complexity 9

Size/Duplication

Total Lines 323
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 20

Importance

Changes 0
Metric Value
dl 0
loc 323
rs 6.4705
c 0
b 0
f 0
wmc 9
lcom 1
cbo 20

7 Methods

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