1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MultiFormStep controls the behaviour of a single form step in the MultiForm |
5
|
|
|
* process. All form steps are required to be subclasses of this class, as it |
6
|
|
|
* encapsulates the functionality required for the step to be aware of itself |
7
|
|
|
* in the process by knowing what it's next step is, and if applicable, it's previous |
8
|
|
|
* step. |
9
|
|
|
* |
10
|
|
|
* @package multiform |
11
|
|
|
*/ |
12
|
|
|
class MultiFormStep extends DataObject { |
|
|
|
|
13
|
|
|
|
14
|
|
|
private static $db = array( |
|
|
|
|
15
|
|
|
'Data' => 'Text' // stores serialized maps with all session information |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
private static $has_one = array( |
|
|
|
|
19
|
|
|
'Session' => 'MultiFormSession' |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Centerpiece of the flow control for the form. |
24
|
|
|
* |
25
|
|
|
* If set to a string, you have a linear form flow |
26
|
|
|
* If set to an array, you should use {@link getNextStep()} |
27
|
|
|
* to enact flow control and branching to different form |
28
|
|
|
* steps, most likely based on previously set session data |
29
|
|
|
* (e.g. a checkbox field or a dropdown). |
30
|
|
|
* |
31
|
|
|
* @var array|string |
32
|
|
|
*/ |
33
|
|
|
public static $next_steps; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Each {@link MultiForm} subclass needs at least |
37
|
|
|
* one step which is marked as the "final" one |
38
|
|
|
* and triggers the {@link MultiForm->finish()} |
39
|
|
|
* method that wraps up the whole submission. |
40
|
|
|
* |
41
|
|
|
* @var boolean |
42
|
|
|
*/ |
43
|
|
|
public static $is_final_step = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* This variable determines whether a user can use |
47
|
|
|
* the "back" action from this step. |
48
|
|
|
* |
49
|
|
|
* @TODO This does not check if the arbitrarily chosen step |
50
|
|
|
* using the step indicator is actually a previous step, so |
51
|
|
|
* unless you remove the link from the indicator template, or |
52
|
|
|
* type in StepID=23 to the address bar you can still go back |
53
|
|
|
* using the step indicator. |
54
|
|
|
* |
55
|
|
|
* @var boolean |
56
|
|
|
*/ |
57
|
|
|
protected static $can_go_back = true; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Title of this step. |
61
|
|
|
* |
62
|
|
|
* Used for the step indicator templates. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
protected $title; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Form class that this step is directly related to. |
70
|
|
|
* |
71
|
|
|
* @var MultiForm subclass |
72
|
|
|
*/ |
73
|
|
|
protected $form; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* List of additional CSS classes for this step |
77
|
|
|
* |
78
|
|
|
* @var array $extraClasses |
79
|
|
|
*/ |
80
|
|
|
protected $extraClasses = array(); |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Form fields to be rendered with this step. |
84
|
|
|
* (Form object is created in {@link MultiForm}. |
85
|
|
|
* |
86
|
|
|
* This function needs to be implemented on your |
87
|
|
|
* subclasses of MultiFormStep. |
88
|
|
|
* |
89
|
|
|
* @return FieldList |
90
|
|
|
*/ |
91
|
|
|
public function getFields() { |
92
|
|
|
user_error('Please implement getFields on your MultiFormStep subclass', E_USER_ERROR); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Additional form actions to be added to this step. |
97
|
|
|
* (Form object is created in {@link MultiForm}. |
98
|
|
|
* |
99
|
|
|
* Note: This is optional, and is to be implemented |
100
|
|
|
* on your subclasses of MultiFormStep. |
101
|
|
|
* |
102
|
|
|
* @return FieldList |
103
|
|
|
*/ |
104
|
|
|
public function getExtraActions() { |
105
|
|
|
return (class_exists('FieldList')) ? new FieldList() : new FieldSet(); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get a validator specific to this form. |
110
|
|
|
* The form is automatically validated in {@link Form->httpSubmission()}. |
111
|
|
|
* |
112
|
|
|
* @return Validator |
113
|
|
|
*/ |
114
|
|
|
public function getValidator() { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Accessor method for $this->title |
120
|
|
|
* |
121
|
|
|
* @return string Title of this step |
122
|
|
|
*/ |
123
|
|
|
public function getTitle() { |
124
|
|
|
return $this->title ? $this->title : $this->class; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Gets a direct link to this step (only works |
129
|
|
|
* if you're allowed to skip steps, or this step |
130
|
|
|
* has already been saved to the database |
131
|
|
|
* for the current {@link MultiFormSession}). |
132
|
|
|
* |
133
|
|
|
* @return string Relative URL to this step |
134
|
|
|
*/ |
135
|
|
|
public function Link() { |
136
|
|
|
return Controller::join_links($this->form->getDisplayLink(), "?MultiFormSessionID={$this->Session()->Hash}"); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Unserialize stored session data and return it. |
141
|
|
|
* This is used for loading data previously saved |
142
|
|
|
* in session back into the form. |
143
|
|
|
* |
144
|
|
|
* You need to overload this method onto your own |
145
|
|
|
* step if you require custom loading. An example |
146
|
|
|
* would be selective loading specific fields, leaving |
147
|
|
|
* others that are not required. |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function loadData() { |
152
|
|
|
return ($this->Data && is_string($this->Data)) ? unserialize($this->Data) : array(); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Save the data for this step into session, serializing it first. |
157
|
|
|
* |
158
|
|
|
* To selectively save fields, instead of it all, this |
159
|
|
|
* method would need to be overloaded on your step class. |
160
|
|
|
* |
161
|
|
|
* @param array $data The processed data from save() on {@link MultiForm} |
162
|
|
|
*/ |
163
|
|
|
public function saveData($data) { |
164
|
|
|
$this->Data = serialize($data); |
|
|
|
|
165
|
|
|
$this->write(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Save the data on this step into an object, |
170
|
|
|
* similiar to {@link Form->saveInto()} - by building |
171
|
|
|
* a stub form from {@link getFields()}. This is necessary |
172
|
|
|
* to trigger each {@link FormField->saveInto()} method |
173
|
|
|
* individually, rather than assuming that all data |
174
|
|
|
* serialized through {@link saveData()} can be saved |
175
|
|
|
* as a simple value outside of the original FormField context. |
176
|
|
|
* |
177
|
|
|
* @param DataObject $obj |
178
|
|
|
*/ |
179
|
|
|
public function saveInto($obj) { |
180
|
|
|
$form = new Form( |
181
|
|
|
Controller::curr(), |
182
|
|
|
'Form', |
183
|
|
|
$this->getFields(), |
|
|
|
|
184
|
|
|
((class_exists('FieldList')) ? new FieldList() : new FieldSet()) |
185
|
|
|
); |
186
|
|
|
$form->loadDataFrom($this->loadData()); |
187
|
|
|
$form->saveInto($obj); |
188
|
|
|
return $obj; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Custom validation for a step. In most cases, it should be sufficient |
193
|
|
|
* to have built-in validation through the {@link Validator} class |
194
|
|
|
* on the {@link getValidator()} method. |
195
|
|
|
* |
196
|
|
|
* Use {@link Form->sessionMessage()} to feed back validation messages |
197
|
|
|
* to the user. Please don't redirect from this method, |
198
|
|
|
* this is taken care of in {@link next()}. |
199
|
|
|
* |
200
|
|
|
* @param array $data Request data |
201
|
|
|
* @param Form $form |
202
|
|
|
* @return boolean Validation success |
203
|
|
|
*/ |
204
|
|
|
public function validateStep($data, $form) { |
|
|
|
|
205
|
|
|
return true; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns the first value of $next_step |
210
|
|
|
* |
211
|
|
|
* @return String Classname of a {@link MultiFormStep} subclass |
212
|
|
|
*/ |
213
|
|
|
public function getNextStep() { |
214
|
|
|
$nextSteps = static::$next_steps; |
215
|
|
|
|
216
|
|
|
// Check if next_steps have been implemented properly if not the final step |
217
|
|
|
if(!$this->isFinalStep()) { |
218
|
|
|
if(!isset($nextSteps)) user_error('MultiFormStep->getNextStep(): Please define at least one $next_steps on ' . $this->class, E_USER_ERROR); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if(is_string($nextSteps)) { |
222
|
|
|
return $nextSteps; |
223
|
|
|
} elseif(is_array($nextSteps) && count($nextSteps)) { |
224
|
|
|
// custom flow control goes here |
225
|
|
|
return $nextSteps[0]; |
226
|
|
|
} else { |
227
|
|
|
return false; |
|
|
|
|
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns the next step to the current step in the database. |
233
|
|
|
* |
234
|
|
|
* This will only return something if you've previously visited |
235
|
|
|
* the step ahead of the current step, and then gone back a step. |
236
|
|
|
* |
237
|
|
|
* @return MultiFormStep|boolean |
238
|
|
|
*/ |
239
|
|
|
public function getNextStepFromDatabase() { |
240
|
|
|
if($this->SessionID && is_numeric($this->SessionID)) { |
|
|
|
|
241
|
|
|
$nextSteps = static::$next_steps; |
242
|
|
|
|
243
|
|
|
if(is_string($nextSteps)) { |
244
|
|
|
return DataObject::get_one($nextSteps, "\"SessionID\" = {$this->SessionID}"); |
|
|
|
|
245
|
|
|
} elseif(is_array($nextSteps)) { |
246
|
|
|
return DataObject::get_one($nextSteps[0], "\"SessionID\" = {$this->SessionID}"); |
|
|
|
|
247
|
|
|
} else { |
248
|
|
|
return false; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Accessor method for self::$next_steps |
255
|
|
|
* |
256
|
|
|
* @return string|array |
257
|
|
|
*/ |
258
|
|
|
public function getNextSteps() { |
259
|
|
|
return static::$next_steps; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Returns the previous step, if there is one. |
264
|
|
|
* |
265
|
|
|
* To determine if there is a previous step, we check the database to see if there's |
266
|
|
|
* a previous step for this multi form session ID. |
267
|
|
|
* |
268
|
|
|
* @return String Classname of a {@link MultiFormStep} subclass |
269
|
|
|
*/ |
270
|
|
|
public function getPreviousStep() { |
271
|
|
|
$steps = DataObject::get('MultiFormStep', "\"SessionID\" = {$this->SessionID}", '"LastEdited" DESC'); |
|
|
|
|
272
|
|
|
if($steps) { |
273
|
|
|
foreach($steps as $step) { |
274
|
|
|
$step->setForm($this->form); |
275
|
|
|
|
276
|
|
|
if($step->getNextStep()) { |
277
|
|
|
if($step->getNextStep() == $this->class) { |
278
|
|
|
return $step->class; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Retrieves the previous step class record from the database. |
287
|
|
|
* |
288
|
|
|
* This will only return a record if you've previously been on the step. |
289
|
|
|
* |
290
|
|
|
* @return MultiFormStep subclass |
291
|
|
|
*/ |
292
|
|
|
public function getPreviousStepFromDatabase() { |
293
|
|
|
if($prevStepClass = $this->getPreviousStep()) { |
294
|
|
|
return DataObject::get_one($prevStepClass, "\"SessionID\" = {$this->SessionID}"); |
|
|
|
|
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Get the text to the use on the button to the previous step. |
300
|
|
|
* @return string |
301
|
|
|
*/ |
302
|
|
|
public function getPrevText() { |
303
|
|
|
return _t('MultiForm.BACK', 'Back'); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Get the text to use on the button to the next step. |
308
|
|
|
* @return string |
309
|
|
|
*/ |
310
|
|
|
public function getNextText() { |
311
|
|
|
return _t('MultiForm.NEXT', 'Next'); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Get the text to use on the button to submit the form. |
316
|
|
|
* @return string |
317
|
|
|
*/ |
318
|
|
|
public function getSubmitText() { |
319
|
|
|
return _t('MultiForm.SUBMIT', 'Submit'); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Sets the form that this step is directly related to. |
324
|
|
|
* |
325
|
|
|
* @param MultiForm subclass $form |
326
|
|
|
*/ |
327
|
|
|
public function setForm($form) { |
328
|
|
|
$this->form = $form; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @return Form |
333
|
|
|
*/ |
334
|
|
|
public function getForm() { |
335
|
|
|
return $this->form; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
// ##################### Utility #################### |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Determines whether the user is able to go back using the "action_back" |
342
|
|
|
* Determines whether the user is able to go back using the "action_back" |
343
|
|
|
* Determines whether the user is able to go back using the "action_back" |
344
|
|
|
* form action, based on the boolean value of $can_go_back. |
345
|
|
|
* |
346
|
|
|
* @return boolean |
347
|
|
|
*/ |
348
|
|
|
public function canGoBack() { |
349
|
|
|
return static::$can_go_back; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Determines whether this step is the final step in the multi-step process or not, |
354
|
|
|
* based on the variable $is_final_step - which must be defined on at least one step. |
355
|
|
|
* |
356
|
|
|
* @return boolean |
357
|
|
|
*/ |
358
|
|
|
public function isFinalStep() { |
359
|
|
|
return static::$is_final_step; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Determines whether the currently viewed step is the current step set in the session. |
364
|
|
|
* This assumes you are checking isCurrentStep() against a data record of a MultiFormStep |
365
|
|
|
* subclass, otherwise it doesn't work. An example of this is using a singleton instance - it won't |
366
|
|
|
* work because there's no data. |
367
|
|
|
* |
368
|
|
|
* @return boolean |
369
|
|
|
*/ |
370
|
|
|
public function isCurrentStep() { |
371
|
|
|
return ($this->class == $this->Session()->CurrentStep()->class) ? true : false; |
|
|
|
|
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Add a CSS-class to the step. If needed, multiple classes can be added by delimiting a string with spaces. |
376
|
|
|
* |
377
|
|
|
* @param string $class A string containing a classname or several class names delimited by a space. |
378
|
|
|
* @return MultiFormStep |
379
|
|
|
*/ |
380
|
|
View Code Duplication |
public function addExtraClass($class) { |
|
|
|
|
381
|
|
|
// split at white space |
382
|
|
|
$classes = preg_split('/\s+/', $class); |
383
|
|
|
foreach($classes as $class) { |
384
|
|
|
// add classes one by one |
385
|
|
|
$this->extraClasses[$class] = $class; |
386
|
|
|
} |
387
|
|
|
return $this; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Remove a CSS-class from the step. Multiple classes names can be passed through as a space delimited string. |
392
|
|
|
* |
393
|
|
|
* @param string $class |
394
|
|
|
* @return MultiFormStep |
395
|
|
|
*/ |
396
|
|
View Code Duplication |
public function removeExtraClass($class) { |
|
|
|
|
397
|
|
|
// split at white space |
398
|
|
|
$classes = preg_split('/\s+/', $class); |
399
|
|
|
foreach ($classes as $class) { |
400
|
|
|
// unset one by one |
401
|
|
|
unset($this->extraClasses[$class]); |
402
|
|
|
} |
403
|
|
|
return $this; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @return string |
408
|
|
|
*/ |
409
|
|
|
public function getExtraClasses() { |
410
|
|
|
return join(' ', array_keys($this->extraClasses)); |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.