Completed
Branch BUG-10381-asset-loading (361215)
by
unknown
158:58 queued 146:07
created

getTicketStatusClasses()   D

Complexity

Conditions 9
Paths 48

Size

Total Lines 44
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 32
nc 48
nop 1
dl 0
loc 44
rs 4.909
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\modules\ticket_selector;
3
4
use EE_Error;
5
use EventEspresso\core\exceptions\UnexpectedEntityException;
6
7
defined('EVENT_ESPRESSO_VERSION') || exit;
8
9
10
11
/**
12
 * Class TicketSelectorRowStandard
13
 * class for loading template and resolving template args for a single ticket row within a standard ticket selector
14
 *
15
 * @package       Event Espresso
16
 * @author        Brent Christensen
17
 * @since         $VID:$
18
 */
19
class TicketSelectorRowStandard extends TicketSelectorRow
20
{
21
22
    /**
23
     * @var TicketDetails $ticket_details
24
     */
25
    protected $ticket_details;
26
27
    /**
28
     * @var \EE_Ticket_Selector_Config $template_settings
29
     */
30
    protected $template_settings;
31
32
    /**
33
     * @var \EE_Tax_Config $tax_settings
34
     */
35
    protected $tax_settings;
36
37
    /**
38
     * @var boolean $prices_displayed_including_taxes
39
     */
40
    protected $prices_displayed_including_taxes;
41
42
    /**
43
     * @var int $row
44
     */
45
    protected $row;
46
47
    /**
48
     * @var int $cols
49
     */
50
    protected $cols;
51
52
    /**
53
     * @var boolean $hidden_input_qty
54
     */
55
    protected $hidden_input_qty;
56
57
    /**
58
     * @var string $ticket_datetime_classes
59
     */
60
    protected $ticket_datetime_classes;
61
62
63
64
    /**
65
     * TicketDetails constructor.
66
     *
67
     * @param TicketDetails  $ticket_details
68
     * @param \EE_Tax_Config $tax_settings
69
     * @param int            $total_tickets
70
     * @param int            $max_atndz
71
     * @param int            $row
72
     * @param int            $cols
73
     * @param boolean        $required_ticket_sold_out
74
     * @param string         $event_status
75
     * @param string         $ticket_datetime_classes
76
     * @throws EE_Error
77
     * @throws UnexpectedEntityException
78
     */
79
    public function __construct(
80
        TicketDetails $ticket_details,
81
        \EE_Tax_Config $tax_settings,
82
        $total_tickets,
83
        $max_atndz,
84
        $row,
85
        $cols,
86
        $required_ticket_sold_out,
87
        $event_status,
88
        $ticket_datetime_classes
89
    ) {
90
        $this->ticket = $ticket_details->getTicket();
91
        $this->ticket_details = $ticket_details;
92
        $this->template_settings = $ticket_details->getTemplateSettings();
93
        $this->tax_settings = $tax_settings;
94
        $this->total_tickets = $total_tickets;
95
        $this->max_atndz = $max_atndz;
96
        $this->row = $row;
97
        $this->cols = $cols;
98
        $this->date_format = $ticket_details->getDateFormat();
99
        $this->ticket_datetime_classes = $ticket_datetime_classes;
100
        parent::__construct($this->ticket, $max_atndz, $this->date_format, $event_status, $required_ticket_sold_out);
101
    }
102
103
104
105
    /**
106
     * other ticket rows will need to know if a required ticket is sold out,
107
     * so that they are not offered for sale
108
     *
109
     * @return boolean
110
     */
111
    public function getRequiredTicketSoldOut()
112
    {
113
        return $this->required_ticket_sold_out;
114
    }
115
116
117
118
    /**
119
     * @return int
120
     */
121
    public function getCols()
122
    {
123
        return $this->cols;
124
    }
125
126
127
128
    /**
129
     * getHtml
130
     *
131
     * @return string
132
     * @throws EE_Error
133
     */
134
    public function getHtml()
135
    {
136
        $min = 0;
137
        $max = $this->ticket->max();
138
        $remaining = $this->ticket->remaining();
139
        if ($this->ticket->is_on_sale() && $this->ticket->is_remaining()) {
140
            list($min, $max) = $this->setTicketMinAndMax($remaining);
141
        } else {
142
            // set flag if ticket is required (flag is set to start date so that future tickets are not blocked)
143
            $this->required_ticket_sold_out = $this->ticket->required() && ! $remaining
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->ticket->required(...equired_ticket_sold_out can also be of type string or object<EE_Error>. However, the property $required_ticket_sold_out is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
144
                ? $this->ticket->start_date()
145
                : $this->required_ticket_sold_out;
146
        }
147
        list($ticket_price, $ticket_bundle) = $this->getTicketPriceDetails();
148
        list($tkt_status, $ticket_status, $status_class) = $this->getTicketStatusClasses($remaining);
149
        /**
150
         * Allow plugins to hook in and abort the generation and display of this row to do
151
         * something else if they want.
152
         * For an addon to abort things, all they have to do is register a filter with this hook, and
153
         * return a value that is NOT false.  Whatever is returned gets echoed instead of the
154
         * current row.
155
         *
156
         * @var string|bool
157
         */
158
        $ticket_selector_row_html = apply_filters(
159
            'FHEE__ticket_selector_chart_template__do_ticket_entire_row',
160
            false,
161
            $this->ticket,
162
            $max,
163
            $min,
164
            $this->required_ticket_sold_out,
165
            $ticket_price,
166
            $ticket_bundle,
167
            $ticket_status,
168
            $status_class
169
        );
170
        if ($ticket_selector_row_html !== false) {
171
            return $ticket_selector_row_html;
172
        }
173
        $ticket_selector_row_html = \EEH_HTML::tr(
174
            '', '',
175
            "tckt-slctr-tbl-tr {$status_class}{$this->ticket_datetime_classes} " . espresso_get_object_css_class($this->ticket)
176
        );
177
        /**
178
         * Allow plugins to hook in and abort the generation and display of the contents of this
179
         * row to do something else if they want.
180
         * For an addon to abort things, all they have to do is register a filter with this hook, and
181
         * return a value that is NOT false.  Whatever is returned gets echoed instead of the
182
         * current row.
183
         *
184
         * @var string|bool
185
         */
186
        $new_row_cells_content = apply_filters(
187
            'FHEE__ticket_selector_chart_template__do_ticket_inside_row',
188
            false,
189
            $this->ticket,
190
            $max,
191
            $min,
192
            $this->required_ticket_sold_out,
193
            $ticket_price,
194
            $ticket_bundle,
195
            $ticket_status,
196
            $status_class
197
        );
198
        if ($new_row_cells_content !== false) {
199
            return $ticket_selector_row_html
200
                   . $new_row_cells_content
201
                   . $this->ticketQtyAndIdHiddenInputs()
202
                   . \EEH_HTML::trx();
203
        }
204
        $this->hidden_input_qty = $this->max_atndz > 1 ? true : false;
205
206
        $ticket_selector_row_html .= $this->ticketNameTableCell();
207
        $ticket_selector_row_html .= $this->ticketPriceTableCell($ticket_price, $ticket_bundle);
208
        $ticket_selector_row_html .= \EEH_HTML::td('', '', 'tckt-slctr-tbl-td-qty cntr');
209
        $this->setTicketStatusDisplay($tkt_status, $ticket_status, $remaining);
210
        if (empty($this->ticket_status_display)) {
211
            if ($this->max_atndz === 1) {
212
                // only ONE attendee is allowed to register at a time
213
                $ticket_selector_row_html .= $this->onlyOneAttendeeCanRegister();
214
            } else if ($max > 0) {
215
                $ticket_selector_row_html .= $this->ticketQuantitySelector($min, $max);
216
            }
217
        }
218
        $ticket_selector_row_html .= $this->ticket_status_display;
219
        $ticket_selector_row_html .= $this->ticketQtyAndIdHiddenInputs();
220
        $ticket_selector_row_html .= $this->ticket_details->display($ticket_price, $remaining, $this->cols);
221
        $ticket_selector_row_html .= \EEH_HTML::tdx();
222
        $ticket_selector_row_html .= \EEH_HTML::trx();
223
224
225
        $this->row++;
226
        return $ticket_selector_row_html;
227
    }
228
229
230
231
    /**
232
     * setTicketMinAndMax
233
     *
234
     * @param int $remaining
235
     * @return array
236
     * @throws EE_Error
237
     */
238
    protected function setTicketMinAndMax($remaining)
239
    {
240
        // offer the number of $tickets_remaining or $this->max_atndz, whichever is smaller
241
        $max = min($remaining, $this->max_atndz);
242
        // but... we also want to restrict the number of tickets by the ticket max setting,
243
        // however, the max still can't be higher than what was just set above
244
        $max = $this->ticket->max() > 0 ? min($this->ticket->max(), $max) : $max;
245
        // and we also want to restrict the minimum number of tickets by the ticket min setting
246
        $min = $this->ticket->min() > 0 ? $this->ticket->min() : 0;
247
        // and if the ticket is required, then make sure that min qty is at least 1
248
        $min = $this->ticket->required() ? max($min, 1) : $min;
249
        return array($min, $max);
250
    }
251
252
253
254
    /**
255
     * getTicketPriceDetails
256
     *
257
     * @return array
258
     * @throws EE_Error
259
     */
260
    protected function getTicketPriceDetails()
261
    {
262
        $ticket_price = $this->tax_settings->prices_displayed_including_taxes
263
            ? $this->ticket->get_ticket_total_with_taxes()
264
            : $this->ticket->get_ticket_subtotal();
265
        $ticket_bundle = false;
266
        $ticket_min = $this->ticket->min();
267
        // for ticket bundles, set min and max qty the same
268
        if ($ticket_min !== 0 && $ticket_min === $this->ticket->max()) {
269
            $ticket_price *= $ticket_min;
270
            $ticket_bundle = true;
271
        }
272
        $ticket_price = apply_filters(
273
            'FHEE__ticket_selector_chart_template__ticket_price',
274
            $ticket_price,
275
            $this->ticket
276
        );
277
        return array($ticket_price, $ticket_bundle);
278
    }
279
280
281
282
283
    /**
284
     * ticketNameTableCell
285
     *
286
     * @return string
287
     * @throws EE_Error
288
     */
289
    protected function ticketNameTableCell()
290
    {
291
        $html = \EEH_HTML::td('', '', 'tckt-slctr-tbl-td-name');
292
        $html .= \EEH_HTML::strong($this->ticket->get_pretty('TKT_name'));
293
        $html .= $this->ticket_details->getShowHideLinks();
294
        if ($this->ticket->required()) {
295
            $html .= \EEH_HTML::p(
296
                    apply_filters(
297
                            'FHEE__ticket_selector_chart_template__ticket_required_message',
298
                            esc_html__('This ticket is required and must be purchased.', 'event_espresso')
299
                    ),
300
                    '', 'ticket-required-pg'
301
            );
302
        }
303
        $html .= \EEH_HTML::tdx();
304
        return $html;
305
    }
306
307
308
309
    /**
310
     * ticketPriceTableCell
311
     *
312
     * @param float $ticket_price
313
     * @param bool  $ticket_bundle
314
     * @return string
315
     * @throws EE_Error
316
     */
317
    protected function ticketPriceTableCell($ticket_price, $ticket_bundle)
318
    {
319
        $html = '';
320
        if (apply_filters('FHEE__ticket_selector_chart_template__display_ticket_price_details', true)) {
321
            $html .= \EEH_HTML::td('', '', 'tckt-slctr-tbl-td-price jst-rght');
322
            $html .= \EEH_Template::format_currency($ticket_price);
323
            $html .= $this->ticket->taxable()
324
                ? \EEH_HTML::span( '*', '', 'taxable-tickets-asterisk grey-text' )
325
                : '';
326
            $html .= '&nbsp;';
327
            $html .= \EEH_HTML::span(
328
                $ticket_bundle
329
                    ? apply_filters(
330
                        'FHEE__ticket_selector_chart_template__per_ticket_bundle_text',
331
                        __(' / bundle', 'event_espresso')
332
                    )
333
                    : apply_filters(
334
                        'FHEE__ticket_selector_chart_template__per_ticket_text',
335
                        __('', 'event_espresso')
336
                    ),
337
                '', 'smaller-text no-bold'
338
            );
339
            $html .= '&nbsp;';
340
            $html .= \EEH_HTML::tdx();
341
            $this->cols++;
342
        }
343
        return $html;
344
    }
345
346
347
348
    /**
349
     * onlyOneAttendeeCanRegister
350
     *
351
     * @return string
352
     */
353
    protected function onlyOneAttendeeCanRegister()
354
    {
355
        // display submit button since we have tickets available
356
        add_filter('FHEE__EE_Ticket_Selector__display_ticket_selector_submit', '__return_true');
357
        $this->hidden_input_qty = false;
358
        $html = '<input type="radio" name="tkt-slctr-qty-' . $this->EVT_ID . '"';
359
        $html .= ' id="ticket-selector-tbl-qty-slct-' . $this->EVT_ID . '-' . $this->row . '"';
360
        $html .= ' class="ticket-selector-tbl-qty-slct" value="' . $this->row . '-1"';
361
        $html .= $this->total_tickets === 1 ? ' checked="checked"' : '';
362
        $html .= ' title=""/>';
363
        return $html;
364
    }
365
366
367
368
    /**
369
     * ticketQuantitySelector
370
     *
371
     * @param int $min
372
     * @param int $max
373
     * @return string
374
     * @throws EE_Error
375
     */
376
    protected function ticketQuantitySelector($min = 0, $max = 0)
377
    {
378
        // display submit button since we have tickets available
379
        add_filter('FHEE__EE_Ticket_Selector__display_ticket_selector_submit', '__return_true');
380
        $this->hidden_input_qty = false;
381
        $html = '<select name="tkt-slctr-qty-' . $this->EVT_ID . '[]"';
382
        $html .= ' id="ticket-selector-tbl-qty-slct-' . $this->EVT_ID . '-' . $this->row . '"';
383
        $html .= ' class="ticket-selector-tbl-qty-slct">';
384
        // this ensures that non-required tickets with non-zero MIN QTYs don't HAVE to be purchased
385
        if ($min !== 0 && ! $this->ticket->required()) {
386
            $html .= '<option value="0">&nbsp;0&nbsp;</option>';
387
        }
388
        // offer ticket quantities from the min to the max
389
        for ($i = $min; $i <= $max; $i++) {
390
            $html .= '<option value="' . $i . '">&nbsp;' . $i . '&nbsp;</option>';
391
        }
392
        $html .= '</select>';
393
        return $html;
394
    }
395
396
397
398
    /**
399
     * getHiddenInputs
400
     *
401
     * @return string
402
     * @throws EE_Error
403
     */
404
    protected function ticketQtyAndIdHiddenInputs()
405
    {
406
        $html = '';
407
        // depending on group reg we need to change the format for qty
408
        if ($this->hidden_input_qty) {
409
            $html .= '<input type="hidden" name="tkt-slctr-qty-' . $this->EVT_ID . '[]" value="0"/>';
410
        }
411
        $html .= '<input type="hidden" name="tkt-slctr-ticket-id-' . $this->EVT_ID . '[]"';
412
        $html .= ' value="' . $this->ticket->ID() . '"/>';
413
        return $html;
414
    }
415
416
}
417
// End of file TicketSelectorRowStandard.php
418
// Location: EventEspresso\modules\ticket_selector/TicketSelectorRowStandard.php