1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\PartialUserforms\Controllers; |
4
|
|
|
|
5
|
|
|
use Firesphere\PartialUserforms\Models\PartialFieldSubmission; |
6
|
|
|
use Firesphere\PartialUserforms\Models\PartialFormSubmission; |
7
|
|
|
use SilverStripe\CMS\Controllers\ContentController; |
8
|
|
|
use SilverStripe\Control\Controller; |
9
|
|
|
use SilverStripe\Control\HTTPRequest; |
10
|
|
|
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; |
11
|
|
|
use SilverStripe\ORM\DataObject; |
12
|
|
|
use SilverStripe\ORM\ValidationException; |
13
|
|
|
use SilverStripe\UserForms\Control\UserDefinedFormController; |
14
|
|
|
use SilverStripe\UserForms\Form\UserForm; |
15
|
|
|
use SilverStripe\UserForms\Model\EditableFormField; |
16
|
|
|
use SilverStripe\UserForms\Model\UserDefinedForm; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class \Firesphere\PartialUserforms\Controllers\PartialUserFormController |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
class PartialUserFormController extends ContentController |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Session key name |
26
|
|
|
*/ |
27
|
|
|
const SESSION_KEY = 'PartialSubmissionID'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private static $url_handlers = [ |
|
|
|
|
33
|
|
|
'' => 'savePartialSubmission', |
34
|
|
|
'$Key/$Token' => 'partial', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private static $allowed_actions = [ |
|
|
|
|
41
|
|
|
'savePartialSubmission', |
42
|
|
|
'partial', |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param HTTPRequest $request |
47
|
|
|
* @return int |
48
|
|
|
* @throws ValidationException |
49
|
|
|
*/ |
50
|
|
|
public function savePartialSubmission(HTTPRequest $request) |
51
|
|
|
{ |
52
|
|
|
$postVars = $request->postVars(); |
53
|
|
|
$editableField = null; |
54
|
|
|
|
55
|
|
|
// We don't want SecurityID and/or the process Action stored as a thing |
56
|
|
|
unset($postVars['SecurityID'], $postVars['action_process']); |
57
|
|
|
$submissionID = $request->getSession()->get(self::SESSION_KEY); |
58
|
|
|
|
59
|
|
|
/** @var PartialFormSubmission $partialSubmission */ |
60
|
|
|
$partialSubmission = PartialFormSubmission::get()->byID($submissionID); |
61
|
|
|
|
62
|
|
|
if (!$submissionID || !$partialSubmission) { |
63
|
|
|
$partialSubmission = PartialFormSubmission::create(); |
64
|
|
|
$submissionID = $partialSubmission->write(); |
65
|
|
|
} |
66
|
|
|
$request->getSession()->set(self::SESSION_KEY, $submissionID); |
67
|
|
|
foreach ($postVars as $field => $value) { |
68
|
|
|
/** @var EditableFormField $editableField */ |
69
|
|
|
$editableField = $this->createOrUpdateSubmission([ |
70
|
|
|
'Name' => $field, |
71
|
|
|
'Value' => $value, |
72
|
|
|
'SubmittedFormID' => $submissionID |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($editableField instanceof EditableFormField && !$partialSubmission->UserDefinedFormID) { |
77
|
|
|
$partialSubmission->update([ |
78
|
|
|
'UserDefinedFormID' => $editableField->Parent()->ID, |
79
|
|
|
'ParentID' => $editableField->Parent()->ID, |
80
|
|
|
'ParentClass' => $editableField->Parent()->ClassName, |
81
|
|
|
'UserDefinedFormClass' => $editableField->Parent()->ClassName |
82
|
|
|
]); |
83
|
|
|
$partialSubmission->write(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $submissionID; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $formData |
91
|
|
|
* @return DataObject|EditableFormField |
92
|
|
|
* @throws ValidationException |
93
|
|
|
*/ |
94
|
|
|
protected function createOrUpdateSubmission($formData) |
95
|
|
|
{ |
96
|
|
|
if (is_array($formData['Value'])) { |
97
|
|
|
$formData['Value'] = implode(', ', $formData['Value']); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$filter = [ |
101
|
|
|
'Name' => $formData['Name'], |
102
|
|
|
'SubmittedFormID' => $formData['SubmittedFormID'], |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
$exists = PartialFieldSubmission::get()->filter($filter)->first(); |
106
|
|
|
// Set the title |
107
|
|
|
$editableField = EditableFormField::get()->filter(['Name' => $formData['Name']])->first(); |
108
|
|
|
if ($editableField) { |
109
|
|
|
$formData['Title'] = $editableField->Title; |
110
|
|
|
$formData['ParentClass'] = $editableField->Parent()->ClassName; |
111
|
|
|
} |
112
|
|
|
if (!$exists) { |
113
|
|
|
$exists = PartialFieldSubmission::create($formData); |
114
|
|
|
$exists->write(); |
115
|
|
|
} else { |
116
|
|
|
$exists->update($formData); |
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 (!$partial || !$partial->UserDefinedFormID) { |
141
|
|
|
return $this->httpError(404); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ($partial->generateKey($token) === $key) { |
145
|
|
|
|
146
|
|
|
// TODO: Recognize visitor with the password |
147
|
|
|
// TODO: Populate form values |
148
|
|
|
|
149
|
|
|
$record = UserDefinedForm::get()->byID($partial->UserDefinedFormID); |
150
|
|
|
$controller = new UserDefinedFormController($record); |
151
|
|
|
$controller->init(); |
|
|
|
|
152
|
|
|
|
153
|
|
|
return $this->customise([ |
154
|
|
|
'Title' => $record->Title, |
155
|
|
|
'Breadcrumbs' => $record->Breadcrumbs(), |
156
|
|
|
'Content' => $this->obj('Content'), |
157
|
|
|
'Form' => $controller->Form(), |
158
|
|
|
'Link' => $partial->getPartialLink() |
159
|
|
|
])->renderWith(['PartialUserform', 'Page']); |
160
|
|
|
} else { |
161
|
|
|
return $this->httpError(404); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|