Completed
Branch BUG-10608-primary-registration... (c40aa6)
by
unknown
65:42 queued 53:47
created

EEM_Event::get_expired_events()   D

Complexity

Conditions 10
Paths 256

Size

Total Lines 51
Code Lines 32

Duplication

Lines 12
Ratio 23.53 %

Importance

Changes 0
Metric Value
cc 10
eloc 32
nc 256
nop 2
dl 12
loc 51
rs 4.2352
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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 if (! defined('EVENT_ESPRESSO_VERSION')) {
2
    exit('No direct script access allowed');
3
}
4
require_once(EE_MODELS . 'EEM_CPT_Base.model.php');
5
6
7
8
/**
9
 * EEM_Event Model
10
 * extends EEM_CPT_Base which extends EEM_Base
11
 *
12
 * @package               Event Espresso
13
 * @subpackage            includes/models/
14
 * @author                Michael Nelson, Brent Christensen
15
 */
16
class EEM_Event extends EEM_CPT_Base
17
{
18
19
    /**
20
     * constant used by status(), indicating that no more tickets can be purchased for any of the datetimes for the
21
     * event
22
     */
23
    const sold_out = 'sold_out';
24
25
    /**
26
     * constant used by status(), indicating that upcoming event dates have been postponed (may be pushed to a later
27
     * date)
28
     */
29
    const postponed = 'postponed';
30
31
    /**
32
     * constant used by status(), indicating that the event will no longer occur
33
     */
34
    const cancelled = 'cancelled';
35
36
37
    /**
38
     * @var string
39
     */
40
    protected static $_default_reg_status;
41
42
43
    /**
44
     * private instance of the Event object
45
     *
46
     * @var EEM_Event
47
     */
48
    protected static $_instance;
49
50
51
52
    /**
53
     *  This function is a singleton method used to instantiate the EEM_Event object
54
     *
55
     * @param string $timezone
56
     * @return EEM_Event
57
     * @throws \EE_Error
58
     */
59 View Code Duplication
    public static function instance($timezone = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        // check if instance of EEM_Event already exists
62
        if (! self::$_instance instanceof EEM_Event) {
63
            // instantiate Espresso_model
64
            self::$_instance = new self($timezone);
65
        }
66
        //we might have a timezone set, let set_timezone decide what to do with it
67
        self::$_instance->set_timezone($timezone);
68
        // EEM_Event object
69
        return self::$_instance;
70
    }
71
72
73
74
    /**
75
     * Adds a relationship to Term_Taxonomy for each CPT_Base
76
     *
77
     * @param string $timezone
78
     * @throws \EE_Error
79
     */
80
    protected function __construct($timezone = null)
81
    {
82
        EE_Registry::instance()->load_model('Registration');
83
        $this->singular_item = esc_html__('Event', 'event_espresso');
84
        $this->plural_item = esc_html__('Events', 'event_espresso');
85
        // to remove Cancelled events from the frontend, copy the following filter to your functions.php file
86
        // add_filter( 'AFEE__EEM_Event__construct___custom_stati__cancelled__Public', '__return_false' );
87
        // to remove Postponed events from the frontend, copy the following filter to your functions.php file
88
        // add_filter( 'AFEE__EEM_Event__construct___custom_stati__postponed__Public', '__return_false' );
89
        // to remove Sold Out events from the frontend, copy the following filter to your functions.php file
90
        //	add_filter( 'AFEE__EEM_Event__construct___custom_stati__sold_out__Public', '__return_false' );
91
        $this->_custom_stati = apply_filters(
92
            'AFEE__EEM_Event__construct___custom_stati',
93
            array(
94
                EEM_Event::cancelled => array(
95
                    'label'  => esc_html__('Cancelled', 'event_espresso'),
96
                    'public' => apply_filters('AFEE__EEM_Event__construct___custom_stati__cancelled__Public', true),
97
                ),
98
                EEM_Event::postponed => array(
99
                    'label'  => esc_html__('Postponed', 'event_espresso'),
100
                    'public' => apply_filters('AFEE__EEM_Event__construct___custom_stati__postponed__Public', true),
101
                ),
102
                EEM_Event::sold_out  => array(
103
                    'label'  => esc_html__('Sold Out', 'event_espresso'),
104
                    'public' => apply_filters('AFEE__EEM_Event__construct___custom_stati__sold_out__Public', true),
105
                ),
106
            )
107
        );
108
        self::$_default_reg_status = empty(self::$_default_reg_status) ? EEM_Registration::status_id_pending_payment
109
            : self::$_default_reg_status;
110
        $this->_tables = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('Event_CPT' => new..., 'EVTM_ID', 'EVT_ID')) of type array<string,object<EE_P...<EE_Secondary_Table>"}> is incompatible with the declared type array<integer,object<EE_Table_Base>> of property $_tables.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
111
            'Event_CPT'  => new EE_Primary_Table('posts', 'ID'),
112
            'Event_Meta' => new EE_Secondary_Table('esp_event_meta', 'EVTM_ID', 'EVT_ID'),
113
        );
114
        $this->_fields = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('Event_CPT' => arr...esso'), false, false))) of type array<string,array<strin...E_Boolean_Field>\"}>"}> is incompatible with the declared type array<integer,object<EE_Model_Field_Base>> of property $_fields.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
115
            'Event_CPT'  => array(
116
                'EVT_ID'         => new EE_Primary_Key_Int_Field('ID',
117
                    esc_html__('Post ID for Event', 'event_espresso')),
118
                'EVT_name'       => new EE_Plain_Text_Field('post_title', esc_html__('Event Name', 'event_espresso'),
119
                    false,
120
                    ''),
121
                'EVT_desc'       => new EE_Post_Content_Field('post_content',
122
                    esc_html__('Event Description', 'event_espresso'),
123
                    false, ''),
124
                'EVT_slug'       => new EE_Slug_Field('post_name', esc_html__('Event Slug', 'event_espresso'), false,
125
                    ''),
126
                'EVT_created'    => new EE_Datetime_Field('post_date',
127
                    esc_html__('Date/Time Event Created', 'event_espresso'),
128
                    false, EE_Datetime_Field::now),
129
                'EVT_short_desc' => new EE_Simple_HTML_Field('post_excerpt',
130
                    esc_html__('Event Short Description', 'event_espresso'), false, ''),
131
                'EVT_modified'   => new EE_Datetime_Field('post_modified',
132
                    esc_html__('Date/Time Event Modified', 'event_espresso'), false, EE_Datetime_Field::now),
133
                'EVT_wp_user'    => new EE_WP_User_Field('post_author',
134
                    esc_html__('Event Creator ID', 'event_espresso'),
135
                    false),
136
                'parent'         => new EE_Integer_Field('post_parent', esc_html__('Event Parent ID', 'event_espresso'),
137
                    false,
138
                    0),
139
                'EVT_order'      => new EE_Integer_Field('menu_order', esc_html__('Event Menu Order', 'event_espresso'),
140
                    false,
141
                    1),
142
                'post_type'      => new EE_WP_Post_Type_Field('espresso_events'),
143
                // EE_Plain_Text_Field( 'post_type', esc_html__( 'Event Post Type', 'event_espresso' ), FALSE, 'espresso_events' ),
144
                'status'         => new EE_WP_Post_Status_Field('post_status',
145
                    esc_html__('Event Status', 'event_espresso'),
146
                    false, 'draft', $this->_custom_stati),
147
            ),
148
            'Event_Meta' => array(
149
                'EVTM_ID'                         => new EE_DB_Only_Float_Field('EVTM_ID',
150
                    esc_html__('Event Meta Row ID', 'event_espresso'), false),
151
                'EVT_ID_fk'                       => new EE_DB_Only_Int_Field('EVT_ID',
152
                    esc_html__('Foreign key to Event ID from Event Meta table', 'event_espresso'), false),
153
                'EVT_display_desc'                => new EE_Boolean_Field('EVT_display_desc',
154
                    esc_html__('Display Description Flag', 'event_espresso'), false, 1),
155
                'EVT_display_ticket_selector'     => new EE_Boolean_Field('EVT_display_ticket_selector',
156
                    esc_html__('Display Ticket Selector Flag', 'event_espresso'), false, 1),
157
                'EVT_visible_on'                  => new EE_Datetime_Field('EVT_visible_on',
158
                    esc_html__('Event Visible Date', 'event_espresso'), true, EE_Datetime_Field::now),
159
                'EVT_additional_limit'            => new EE_Integer_Field('EVT_additional_limit',
160
                    esc_html__('Limit of Additional Registrations on Same Transaction', 'event_espresso'), true, 10),
161
                'EVT_default_registration_status' => new EE_Enum_Text_Field(
162
                    'EVT_default_registration_status',
163
                    esc_html__('Default Registration Status on this Event', 'event_espresso'), false,
164
                    EEM_Event::$_default_reg_status, EEM_Registration::reg_status_array()
165
                ),
166
                'EVT_member_only'                 => new EE_Boolean_Field('EVT_member_only',
167
                    esc_html__('Member-Only Event Flag', 'event_espresso'), false, false),
168
                'EVT_phone'                       => new EE_Plain_Text_Field('EVT_phone',
169
                    esc_html__('Event Phone Number', 'event_espresso'), false),
170
                'EVT_allow_overflow'              => new EE_Boolean_Field('EVT_allow_overflow',
171
                    esc_html__('Allow Overflow on Event', 'event_espresso'), false, false),
172
                'EVT_timezone_string'             => new EE_Plain_Text_Field('EVT_timezone_string',
173
                    esc_html__('Timezone (name) for Event times', 'event_espresso'), false),
174
                'EVT_external_URL'                => new EE_Plain_Text_Field('EVT_external_URL',
175
                    esc_html__('URL of Event Page if hosted elsewhere', 'event_espresso'), true),
176
                'EVT_donations'                   => new EE_Boolean_Field('EVT_donations',
177
                    esc_html__('Accept Donations?', 'event_espresso'), false, false),
178
            ),
179
        );
180
        $this->_model_relations = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('Registration' => ..._Belongs_To_Relation()) of type array<string,object<EE_H...Belongs_To_Relation>"}> is incompatible with the declared type array<integer,object<EE_Model_Relation_Base>> of property $_model_relations.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
181
            'Registration'           => new EE_Has_Many_Relation(),
182
            'Datetime'               => new EE_Has_Many_Relation(),
183
            'Question_Group'         => new EE_HABTM_Relation('Event_Question_Group'),
0 ignored issues
show
Documentation introduced by
'Event_Question_Group' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
184
            'Venue'                  => new EE_HABTM_Relation('Event_Venue'),
0 ignored issues
show
Documentation introduced by
'Event_Venue' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
185
            'Term_Relationship'      => new EE_Has_Many_Relation(),
186
            'Term_Taxonomy'          => new EE_HABTM_Relation('Term_Relationship'),
0 ignored issues
show
Documentation introduced by
'Term_Relationship' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
187
            'Message_Template_Group' => new EE_HABTM_Relation('Event_Message_Template'),
0 ignored issues
show
Documentation introduced by
'Event_Message_Template' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
188
            'Attendee'               => new EE_HABTM_Relation('Registration'),
0 ignored issues
show
Documentation introduced by
'Registration' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
189
            'WP_User'                => new EE_Belongs_To_Relation(),
190
        );
191
        //this model is generally available for reading
192
        $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Public();
193
        parent::__construct($timezone);
194
    }
195
196
197
198
    /**
199
     * @param string $default_reg_status
200
     */
201
    public static function set_default_reg_status($default_reg_status)
202
    {
203
        self::$_default_reg_status = $default_reg_status;
204
        // if EEM_Event has already been instantiated,
205
        // then we need to reset the `EVT_default_reg_status` field to use the new default.
206
        if (self::$_instance instanceof EEM_Event) {
207
            $default_reg_status = new EE_Enum_Text_Field(
208
                'EVT_default_registration_status',
209
                esc_html__('Default Registration Status on this Event', 'event_espresso'),
210
                false,
211
                $default_reg_status,
212
                EEM_Registration::reg_status_array()
213
            );
214
            $default_reg_status->_construct_finalize(
215
                'Event_Meta',
216
                'EVT_default_registration_status',
217
                'EEM_Event'
218
            );
219
            self::$_instance->_fields['Event_Meta']['EVT_default_registration_status'] = $default_reg_status;
220
        }
221
    }
222
223
224
225
    /**
226
     * get_question_groups
227
     *
228
     * @return array
229
     * @throws \EE_Error
230
     */
231
    public function get_all_question_groups()
232
    {
233
        return EE_Registry::instance()->load_model('Question_Group')->get_all(
234
            array(
235
                array('QSG_deleted' => false),
236
                'order_by' => array('QSG_order' => 'ASC'),
237
            )
238
        );
239
    }
240
241
242
243
    /**
244
     * get_question_groups
245
     *
246
     * @param int $EVT_ID
247
     * @return array|bool
248
     * @throws \EE_Error
249
     */
250 View Code Duplication
    public function get_all_event_question_groups($EVT_ID = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
251
    {
252
        if (! isset($EVT_ID) || ! absint($EVT_ID)) {
253
            EE_Error::add_error(
254
                esc_html__(
255
                    'An error occurred. No Event Question Groups could be retrieved because an Event ID was not received.',
256
                    'event_espresso'
257
                ),
258
                __FILE__, __FUNCTION__, __LINE__
259
            );
260
            return false;
261
        }
262
        return EE_Registry::instance()->load_model('Event_Question_Group')->get_all(
263
            array(
264
                array('EVT_ID' => $EVT_ID),
265
            )
266
        );
267
    }
268
269
270
271
    /**
272
     * get_question_groups
273
     *
274
     * @param int     $EVT_ID
275
     * @param boolean $for_primary_attendee
276
     * @return array|bool
277
     * @throws \EE_Error
278
     */
279 View Code Duplication
    public function get_event_question_groups($EVT_ID = 0, $for_primary_attendee = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
280
    {
281
        if (! isset($EVT_ID) || ! absint($EVT_ID)) {
282
            EE_Error::add_error(
283
                esc_html__(
284
                    'An error occurred. No Event Question Groups could be retrieved because an Event ID was not received.',
285
                    'event_espresso'
286
                ),
287
                __FILE__, __FUNCTION__, __LINE__
288
            );
289
            return false;
290
        }
291
        return EE_Registry::instance()->load_model('Event_Question_Group')->get_all(
292
            array(
293
                array(
294
                    'EVT_ID'      => $EVT_ID,
295
                    'EQG_primary' => $for_primary_attendee,
296
                ),
297
            )
298
        );
299
    }
300
301
302
303
    /**
304
     * get_question_groups
305
     *
306
     * @param int             $EVT_ID
307
     * @param EE_Registration $registration
308
     * @return array|bool
309
     * @throws \EE_Error
310
     */
311
    public function get_question_groups_for_event($EVT_ID = 0, EE_Registration $registration)
312
    {
313
        if (! isset($EVT_ID) || ! absint($EVT_ID)) {
314
            EE_Error::add_error(
315
                esc_html__(
316
                    'An error occurred. No Question Groups could be retrieved because an Event ID was not received.',
317
                    'event_espresso'
318
                ),
319
                __FILE__, __FUNCTION__, __LINE__
320
            );
321
            return false;
322
        }
323
        $where_params = array(
324
            'Event_Question_Group.EVT_ID'      => $EVT_ID,
325
            'Event_Question_Group.EQG_primary' => $registration->count() === 1 ? true : false,
326
            'QSG_deleted'                      => false,
327
        );
328
        return EE_Registry::instance()->load_model('Question_Group')->get_all(
329
            array(
330
                $where_params,
331
                'order_by' => array('QSG_order' => 'ASC'),
332
            )
333
        );
334
    }
335
336
337
338
    /**
339
     * get_question_target_db_column
340
     *
341
     * @param string $QSG_IDs csv list of $QSG IDs
342
     * @return array|bool
343
     * @throws \EE_Error
344
     */
345 View Code Duplication
    public function get_questions_in_groups($QSG_IDs = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
346
    {
347
        if (empty($QSG_IDs)) {
348
            EE_Error::add_error(
349
                esc_html__('An error occurred. No Question Group IDs were received.', 'event_espresso'),
350
                __FILE__, __FUNCTION__, __LINE__
351
            );
352
            return false;
353
        }
354
        return EE_Registry::instance()->load_model('Question')->get_all(
355
            array(
356
                array(
357
                    'Question_Group.QSG_ID' => array('IN', $QSG_IDs),
358
                    'QST_deleted'           => false,
359
                    'QST_admin_only'        => is_admin(),
360
                ),
361
                'order_by' => 'QST_order',
362
            )
363
        );
364
    }
365
366
367
368
    /**
369
     * get_options_for_question
370
     *
371
     * @param string $QST_IDs csv list of $QST IDs
372
     * @return array|bool
373
     * @throws \EE_Error
374
     */
375 View Code Duplication
    public function get_options_for_question($QST_IDs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
376
    {
377
        if (empty($QST_IDs)) {
378
            EE_Error::add_error(
379
                esc_html__('An error occurred. No Question IDs were received.', 'event_espresso'),
380
                __FILE__, __FUNCTION__, __LINE__
381
            );
382
            return false;
383
        }
384
        return EE_Registry::instance()->load_model('Question_Option')->get_all(
385
            array(
386
                array(
387
                    'Question.QST_ID' => array('IN', $QST_IDs),
388
                    'QSO_deleted'     => false,
389
                ),
390
                'order_by' => 'QSO_ID',
391
            )
392
        );
393
    }
394
395
396
397
398
399
400
401
    /**
402
     * Gets all events that are published
403
     * and have event start time earlier than now and an event end time later than now
404
     *
405
     * @param  array $query_params An array of query params to further filter on
406
     *                             (note that status and DTT_EVT_start and DTT_EVT_end will be overridden)
407
     * @param bool   $count        whether to return the count or not (default FALSE)
408
     * @return EE_Event[]|int
409
     * @throws \EE_Error
410
     */
411
    public function get_active_events($query_params, $count = false)
412
    {
413
        if (array_key_exists(0, $query_params)) {
414
            $where_params = $query_params[0];
415
            unset($query_params[0]);
416
        } else {
417
            $where_params = array();
418
        }
419
        // if we have count make sure we don't include group by
420
        if ($count && isset($query_params['group_by'])) {
421
            unset($query_params['group_by']);
422
        }
423
        // let's add specific query_params for active_events
424
        // keep in mind this will override any sent status in the query AND any date queries.
425
        $where_params['status'] = array('IN', array('publish', EEM_Event::sold_out));
426
        //if already have where params for DTT_EVT_start or DTT_EVT_end then append these conditions
427
        if (isset($where_params['Datetime.DTT_EVT_start'])) {
428
            $where_params['Datetime.DTT_EVT_start******'] = array(
429
                '<',
430
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'),
431
            );
432
        } else {
433
            $where_params['Datetime.DTT_EVT_start'] = array(
434
                '<',
435
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'),
436
            );
437
        }
438
        if (isset($where_params['Datetime.DTT_EVT_end'])) {
439
            $where_params['Datetime.DTT_EVT_end*****'] = array(
440
                '>',
441
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'),
442
            );
443
        } else {
444
            $where_params['Datetime.DTT_EVT_end'] = array(
445
                '>',
446
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'),
447
            );
448
        }
449
        $query_params[0] = $where_params;
450
        // don't use $query_params with count()
451
        // because we don't want to include additional query clauses like "GROUP BY"
452
        return $count
453
            ? $this->count(array($where_params), 'EVT_ID', true)
454
            : $this->get_all($query_params);
455
    }
456
457
458
459
    /**
460
     * get all events that are published and have an event start time later than now
461
     *
462
     * @param  array $query_params An array of query params to further filter on
463
     *                             (Note that status and DTT_EVT_start will be overridden)
464
     * @param bool   $count        whether to return the count or not (default FALSE)
465
     * @return EE_Event[]|int
466
     * @throws \EE_Error
467
     */
468 View Code Duplication
    public function get_upcoming_events($query_params, $count = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
469
    {
470
        if (array_key_exists(0, $query_params)) {
471
            $where_params = $query_params[0];
472
            unset($query_params[0]);
473
        } else {
474
            $where_params = array();
475
        }
476
        // if we have count make sure we don't include group by
477
        if ($count && isset($query_params['group_by'])) {
478
            unset($query_params['group_by']);
479
        }
480
        // let's add specific query_params for active_events
481
        // keep in mind this will override any sent status in the query AND any date queries.
482
        $where_params['status'] = array('IN', array('publish', EEM_Event::sold_out));
483
        // if there are already query_params matching DTT_EVT_start then we need to modify that to add them.
484
        if (isset($where_params['Datetime.DTT_EVT_start'])) {
485
            $where_params['Datetime.DTT_EVT_start*****'] = array(
486
                '>',
487
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'),
488
            );
489
        } else {
490
            $where_params['Datetime.DTT_EVT_start'] = array(
491
                '>',
492
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start'),
493
            );
494
        }
495
        $query_params[0] = $where_params;
496
        // don't use $query_params with count()
497
        // because we don't want to include additional query clauses like "GROUP BY"
498
        return $count
499
            ? $this->count(array($where_params), 'EVT_ID', true)
500
            : $this->get_all($query_params);
501
    }
502
503
504
505
    /**
506
     * Gets all events that are published
507
     * and have an event end time later than now
508
     *
509
     * @param  array $query_params An array of query params to further filter on
510
     *                             (note that status and DTT_EVT_end will be overridden)
511
     * @param bool   $count        whether to return the count or not (default FALSE)
512
     * @return EE_Event[]|int
513
     * @throws \EE_Error
514
     */
515 View Code Duplication
    public function get_active_and_upcoming_events($query_params, $count = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
516
    {
517
        if (array_key_exists(0, $query_params)) {
518
            $where_params = $query_params[0];
519
            unset($query_params[0]);
520
        } else {
521
            $where_params = array();
522
        }
523
        // if we have count make sure we don't include group by
524
        if ($count && isset($query_params['group_by'])) {
525
            unset($query_params['group_by']);
526
        }
527
        // let's add specific query_params for active_events
528
        // keep in mind this will override any sent status in the query AND any date queries.
529
        $where_params['status'] = array('IN', array('publish', EEM_Event::sold_out));
530
        // add where params for DTT_EVT_end
531
        if (isset($where_params['Datetime.DTT_EVT_end'])) {
532
            $where_params['Datetime.DTT_EVT_end*****'] = array(
533
                '>',
534
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'),
535
            );
536
        } else {
537
            $where_params['Datetime.DTT_EVT_end'] = array(
538
                '>',
539
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'),
540
            );
541
        }
542
        $query_params[0] = $where_params;
543
        // don't use $query_params with count()
544
        // because we don't want to include additional query clauses like "GROUP BY"
545
        return $count
546
            ? $this->count(array($where_params), 'EVT_ID', true)
547
            : $this->get_all($query_params);
548
    }
549
550
551
552
    /**
553
     * This only returns events that are expired.
554
     * They may still be published but all their datetimes have expired.
555
     *
556
     * @param  array $query_params An array of query params to further filter on
557
     *                             (note that status and DTT_EVT_end will be overridden)
558
     * @param bool   $count        whether to return the count or not (default FALSE)
559
     * @return EE_Event[]|int
560
     * @throws \EE_Error
561
     */
562
    public function get_expired_events($query_params, $count = false)
563
    {
564
        $where_params = isset($query_params[0]) ? $query_params[0] : array();
565
        // if we have count make sure we don't include group by
566
        if ($count && isset($query_params['group_by'])) {
567
            unset($query_params['group_by']);
568
        }
569
        // let's add specific query_params for active_events
570
        // keep in mind this will override any sent status in the query AND any date queries.
571
        if (isset($where_params['status'])) {
572
            unset($where_params['status']);
573
        }
574
        $exclude_query = $query_params;
575
        if (isset($exclude_query[0])) {
576
            unset($exclude_query[0]);
577
        }
578
        $exclude_query[0] = array(
579
            'Datetime.DTT_EVT_end' => array(
580
                '>',
581
                EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end'),
582
            ),
583
        );
584
        // first get all events that have datetimes where its not expired.
585
        $event_ids = $this->_get_all_wpdb_results($exclude_query, OBJECT_K, 'Event_CPT.ID');
586
        $event_ids = array_keys($event_ids);
587
        // if we have any additional query_params, let's add them to the 'AND' condition
588
        $and_condition = array(
589
            'Datetime.DTT_EVT_end' => array('<', EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end')),
590
            'EVT_ID'               => array('NOT IN', $event_ids),
591
        );
592 View Code Duplication
        if (isset($where_params['OR'])) {
593
            $and_condition['OR'] = $where_params['OR'];
594
            unset($where_params['OR']);
595
        }
596 View Code Duplication
        if (isset($where_params['Datetime.DTT_EVT_end'])) {
597
            $and_condition['Datetime.DTT_EVT_end****'] = $where_params['Datetime.DTT_EVT_end'];
598
            unset($where_params['Datetime.DTT_EVT_end']);
599
        }
600 View Code Duplication
        if (isset($where_params['Datetime.DTT_EVT_start'])) {
601
            $and_condition['Datetime.DTT_EVT_start'] = $where_params['Datetime.DTT_EVT_start'];
602
            unset($where_params['Datetime.DTT_EVT_start']);
603
        }
604
        // merge remaining $where params with the and conditions.
605
        $where_params['AND'] = array_merge($and_condition, $where_params);
606
        $query_params[0] = $where_params;
607
        // don't use $query_params with count()
608
        // because we don't want to include additional query clauses like "GROUP BY"
609
        return $count
610
            ? $this->count(array($where_params), 'EVT_ID', true)
611
            : $this->get_all($query_params);
612
    }
613
614
615
616
    /**
617
     * This basically just returns the events that do not have the publish status.
618
     *
619
     * @param  array   $query_params An array of query params to further filter on
620
     *                               (note that status will be overwritten)
621
     * @param  boolean $count        whether to return the count or not (default FALSE)
622
     * @return EE_Event[]|int
623
     * @throws \EE_Error
624
     */
625
    public function get_inactive_events($query_params, $count = false)
626
    {
627
        $where_params = isset($query_params[0]) ? $query_params[0] : array();
628
        // let's add in specific query_params for inactive events.
629
        if (isset($where_params['status'])) {
630
            unset($where_params['status']);
631
        }
632
        // if we have count make sure we don't include group by
633
        if ($count && isset($query_params['group_by'])) {
634
            unset($query_params['group_by']);
635
        }
636
        // if we have any additional query_params, let's add them to the 'AND' condition
637
        $where_params['AND']['status'] = array('!=', 'publish');
638 View Code Duplication
        if (isset($where_params['OR'])) {
639
            $where_params['AND']['OR'] = $where_params['OR'];
640
            unset($where_params['OR']);
641
        }
642 View Code Duplication
        if (isset($where_params['Datetime.DTT_EVT_end'])) {
643
            $where_params['AND']['Datetime.DTT_EVT_end****'] = $where_params['Datetime.DTT_EVT_end'];
644
            unset($where_params['Datetime.DTT_EVT_end']);
645
        }
646 View Code Duplication
        if (isset($where_params['Datetime.DTT_EVT_start'])) {
647
            $where_params['AND']['Datetime.DTT_EVT_start'] = $where_params['Datetime.DTT_EVT_start'];
648
            unset($where_params['Datetime.DTT_EVT_start']);
649
        }
650
        $query_params[0] = $where_params;
651
        // don't use $query_params with count()
652
        // because we don't want to include additional query clauses like "GROUP BY"
653
        return $count
654
            ? $this->count(array($where_params), 'EVT_ID', true)
655
            : $this->get_all($query_params);
656
    }
657
658
659
660
    /**
661
     * This is just injecting into the parent add_relationship_to so we do special handling on price relationships
662
     * because we don't want to override any existing global default prices but instead insert NEW prices that get
663
     * attached to the event. See parent for param descriptions
664
     *
665
     * @param        $id_or_obj
666
     * @param        $other_model_id_or_obj
667
     * @param string $relationName
668
     * @param array  $where_query
669
     * @return EE_Base_Class
670
     * @throws EE_Error
671
     */
672
    public function add_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query = array())
673
    {
674
        if ($relationName === 'Price') {
675
            //let's get the PRC object for the given ID to make sure that we aren't dealing with a default
676
            $prc_chk = $this->get_related_model_obj($relationName)->ensure_is_obj($other_model_id_or_obj);
677
            //if EVT_ID = 0, then this is a default
678
            if ((int) $prc_chk->get('EVT_ID') === 0) {
679
                //let's set the prc_id as 0 so we force an insert on the add_relation_to carried out by relation
680
                $prc_chk->set('PRC_ID', 0);
681
            }
682
            //run parent
683
            return parent::add_relationship_to($id_or_obj, $prc_chk, $relationName, $where_query);
684
        }
685
        //otherwise carry on as normal
686
        return parent::add_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query);
687
    }
688
689
690
691
    /******************** DEPRECATED METHODS ********************/
692
693
694
695
    /**
696
     * _get_question_target_db_column
697
     *
698
     * @deprecated as of 4.8.32.rc.001. Instead consider using
699
     *             EE_Registration_Custom_Questions_Form located in
700
     *             admin_pages/registrations/form_sections/EE_Registration_Custom_Questions_Form.form.php
701
     * @access     public
702
     * @param    EE_Registration $registration (so existing answers for registration are included)
703
     * @param    int             $EVT_ID       so all question groups are included for event (not just answers from
704
     *                                         registration).
705
     * @throws EE_Error
706
     * @return    array
707
     */
708
    public function assemble_array_of_groups_questions_and_options(EE_Registration $registration, $EVT_ID = 0)
709
    {
710
        if (empty($EVT_ID)) {
711
            throw new EE_Error(__('An error occurred. No EVT_ID is included.  Needed to know which question groups to retrieve.',
712
                'event_espresso'));
713
        }
714
        $questions = array();
715
        // get all question groups for event
716
        $qgs = $this->get_question_groups_for_event($EVT_ID, $registration);
717
        if (! empty($qgs)) {
718
            foreach ($qgs as $qg) {
0 ignored issues
show
Bug introduced by
The expression $qgs of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
719
                $qsts = $qg->questions();
720
                $questions[$qg->ID()] = $qg->model_field_array();
721
                $questions[$qg->ID()]['QSG_questions'] = array();
722
                foreach ($qsts as $qst) {
723
                    if ($qst->is_system_question()) {
724
                        continue;
725
                    }
726
                    $answer = EEM_Answer::instance()->get_one(array(
727
                        array(
728
                            'QST_ID' => $qst->ID(),
729
                            'REG_ID' => $registration->ID(),
730
                        ),
731
                    ));
732
                    $answer = $answer instanceof EE_Answer ? $answer : EEM_Answer::instance()->create_default_object();
733
                    $qst_name = $qstn_id = $qst->ID();
734
                    $ans_id = $answer->ID();
735
                    $qst_name = ! empty($ans_id) ? '[' . $qst_name . '][' . $ans_id . ']' : '[' . $qst_name . ']';
736
                    $input_name = '';
737
                    $input_id = sanitize_key($qst->display_text());
738
                    $input_class = '';
739
                    $questions[$qg->ID()]['QSG_questions'][$qst->ID()] = $qst->model_field_array();
740
                    $questions[$qg->ID()]['QSG_questions'][$qst->ID()]['QST_input_name'] = 'qstn'
741
                                                                                           . $input_name
742
                                                                                           . $qst_name;
743
                    $questions[$qg->ID()]['QSG_questions'][$qst->ID()]['QST_input_id'] = $input_id . '-' . $qstn_id;
744
                    $questions[$qg->ID()]['QSG_questions'][$qst->ID()]['QST_input_class'] = $input_class;
745
                    $questions[$qg->ID()]['QSG_questions'][$qst->ID()]['QST_options'] = array();
746
                    $questions[$qg->ID()]['QSG_questions'][$qst->ID()]['qst_obj'] = $qst;
747
                    $questions[$qg->ID()]['QSG_questions'][$qst->ID()]['ans_obj'] = $answer;
748
                    //leave responses as-is, don't convert stuff into html entities please!
749
                    $questions[$qg->ID()]['QSG_questions'][$qst->ID()]['htmlentities'] = false;
750
                    if ($qst->type() == 'RADIO_BTN' || $qst->type() == 'CHECKBOX' || $qst->type() == 'DROPDOWN') {
751
                        $QSOs = $qst->options(true, $answer->value());
0 ignored issues
show
Documentation Bug introduced by
The method value does not exist on object<EE_Base_Class>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
752
                        if (is_array($QSOs)) {
753
                            foreach ($QSOs as $QSO_ID => $QSO) {
754
                                $questions[$qg->ID()]['QSG_questions'][$qst->ID()]['QST_options'][$QSO_ID] = $QSO->model_field_array();
755
                            }
756
                        }
757
                    }
758
                }
759
            }
760
        }
761
        return $questions;
762
    }
763
764
765
}
766
// End of file EEM_Event.model.php
767
// Location: /includes/models/EEM_Event.model.php
768