|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Firesphere\PartialUserforms\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Firesphere\PartialUserforms\Models\PartialFormSubmission; |
|
7
|
|
|
use Page; |
|
8
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
9
|
|
|
use Firesphere\PartialUserforms\Forms\PasswordForm; |
|
10
|
|
|
use SilverStripe\Control\HTTPResponse; |
|
11
|
|
|
use SilverStripe\Control\HTTPResponse_Exception; |
|
12
|
|
|
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; |
|
13
|
|
|
use SilverStripe\Forms\Form; |
|
14
|
|
|
use SilverStripe\ORM\DataObject; |
|
15
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
|
16
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
|
17
|
|
|
use SilverStripe\UserForms\Control\UserDefinedFormController; |
|
18
|
|
|
use SilverStripe\UserForms\Model\UserDefinedForm; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class PartialUserFormController |
|
22
|
|
|
* |
|
23
|
|
|
* @package Firesphere\PartialUserforms\Controllers |
|
24
|
|
|
*/ |
|
25
|
|
|
class PartialUserFormController extends UserDefinedFormController |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $url_handlers = [ |
|
|
|
|
|
|
31
|
|
|
'$Key/$Token' => 'partial', |
|
32
|
|
|
]; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private static $allowed_actions = [ |
|
|
|
|
|
|
37
|
|
|
'partial', |
|
38
|
|
|
]; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var PartialFormSubmission |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $partialFormSubmission; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
3 |
|
* Partial form |
|
46
|
|
|
* |
|
47
|
|
|
* @param HTTPRequest $request |
|
48
|
3 |
|
* @return HTTPResponse|DBHTMLText|void |
|
49
|
|
|
* @throws HTTPResponse_Exception |
|
50
|
3 |
|
* @throws Exception |
|
51
|
3 |
|
*/ |
|
52
|
3 |
|
public function partial(HTTPRequest $request) |
|
53
|
1 |
|
{ |
|
54
|
|
|
/** @var PartialFormSubmission $partial */ |
|
55
|
|
|
$partial = $this->setData($request); |
|
56
|
|
|
if ($this->dataRecord->PasswordProtected && |
|
57
|
2 |
|
$request->getSession()->get(PasswordForm::PASSWORD_SESSION_KEY) !== $partial->ID |
|
58
|
2 |
|
) { |
|
59
|
|
|
return $this->redirect('verify'); |
|
60
|
2 |
|
} |
|
61
|
|
|
$record = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
|
|
|
|
|
|
62
|
2 |
|
/** @var self $controller */ |
|
63
|
|
|
$controller = self::create($record); |
|
64
|
|
|
$controller->doInit(); |
|
65
|
|
|
|
|
66
|
|
|
/** @var Form $form */ |
|
67
|
|
|
$form = $controller->Form(); |
|
68
|
|
|
$fields = $partial->PartialFields()->map('Name', 'Value')->toArray(); |
|
69
|
|
|
$form->loadDataFrom($fields); |
|
70
|
|
|
|
|
71
|
|
|
// Copied from {@link UserDefinedFormController} |
|
72
|
|
|
if ($controller->Content && $form && !$controller->config()->disable_form_content_shortcode) { |
|
|
|
|
|
|
73
|
|
|
$hasLocation = stristr($controller->Content, '$UserDefinedForm'); |
|
|
|
|
|
|
74
|
|
|
if ($hasLocation) { |
|
75
|
|
|
/** @see Requirements_Backend::escapeReplacement */ |
|
76
|
|
|
$formEscapedForRegex = addcslashes($form->forTemplate(), '\\$'); |
|
77
|
|
|
$content = preg_replace( |
|
78
|
|
|
'/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', |
|
79
|
|
|
$formEscapedForRegex, |
|
80
|
|
|
$controller->Content |
|
|
|
|
|
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
return $controller->customise([ |
|
84
|
|
|
'Content' => DBField::create_field('HTMLText', $content), |
|
85
|
|
|
'Form' => '', |
|
86
|
|
|
'PartialLink' => $partial->getPartialLink() |
|
87
|
|
|
])->renderWith([static::class, Page::class]); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $controller->customise([ |
|
92
|
|
|
'Content' => DBField::create_field('HTMLText', $controller->Content), |
|
|
|
|
|
|
93
|
|
|
'Form' => $form, |
|
94
|
|
|
'PartialLink' => $partial->getPartialLink() |
|
95
|
|
|
])->renderWith([static::class, Page::class]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* A little abstraction to be more readable |
|
100
|
|
|
* |
|
101
|
|
|
* @param HTTPRequest $request |
|
102
|
|
|
* @return PartialFormSubmission|void |
|
103
|
|
|
* @throws HTTPResponse_Exception |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setData($request) |
|
106
|
|
|
{ |
|
107
|
|
|
// Ensure this URL doesn't get picked up by HTTP caches |
|
108
|
|
|
HTTPCacheControlMiddleware::singleton()->disableCache(); |
|
109
|
|
|
|
|
110
|
|
|
$key = $request->param('Key'); |
|
111
|
|
|
$token = $request->param('Token'); |
|
112
|
|
|
if (!$key || !$token) { |
|
113
|
|
|
return $this->httpError(404); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** @var PartialFormSubmission $partial */ |
|
117
|
|
|
$partial = PartialFormSubmission::get()->find('Token', $token); |
|
118
|
|
|
if (!$token || |
|
119
|
|
|
!$partial || |
|
120
|
|
|
!$partial->UserDefinedFormID || |
|
121
|
|
|
!hash_equals($partial->generateKey($token), $key) |
|
122
|
|
|
) { |
|
123
|
|
|
return $this->httpError(404); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Set the session if the last session has expired |
|
127
|
|
|
if (!$request->getSession()->get(PartialSubmissionController::SESSION_KEY)) { |
|
128
|
|
|
$request->getSession()->set(PartialSubmissionController::SESSION_KEY, $partial->ID); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $partial; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return PartialFormSubmission |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getPartialFormSubmission(): PartialFormSubmission |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->partialFormSubmission; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param PartialFormSubmission $partialFormSubmission |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setPartialFormSubmission(PartialFormSubmission $partialFormSubmission): void |
|
146
|
|
|
{ |
|
147
|
|
|
$this->partialFormSubmission = $partialFormSubmission; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|