Completed
Pull Request — master (#647)
by Robbie
02:13
created

UserForm::getRequiredFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\UserForms\Form;
4
5
use ResetFormAction;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Control\Session;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\Forms\FormAction;
11
use SilverStripe\Forms\RequiredFields;
12
use SilverStripe\UserForms\FormField\UserFormsStepField;
13
use SilverStripe\UserForms\FormField\UserFormsFieldList;
14
15
/**
16
 * @package userforms
17
 */
18
class UserForm extends Form
19
{
20
    /**
21
     * @param Controller $controller
22
     * @param string $name
23
     */
24
    public function __construct(Controller $controller, $name = Form::class)
25
    {
26
        $this->controller = $controller;
27
        $this->setRedirectToFormOnValidationError(true);
28
29
        parent::__construct(
30
            $controller,
31
            $name,
32
            new FieldList(),
33
            new FieldList()
34
        );
35
36
        $this->setFields($fields = $this->getFormFields());
37
        $fields->setForm($this);
38
        $this->setActions($actions = $this->getFormActions());
39
        $actions->setForm($this);
40
        $this->setValidator($this->getRequiredFields());
41
42
        // This needs to be re-evaluated since fields have been assigned
43
        $this->restoreFormState();
44
45
        // Number each page
46
        $stepNumber = 1;
47
        foreach ($this->getSteps() as $step) {
48
            $step->setStepNumber($stepNumber++);
49
        }
50
51
        if ($controller->DisableCsrfSecurityToken) {
52
            $this->disableSecurityToken();
53
        }
54
55
        $data = $this->getRequest()->getSession()->get("FormInfo.{$this->FormName()}.data");
56
57
        if (is_array($data)) {
58
            $this->loadDataFrom($data);
59
        }
60
61
        $this->extend('updateForm');
62
    }
63
64
    public function restoreFormState()
65
    {
66
        // Suppress restoreFormState if fields haven't been bootstrapped
67
        if ($this->fields && $this->fields->exists()) {
68
            return parent::restoreFormState();
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * Used for partial caching in the template.
76
     *
77
     * @return string
78
     */
79
    public function getLastEdited()
80
    {
81
        return $this->controller->LastEdited;
82
    }
83
84
    /**
85
     * @return bool
86
     */
87
    public function getDisplayErrorMessagesAtTop()
88
    {
89
        return (bool)$this->controller->DisplayErrorMessagesAtTop;
90
    }
91
92
    /**
93
     * Return the fieldlist, filtered to only contain steps
94
     *
95
     * @return \SilverStripe\ORM\ArrayList
96
     */
97
    public function getSteps()
98
    {
99
        return $this->Fields()->filterByCallback(function ($field) {
100
            return $field instanceof UserFormsStepField;
101
        });
102
    }
103
104
    /**
105
     * Get the form fields for the form on this page. Can modify this FieldSet
106
     * by using {@link updateFormFields()} on an {@link Extension} subclass which
107
     * is applied to this controller.
108
     *
109
     * This will be a list of top level composite steps
110
     *
111
     * @return FieldList
112
     */
113
    public function getFormFields()
114
    {
115
        $fields = new UserFormsFieldList();
116
        $target = $fields;
117
        foreach ($this->controller->Fields() as $field) {
118
            $target = $target->processNext($field);
119
        }
120
        $fields->clearEmptySteps();
121
        $this->extend('updateFormFields', $fields);
122
        $fields->setForm($this);
123
        return $fields;
124
    }
125
126
    /**
127
     * Generate the form actions for the UserDefinedForm. You
128
     * can manipulate these by using {@link updateFormActions()} on
129
     * a decorator.
130
     *
131
     * @todo Make form actions editable via their own field editor.
132
     *
133
     * @return FieldList
134
     */
135
    public function getFormActions()
136
    {
137
        $submitText = ($this->controller->SubmitButtonText)
138
            ? $this->controller->SubmitButtonText
139
            : _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SUBMITBUTTON', 'Submit');
140
        $clearText = ($this->controller->ClearButtonText)
141
            ? $this->controller->ClearButtonText
142
            : _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.CLEARBUTTON', 'Clear');
143
144
        $actions = FieldList::create(FormAction::create('process', $submitText));
145
146
        if ($this->controller->ShowClearButton) {
147
            $actions->push(FormAction::create('clearForm', $clearText)->setAttribute('type', 'reset'));
148
        }
149
150
        $this->extend('updateFormActions', $actions);
151
        $actions->setForm($this);
152
        return $actions;
153
    }
154
155
    /**
156
     * Get the required form fields for this form.
157
     *
158
     * @return RequiredFields
159
     */
160
    public function getRequiredFields()
161
    {
162
        // Generate required field validator
163
        $requiredNames = $this
164
            ->getController()
165
            ->Fields()
166
            ->filter('Required', true)
167
            ->column('Name');
168
        $required = new RequiredFields($requiredNames);
169
        $this->extend('updateRequiredFields', $required);
170
        $required->setForm($this);
171
        return $required;
172
    }
173
174
    /**
175
     * Override some we can add UserForm specific attributes to the form.
176
     *
177
     * @return array
178
     */
179
    public function getAttributes()
180
    {
181
        $attrs = parent::getAttributes();
182
183
        $attrs['class'] = $attrs['class'] . ' userform';
184
        $attrs['data-livevalidation'] = (bool)$this->controller->EnableLiveValidation;
185
        $attrs['data-toperrors'] = (bool)$this->controller->DisplayErrorMessagesAtTop;
186
187
        return $attrs;
188
    }
189
190
    /**
191
     * @return string
192
     */
193
    public function getButtonText()
194
    {
195
        return $this->config()->get('button_text');
196
    }
197
}
198