FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
multistep-form/src/MultistepForm/MultistepForm.php at master · infinityloop-dev/multistep-form · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
infinityloop-dev
/
multistep-form
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
5
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
multistep-form
/
src
/
MultistepForm
/
MultistepForm.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
269 lines (215 loc) · 6.97 KB
Breadcrumbs
multistep-form
/
src
/
MultistepForm
/
MultistepForm.php
Copy path
File metadata and controls
269 lines (215 loc) · 6.97 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
declare
(strict_types =
1
);
namespace
Infinityloop
\
MultistepForm
;
final
class
MultistepForm
extends
\
Nette
\
Application
\
UI
\Control
{
private
const
FORM_IDENTIFIER
=
'
sub_session_id
'
;
private
\
Nette
\
Http
\
Session
$
session
;
private
string
$
subsessionId
;
private
int
$
currentStep
;
private
array
$
factories
= [];
private
array
$
templates
= [];
private
array
$
defaults
= [];
private
bool
$
buttonSubmitted
=
false
;
private
$
successCallback
=
null
;
public
function
__construct
(
\
Nette
\
Http
\
Session
$
session
,
\
Nette
\
Http
\
Request
$
httpRequest
)
{
$
this
->
session
=
$
session
;
$
this
->
subsessionId
=
$
httpRequest
->
getPost
(
self
::
FORM_IDENTIFIER
) ?? \
Nette
\
Utils
\Random::
generate
(
10
);
}
public
function
render
() :
void
{
if
(
\count
(
$
this
->
factories
) <
$
this
->
currentStep
||
\count
(
$
this
->
templates
) <
$
this
->
currentStep
) {
throw
new
\
Nette
\
InvalidStateException
(
'
No factory given
'
);
}
$
this
->
template
->
customTemplate
=
$
this
->
templates
[
$
this
->
currentStep
-
1
];
$
this
->
template
->
setFile
(
__DIR__
.
'
/MultistepForm.latte
'
);
$
this
->
template
->
render
();
}
public
function
setFactories
(
array
$
factories
) :
self
{
$
this
->
factories
=
$
factories
;
return
$
this
;
}
public
function
addFactory
(
$
factory
,
string
$
templatePath
=
null
) :
self
{
$
this
->
factories
[] =
$
factory
;
$
this
->
templates
[] =
$
templatePath
;
return
$
this
;
}
public
function
setDefaults
(
array
$
defaultValues
) :
self
{
$
this
->
defaults
=
$
defaultValues
;
return
$
this
;
}
public
function
setSuccessCallback
(
callable
$
successCallback
) :
self
{
$
this
->
successCallback
=
$
successCallback
;
return
$
this
;
}
/**
* Returns current step.
*/
public
function
getCurrentStep
() :
int
{
return
$
this
->
currentStep
;
}
/**
* Is current step the first one?
*/
public
function
isFirst
() :
bool
{
return
$
this
->
currentStep
===
1
;
}
/**
* Is current step the last one?
*/
public
function
isLast
() :
bool
{
return
$
this
->
currentStep
===
\count
(
$
this
->
factories
);
}
/**
* Is editing?
*/
public
function
isEdit
() :
bool
{
return
false
;
}
/**
* Returns maximum achieved step.
*/
public
function
getMaxStep
() :
int
{
return
\iterator_count
(
$
this
->
getSection
()->
getIterator
());
}
/**
* Returns all filled values.
*
* @return array
*/
public
function
getValues
() :
array
{
$
return
= [];
foreach
(
$
this
->
getSection
()->
getIterator
()
as
$
saved
) {
$
return
+=
$
saved
;
}
return
$
return
;
}
/**
* @internal
*/
public
function
saveState
(
array
&
$
params
):
void
{
$
params
[
'
step
'
] =
$
this
->
currentStep
??
1
;
$
params
[
self
::
FORM_IDENTIFIER
] =
$
this
->
subsessionId
;
}
/**
* @internal
*/
public
function
loadState
(
array
$
params
):
void
{
$
this
->
currentStep
= (
int
) (
$
params
[
'
step
'
] ??
1
);
$
this
->
subsessionId
=
$
params
[
self
::
FORM_IDENTIFIER
] ??
$
this
->
subsessionId
;
}
protected
function
createComponentForm
() :
\
Nette
\
Forms
\
Form
{
$
factory
=
$
this
->
factories
[
$
this
->
currentStep
-
1
];
if
(
\is_object
(
$
factory
) &&
\method_exists
(
$
factory
,
'
create
'
)) {
$
step
=
$
factory
->
create
(
$
this
->
getValues
());
}
elseif
(
\is_callable
(
$
factory
)) {
$
step
=
$
factory
(
$
this
->
getValues
());
}
else
{
throw
new
\
Nette
\
InvalidStateException
(
'
Factory must implement create method or be callable itself.
'
);
}
if
(
$
step
instanceof
\
Nette
\
Forms
\Form) {
$
step
->
addHidden
(
self
::
FORM_IDENTIFIER
,
$
this
->
subsessionId
)
->
setOmitted
();
$
this
->
appendButtons
(
$
step
);
$
step
->
monitor
(
self
::class,
static
function
(
MultistepForm
$
multistepForm
)
use
(
$
step
) :
void
{
$
step
->
setDefaults
(
$
multistepForm
->
defaults
+
$
multistepForm
->
getTempValues
());
});
return
$
step
;
}
throw
new
\
Nette
\
InvalidStateException
(
'
Return value of factory must be instance of Step
'
);
}
private
function
appendButtons
(
\
Nette
\
Forms
\
Form
$
form
) :
\
Nette
\
Forms
\
Form
{
$
next
=
$
form
->
addSubmit
(
'
next
'
,
'
Další
'
);
$
previous
=
$
form
->
addSubmit
(
'
previous
'
,
'
Předchozí
'
);
if
(
$
this
->
isFirst
()) {
$
previous
->
setDisabled
();
}
if
(
$
this
->
isEdit
()) {
$
form
->
addSubmit
(
'
quicksave
'
,
'
Uložit
'
);
}
$
form
->
onSuccess
[] =
function
(
\
Nette
\
Forms
\
Form
$
form
) :
void
{
if
(
$
this
->
buttonSubmitted
) {
return
;
}
$
this
->
onNext
(
$
form
,
$
form
->
getValues
(
'
array
'
));
};
$
next
->
onClick
[] =
function
(
\
Nette
\
Forms
\
Controls
\
SubmitButton
$
button
) :
void
{
$
this
->
buttonSubmitted
=
true
;
$
form
=
$
button
->
getForm
();
\assert
(
$
form
instanceof
\
Nette
\
Forms
\Form);
$
this
->
onNext
(
$
form
,
$
form
->
getValues
(
'
array
'
));
};
$
previous
->
setValidationScope
([]);
$
previous
->
onClick
[] =
function
(
\
Nette
\
Forms
\
Controls
\
SubmitButton
$
button
) :
void
{
$
this
->
buttonSubmitted
=
true
;
$
form
=
$
button
->
getForm
();
\assert
(
$
form
instanceof
\
Nette
\
Forms
\Form);
$
this
->
onPrevious
(
$
form
,
$
form
->
getValues
(
'
array
'
));
};
return
$
form
;
}
private
function
getSection
() :
\
Nette
\
Http
\
SessionSection
{
$
section
=
$
this
->
session
->
getSection
(
'
multiStepForm-
'
.
$
this
->
subsessionId
);
$
section
->
warnOnUndefined
=
true
;
$
section
->
setExpiration
(
'
+ 14 days
'
);
return
$
section
;
}
private
function
getTempValues
() :
array
{
$
section
=
$
this
->
getSection
();
return
$
section
[(
string
)
$
this
->
currentStep
] ?? [];
}
private
function
saveTempValues
(
array
$
values
) :
void
{
$
section
=
$
this
->
getSection
();
$
section
[(
string
)
$
this
->
currentStep
] =
$
values
;
}
private
function
onNext
(
\
Nette
\
Forms
\
Form
$
step
,
array
$
submittedValues
) :
void
{
$
this
->
saveTempValues
(
$
submittedValues
);
if
(
$
this
->
isLast
()) {
\call_user_func
(
$
this
->
successCallback
,
$
this
->
getValues
());
return
;
}
++
$
this
->
currentStep
;
$
this
->
redraw
();
}
private
function
onPrevious
(
\
Nette
\
Forms
\
Form
$
step
,
array
$
submittedValues
) :
void
{
if
(
$
this
->
isFirst
()) {
return
;
}
$
this
->
saveTempValues
(
$
submittedValues
);
--
$
this
->
currentStep
;
$
this
->
redraw
();
}
private
function
redraw
() :
void
{
$
form
=
$
this
->
getComponent
(
'
form
'
);
if
(
$
form
instanceof
\
Nette
\
ComponentModel
\IComponent) {
$
this
->
removeComponent
(
$
form
);
}
$
this
->
redrawControl
(
'
snippet
'
);
}
}
Back
|
FazBrowse Home
|
New Git URL