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\HTTPRequest; |
9
|
|
|
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; |
10
|
|
|
use SilverStripe\ORM\DataObject; |
11
|
|
|
use SilverStripe\ORM\ValidationException; |
12
|
|
|
use SilverStripe\UserForms\Control\UserDefinedFormController; |
13
|
|
|
use SilverStripe\UserForms\Model\EditableFormField; |
14
|
|
|
use SilverStripe\View\Requirements; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class \Firesphere\PartialUserforms\Controllers\PartialUserFormController |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class PartialUserFormController extends ContentController |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Session key name |
24
|
|
|
*/ |
25
|
|
|
const SESSION_KEY = 'PartialSubmissionID'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private static $url_handlers = [ |
|
|
|
|
31
|
|
|
'' => 'savePartialSubmission', |
32
|
|
|
'$Key/$Token' => 'partial', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private static $allowed_actions = [ |
|
|
|
|
39
|
|
|
'savePartialSubmission', |
40
|
|
|
'partial', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param HTTPRequest $request |
45
|
|
|
* @return int |
46
|
|
|
* @throws ValidationException |
47
|
|
|
*/ |
48
|
|
|
public function savePartialSubmission(HTTPRequest $request) |
49
|
|
|
{ |
50
|
|
|
$postVars = $request->postVars(); |
51
|
|
|
$editableField = null; |
52
|
|
|
|
53
|
|
|
// We don't want SecurityID and/or the process Action stored as a thing |
54
|
|
|
unset($postVars['SecurityID'], $postVars['action_process']); |
55
|
|
|
$submissionID = $request->getSession()->get(self::SESSION_KEY); |
56
|
|
|
|
57
|
|
|
/** @var PartialFormSubmission $partialSubmission */ |
58
|
|
|
$partialSubmission = PartialFormSubmission::get()->byID($submissionID); |
59
|
|
|
|
60
|
|
|
if (!$submissionID || !$partialSubmission) { |
61
|
|
|
$partialSubmission = PartialFormSubmission::create(); |
62
|
|
|
$submissionID = $partialSubmission->write(); |
63
|
|
|
} |
64
|
|
|
$request->getSession()->set(self::SESSION_KEY, $submissionID); |
65
|
|
|
foreach ($postVars as $field => $value) { |
66
|
|
|
/** @var EditableFormField $editableField */ |
67
|
|
|
$editableField = $this->createOrUpdateSubmission([ |
68
|
|
|
'Name' => $field, |
69
|
|
|
'Value' => $value, |
70
|
|
|
'SubmittedFormID' => $submissionID |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($editableField instanceof EditableFormField && !$partialSubmission->UserDefinedFormID) { |
75
|
|
|
$partialSubmission->update([ |
76
|
|
|
'UserDefinedFormID' => $editableField->Parent()->ID, |
77
|
|
|
'ParentID' => $editableField->Parent()->ID, |
78
|
|
|
'ParentClass' => $editableField->Parent()->ClassName, |
79
|
|
|
'UserDefinedFormClass' => $editableField->Parent()->ClassName |
80
|
|
|
]); |
81
|
|
|
$partialSubmission->write(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $submissionID; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $formData |
89
|
|
|
* @return DataObject|EditableFormField |
90
|
|
|
* @throws ValidationException |
91
|
|
|
*/ |
92
|
|
|
protected function createOrUpdateSubmission($formData) |
93
|
|
|
{ |
94
|
|
|
if (is_array($formData['Value'])) { |
95
|
|
|
$formData['Value'] = implode(', ', $formData['Value']); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$filter = [ |
99
|
|
|
'Name' => $formData['Name'], |
100
|
|
|
'SubmittedFormID' => $formData['SubmittedFormID'], |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
$exists = PartialFieldSubmission::get()->filter($filter)->first(); |
104
|
|
|
// Set the title |
105
|
|
|
$editableField = EditableFormField::get()->filter(['Name' => $formData['Name']])->first(); |
106
|
|
|
if ($editableField) { |
107
|
|
|
$formData['Title'] = $editableField->Title; |
108
|
|
|
$formData['ParentClass'] = $editableField->Parent()->ClassName; |
109
|
|
|
} |
110
|
|
|
if (!$exists) { |
111
|
|
|
$exists = PartialFieldSubmission::create($formData); |
112
|
|
|
$exists->write(); |
113
|
|
|
} else { |
114
|
|
|
$exists->update($formData); |
115
|
|
|
$exists->write(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Return the ParentID to link the PartialSubmission to it's proper thingy |
119
|
|
|
return $editableField; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Partial form |
124
|
|
|
* |
125
|
|
|
* @param HTTPRequest $request |
126
|
|
|
* @return \SilverStripe\ORM\FieldType\DBHTMLText|void |
127
|
|
|
* @throws \SilverStripe\Control\HTTPResponse_Exception |
128
|
|
|
*/ |
129
|
|
|
public function partial(HTTPRequest $request) |
130
|
|
|
{ |
131
|
|
|
// Ensure this URL doesn't get picked up by HTTP caches |
132
|
|
|
HTTPCacheControlMiddleware::singleton()->disableCache(); |
133
|
|
|
|
134
|
|
|
$key = $request->param('Key'); |
135
|
|
|
$token = $request->param('Token'); |
136
|
|
|
|
137
|
|
|
$partial = PartialFormSubmission::get()->find('Token', $token); |
138
|
|
|
if (!$partial || !$partial->UserDefinedFormID) { |
139
|
|
|
return $this->httpError(404); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ($partial->generateKey($token) === $key) { |
143
|
|
|
|
144
|
|
|
// TODO: Recognize visitor with the password |
145
|
|
|
// TODO: Populate form values |
146
|
|
|
|
147
|
|
|
$record = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
148
|
|
|
$controller = new UserDefinedFormController($record); |
149
|
|
|
$controller->init(); |
|
|
|
|
150
|
|
|
|
151
|
|
|
Requirements::javascript('firesphere/partialuserforms:client/dist/main.js'); |
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
|
|
|
|