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