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