Completed
Branch EDTR/master (83b47e)
by
unknown
25:37 queued 16:41
created

Extend_Events_Admin_Page::handleActionRequest()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\domain\services\admin\events\default_settings\AdvancedEditorAdminFormSection;
4
use EventEspresso\core\exceptions\InvalidDataTypeException;
5
use EventEspresso\core\exceptions\InvalidInterfaceException;
6
use EventEspresso\core\services\loaders\LoaderFactory;
7
8
/**
9
 * Extend_Events_Admin_Page
10
 * This is the Events Caffeinated admin page.
11
 *
12
 * @package         Extend_Events_Admin_Page
13
 * @subpackage      includes/core/admin/Extend_Events_Admin_Page.core.php
14
 * @author          Darren Ethier
15
 */
16
class Extend_Events_Admin_Page extends Events_Admin_Page
17
{
18
19
    /**
20
     * @var EE_Admin_Config
21
     */
22
    protected $admin_config;
23
24
    /**
25
     * @var AdvancedEditorAdminFormSection
26
     */
27
    protected $advanced_editor_admin_form;
28
29
30
    /**
31
     * Extend_Events_Admin_Page constructor.
32
     *
33
     * @param bool $routing
34
     * @throws ReflectionException
35
     */
36
    public function __construct($routing = true)
37
    {
38
        if (! defined('EVENTS_CAF_TEMPLATE_PATH')) {
39
            define('EVENTS_CAF_TEMPLATE_PATH', EE_CORE_CAF_ADMIN_EXTEND . 'events/templates/');
40
            define('EVENTS_CAF_ASSETS', EE_CORE_CAF_ADMIN_EXTEND . 'events/assets/');
41
            define('EVENTS_CAF_ASSETS_URL', EE_CORE_CAF_ADMIN_EXTEND_URL . 'events/assets/');
42
        }
43
        parent::__construct($routing);
44
        $this->admin_config = $this->loader->getShared('EE_Admin_Config');
45
    }
46
47
48
    /**
49
     * Sets routes.
50
     *
51
     * @throws EE_Error
52
     * @throws InvalidArgumentException
53
     * @throws InvalidDataTypeException
54
     * @throws InvalidInterfaceException
55
     * @throws Exception
56
     */
57
    protected function _extend_page_config()
58
    {
59
        $this->_admin_base_path = EE_CORE_CAF_ADMIN_EXTEND . 'events';
60
        // is there a evt_id in the request?
61
        $evt_id = ! empty($this->_req_data['EVT_ID']) && ! is_array($this->_req_data['EVT_ID'])
62
            ? $this->_req_data['EVT_ID']
63
            : 0;
64
        $evt_id = ! empty($this->_req_data['post']) ? $this->_req_data['post'] : $evt_id;
65
        // tkt_id?
66
        $tkt_id = ! empty($this->_req_data['TKT_ID']) && ! is_array($this->_req_data['TKT_ID'])
67
            ? $this->_req_data['TKT_ID']
68
            : 0;
69
        $new_page_routes = array(
70
            'duplicate_event'          => array(
71
                'func'       => '_duplicate_event',
72
                'capability' => 'ee_edit_event',
73
                'obj_id'     => $evt_id,
74
                'noheader'   => true,
75
            ),
76
            'ticket_list_table'        => array(
77
                'func'       => '_tickets_overview_list_table',
78
                'capability' => 'ee_read_default_tickets',
79
            ),
80
            'trash_ticket'             => array(
81
                'func'       => '_trash_or_restore_ticket',
82
                'capability' => 'ee_delete_default_ticket',
83
                'obj_id'     => $tkt_id,
84
                'noheader'   => true,
85
                'args'       => array('trash' => true),
86
            ),
87
            'trash_tickets'            => array(
88
                'func'       => '_trash_or_restore_ticket',
89
                'capability' => 'ee_delete_default_tickets',
90
                'noheader'   => true,
91
                'args'       => array('trash' => true),
92
            ),
93
            'restore_ticket'           => array(
94
                'func'       => '_trash_or_restore_ticket',
95
                'capability' => 'ee_delete_default_ticket',
96
                'obj_id'     => $tkt_id,
97
                'noheader'   => true,
98
            ),
99
            'restore_tickets'          => array(
100
                'func'       => '_trash_or_restore_ticket',
101
                'capability' => 'ee_delete_default_tickets',
102
                'noheader'   => true,
103
            ),
104
            'delete_ticket'            => array(
105
                'func'       => '_delete_ticket',
106
                'capability' => 'ee_delete_default_ticket',
107
                'obj_id'     => $tkt_id,
108
                'noheader'   => true,
109
            ),
110
            'delete_tickets'           => array(
111
                'func'       => '_delete_ticket',
112
                'capability' => 'ee_delete_default_tickets',
113
                'noheader'   => true,
114
            ),
115
            'import_page'              => array(
116
                'func'       => '_import_page',
117
                'capability' => 'import',
118
            ),
119
            'import'                   => array(
120
                'func'       => '_import_events',
121
                'capability' => 'import',
122
                'noheader'   => true,
123
            ),
124
            'import_events'            => array(
125
                'func'       => '_import_events',
126
                'capability' => 'import',
127
                'noheader'   => true,
128
            ),
129
            'export_events'            => array(
130
                'func'       => '_events_export',
131
                'capability' => 'export',
132
                'noheader'   => true,
133
            ),
134
            'export_categories'        => array(
135
                'func'       => '_categories_export',
136
                'capability' => 'export',
137
                'noheader'   => true,
138
            ),
139
            'sample_export_file'       => array(
140
                'func'       => '_sample_export_file',
141
                'capability' => 'export',
142
                'noheader'   => true,
143
            ),
144
            'update_template_settings' => array(
145
                'func'       => '_update_template_settings',
146
                'capability' => 'manage_options',
147
                'noheader'   => true,
148
            ),
149
        );
150
        $this->_page_routes = array_merge($this->_page_routes, $new_page_routes);
151
        // partial route/config override
152
        $this->_page_config['import_events']['metaboxes'] = $this->_default_espresso_metaboxes;
153
        $this->_page_config['create_new']['metaboxes'][] = '_premium_event_editor_meta_boxes';
154
        // don't load qTips if using the advanced editor
155
        if (! $this->admin_config->useAdvancedEditor()) {
156
            $this->_page_config['create_new']['qtips'][] = 'EE_Event_Editor_Tips';
157
            $this->_page_config['edit']['qtips'][] = 'EE_Event_Editor_Tips';
158
        }
159
        $this->_page_config['edit']['metaboxes'][] = '_premium_event_editor_meta_boxes';
160
        $this->_page_config['default']['list_table'] = 'Extend_Events_Admin_List_Table';
161
        // add tickets tab but only if there are more than one default ticket!
162
        $tkt_count = EEM_Ticket::instance()->count_deleted_and_undeleted(
163
            array(array('TKT_is_default' => 1)),
164
            'TKT_ID',
165
            true
166
        );
167
        if ($tkt_count > 1) {
168
            $new_page_config = array(
169
                'ticket_list_table' => array(
170
                    'nav'           => array(
171
                        'label' => esc_html__('Default Tickets', 'event_espresso'),
172
                        'order' => 60,
173
                    ),
174
                    'list_table'    => 'Tickets_List_Table',
175
                    'require_nonce' => false,
176
                ),
177
            );
178
        }
179
        // template settings
180
        $new_page_config['template_settings'] = array(
181
            'nav'           => array(
182
                'label' => esc_html__('Templates', 'event_espresso'),
183
                'order' => 30,
184
            ),
185
            'metaboxes'     => array_merge($this->_default_espresso_metaboxes, array('_publish_post_box')),
186
            'help_tabs'     => array(
187
                'general_settings_templates_help_tab' => array(
188
                    'title'    => esc_html__('Templates', 'event_espresso'),
189
                    'filename' => 'general_settings_templates',
190
                ),
191
            ),
192
            'help_tour'     => array('Templates_Help_Tour'),
193
            'require_nonce' => false,
194
        );
195
        $this->_page_config = array_merge($this->_page_config, $new_page_config);
196
        // add filters and actions
197
        // modifying _views
198
        add_filter(
199
            'FHEE_event_datetime_metabox_add_additional_date_time_template',
200
            array($this, 'add_additional_datetime_button'),
201
            10,
202
            2
203
        );
204
        add_filter(
205
            'FHEE_event_datetime_metabox_clone_button_template',
206
            array($this, 'add_datetime_clone_button'),
207
            10,
208
            2
209
        );
210
        add_filter(
211
            'FHEE_event_datetime_metabox_timezones_template',
212
            array($this, 'datetime_timezones_template'),
213
            10,
214
            2
215
        );
216
        // filters for event list table
217
        add_filter('FHEE__Extend_Events_Admin_List_Table__filters', array($this, 'list_table_filters'), 10, 2);
218
        add_filter(
219
            'FHEE__Events_Admin_List_Table__column_actions__action_links',
220
            array($this, 'extra_list_table_actions'),
221
            10,
222
            2
223
        );
224
        // legend item
225
        add_filter('FHEE__Events_Admin_Page___event_legend_items__items', array($this, 'additional_legend_items'));
226
        add_action('admin_init', array($this, 'admin_init'));
227
       // load additional handlers
228
        $this->handleActionRequest();
229
    }
230
231
232
    private function getRequestAction()
233
    {
234
        return isset($this->_req_data['action']) ? sanitize_key($this->_req_data['action']) : null;
235
    }
236
237
238
    /**
239
     * @throws Exception
240
     */
241
    private function handleActionRequest()
242
    {
243
        $action = $this->getRequestAction();
244
        if ($action) {
245
            // setup Advanced Editor ???
246
            if ($action === 'default_event_settings' || $action === 'update_default_event_settings') {
247
                $this->advanced_editor_admin_form = $this->loader->getShared(
248
                    'EventEspresso\core\domain\services\admin\events\default_settings\AdvancedEditorAdminFormSection'
249
                );
250
            }
251
        }
252
    }
253
254
255
    /**
256
     * admin_init
257
     */
258
    public function admin_init()
259
    {
260
        EE_Registry::$i18n_js_strings = array_merge(
261
            EE_Registry::$i18n_js_strings,
262
            array(
263
                'image_confirm'          => esc_html__(
264
                    'Do you really want to delete this image? Please remember to update your event to complete the removal.',
265
                    'event_espresso'
266
                ),
267
                'event_starts_on'        => esc_html__('Event Starts on', 'event_espresso'),
268
                'event_ends_on'          => esc_html__('Event Ends on', 'event_espresso'),
269
                'event_datetime_actions' => esc_html__('Actions', 'event_espresso'),
270
                'event_clone_dt_msg'     => esc_html__('Clone this Event Date and Time', 'event_espresso'),
271
                'remove_event_dt_msg'    => esc_html__('Remove this Event Time', 'event_espresso'),
272
            )
273
        );
274
    }
275
276
277
    /**
278
     * Add per page screen options to the default ticket list table view.
279
     *
280
     * @throws InvalidArgumentException
281
     * @throws InvalidDataTypeException
282
     * @throws InvalidInterfaceException
283
     */
284
    protected function _add_screen_options_ticket_list_table()
285
    {
286
        $this->_per_page_screen_option();
287
    }
288
289
290
    /**
291
     * @param string $return
292
     * @param int    $id
293
     * @param string $new_title
294
     * @param string $new_slug
295
     * @return string
296
     */
297
    public function extra_permalink_field_buttons($return, $id, $new_title, $new_slug)
298
    {
299
        $return = parent::extra_permalink_field_buttons($return, $id, $new_title, $new_slug);
300
        // make sure this is only when editing
301
        if (! empty($id)) {
302
            $href = EE_Admin_Page::add_query_args_and_nonce(
303
                array('action' => 'duplicate_event', 'EVT_ID' => $id),
304
                $this->_admin_base_url
305
            );
306
            $title = esc_attr__('Duplicate Event', 'event_espresso');
307
            $return .= '<a href="'
308
                       . $href
309
                       . '" title="'
310
                       . $title
311
                       . '" id="ee-duplicate-event-button" class="button button-small"  value="duplicate_event">'
312
                       . $title
313
                       . '</a>';
314
        }
315
        return $return;
316
    }
317
318
319
    /**
320
     * Set the list table views for the default ticket list table view.
321
     */
322 View Code Duplication
    public function _set_list_table_views_ticket_list_table()
323
    {
324
        $this->_views = array(
325
            'all'     => array(
326
                'slug'        => 'all',
327
                'label'       => esc_html__('All', 'event_espresso'),
328
                'count'       => 0,
329
                'bulk_action' => array(
330
                    'trash_tickets' => esc_html__('Move to Trash', 'event_espresso'),
331
                ),
332
            ),
333
            'trashed' => array(
334
                'slug'        => 'trashed',
335
                'label'       => esc_html__('Trash', 'event_espresso'),
336
                'count'       => 0,
337
                'bulk_action' => array(
338
                    'restore_tickets' => esc_html__('Restore from Trash', 'event_espresso'),
339
                    'delete_tickets'  => esc_html__('Delete Permanently', 'event_espresso'),
340
                ),
341
            ),
342
        );
343
    }
344
345
346
    /**
347
     * Enqueue scripts and styles for the event editor.
348
     */
349 View Code Duplication
    public function load_scripts_styles_edit()
350
    {
351
        wp_register_script(
352
            'ee-event-editor-heartbeat',
353
            EVENTS_CAF_ASSETS_URL . 'event-editor-heartbeat.js',
354
            array('ee_admin_js', 'heartbeat'),
355
            EVENT_ESPRESSO_VERSION,
356
            true
357
        );
358
        wp_enqueue_script('ee-accounting');
359
        // styles
360
        wp_enqueue_style('espresso-ui-theme');
361
        wp_enqueue_script('event_editor_js');
362
        wp_enqueue_script('ee-event-editor-heartbeat');
363
    }
364
365
366
    /**
367
     * Returns template for the additional datetime.
368
     *
369
     * @param $template
370
     * @param $template_args
371
     * @return mixed
372
     * @throws DomainException
373
     */
374
    public function add_additional_datetime_button($template, $template_args)
375
    {
376
        return EEH_Template::display_template(
377
            EVENTS_CAF_TEMPLATE_PATH . 'event_datetime_add_additional_time.template.php',
378
            $template_args,
379
            true
380
        );
381
    }
382
383
384
    /**
385
     * Returns the template for cloning a datetime.
386
     *
387
     * @param $template
388
     * @param $template_args
389
     * @return mixed
390
     * @throws DomainException
391
     */
392
    public function add_datetime_clone_button($template, $template_args)
393
    {
394
        return EEH_Template::display_template(
395
            EVENTS_CAF_TEMPLATE_PATH . 'event_datetime_metabox_clone_button.template.php',
396
            $template_args,
397
            true
398
        );
399
    }
400
401
402
    /**
403
     * Returns the template for datetime timezones.
404
     *
405
     * @param $template
406
     * @param $template_args
407
     * @return mixed
408
     * @throws DomainException
409
     */
410
    public function datetime_timezones_template($template, $template_args)
411
    {
412
        return EEH_Template::display_template(
413
            EVENTS_CAF_TEMPLATE_PATH . 'event_datetime_timezones.template.php',
414
            $template_args,
415
            true
416
        );
417
    }
418
419
420
    /**
421
     * Sets the views for the default list table view.
422
     *
423
     * @throws EE_Error
424
     */
425
    protected function _set_list_table_views_default()
426
    {
427
        parent::_set_list_table_views_default();
428
        $new_views = array(
429
            'today' => array(
430
                'slug'        => 'today',
431
                'label'       => esc_html__('Today', 'event_espresso'),
432
                'count'       => $this->total_events_today(),
433
                'bulk_action' => array(
434
                    'trash_events' => esc_html__('Move to Trash', 'event_espresso'),
435
                ),
436
            ),
437
            'month' => array(
438
                'slug'        => 'month',
439
                'label'       => esc_html__('This Month', 'event_espresso'),
440
                'count'       => $this->total_events_this_month(),
441
                'bulk_action' => array(
442
                    'trash_events' => esc_html__('Move to Trash', 'event_espresso'),
443
                ),
444
            ),
445
        );
446
        $this->_views = array_merge($this->_views, $new_views);
447
    }
448
449
450
    /**
451
     * Returns the extra action links for the default list table view.
452
     *
453
     * @param array    $action_links
454
     * @param EE_Event $event
455
     * @return array
456
     * @throws EE_Error
457
     * @throws InvalidArgumentException
458
     * @throws InvalidDataTypeException
459
     * @throws InvalidInterfaceException
460
     * @throws ReflectionException
461
     */
462
    public function extra_list_table_actions(array $action_links, EE_Event $event)
463
    {
464 View Code Duplication
        if (EE_Registry::instance()->CAP->current_user_can(
465
            'ee_read_registrations',
466
            'espresso_registrations_reports',
467
            $event->ID()
468
        )
469
        ) {
470
            $reports_query_args = array(
471
                'action' => 'reports',
472
                'EVT_ID' => $event->ID(),
473
            );
474
            $reports_link = EE_Admin_Page::add_query_args_and_nonce($reports_query_args, REG_ADMIN_URL);
475
            $action_links[] = '<a href="'
476
                              . $reports_link
477
                              . '" title="'
478
                              . esc_attr__('View Report', 'event_espresso')
479
                              . '"><div class="dashicons dashicons-chart-bar"></div></a>'
480
                              . "\n\t";
481
        }
482
        if (EE_Registry::instance()->CAP->current_user_can('ee_read_global_messages', 'view_filtered_messages')) {
483
            EE_Registry::instance()->load_helper('MSG_Template');
484
            $action_links[] = EEH_MSG_Template::get_message_action_link(
485
                'see_notifications_for',
486
                null,
487
                array('EVT_ID' => $event->ID())
488
            );
489
        }
490
        return $action_links;
491
    }
492
493
494
    /**
495
     * @param $items
496
     * @return mixed
497
     */
498
    public function additional_legend_items($items)
499
    {
500
        if (EE_Registry::instance()->CAP->current_user_can(
501
            'ee_read_registrations',
502
            'espresso_registrations_reports'
503
        )
504
        ) {
505
            $items['reports'] = array(
506
                'class' => 'dashicons dashicons-chart-bar',
507
                'desc'  => esc_html__('Event Reports', 'event_espresso'),
508
            );
509
        }
510 View Code Duplication
        if (EE_Registry::instance()->CAP->current_user_can('ee_read_global_messages', 'view_filtered_messages')) {
511
            $related_for_icon = EEH_MSG_Template::get_message_action_icon('see_notifications_for');
512
            // $related_for_icon can sometimes be a string so 'css_class' would be an illegal offset
513
            // (can only use numeric offsets when treating strings as arrays)
514
            if (is_array($related_for_icon) && isset($related_for_icon['css_class'], $related_for_icon['label'])) {
515
                $items['view_related_messages'] = array(
516
                    'class' => $related_for_icon['css_class'],
517
                    'desc'  => $related_for_icon['label'],
518
                );
519
            }
520
        }
521
        return $items;
522
    }
523
524
525
    /**
526
     * This is the callback method for the duplicate event route
527
     * Method looks for 'EVT_ID' in the request and retrieves that event and its details and duplicates them
528
     * into a new event.  We add a hook so that any plugins that add extra event details can hook into this
529
     * action.  Note that the dupe will have **DUPLICATE** as its title and slug.
530
     * After duplication the redirect is to the new event edit page.
531
     *
532
     * @return void
533
     * @throws EE_Error If EE_Event is not available with given ID
534
     * @throws InvalidArgumentException
535
     * @throws InvalidDataTypeException
536
     * @throws InvalidInterfaceException
537
     * @throws ReflectionException
538
     * @access protected
539
     */
540
    protected function _duplicate_event()
541
    {
542
        // first make sure the ID for the event is in the request.
543
        //  If it isn't then we need to bail and redirect back to overview list table (cause how did we get here?)
544 View Code Duplication
        if (! isset($this->_req_data['EVT_ID'])) {
545
            EE_Error::add_error(
546
                esc_html__(
547
                    'In order to duplicate an event an Event ID is required.  None was given.',
548
                    'event_espresso'
549
                ),
550
                __FILE__,
551
                __FUNCTION__,
552
                __LINE__
553
            );
554
            $this->_redirect_after_action(false, '', '', array(), true);
555
            return;
556
        }
557
        // k we've got EVT_ID so let's use that to get the event we'll duplicate
558
        $orig_event = EEM_Event::instance()->get_one_by_ID($this->_req_data['EVT_ID']);
559
        if (! $orig_event instanceof EE_Event) {
560
            throw new EE_Error(
561
                sprintf(
562
                    esc_html__('An EE_Event object could not be retrieved for the given ID (%s)', 'event_espresso'),
563
                    $this->_req_data['EVT_ID']
564
                )
565
            );
566
        }
567
        // k now let's clone the $orig_event before getting relations
568
        $new_event = clone $orig_event;
569
        // original datetimes
570
        $orig_datetimes = $orig_event->get_many_related('Datetime');
571
        // other original relations
572
        $orig_ven = $orig_event->get_many_related('Venue');
573
        // reset the ID and modify other details to make it clear this is a dupe
574
        $new_event->set('EVT_ID', 0);
575
        $new_name = $new_event->name() . ' ' . esc_html__('**DUPLICATE**', 'event_espresso');
576
        $new_event->set('EVT_name', $new_name);
577
        $new_event->set(
578
            'EVT_slug',
579
            wp_unique_post_slug(
580
                sanitize_title($orig_event->name()),
581
                0,
582
                'publish',
583
                'espresso_events',
584
                0
585
            )
586
        );
587
        $new_event->set('status', 'draft');
588
        // duplicate discussion settings
589
        $new_event->set('comment_status', $orig_event->get('comment_status'));
590
        $new_event->set('ping_status', $orig_event->get('ping_status'));
591
        // save the new event
592
        $new_event->save();
593
        // venues
594
        foreach ($orig_ven as $ven) {
595
            $new_event->_add_relation_to($ven, 'Venue');
596
        }
597
        $new_event->save();
598
        // now we need to get the question group relations and handle that
599
        // first primary question groups
600
        $orig_primary_qgs = $orig_event->get_many_related(
601
            'Question_Group',
602
            [['Event_Question_Group.EQG_primary' => true]]
603
        );
604 View Code Duplication
        if (! empty($orig_primary_qgs)) {
605
            foreach ($orig_primary_qgs as $id => $obj) {
606
                if ($obj instanceof EE_Question_Group) {
607
                    $new_event->_add_relation_to($obj, 'Question_Group', ['EQG_primary' => true]);
608
                }
609
            }
610
        }
611
        // next additional attendee question groups
612
        $orig_additional_qgs = $orig_event->get_many_related(
613
            'Question_Group',
614
            [['Event_Question_Group.EQG_additional' => true]]
615
        );
616 View Code Duplication
        if (! empty($orig_additional_qgs)) {
617
            foreach ($orig_additional_qgs as $id => $obj) {
618
                if ($obj instanceof EE_Question_Group) {
619
                    $new_event->_add_relation_to($obj, 'Question_Group', ['EQG_additional' => true]);
620
                }
621
            }
622
        }
623
624
        $new_event->save();
625
626
        // k now that we have the new event saved we can loop through the datetimes and start adding relations.
627
        $cloned_tickets = array();
628
        foreach ($orig_datetimes as $orig_dtt) {
629
            if (! $orig_dtt instanceof EE_Datetime) {
630
                continue;
631
            }
632
            $new_dtt = clone $orig_dtt;
633
            $orig_tkts = $orig_dtt->tickets();
634
            // save new dtt then add to event
635
            $new_dtt->set('DTT_ID', 0);
636
            $new_dtt->set('DTT_sold', 0);
637
            $new_dtt->set_reserved(0);
638
            $new_dtt->save();
639
            $new_event->_add_relation_to($new_dtt, 'Datetime');
640
            $new_event->save();
641
            // now let's get the ticket relations setup.
642
            foreach ((array) $orig_tkts as $orig_tkt) {
643
                // it's possible a datetime will have no tickets so let's verify we HAVE a ticket first.
644
                if (! $orig_tkt instanceof EE_Ticket) {
645
                    continue;
646
                }
647
                // is this ticket archived?  If it is then let's skip
648
                if ($orig_tkt->get('TKT_deleted')) {
649
                    continue;
650
                }
651
                // does this original ticket already exist in the clone_tickets cache?
652
                //  If so we'll just use the new ticket from it.
653
                if (isset($cloned_tickets[ $orig_tkt->ID() ])) {
654
                    $new_tkt = $cloned_tickets[ $orig_tkt->ID() ];
655
                } else {
656
                    $new_tkt = clone $orig_tkt;
657
                    // get relations on the $orig_tkt that we need to setup.
658
                    $orig_prices = $orig_tkt->prices();
659
                    $new_tkt->set('TKT_ID', 0);
660
                    $new_tkt->set('TKT_sold', 0);
661
                    $new_tkt->set('TKT_reserved', 0);
662
                    $new_tkt->save(); // make sure new ticket has ID.
663
                    // price relations on new ticket need to be setup.
664
                    foreach ($orig_prices as $orig_price) {
665
                        $new_price = clone $orig_price;
666
                        $new_price->set('PRC_ID', 0);
667
                        $new_price->save();
668
                        $new_tkt->_add_relation_to($new_price, 'Price');
669
                        $new_tkt->save();
670
                    }
671
672
                    do_action(
673
                        'AHEE__Extend_Events_Admin_Page___duplicate_event__duplicate_ticket__after',
674
                        $orig_tkt,
675
                        $new_tkt,
676
                        $orig_prices,
677
                        $orig_event,
678
                        $orig_dtt,
679
                        $new_dtt
680
                    );
681
                }
682
                // k now we can add the new ticket as a relation to the new datetime
683
                // and make sure its added to our cached $cloned_tickets array
684
                // for use with later datetimes that have the same ticket.
685
                $new_dtt->_add_relation_to($new_tkt, 'Ticket');
686
                $new_dtt->save();
687
                $cloned_tickets[ $orig_tkt->ID() ] = $new_tkt;
688
            }
689
        }
690
        // clone taxonomy information
691
        $taxonomies_to_clone_with = apply_filters(
692
            'FHEE__Extend_Events_Admin_Page___duplicate_event__taxonomies_to_clone',
693
            array('espresso_event_categories', 'espresso_event_type', 'post_tag')
694
        );
695
        // get terms for original event (notice)
696
        $orig_terms = wp_get_object_terms($orig_event->ID(), $taxonomies_to_clone_with);
697
        // loop through terms and add them to new event.
698
        foreach ($orig_terms as $term) {
699
            wp_set_object_terms($new_event->ID(), $term->term_id, $term->taxonomy, true);
700
        }
701
702
        // duplicate other core WP_Post items for this event.
703
        // post thumbnail (feature image).
704
        $feature_image_id = get_post_thumbnail_id($orig_event->ID());
705
        if ($feature_image_id) {
706
            update_post_meta($new_event->ID(), '_thumbnail_id', $feature_image_id);
707
        }
708
709
        // duplicate page_template setting
710
        $page_template = get_post_meta($orig_event->ID(), '_wp_page_template', true);
711
        if ($page_template) {
712
            update_post_meta($new_event->ID(), '_wp_page_template', $page_template);
713
        }
714
715
        do_action('AHEE__Extend_Events_Admin_Page___duplicate_event__after', $new_event, $orig_event);
716
        // now let's redirect to the edit page for this duplicated event if we have a new event id.
717
        if ($new_event->ID()) {
718
            $redirect_args = array(
719
                'post'   => $new_event->ID(),
720
                'action' => 'edit',
721
            );
722
            EE_Error::add_success(
723
                esc_html__(
724
                    'Event successfully duplicated.  Please review the details below and make any necessary edits',
725
                    'event_espresso'
726
                )
727
            );
728
        } else {
729
            $redirect_args = array(
730
                'action' => 'default',
731
            );
732
            EE_Error::add_error(
733
                esc_html__('Not able to duplicate event.  Something went wrong.', 'event_espresso'),
734
                __FILE__,
735
                __FUNCTION__,
736
                __LINE__
737
            );
738
        }
739
        $this->_redirect_after_action(false, '', '', $redirect_args, true);
740
    }
741
742
743
    /**
744
     * Generates output for the import page.
745
     *
746
     * @throws DomainException
747
     * @throws EE_Error
748
     * @throws InvalidArgumentException
749
     * @throws InvalidDataTypeException
750
     * @throws InvalidInterfaceException
751
     */
752
    protected function _import_page()
753
    {
754
        $title = esc_html__('Import', 'event_espresso');
755
        $intro = esc_html__(
756
            'If you have a previously exported Event Espresso 4 information in a Comma Separated Value (CSV) file format, you can upload the file here: ',
757
            'event_espresso'
758
        );
759
        $form_url = EVENTS_ADMIN_URL;
760
        $action = 'import_events';
761
        $type = 'csv';
762
        $this->_template_args['form'] = EE_Import::instance()->upload_form(
763
            $title,
764
            $intro,
765
            $form_url,
766
            $action,
767
            $type
768
        );
769
        $this->_template_args['sample_file_link'] = EE_Admin_Page::add_query_args_and_nonce(
770
            array('action' => 'sample_export_file'),
771
            $this->_admin_base_url
772
        );
773
        $content = EEH_Template::display_template(
774
            EVENTS_CAF_TEMPLATE_PATH . 'import_page.template.php',
775
            $this->_template_args,
776
            true
777
        );
778
        $this->_template_args['admin_page_content'] = $content;
779
        $this->display_admin_page_with_sidebar();
780
    }
781
782
783
    /**
784
     * _import_events
785
     * This handles displaying the screen and running imports for importing events.
786
     *
787
     * @return void
788
     * @throws EE_Error
789
     * @throws InvalidArgumentException
790
     * @throws InvalidDataTypeException
791
     * @throws InvalidInterfaceException
792
     */
793
    protected function _import_events()
794
    {
795
        require_once(EE_CLASSES . 'EE_Import.class.php');
796
        $success = EE_Import::instance()->import();
797
        $this->_redirect_after_action($success, 'Import File', 'ran', array('action' => 'import_page'), true);
798
    }
799
800
801
    /**
802
     * _events_export
803
     * Will export all (or just the given event) to a Excel compatible file.
804
     *
805
     * @access protected
806
     * @return void
807
     */
808
    protected function _events_export()
809
    {
810
        if (isset($this->_req_data['EVT_ID'])) {
811
            $event_ids = $this->_req_data['EVT_ID'];
812
        } elseif (isset($this->_req_data['EVT_IDs'])) {
813
            $event_ids = $this->_req_data['EVT_IDs'];
814
        } else {
815
            $event_ids = null;
816
        }
817
        // todo: I don't like doing this but it'll do until we modify EE_Export Class.
818
        $new_request_args = array(
819
            'export' => 'report',
820
            'action' => 'all_event_data',
821
            'EVT_ID' => $event_ids,
822
        );
823
        $this->_req_data = array_merge($this->_req_data, $new_request_args);
824
        if (is_readable(EE_CLASSES . 'EE_Export.class.php')) {
825
            require_once(EE_CLASSES . 'EE_Export.class.php');
826
            $EE_Export = EE_Export::instance($this->_req_data);
827
            if ($EE_Export instanceof EE_Export) {
828
                $EE_Export->export();
829
            }
830
        }
831
    }
832
833
834
    /**
835
     * handle category exports()
836
     *
837
     * @return void
838
     */
839 View Code Duplication
    protected function _categories_export()
840
    {
841
        // todo: I don't like doing this but it'll do until we modify EE_Export Class.
842
        $new_request_args = array(
843
            'export'       => 'report',
844
            'action'       => 'categories',
845
            'category_ids' => $this->_req_data['EVT_CAT_ID'],
846
        );
847
        $this->_req_data = array_merge($this->_req_data, $new_request_args);
848
        if (is_readable(EE_CLASSES . 'EE_Export.class.php')) {
849
            require_once(EE_CLASSES . 'EE_Export.class.php');
850
            $EE_Export = EE_Export::instance($this->_req_data);
851
            if ($EE_Export instanceof EE_Export) {
852
                $EE_Export->export();
853
            }
854
        }
855
    }
856
857
858
    /**
859
     * Creates a sample CSV file for importing
860
     */
861
    protected function _sample_export_file()
862
    {
863
        $EE_Export = EE_Export::instance();
864
        if ($EE_Export instanceof EE_Export) {
865
            $EE_Export->export();
866
        }
867
    }
868
869
870
    /*************        Template Settings        *************/
871
    /**
872
     * Generates template settings page output
873
     *
874
     * @throws DomainException
875
     * @throws EE_Error
876
     * @throws InvalidArgumentException
877
     * @throws InvalidDataTypeException
878
     * @throws InvalidInterfaceException
879
     */
880
    protected function _template_settings()
881
    {
882
        $this->_template_args['values'] = $this->_yes_no_values;
883
        /**
884
         * Note leaving this filter in for backward compatibility this was moved in 4.6.x
885
         * from General_Settings_Admin_Page to here.
886
         */
887
        $this->_template_args = apply_filters(
888
            'FHEE__General_Settings_Admin_Page__template_settings__template_args',
889
            $this->_template_args
890
        );
891
        $this->_set_add_edit_form_tags('update_template_settings');
892
        $this->_set_publish_post_box_vars(null, false, false, null, false);
893
        $this->_template_args['admin_page_content'] = EEH_Template::display_template(
894
            EVENTS_CAF_TEMPLATE_PATH . 'template_settings.template.php',
895
            $this->_template_args,
896
            true
897
        );
898
        $this->display_admin_page_with_sidebar();
899
    }
900
901
902
    /**
903
     * Handler for updating template settings.
904
     *
905
     * @throws EE_Error
906
     * @throws InvalidArgumentException
907
     * @throws InvalidDataTypeException
908
     * @throws InvalidInterfaceException
909
     */
910
    protected function _update_template_settings()
911
    {
912
        /**
913
         * Note leaving this filter in for backward compatibility this was moved in 4.6.x
914
         * from General_Settings_Admin_Page to here.
915
         */
916
        EE_Registry::instance()->CFG->template_settings = apply_filters(
917
            'FHEE__General_Settings_Admin_Page__update_template_settings__data',
918
            EE_Registry::instance()->CFG->template_settings,
919
            $this->_req_data
920
        );
921
        // update custom post type slugs and detect if we need to flush rewrite rules
922
        $old_slug = EE_Registry::instance()->CFG->core->event_cpt_slug;
923
        EE_Registry::instance()->CFG->core->event_cpt_slug = empty($this->_req_data['event_cpt_slug'])
924
            ? EE_Registry::instance()->CFG->core->event_cpt_slug
925
            : EEH_URL::slugify($this->_req_data['event_cpt_slug'], 'events');
926
        $what = 'Template Settings';
927
        $success = $this->_update_espresso_configuration(
928
            $what,
929
            EE_Registry::instance()->CFG->template_settings,
930
            __FILE__,
931
            __FUNCTION__,
932
            __LINE__
933
        );
934
        if (EE_Registry::instance()->CFG->core->event_cpt_slug !== $old_slug) {
935
            /** @var EventEspresso\core\domain\services\custom_post_types\RewriteRules $rewrite_rules */
936
            $rewrite_rules = LoaderFactory::getLoader()->getShared(
937
                'EventEspresso\core\domain\services\custom_post_types\RewriteRules'
938
            );
939
            $rewrite_rules->flush();
940
        }
941
        $this->_redirect_after_action($success, $what, 'updated', array('action' => 'template_settings'));
942
    }
943
944
945
    /**
946
     * _premium_event_editor_meta_boxes
947
     * add all metaboxes related to the event_editor
948
     *
949
     * @access protected
950
     * @return void
951
     * @throws EE_Error
952
     * @throws InvalidArgumentException
953
     * @throws InvalidDataTypeException
954
     * @throws InvalidInterfaceException
955
     * @throws ReflectionException
956
     */
957
    protected function _premium_event_editor_meta_boxes()
958
    {
959
        $this->verify_cpt_object();
960
        add_meta_box(
961
            'espresso_event_editor_event_options',
962
            esc_html__('Event Registration Options', 'event_espresso'),
963
            array($this, 'registration_options_meta_box'),
964
            $this->page_slug,
965
            'side',
966
            'core'
967
        );
968
    }
969
970
971
    /**
972
     * override caf metabox
973
     *
974
     * @return void
975
     * @throws DomainException
976
     * @throws EE_Error
977
     */
978
    public function registration_options_meta_box()
979
    {
980
        $yes_no_values = array(
981
            array('id' => true, 'text' => esc_html__('Yes', 'event_espresso')),
982
            array('id' => false, 'text' => esc_html__('No', 'event_espresso')),
983
        );
984
        $default_reg_status_values = EEM_Registration::reg_status_array(
985
            array(
986
                EEM_Registration::status_id_cancelled,
987
                EEM_Registration::status_id_declined,
988
                EEM_Registration::status_id_incomplete,
989
                EEM_Registration::status_id_wait_list,
990
            ),
991
            true
992
        );
993
        $template_args['active_status'] = $this->_cpt_model_obj->pretty_active_status(false);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$template_args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $template_args = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
994
        $template_args['_event'] = $this->_cpt_model_obj;
995
        $template_args['additional_limit'] = $this->_cpt_model_obj->additional_limit();
996
        $template_args['default_registration_status'] = EEH_Form_Fields::select_input(
997
            'default_reg_status',
998
            $default_reg_status_values,
999
            $this->_cpt_model_obj->default_registration_status()
0 ignored issues
show
Bug introduced by
It seems like $this->_cpt_model_obj->d...t_registration_status() targeting EE_Event::default_registration_status() can also be of type boolean; however, EEH_Form_Fields::select_input() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
1000
        );
1001
        $template_args['display_description'] = EEH_Form_Fields::select_input(
1002
            'display_desc',
1003
            $yes_no_values,
1004
            $this->_cpt_model_obj->display_description()
1005
        );
1006
        $template_args['display_ticket_selector'] = EEH_Form_Fields::select_input(
1007
            'display_ticket_selector',
1008
            $yes_no_values,
1009
            $this->_cpt_model_obj->display_ticket_selector(),
1010
            '',
1011
            '',
1012
            false
1013
        );
1014
        $template_args['EVT_default_registration_status'] = EEH_Form_Fields::select_input(
1015
            'EVT_default_registration_status',
1016
            $default_reg_status_values,
1017
            $this->_cpt_model_obj->default_registration_status()
0 ignored issues
show
Bug introduced by
It seems like $this->_cpt_model_obj->d...t_registration_status() targeting EE_Event::default_registration_status() can also be of type boolean; however, EEH_Form_Fields::select_input() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
1018
        );
1019
        $template_args['additional_registration_options'] = apply_filters(
1020
            'FHEE__Events_Admin_Page__registration_options_meta_box__additional_registration_options',
1021
            '',
1022
            $template_args,
1023
            $yes_no_values,
1024
            $default_reg_status_values
1025
        );
1026
        EEH_Template::display_template(
1027
            EVENTS_CAF_TEMPLATE_PATH . 'event_registration_options.template.php',
1028
            $template_args
1029
        );
1030
    }
1031
1032
1033
1034
    /**
1035
     * wp_list_table_mods for caf
1036
     * ============================
1037
     */
1038
    /**
1039
     * hook into list table filters and provide filters for caffeinated list table
1040
     *
1041
     * @param array $old_filters    any existing filters present
1042
     * @param array $list_table_obj the list table object
1043
     * @return array                  new filters
1044
     * @throws EE_Error
1045
     * @throws InvalidArgumentException
1046
     * @throws InvalidDataTypeException
1047
     * @throws InvalidInterfaceException
1048
     * @throws ReflectionException
1049
     */
1050
    public function list_table_filters($old_filters, $list_table_obj)
1051
    {
1052
        $filters = array();
1053
        // first month/year filters
1054
        $filters[] = $this->espresso_event_months_dropdown();
1055
        $status = isset($this->_req_data['status']) ? $this->_req_data['status'] : null;
1056
        // active status dropdown
1057
        if ($status !== 'draft') {
1058
            $filters[] = $this->active_status_dropdown(
1059
                isset($this->_req_data['active_status']) ? $this->_req_data['active_status'] : ''
1060
            );
1061
            $filters[] = $this->venuesDropdown(
1062
                isset($this->_req_data['venue']) ? $this->_req_data['venue'] : ''
1063
            );
1064
        }
1065
        // category filter
1066
        $filters[] = $this->category_dropdown();
1067
        return array_merge($old_filters, $filters);
1068
    }
1069
1070
1071
    /**
1072
     * espresso_event_months_dropdown
1073
     *
1074
     * @access public
1075
     * @return string                dropdown listing month/year selections for events.
1076
     */
1077
    public function espresso_event_months_dropdown()
1078
    {
1079
        // what we need to do is get all PRIMARY datetimes for all events to filter on.
1080
        // Note we need to include any other filters that are set!
1081
        $status = isset($this->_req_data['status']) ? $this->_req_data['status'] : null;
1082
        // categories?
1083
        $category = isset($this->_req_data['EVT_CAT']) && $this->_req_data['EVT_CAT'] > 0
1084
            ? $this->_req_data['EVT_CAT']
1085
            : null;
1086
        // active status?
1087
        $active_status = isset($this->_req_data['active_status']) ? $this->_req_data['active_status'] : null;
1088
        $cur_date = isset($this->_req_data['month_range']) ? $this->_req_data['month_range'] : '';
1089
        return EEH_Form_Fields::generate_event_months_dropdown($cur_date, $status, $category, $active_status);
1090
    }
1091
1092
1093
    /**
1094
     * returns a list of "active" statuses on the event
1095
     *
1096
     * @param  string $current_value whatever the current active status is
1097
     * @return string
1098
     */
1099
    public function active_status_dropdown($current_value = '')
1100
    {
1101
        $select_name = 'active_status';
1102
        $values = array(
1103
            'none'     => esc_html__('Show Active/Inactive', 'event_espresso'),
1104
            'active'   => esc_html__('Active', 'event_espresso'),
1105
            'upcoming' => esc_html__('Upcoming', 'event_espresso'),
1106
            'expired'  => esc_html__('Expired', 'event_espresso'),
1107
            'inactive' => esc_html__('Inactive', 'event_espresso'),
1108
        );
1109
1110
        return EEH_Form_Fields::select_input($select_name, $values, $current_value, '', 'wide');
1111
    }
1112
1113
1114
    /**
1115
     * returns a list of "venues"
1116
     *
1117
     * @param string $current_value whatever the current active status is
1118
     * @return string
1119
     * @throws EE_Error
1120
     * @throws InvalidArgumentException
1121
     * @throws InvalidDataTypeException
1122
     * @throws InvalidInterfaceException
1123
     * @throws ReflectionException
1124
     */
1125
    protected function venuesDropdown($current_value = '')
1126
    {
1127
        $select_name = 'venue';
1128
        $values = array(
1129
            '' => esc_html__('All Venues', 'event_espresso'),
1130
        );
1131
        // populate the list of venues.
1132
        $venue_model = EE_Registry::instance()->load_model('Venue');
1133
        $venues = $venue_model->get_all(array('order_by' => array('VNU_name' => 'ASC')));
1134
1135
        foreach ($venues as $venue) {
1136
            $values[ $venue->ID() ] = $venue->name();
1137
        }
1138
1139
        return EEH_Form_Fields::select_input($select_name, $values, $current_value, '', 'wide');
1140
    }
1141
1142
1143
    /**
1144
     * output a dropdown of the categories for the category filter on the event admin list table
1145
     *
1146
     * @access  public
1147
     * @return string html
1148
     */
1149
    public function category_dropdown()
1150
    {
1151
        $cur_cat = isset($this->_req_data['EVT_CAT']) ? $this->_req_data['EVT_CAT'] : -1;
1152
        return EEH_Form_Fields::generate_event_category_dropdown($cur_cat);
1153
    }
1154
1155
1156
    /**
1157
     * get total number of events today
1158
     *
1159
     * @access public
1160
     * @return int
1161
     * @throws EE_Error
1162
     * @throws InvalidArgumentException
1163
     * @throws InvalidDataTypeException
1164
     * @throws InvalidInterfaceException
1165
     */
1166
    public function total_events_today()
1167
    {
1168
        $start = EEM_Datetime::instance()->convert_datetime_for_query(
1169
            'DTT_EVT_start',
1170
            date('Y-m-d') . ' 00:00:00',
1171
            'Y-m-d H:i:s',
1172
            'UTC'
1173
        );
1174
        $end = EEM_Datetime::instance()->convert_datetime_for_query(
1175
            'DTT_EVT_start',
1176
            date('Y-m-d') . ' 23:59:59',
1177
            'Y-m-d H:i:s',
1178
            'UTC'
1179
        );
1180
        $where = array(
1181
            'Datetime.DTT_EVT_start' => array('BETWEEN', array($start, $end)),
1182
        );
1183
        return EEM_Event::instance()->count(array($where, 'caps' => 'read_admin'), 'EVT_ID', true);
1184
    }
1185
1186
1187
    /**
1188
     * get total number of events this month
1189
     *
1190
     * @access public
1191
     * @return int
1192
     * @throws EE_Error
1193
     * @throws InvalidArgumentException
1194
     * @throws InvalidDataTypeException
1195
     * @throws InvalidInterfaceException
1196
     */
1197
    public function total_events_this_month()
1198
    {
1199
        // Dates
1200
        $this_year_r = date('Y');
1201
        $this_month_r = date('m');
1202
        $days_this_month = date('t');
1203
        $start = EEM_Datetime::instance()->convert_datetime_for_query(
1204
            'DTT_EVT_start',
1205
            $this_year_r . '-' . $this_month_r . '-01 00:00:00',
1206
            'Y-m-d H:i:s',
1207
            'UTC'
1208
        );
1209
        $end = EEM_Datetime::instance()->convert_datetime_for_query(
1210
            'DTT_EVT_start',
1211
            $this_year_r . '-' . $this_month_r . '-' . $days_this_month . ' 23:59:59',
1212
            'Y-m-d H:i:s',
1213
            'UTC'
1214
        );
1215
        $where = array(
1216
            'Datetime.DTT_EVT_start' => array('BETWEEN', array($start, $end)),
1217
        );
1218
        return EEM_Event::instance()->count(array($where, 'caps' => 'read_admin'), 'EVT_ID', true);
1219
    }
1220
1221
1222
    /** DEFAULT TICKETS STUFF **/
1223
1224
    /**
1225
     * Output default tickets list table view.
1226
     *
1227
     * @throws DomainException
1228
     * @throws EE_Error
1229
     * @throws InvalidArgumentException
1230
     * @throws InvalidDataTypeException
1231
     * @throws InvalidInterfaceException
1232
     */
1233
    public function _tickets_overview_list_table()
1234
    {
1235
        $this->_search_btn_label = esc_html__('Tickets', 'event_espresso');
1236
        $this->display_admin_list_table_page_with_no_sidebar();
1237
    }
1238
1239
1240
    /**
1241
     * @param int  $per_page
1242
     * @param bool $count
1243
     * @param bool $trashed
1244
     * @return EE_Soft_Delete_Base_Class[]|int
1245
     * @throws EE_Error
1246
     * @throws InvalidArgumentException
1247
     * @throws InvalidDataTypeException
1248
     * @throws InvalidInterfaceException
1249
     */
1250 View Code Duplication
    public function get_default_tickets($per_page = 10, $count = false, $trashed = false)
1251
    {
1252
        $orderby = empty($this->_req_data['orderby']) ? 'TKT_name' : $this->_req_data['orderby'];
1253
        $order = empty($this->_req_data['order']) ? 'ASC' : $this->_req_data['order'];
1254
        switch ($orderby) {
1255
            case 'TKT_name':
1256
                $orderby = array('TKT_name' => $order);
1257
                break;
1258
            case 'TKT_price':
1259
                $orderby = array('TKT_price' => $order);
1260
                break;
1261
            case 'TKT_uses':
1262
                $orderby = array('TKT_uses' => $order);
1263
                break;
1264
            case 'TKT_min':
1265
                $orderby = array('TKT_min' => $order);
1266
                break;
1267
            case 'TKT_max':
1268
                $orderby = array('TKT_max' => $order);
1269
                break;
1270
            case 'TKT_qty':
1271
                $orderby = array('TKT_qty' => $order);
1272
                break;
1273
        }
1274
        $current_page = isset($this->_req_data['paged']) && ! empty($this->_req_data['paged'])
1275
            ? $this->_req_data['paged']
1276
            : 1;
1277
        $per_page = isset($this->_req_data['perpage']) && ! empty($this->_req_data['perpage'])
1278
            ? $this->_req_data['perpage']
1279
            : $per_page;
1280
        $_where = array(
1281
            'TKT_is_default' => 1,
1282
            'TKT_deleted'    => $trashed,
1283
        );
1284
        $offset = ($current_page - 1) * $per_page;
1285
        $limit = array($offset, $per_page);
1286
        if (isset($this->_req_data['s'])) {
1287
            $sstr = '%' . $this->_req_data['s'] . '%';
1288
            $_where['OR'] = array(
1289
                'TKT_name'        => array('LIKE', $sstr),
1290
                'TKT_description' => array('LIKE', $sstr),
1291
            );
1292
        }
1293
        $query_params = array(
1294
            $_where,
1295
            'order_by' => $orderby,
1296
            'limit'    => $limit,
1297
            'group_by' => 'TKT_ID',
1298
        );
1299
        if ($count) {
1300
            return EEM_Ticket::instance()->count_deleted_and_undeleted(array($_where));
1301
        }
1302
        return EEM_Ticket::instance()->get_all_deleted_and_undeleted($query_params);
1303
    }
1304
1305
1306
    /**
1307
     * @param bool $trash
1308
     * @throws EE_Error
1309
     * @throws InvalidArgumentException
1310
     * @throws InvalidDataTypeException
1311
     * @throws InvalidInterfaceException
1312
     */
1313
    protected function _trash_or_restore_ticket($trash = false)
1314
    {
1315
        $success = 1;
1316
        $TKT = EEM_Ticket::instance();
1317
        // checkboxes?
1318
        if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
1319
            // if array has more than one element then success message should be plural
1320
            $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
1321
            // cycle thru the boxes
1322
            foreach ($this->_req_data['checkbox'] as $TKT_ID) {
1323
                if ($trash) {
1324
                    if (! $TKT->delete_by_ID($TKT_ID)) {
1325
                        $success = 0;
1326
                    }
1327
                } elseif (! $TKT->restore_by_ID($TKT_ID)) {
1328
                    $success = 0;
1329
                }
1330
            }
1331 View Code Duplication
        } else {
1332
            // grab single id and trash
1333
            $TKT_ID = absint($this->_req_data['TKT_ID']);
1334
            if ($trash) {
1335
                if (! $TKT->delete_by_ID($TKT_ID)) {
1336
                    $success = 0;
1337
                }
1338
            } elseif (! $TKT->restore_by_ID($TKT_ID)) {
1339
                $success = 0;
1340
            }
1341
        }
1342
        $action_desc = $trash ? 'moved to the trash' : 'restored';
1343
        $query_args = array(
1344
            'action' => 'ticket_list_table',
1345
            'status' => $trash ? '' : 'trashed',
1346
        );
1347
        $this->_redirect_after_action($success, 'Tickets', $action_desc, $query_args);
1348
    }
1349
1350
1351
    /**
1352
     * Handles trashing default ticket.
1353
     *
1354
     * @throws EE_Error
1355
     * @throws InvalidArgumentException
1356
     * @throws InvalidDataTypeException
1357
     * @throws InvalidInterfaceException
1358
     * @throws ReflectionException
1359
     */
1360
    protected function _delete_ticket()
1361
    {
1362
        $success = 1;
1363
        // checkboxes?
1364 View Code Duplication
        if (! empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) {
1365
            // if array has more than one element then success message should be plural
1366
            $success = count($this->_req_data['checkbox']) > 1 ? 2 : 1;
1367
            // cycle thru the boxes
1368
            foreach ($this->_req_data['checkbox'] as $TKT_ID) {
1369
                // delete
1370
                if (! $this->_delete_the_ticket($TKT_ID)) {
1371
                    $success = 0;
1372
                }
1373
            }
1374
        } else {
1375
            // grab single id and trash
1376
            $TKT_ID = absint($this->_req_data['TKT_ID']);
1377
            if (! $this->_delete_the_ticket($TKT_ID)) {
1378
                $success = 0;
1379
            }
1380
        }
1381
        $action_desc = 'deleted';
1382
        // fail safe.  If the default ticket count === 1 then we need to redirect to event overview.
1383
        $ticket_count = EEM_Ticket::instance()->count_deleted_and_undeleted(
1384
            [['TKT_is_default' => 1]],
1385
            'TKT_ID',
1386
            true
1387
        );
1388
        $query_args = $ticket_count
1389
            ? []
1390
            : [
1391
                'action' => 'ticket_list_table',
1392
                'status' => 'trashed'
1393
            ];
1394
        $this->_redirect_after_action($success, 'Tickets', $action_desc, $query_args);
1395
    }
1396
1397
1398
    /**
1399
     * @param int $TKT_ID
1400
     * @return bool|int
1401
     * @throws EE_Error
1402
     * @throws InvalidArgumentException
1403
     * @throws InvalidDataTypeException
1404
     * @throws InvalidInterfaceException
1405
     * @throws ReflectionException
1406
     */
1407
    protected function _delete_the_ticket($TKT_ID)
1408
    {
1409
        $ticket = EEM_Ticket::instance()->get_one_by_ID($TKT_ID);
1410
        if (! $ticket instanceof EE_Ticket) {
1411
            return false;
1412
        }
1413
        $ticket->_remove_relations('Datetime');
1414
        // delete all related prices first
1415
        $ticket->delete_related_permanently('Price');
1416
        return $ticket->delete_permanently();
1417
    }
1418
}
1419