Completed
Branch FET-3467-waitlists (94dbd6)
by
unknown
37:58 queued 26:19
created

EEM_Ticket::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 183
Code Lines 144

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 144
nc 1
nop 1
dl 0
loc 183
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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',
60
                    esc_html__('Ticket Name', 'event_espresso'),
61
                    false,
62
                    ''
63
                ),
64
                'TKT_description' => new EE_Post_Content_Field(
65
                    'TKT_description',
66
                    esc_html__('Description of Ticket', 'event_espresso'),
67
                    false,
68
                    ''
69
                ),
70
                'TKT_start_date'  => new EE_Datetime_Field(
71
                    'TKT_start_date',
72
                    esc_html__('Start time/date of Ticket', 'event_espresso'),
73
                    false,
74
                    EE_Datetime_Field::now,
75
                    $timezone
76
                ),
77
                'TKT_end_date'    => new EE_Datetime_Field(
78
                    'TKT_end_date',
79
                    esc_html__('End time/date of Ticket', 'event_espresso'),
80
                    false,
81
                    EE_Datetime_Field::now,
82
                    $timezone
83
                ),
84
                'TKT_min'         => new EE_Integer_Field(
85
                    'TKT_min',
86
                    esc_html__('Minimum quantity of this ticket that must be purchased', 'event_espresso'),
87
                    false,
88
                    0
89
                ),
90
                'TKT_max'         => new EE_Infinite_Integer_Field(
91
                    'TKT_max',
92
                    esc_html__(
93
                        'Maximum quantity of this ticket that can be purchased in one transaction',
94
                        'event_espresso'
95
                    ),
96
                    false,
97
                    EE_INF
98
                ),
99
                'TKT_price'       => new EE_Money_Field(
100
                    'TKT_price',
101
                    esc_html__('Final calculated price for ticket', 'event_espresso'),
102
                    false,
103
                    0
104
                ),
105
                'TKT_sold'        => new EE_Integer_Field(
106
                    'TKT_sold',
107
                    esc_html__('Number of this ticket sold', 'event_espresso'),
108
                    false,
109
                    0
110
                ),
111
                'TKT_qty'         => new EE_Infinite_Integer_Field(
112
                    'TKT_qty',
113
                    esc_html__('Quantity of this ticket that is available', 'event_espresso'),
114
                    false,
115
                    EE_INF
116
                ),
117
                'TKT_reserved'    => new EE_Integer_Field(
118
                    'TKT_reserved',
119
                    esc_html__(
120
                        'Quantity of this ticket that is reserved, but not yet fully purchased',
121
                        'event_espresso'
122
                    ),
123
                    false,
124
                    0
125
                ),
126
                'TKT_uses'        => new EE_Infinite_Integer_Field(
127
                    'TKT_uses',
128
                    esc_html__('Number of datetimes this ticket can be used at', 'event_espresso'),
129
                    false,
130
                    EE_INF
131
                ),
132
                'TKT_required'    => new EE_Boolean_Field(
133
                    'TKT_required',
134
                    esc_html__(
135
                        'Flag indicating whether this ticket must be purchased with a transaction',
136
                        'event_espresso'
137
                    ),
138
                    false,
139
                    false
140
                ),
141
                'TKT_taxable'     => new EE_Boolean_Field(
142
                    'TKT_taxable',
143
                    esc_html__(
144
                        'Flag indicating whether there is tax applied on this ticket',
145
                        'event_espresso'
146
                    ),
147
                    false,
148
                    false
149
                ),
150
                'TKT_is_default'  => new EE_Boolean_Field(
151
                    'TKT_is_default',
152
                    esc_html__('Flag indicating that this ticket is a default ticket', 'event_espresso'),
153
                    false,
154
                    false
155
                ),
156
                'TKT_order'       => new EE_Integer_Field(
157
                    'TKT_order',
158
                    esc_html__(
159
                        'The order in which the Ticket is displayed in the editor (used for autosaves when the form doesn\'t have the ticket ID yet)',
160
                        'event_espresso'
161
                    ),
162
                    false,
163
                    0
164
                ),
165
                'TKT_row'         => new EE_Integer_Field(
166
                    'TKT_row',
167
                    esc_html__('How tickets are displayed in the ui', 'event_espresso'),
168
                    false,
169
                    0
170
                ),
171
                'TKT_deleted'     => new EE_Trashed_Flag_Field(
172
                    'TKT_deleted',
173
                    esc_html__('Flag indicating if this has been archived or not', 'event_espresso'),
174
                    false,
175
                    false
176
                ),
177
                'TKT_wp_user'     => new EE_WP_User_Field(
178
                    'TKT_wp_user',
179
                    esc_html__('Ticket Creator ID', 'event_espresso'),
180
                    false
181
                ),
182
                'TKT_parent'      => new EE_Integer_Field(
183
                    'TKT_parent',
184
                    esc_html__(
185
                        'Indicates what TKT_ID is the parent of this TKT_ID (used in autosaves/revisions)',
186
                        'event_espresso'
187
                    ),
188
                    true,
189
                    0
190
                ),
191
            ),
192
        );
193
        $this->_model_relations = array(
194
            'Datetime'        => new EE_HABTM_Relation('Datetime_Ticket'),
195
            'Datetime_Ticket' => new EE_Has_Many_Relation(),
196
            'Price'           => new EE_HABTM_Relation('Ticket_Price'),
197
            'Ticket_Template' => new EE_Belongs_To_Relation(),
198
            'Registration'    => new EE_Has_Many_Relation(),
199
            'WP_User'         => new EE_Belongs_To_Relation(),
200
        );
201
        //this model is generally available for reading
202
        $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Default_Public(
203
            'TKT_is_default',
204
            'Datetime.Event'
205
        );
206
        //account for default tickets in the caps
207
        $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = new EE_Restriction_Generator_Default_Protected(
208
            'TKT_is_default',
209
            'Datetime.Event'
210
        );
211
        $this->_cap_restriction_generators[EEM_Base::caps_edit] = new EE_Restriction_Generator_Default_Protected(
212
            'TKT_is_default',
213
            'Datetime.Event'
214
        );
215
        $this->_cap_restriction_generators[EEM_Base::caps_delete] = new EE_Restriction_Generator_Default_Protected(
216
            'TKT_is_default',
217
            'Datetime.Event'
218
        );
219
        parent::__construct($timezone);
220
    }
221
222
223
224
    /**
225
     * This returns all tickets that are defaults from the db
226
     *
227
     * @return EE_Ticket[]
228
     * @throws EE_Error
229
     */
230
    public function get_all_default_tickets()
231
    {
232
        /** @type EE_Ticket[] $tickets */
233
        $tickets = $this->get_all(array(array('TKT_is_default' => 1), 'order_by' => array('TKT_ID' => 'ASC')));
234
        //we need to set the start date and end date to today's date and the start of the default dtt
235
        return $this->_set_default_dates($tickets);
236
    }
237
238
239
240
    /**
241
     * sets up relevant start and end date for EE_Ticket (s)
242
     *
243
     * @param EE_Ticket[] $tickets
244
     * @return EE_Ticket[]
245
     * @throws EE_Error
246
     */
247
    private function _set_default_dates($tickets)
248
    {
249
        foreach ($tickets as $ticket) {
250
            $ticket->set(
251
                'TKT_start_date',
252
                (int)$this->current_time_for_query('TKT_start_date', true)
253
            );
254
            $ticket->set('TKT_end_date',
255
                (int)$this->current_time_for_query('TKT_end_date', true) + MONTH_IN_SECONDS
256
            );
257
            $ticket->set_end_time($this->convert_datetime_for_query(
258
                'TKT_end_date',
259
                '11:59 pm',
260
                'g:i a',
261
                $this->_timezone)
262
            );
263
        }
264
        return $tickets;
265
    }
266
267
268
269
    /**
270
     * Gets the total number of tickets available at a particular datetime (does
271
     * NOT take int account the datetime's spaces available)
272
     *
273
     * @param int   $DTT_ID
274
     * @param array $query_params
275
     * @return int
276
     */
277
    public function sum_tickets_currently_available_at_datetime($DTT_ID, $query_params = array())
278
    {
279
        return EEM_Datetime::instance()->sum_tickets_currently_available_at_datetime($DTT_ID, $query_params);
280
    }
281
282
283
284
    /**
285
     * Updates the TKT_sold quantity on all the tickets matching $query_params
286
     *
287
     * @param EE_Ticket[] $tickets
288
     * @return void
289
     * @throws EE_Error
290
     */
291
    public function update_tickets_sold($tickets)
292
    {
293
        foreach ($tickets as $ticket) {
294
            /* @var  $ticket EE_Ticket */
295
            $ticket->update_tickets_sold();
296
        }
297
    }
298
299
300
301
    /**
302
     * returns an array of EE_Ticket objects with a non-zero value for TKT_reserved
303
     *
304
     * @return EE_Base_Class[]|EE_Ticket[]
305
     * @throws EE_Error
306
     */
307
    public function get_tickets_with_reservations()
308
    {
309
        return $this->get_all(
310
            array(
311
                array(
312
                    'TKT_reserved' => array('>', 0),
313
                ),
314
            )
315
        );
316
    }
317
318
319
320
    /**
321
     * returns an array of EE_Ticket objects matching the supplied list of IDs
322
     *
323
     * @param array $ticket_IDs
324
     * @return EE_Base_Class[]|EE_Ticket[]
325
     * @throws EE_Error
326
     */
327
    public function get_tickets_with_IDs(array $ticket_IDs)
328
    {
329
        return $this->get_all(
330
            array(
331
                array(
332
                    'TKT_ID' => array('IN', $ticket_IDs)
333
                )
334
            )
335
        );
336
    }
337
338
339
340
}
341