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

RegistrantForm   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 192
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A hasQuestions() 0 4 1
C generateFormArgs() 0 100 10
A additionalAttendeeRegInfoInput() 0 10 1
A additionalPrimaryRegistrantInputs() 0 10 1
1
<?php
2
3
namespace EventEspresso\core\domain\services\registration\form\v1;
4
5
use EE_Error;
6
use EE_Event;
7
use EE_Fieldset_Section_Layout;
8
use EE_Form_Input_Base;
9
use EE_Form_Section_Proper;
10
use EE_Hidden_Input;
11
use EE_Question_Group;
12
use EE_Registration;
13
use EEM_Event_Question_Group;
14
use EventEspresso\core\services\loaders\LoaderFactory;
15
use ReflectionException;
16
17
class RegistrantForm extends EE_Form_Section_Proper
18
{
19
20
    /**
21
     * @var EEM_Event_Question_Group
22
     */
23
    public $event_question_group_model;
24
25
    /**
26
     * @var bool
27
     */
28
    private $has_questions = false;
29
30
31
    /**
32
     * RegistrantForm constructor.
33
     *
34
     * @param EE_Registration          $registration
35
     * @param bool                     $admin_request
36
     * @param bool                     $copy_attendee_info
37
     * @param callable                 $enablePrintCopyInfo
38
     * @param EEM_Event_Question_Group $event_question_group_model
39
     * @throws EE_Error
40
     * @throws ReflectionException
41
     */
42
    public function __construct(
43
        EE_Registration $registration,
44
        bool $admin_request,
45
        bool $copy_attendee_info,
46
        callable $enablePrintCopyInfo,
47
        EEM_Event_Question_Group $event_question_group_model
48
    ) {
49
        $this->event_question_group_model = $event_question_group_model;
50
        parent::__construct(
51
            $this->generateFormArgs($registration, $admin_request, $copy_attendee_info, $enablePrintCopyInfo)
52
        );
53
    }
54
55
56
    /**
57
     * @return bool
58
     */
59
    public function hasQuestions(): bool
60
    {
61
        return $this->has_questions;
62
    }
63
64
65
    /**
66
     * @param EE_Registration $registration
67
     * @param bool            $admin_request
68
     * @param bool            $copy_attendee_info
69
     * @param callable        $enablePrintCopyInfo
70
     * @return array
71
     * @throws EE_Error
72
     * @throws ReflectionException
73
     */
74
    private function generateFormArgs(
75
        EE_Registration $registration,
76
        bool $admin_request,
77
        bool $copy_attendee_info,
78
        callable $enablePrintCopyInfo
79
    ): array {
80
        static $attendee_nmbr = 1;
81
        $form_args = [];
82
        // verify that registration has valid event
83
        if ($registration->event() instanceof EE_Event) {
84
            $field_name      = 'Event_Question_Group.' . $this->event_question_group_model->fieldNameForContext(
85
                $registration->is_primary_registrant()
86
            );
87
            $question_groups = $registration->event()->question_groups(
88
                apply_filters(
89
                    // @codingStandardsIgnoreStart
90
                    'FHEE__EE_SPCO_Reg_Step_Attendee_Information___registrations_reg_form__question_groups_query_parameters',
91
                    // @codingStandardsIgnoreEnd
92
                    [
93
                        [
94
                            'Event.EVT_ID' => $registration->event()->ID(),
95
                            $field_name    => true,
96
                        ],
97
                        'order_by' => ['QSG_order' => 'ASC'],
98
                    ],
99
                    $registration,
100
                    $this
101
                )
102
            );
103
            if ($question_groups) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $question_groups of type EE_Base_Class[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104
                // array of params to pass to parent constructor
105
                $form_args = [
106
                    'html_id'         => 'ee-registration-' . $registration->reg_url_link(),
107
                    'html_class'      => 'ee-reg-form-attendee-dv',
108
                    'html_style'      => $admin_request
109
                        ? 'padding:0em 2em 1em; margin:3em 0 0; border:1px solid #ddd;'
110
                        : '',
111
                    'subsections'     => [],
112
                    'layout_strategy' => new EE_Fieldset_Section_Layout(
113
                        [
114
                            'legend_class' => 'spco-attendee-lgnd smaller-text lt-grey-text',
115
                            'legend_text'  => sprintf(
116
                                esc_html_x(
117
                                    'Attendee %d',
118
                                    'Attendee 123',
119
                                    'event_espresso'
120
                                ),
121
                                $attendee_nmbr
122
                            ),
123
                        ]
124
                    ),
125
                ];
126
                foreach ($question_groups as $question_group) {
127
                    if ($question_group instanceof EE_Question_Group) {
128
                        $question_group_reg_form = LoaderFactory::getNew(
129
                            RegFormQuestionGroup::class,
130
                            [$registration, $question_group, $admin_request]
131
                        );
132
                        $form_args['subsections'][ $question_group->identifier() ] = apply_filters(
133
                            'FHEE__EE_SPCO_Reg_Step_Attendee_Information___question_group_reg_form__question_group_reg_form',
134
                            $question_group_reg_form,
135
                            $registration,
136
                            $question_group,
137
                            $this
138
                        );
139
                    }
140
                }
141
                // add hidden input
142
                $form_args['subsections']['additional_attendee_reg_info'] = $this->additionalAttendeeRegInfoInput(
143
                    $registration
144
                );
145
146
                // If we have question groups for additional attendees, then display the copy options
147
                $printCopyInfo = apply_filters(
148
                    'FHEE__EE_SPCO_Reg_Step_Attendee_Information___registrations_reg_form___printCopyInfo',
149
                    $attendee_nmbr > 1 && $copy_attendee_info,
150
                    $attendee_nmbr
151
                );
152
                if ($printCopyInfo) {
153
                    $enablePrintCopyInfo();
154
                }
155
156
157
                if ($registration->is_primary_registrant()) {
158
                    // generate hidden input
159
                    $form_args['subsections']['primary_registrant'] = $this->additionalPrimaryRegistrantInputs(
160
                        $registration
161
                    );
162
                }
163
            }
164
        }
165
        $attendee_nmbr++;
166
167
        // Increment the reg forms number if form is valid.
168
        if (! empty($form_args)) {
169
            $this->has_questions = true;
170
        }
171
172
        return $form_args;
173
    }
174
175
176
    /**
177
     * @param EE_Registration $registration
178
     * @return EE_Form_Input_Base
179
     * @throws EE_Error
180
     */
181
    private function additionalAttendeeRegInfoInput(EE_Registration $registration)
182
    {
183
        // generate hidden input
184
        return new EE_Hidden_Input(
185
            [
186
                'html_id' => 'additional-attendee-reg-info-' . $registration->reg_url_link(),
187
                'default' => true,
188
            ]
189
        );
190
    }
191
192
193
    /**
194
     * @param EE_Registration $registration
195
     * @return    EE_Form_Input_Base
196
     * @throws EE_Error
197
     */
198
    private function additionalPrimaryRegistrantInputs(EE_Registration $registration)
199
    {
200
        // generate hidden input
201
        return new EE_Hidden_Input(
202
            [
203
                'html_id' => 'primary_registrant',
204
                'default' => $registration->reg_url_link(),
205
            ]
206
        );
207
    }
208
}
209