Completed
Branch FET/reg-form-builder/extract-a... (6e8a58)
by
unknown
35:36 queued 25:38
created

CopyAttendeeInfoForm   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
B copyAttendeeInfoInputs() 0 38 6
1
<?php
2
3
namespace EventEspresso\core\domain\services\registration\form\v1;
4
5
use EE_Checkbox_Multi_Input;
6
use EE_Error;
7
use EE_Form_Section_HTML;
8
use EE_Form_Section_Proper;
9
use EE_Registration;
10
use EE_Template_Layout;
11
use ReflectionException;
12
13
class CopyAttendeeInfoForm extends EE_Form_Section_Proper
14
{
15
    /**
16
     * CopyAttendeeInfoForm constructor.
17
     *
18
     * @param EE_Registration[] $registrations
19
     * @param string            $slug
20
     * @throws EE_Error
21
     * @throws ReflectionException
22
     */
23
    public function __construct(array $registrations, string $slug)
24
    {
25
        parent::__construct(
26
            [
27
                'subsections'     => $this->copyAttendeeInfoInputs($registrations),
28
                'layout_strategy' => new EE_Template_Layout(
29
                    [
30
                        'layout_template_file'     => SPCO_REG_STEPS_PATH
31
                                                      . $slug
32
                                                      . '/copy_attendee_info.template.php',
33
                        'begin_template_file'      => null,
34
                        'input_template_file'      => null,
35
                        'subsection_template_file' => null,
36
                        'end_template_file'        => null,
37
                    ]
38
                ),
39
            ]);
40
    }
41
42
43
    /**
44
     * @param array $registrations
45
     * @return array
46
     * @throws EE_Error
47
     * @throws ReflectionException
48
     */
49
    private function copyAttendeeInfoInputs(array $registrations): array
50
    {
51
        $copy_attendee_info_inputs = [];
52
        $prev_ticket               = null;
53
        foreach ($registrations as $registration) {
54
            // for all  attendees other than the primary attendee
55
            if ($registration instanceof EE_Registration && ! $registration->is_primary_registrant()) {
56
                // if this is a new ticket OR if this is the very first additional attendee after the primary attendee
57
                if ($registration->ticket()->ID() !== $prev_ticket) {
58
                    $item_name   = $registration->ticket()->name();
59
                    $item_name   .= $registration->ticket()->description() !== ''
60
                        ? ' - ' . $registration->ticket()->description()
61
                        : '';
62
                    $copy_attendee_info_inputs[ 'spco_copy_attendee_chk[ticket-' . $registration->ticket()->ID() . ']' ]
63
                                 = new EE_Form_Section_HTML(
64
                        '<h6 class="spco-copy-attendee-event-hdr">' . $item_name . '</h6>'
65
                    );
66
                    $prev_ticket = $registration->ticket()->ID();
67
                }
68
69
                $copy_attendee_info_inputs[ 'spco_copy_attendee_chk[' . $registration->ID() . ']' ]
70
                    = new EE_Checkbox_Multi_Input(
71
                    [
72
                        $registration->ID() => sprintf(
73
                            esc_html_x('Attendee #%s', 'Attendee #123', 'event_espresso'),
74
                            $registration->count()
75
                        ),
76
                    ],
77
                    [
78
                        'html_id'                 => 'spco-copy-attendee-chk-' . $registration->reg_url_link(),
79
                        'html_class'              => 'spco-copy-attendee-chk ee-do-not-validate',
80
                        'display_html_label_text' => false,
81
                    ]
82
                );
83
            }
84
        }
85
        return $copy_attendee_info_inputs;
86
    }
87
}
88