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
|
|
|
* @throws HTTPResponse_Exception |
49
|
|
|
*/ |
50
|
|
|
public function setData($request) |
51
|
|
|
{ |
52
|
|
|
// Ensure this URL doesn't get picked up by HTTP caches |
53
|
|
|
HTTPCacheControlMiddleware::singleton()->disableCache(); |
54
|
|
|
|
55
|
|
|
$key = $request->param('Key'); |
56
|
|
|
$token = $request->param('Token'); |
57
|
|
|
|
58
|
|
|
/** @var PartialFormSubmission $partial */ |
59
|
|
|
$partial = PartialFormSubmission::get()->find('Token', $token); |
60
|
|
|
if (!$token || |
61
|
|
|
!$partial || |
62
|
|
|
!$partial->UserDefinedFormID || |
63
|
|
|
!hash_equals($partial->generateKey($token), $key) |
64
|
|
|
) { |
65
|
|
|
return $this->httpError(404); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$session = $this->getRequest()->getSession(); |
69
|
|
|
// Set the session if the last session has expired |
70
|
|
|
if (!$session->get(PartialSubmissionController::SESSION_KEY)) { |
71
|
|
|
$session->set(PartialSubmissionController::SESSION_KEY, $partial->ID); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->setPartialFormSubmission($partial); |
75
|
|
|
// Set data record and load the form |
76
|
|
|
/** @var UserDefinedForm dataRecord */ |
77
|
|
|
$this->dataRecord = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Partial form |
82
|
|
|
* |
83
|
|
|
* @param HTTPRequest $request |
84
|
|
|
* @return HTTPResponse|DBHTMLText|void |
85
|
|
|
* @throws \Exception |
86
|
|
|
*/ |
87
|
|
|
public function partial(HTTPRequest $request) |
88
|
|
|
{ |
89
|
|
|
$this->setData($request); |
90
|
|
|
if ($this->dataRecord->PasswordProtected && |
91
|
|
|
!$request->getSession()->get(PasswordForm::PASSWORD_SESSION_KEY) |
92
|
|
|
) { |
93
|
|
|
return $this->redirect('verify'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->setFailover($this->dataRecord); |
97
|
|
|
|
98
|
|
|
$form = $this->Form(); |
99
|
|
|
$partial = $this->getPartialFormSubmission(); |
100
|
|
|
$fields = $partial->PartialFields()->map('Name', 'Value')->toArray(); |
101
|
|
|
$form->loadDataFrom($fields); |
|
|
|
|
102
|
|
|
|
103
|
|
|
// Copied from {@link UserDefinedFormController} |
104
|
|
|
if ($this->Content && $form && !$this->config()->disable_form_content_shortcode) { |
|
|
|
|
105
|
|
|
$hasLocation = stristr($this->Content, '$UserDefinedForm'); |
|
|
|
|
106
|
|
|
if ($hasLocation) { |
107
|
|
|
/** @see Requirements_Backend::escapeReplacement */ |
108
|
|
|
$formEscapedForRegex = addcslashes($form->forTemplate(), '\\$'); |
|
|
|
|
109
|
|
|
$content = preg_replace( |
110
|
|
|
'/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', |
111
|
|
|
$formEscapedForRegex, |
112
|
|
|
$this->Content |
|
|
|
|
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
return $this->customise([ |
116
|
|
|
'Content' => DBField::create_field('HTMLText', $content), |
117
|
|
|
'Form' => '', |
118
|
|
|
'PartialLink' => $partial->getPartialLink() |
119
|
|
|
])->renderWith([static::class, Page::class]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this->customise([ |
124
|
|
|
'Content' => DBField::create_field('HTMLText', $this->Content), |
|
|
|
|
125
|
|
|
'Form' => $form, |
126
|
|
|
'PartialLink' => $partial->getPartialLink() |
127
|
|
|
])->renderWith([static::class, Page::class]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return PartialFormSubmission |
132
|
|
|
*/ |
133
|
|
|
public function getPartialFormSubmission(): PartialFormSubmission |
134
|
|
|
{ |
135
|
|
|
return $this->partialFormSubmission; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param PartialFormSubmission $partialFormSubmission |
140
|
|
|
*/ |
141
|
|
|
public function setPartialFormSubmission(PartialFormSubmission $partialFormSubmission): void |
142
|
|
|
{ |
143
|
|
|
$this->partialFormSubmission = $partialFormSubmission; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|