|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Firesphere\PartialUserforms\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Page; |
|
6
|
|
|
use Firesphere\PartialUserforms\Models\PartialFormSubmission; |
|
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
8
|
|
|
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; |
|
9
|
|
|
use SilverStripe\ORM\DataObject; |
|
10
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
|
11
|
|
|
use SilverStripe\UserForms\Control\UserDefinedFormController; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class PartialUserFormController |
|
15
|
|
|
* |
|
16
|
|
|
* @package Firesphere\PartialUserforms\Controllers |
|
17
|
|
|
*/ |
|
18
|
|
|
class PartialUserFormController extends UserDefinedFormController |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Session key name |
|
22
|
|
|
*/ |
|
23
|
|
|
public const SESSION_KEY = 'PartialSubmissionID'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private static $url_handlers = [ |
|
|
|
|
|
|
29
|
|
|
'$Key/$Token' => 'partial', |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private static $allowed_actions = [ |
|
|
|
|
|
|
36
|
|
|
'partial', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param HTTPRequest $request |
|
41
|
|
|
* @return int|mixed|void |
|
42
|
|
|
* @throws ValidationException |
|
43
|
|
|
* @throws \SilverStripe\Control\HTTPResponse_Exception |
|
44
|
|
|
*/ |
|
45
|
|
|
public function savePartialSubmission(HTTPRequest $request) |
|
46
|
|
|
{ |
|
47
|
|
|
if (!$request->isPOST()) { |
|
48
|
|
|
return $this->httpError(404); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$postVars = $request->postVars(); |
|
52
|
|
|
$editableField = null; |
|
53
|
|
|
|
|
54
|
|
|
// We don't want SecurityID and/or the process Action stored as a thing |
|
55
|
|
|
unset($postVars['SecurityID'], $postVars['action_process']); |
|
56
|
|
|
$submissionID = $request->getSession()->get(self::SESSION_KEY); |
|
57
|
|
|
|
|
58
|
|
|
/** @var PartialFormSubmission $partialSubmission */ |
|
59
|
|
|
$partialSubmission = PartialFormSubmission::get()->byID($submissionID); |
|
60
|
|
|
|
|
61
|
|
|
if (!$submissionID || !$partialSubmission) { |
|
62
|
|
|
$partialSubmission = PartialFormSubmission::create(); |
|
63
|
|
|
$submissionID = $partialSubmission->write(); |
|
64
|
|
|
} |
|
65
|
|
|
$request->getSession()->set(self::SESSION_KEY, $submissionID); |
|
66
|
|
|
foreach ($postVars as $field => $value) { |
|
67
|
|
|
/** @var EditableFormField $editableField */ |
|
68
|
|
|
$editableField = $this->createOrUpdateSubmission([ |
|
69
|
|
|
'Name' => $field, |
|
70
|
|
|
'Value' => $value, |
|
71
|
|
|
'SubmittedFormID' => $submissionID |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($editableField instanceof EditableFormField && !$partialSubmission->UserDefinedFormID) { |
|
|
|
|
|
|
76
|
|
|
$partialSubmission->update([ |
|
77
|
|
|
'UserDefinedFormID' => $editableField->Parent()->ID, |
|
78
|
|
|
'ParentID' => $editableField->Parent()->ID, |
|
79
|
|
|
'ParentClass' => $editableField->Parent()->ClassName, |
|
80
|
|
|
'UserDefinedFormClass' => $editableField->Parent()->ClassName |
|
81
|
|
|
]); |
|
82
|
|
|
$partialSubmission->write(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $submissionID; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param $formData |
|
90
|
|
|
* @return DataObject|EditableFormField |
|
91
|
|
|
* @throws ValidationException |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function createOrUpdateSubmission($formData) |
|
94
|
|
|
{ |
|
95
|
|
|
if (is_array($formData['Value'])) { |
|
96
|
|
|
$formData['Value'] = implode(', ', $formData['Value']); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$filter = [ |
|
100
|
|
|
'Name' => $formData['Name'], |
|
101
|
|
|
'SubmittedFormID' => $formData['SubmittedFormID'], |
|
102
|
|
|
]; |
|
103
|
|
|
|
|
104
|
|
|
$exists = PartialFieldSubmission::get()->filter($filter)->first(); |
|
105
|
|
|
// Set the title |
|
106
|
|
|
$editableField = EditableFormField::get()->filter(['Name' => $formData['Name']])->first(); |
|
107
|
|
|
if ($editableField) { |
|
108
|
|
|
$formData['Title'] = $editableField->Title; |
|
109
|
|
|
$formData['ParentClass'] = $editableField->Parent()->ClassName; |
|
110
|
|
|
} |
|
111
|
|
|
if (!$exists) { |
|
112
|
|
|
$exists = PartialFieldSubmission::create($formData); |
|
113
|
|
|
} else { |
|
114
|
|
|
$exists->update($formData); |
|
115
|
|
|
} |
|
116
|
|
|
if ($formData['Value'] !== '') { |
|
117
|
|
|
$exists->write(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Return the ParentID to link the PartialSubmission to it's proper thingy |
|
121
|
|
|
return $editableField; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Partial form |
|
126
|
|
|
* |
|
127
|
|
|
* @param HTTPRequest $request |
|
128
|
|
|
* @return \SilverStripe\ORM\FieldType\DBHTMLText|void |
|
129
|
|
|
* @throws \SilverStripe\Control\HTTPResponse_Exception |
|
130
|
|
|
*/ |
|
131
|
|
|
public function partial(HTTPRequest $request) |
|
132
|
|
|
{ |
|
133
|
|
|
// Ensure this URL doesn't get picked up by HTTP caches |
|
134
|
|
|
HTTPCacheControlMiddleware::singleton()->disableCache(); |
|
135
|
|
|
|
|
136
|
|
|
$key = $request->param('Key'); |
|
137
|
|
|
$token = $request->param('Token'); |
|
138
|
|
|
|
|
139
|
|
|
$partial = PartialFormSubmission::get()->find('Token', $token); |
|
140
|
|
|
if (!$token || !$partial || !$partial->UserDefinedFormID) { |
|
141
|
|
|
return $this->httpError(404); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// TODO: Recognize visitor with the password |
|
145
|
|
|
if ($partial->generateKey($token) === $key) { |
|
146
|
|
|
// Set the session if the last session has expired |
|
147
|
|
|
if (!$request->getSession()->get(PartialSubmissionController::SESSION_KEY)) { |
|
148
|
|
|
$request->getSession()->set(PartialSubmissionController::SESSION_KEY, $partial->ID); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// Set data record and load the form |
|
152
|
|
|
$this->dataRecord = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
|
|
|
|
|
|
153
|
|
|
$this->setFailover($this->dataRecord); |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
$form = $this->Form(); |
|
156
|
|
|
$fields = $partial->PartialFields()->map('Name', 'Value')->toArray(); |
|
157
|
|
|
$form->loadDataFrom($fields); |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
// Copied from {@link UserDefinedFormController} |
|
160
|
|
|
if ($this->Content && $form && !$this->config()->disable_form_content_shortcode) { |
|
|
|
|
|
|
161
|
|
|
$hasLocation = stristr($this->Content, '$UserDefinedForm'); |
|
|
|
|
|
|
162
|
|
|
if ($hasLocation) { |
|
163
|
|
|
/** @see Requirements_Backend::escapeReplacement */ |
|
164
|
|
|
$formEscapedForRegex = addcslashes($form->forTemplate(), '\\$'); |
|
|
|
|
|
|
165
|
|
|
$content = preg_replace( |
|
166
|
|
|
'/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', |
|
167
|
|
|
$formEscapedForRegex, |
|
168
|
|
|
$this->Content |
|
|
|
|
|
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
return $this->customise([ |
|
172
|
|
|
'Content' => DBField::create_field('HTMLText', $content), |
|
173
|
|
|
'Form' => '', |
|
174
|
|
|
'PartialLink' => $partial->getPartialLink() |
|
175
|
|
|
])->renderWith([static::class, Page::class]); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return $this->customise([ |
|
180
|
|
|
'Content' => DBField::create_field('HTMLText', $this->Content), |
|
|
|
|
|
|
181
|
|
|
'Form' => $form, |
|
182
|
|
|
'PartialLink' => $partial->getPartialLink() |
|
183
|
|
|
])->renderWith([static::class, Page::class]); |
|
184
|
|
|
} else { |
|
185
|
|
|
return $this->httpError(404); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|