Completed
Branch BUG-9042-ics-organizer-email (7b2e23)
by
unknown
29:24 queued 16:15
created

DatetimeSelector   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 176
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A getAllDatetimesForAllTicket() 0 8 2
A getTicketDatetimes() 0 19 3
B getTicketDatetimeClasses() 0 14 5
A getUniqueDatetimeOptions() 0 11 3
A getDatetimeSelector() 0 20 2
1
<?php
2
namespace EventEspresso\modules\ticket_selector;
3
4
defined('ABSPATH') || exit;
5
6
7
8
/**
9
 * Class DatetimeSelector
10
 * adds the ability to filter the Ticket Selector by the available ticket datetimes
11
 *
12
 * @package       Event Espresso
13
 * @author        Brent Christensen
14
 * @since         $VID:$
15
 */
16
class DatetimeSelector
17
{
18
19
    /**
20
     * @var \EE_Event $event
21
     */
22
    protected $event;
23
24
    /**
25
     * @var \EE_Ticket[] $tickets
26
     */
27
    protected $tickets;
28
29
    /**
30
     * @var \EE_Datetime[] $datetimes
31
     */
32
    protected $datetimes;
33
34
    /**
35
     * @var \EE_Datetime[] $unique_dates
36
     */
37
    protected $unique_dates;
38
39
    /**
40
     * @var \EE_Ticket_Selector_Config $template_settings
41
     */
42
    protected $template_settings;
43
44
    /**
45
     * @var boolean $active
46
     */
47
    protected $active = false;
48
49
50
51
    /**
52
     * DatetimeSelector constructor.
53
     *
54
     * @param \EE_Event                  $event
55
     * @param \EE_Ticket[]               $tickets
56
     * @param \EE_Ticket_Selector_Config $template_settings
57
     * @param string                     $date_format
58
     * @param string                     $time_format
59
     * @throws \EE_Error
60
     */
61
    public function __construct(
62
        \EE_Event $event,
63
        array $tickets,
64
        \EE_Ticket_Selector_Config $template_settings,
65
        $date_format = 'Y-m-d',
66
        $time_format = 'g:i a'
67
    ) {
68
        $this->event = $event;
69
        $this->tickets = $tickets;
70
        $this->template_settings = $template_settings;
71
        $this->datetimes = $this->getAllDatetimesForAllTicket($tickets);
72
        $this->unique_dates = $this->getUniqueDatetimeOptions($date_format, $time_format);
73
        $this->active = $this->template_settings->showDatetimeSelector($this->unique_dates);
74
    }
75
76
77
78
    /**
79
     * @param \EE_Ticket[] $tickets
80
     * @return array
81
     * @throws \EE_Error
82
     */
83
    protected function getAllDatetimesForAllTicket($tickets = array())
84
    {
85
        $datetimes = array();
86
        foreach ($tickets as $ticket) {
87
            $datetimes = $this->getTicketDatetimes($ticket, $datetimes);
88
        }
89
        return $datetimes;
90
    }
91
92
93
94
    /**
95
     * @param \EE_Ticket      $ticket
96
     * @param  \EE_Datetime[] $datetimes
97
     * @return \EE_Datetime[]
98
     * @throws \EE_Error
99
     */
100
    protected function getTicketDatetimes(\EE_Ticket $ticket, $datetimes = array())
101
    {
102
        $ticket_datetimes = $ticket->datetimes(
103
            array(
104
                'order_by'                 => array(
105
                    'DTT_order'     => 'ASC',
106
                    'DTT_EVT_start' => 'ASC'
107
                ),
108
                'default_where_conditions' => 'none',
109
            )
110
        );
111
        foreach ($ticket_datetimes as $ticket_datetime) {
112
            if ( ! $ticket_datetime instanceof \EE_Datetime) {
113
                continue;
114
            }
115
            $datetimes[ $ticket_datetime->ID() ] = $ticket_datetime;
116
        }
117
        return $datetimes;
118
    }
119
120
121
122
    /**
123
     * @param \EE_Ticket                 $ticket
124
     * @return string
125
     * @throws \EE_Error
126
     */
127
    public function getTicketDatetimeClasses( \EE_Ticket $ticket ) {
128
        if ( ! $this->active) {
129
            return '';
130
        }
131
        $ticket_datetimes = $this->getTicketDatetimes($ticket);
132
        $classes = '';
133
        foreach ($this->datetimes as $datetime) {
134
            if ( ! $datetime instanceof \EE_Datetime || ! in_array($datetime, $ticket_datetimes, true)) {
135
                continue;
136
            }
137
            $classes .= ' ee-ticket-datetimes-' . $datetime->date_and_time_range('Y_m_d', 'H_i', '-', '_');
138
        }
139
        return $classes;
140
    }
141
142
143
144
    /**
145
     * @param string $date_format
146
     * @param string $time_format
147
     * @return array
148
     * @throws \EE_Error
149
     */
150
    public function getUniqueDatetimeOptions($date_format = 'Y-m-d', $time_format = 'g:i a') {
151
        $datetime_options = array();
152
        foreach ($this->datetimes as $datetime) {
153
            if ( ! $datetime instanceof \EE_Datetime) {
154
                continue;
155
            }
156
            $datetime_options[$datetime->date_and_time_range('Y_m_d', 'H_i', '-', '_')] =
157
                $datetime->date_and_time_range($date_format, $time_format, ' - ');
158
        }
159
        return $datetime_options;
160
    }
161
162
163
164
    /**
165
     * @return string
166
     * @throws \EE_Error
167
     */
168
    public function getDatetimeSelector() {
169
        if ( ! $this->active) {
170
            return '';
171
        }
172
        $dropdown_selector = new \EE_Checkbox_Dropdown_Selector_Input(
173
            $this->unique_dates,
174
            array(
175
                'html_id'               => 'datetime-selector-' . $this->event->ID(),
176
                'html_name'             => 'datetime_selector_' . $this->event->ID(),
177
                'html_class'            => 'datetime-selector',
178
                'select_button_text'       => '<span class="dashicons dashicons-calendar-alt"></span> '
179
                                            . esc_html__('Filter by Date', 'event_espresso'),
180
                'other_html_attributes' => ' data-tkt_slctr_evt="' . $this->event->ID() . '"',
181
            )
182
        );
183
        return \EEH_HTML::div(
184
            $dropdown_selector->get_html_for_input(),
185
            '', 'datetime_selector-dv'
186
        );
187
    }
188
189
190
191
}
192
// End of file DatetimeSelector.php
193
// Location: EventEspresso\modules\ticket_selector/DatetimeSelector.php