1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DNADesign\ElementalUserForms\Control; |
4
|
|
|
|
5
|
|
|
use DNADesign\Elemental\Controllers\ElementController; |
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use SilverStripe\Control\Director; |
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
9
|
|
|
use SilverStripe\UserForms\Control\UserDefinedFormController; |
10
|
|
|
use SilverStripe\UserForms\Form\UserForm; |
11
|
|
|
|
12
|
|
|
class ElementFormController extends ElementController |
13
|
|
|
{ |
14
|
|
|
private static $allowed_actions = [ |
|
|
|
|
15
|
|
|
'Form', |
16
|
|
|
'process', |
17
|
|
|
'finished' |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var UserDefinedFormController |
22
|
|
|
*/ |
23
|
|
|
protected $userFormController; |
24
|
|
|
|
25
|
|
|
protected function init() |
26
|
|
|
{ |
27
|
|
|
parent::init(); |
28
|
|
|
|
29
|
|
|
$controller = $this->getUserFormController() ?: UserDefinedFormController::create($this->element); |
30
|
|
|
$controller->setRequest($this->getRequest()); |
31
|
|
|
$controller->doInit(); |
32
|
|
|
|
33
|
|
|
$this->setUserFormController($controller); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return UserForm |
38
|
|
|
*/ |
39
|
|
|
public function Form() |
40
|
|
|
{ |
41
|
|
|
return $this->getUserFormController()->Form(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function process($data) |
45
|
|
|
{ |
46
|
|
|
$user = $this->getUserFormController(); |
47
|
|
|
|
48
|
|
|
return $user->process($data, $user->Form()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function finished() |
52
|
|
|
{ |
53
|
|
|
$user = $this->getUserFormController(); |
54
|
|
|
|
55
|
|
|
$user->finished(); |
56
|
|
|
|
57
|
|
|
$page = $this->getPage(); |
|
|
|
|
58
|
|
|
$controller = Injector::inst()->create($page->getControllerName(), $this->element->getPage()); |
59
|
|
|
$element = $this->element; |
60
|
|
|
|
61
|
|
|
return $controller->customise([ |
62
|
|
|
'Content' => $element->renderWith($element->getRenderTemplates('_ReceivedFormSubmission')), |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $action |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function Link($action = null) |
72
|
|
|
{ |
73
|
|
|
$id = $this->element->ID; |
74
|
|
|
$segment = Controller::join_links('element', $id, $action); |
75
|
|
|
$page = Director::get_current_page(); |
76
|
|
|
|
77
|
|
|
if ($page && !($page instanceof ElementController)) { |
78
|
|
|
return $page->Link($segment); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($controller = $this->getParentController()) { |
|
|
|
|
82
|
|
|
return $controller->Link($segment); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $segment; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return the associated UserDefinedFormController |
90
|
|
|
* |
91
|
|
|
* @return UserDefinedFormController |
92
|
|
|
*/ |
93
|
|
|
public function getUserFormController() |
94
|
|
|
{ |
95
|
|
|
return $this->userFormController; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set the associated UserDefinedFormController |
100
|
|
|
* |
101
|
|
|
* @param UserDefinedFormController $controller |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function setUserFormController(UserDefinedFormController $controller) |
105
|
|
|
{ |
106
|
|
|
$this->userFormController = $controller; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.