Completed
Push — master ( 67ead9...61a703 )
by Daniel
12s
created

UserForm::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 4.0011

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 23
cts 24
cp 0.9583
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 23
nc 8
nop 2
crap 4.0011
1
<?php
2
3
/**
4
 * @package userforms
5
 */
6
class UserForm extends Form
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

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