1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\PartialUserforms\Controllers; |
4
|
|
|
|
5
|
|
|
use Firesphere\PartialUserforms\Models\PartialFieldSubmission; |
6
|
|
|
use Firesphere\PartialUserforms\Models\PartialFileFieldSubmission; |
7
|
|
|
use Firesphere\PartialUserforms\Models\PartialFormSubmission; |
8
|
|
|
use SilverStripe\Assets\File; |
9
|
|
|
use SilverStripe\Assets\Upload; |
10
|
|
|
use SilverStripe\CMS\Controllers\ContentController; |
11
|
|
|
use SilverStripe\Control\Controller; |
12
|
|
|
use SilverStripe\Control\HTTPRequest; |
13
|
|
|
use SilverStripe\ORM\DataObject; |
14
|
|
|
use SilverStripe\ORM\ValidationException; |
15
|
|
|
use SilverStripe\ORM\ValidationResult; |
16
|
|
|
use SilverStripe\UserForms\Model\EditableFormField; |
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
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private static $allowed_actions = [ |
|
|
|
|
40
|
|
|
'savePartialSubmission' |
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
|
|
|
$filter = [ |
95
|
|
|
'Name' => $formData['Name'], |
96
|
|
|
'SubmittedFormID' => $formData['SubmittedFormID'], |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
// Set the title |
100
|
|
|
/** @var EditableFormField $editableField */ |
101
|
|
|
$editableField = EditableFormField::get()->filter(['Name' => $formData['Name']])->first(); |
102
|
|
|
if ($editableField instanceof EditableFormField\EditableFileField) { |
103
|
|
|
$this->savePartialFile($formData, $filter, $editableField); |
104
|
|
|
} elseif ($editableField instanceof EditableFormField) { |
105
|
|
|
$this->savePartialField($formData, $filter, $editableField); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Return the ParentID to link the PartialSubmission to it's proper thingy |
109
|
|
|
return $editableField; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $formData |
114
|
|
|
* @param array $filter |
115
|
|
|
* @param EditableFormField\EditableFileField $editableField |
116
|
|
|
* @throws ValidationException |
117
|
|
|
* @throws \Exception |
118
|
|
|
*/ |
119
|
|
|
protected function savePartialFile($formData, array $filter, EditableFormField\EditableFileField $editableField) |
120
|
|
|
{ |
121
|
|
|
$partialFileSubmission = PartialFileFieldSubmission::get()->filter($filter)->first(); |
122
|
|
|
$partialData = []; |
123
|
|
|
// Don't overwrite existing uploads |
124
|
|
|
if (!$partialFileSubmission) { |
125
|
|
|
if ($editableField) { |
126
|
|
|
$partialData['Title'] = $editableField->Title; |
127
|
|
|
$partialData['ParentClass'] = $editableField->Parent()->ClassName; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
if (is_array($formData['Value']) && !$partialFileSubmission->UploadedFileID) { |
131
|
|
|
$file = $this->uploadFile($partialData, $editableField); |
132
|
|
|
$formData['UploadedFileID'] = $file->ID ?? 0; |
133
|
|
|
$partialFileSubmission = PartialFileFieldSubmission::create($partialData); |
134
|
|
|
} |
135
|
|
|
$partialFileSubmission->write(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $formData |
140
|
|
|
* @param array $filter |
141
|
|
|
* @param EditableFormField $editableField |
142
|
|
|
* @throws ValidationException |
143
|
|
|
*/ |
144
|
|
|
protected function savePartialField($formData, array $filter, EditableFormField $editableField) |
145
|
|
|
{ |
146
|
|
|
$exists = PartialFieldSubmission::get()->filter($filter)->first(); |
147
|
|
|
if (is_array($formData['Value'])) { |
148
|
|
|
$formData['Value'] = implode(', ', $formData['Value']); |
149
|
|
|
} |
150
|
|
|
if ($editableField) { |
151
|
|
|
$formData['Title'] = $editableField->Title; |
152
|
|
|
$formData['ParentClass'] = $editableField->Parent()->ClassName; |
153
|
|
|
} |
154
|
|
|
if (!$exists) { |
155
|
|
|
$exists = PartialFieldSubmission::create($formData); |
156
|
|
|
} else { |
157
|
|
|
$exists->update($formData); |
158
|
|
|
} |
159
|
|
|
$exists->write(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param array $formData |
164
|
|
|
* @param EditableFormField\EditableFileField $field |
165
|
|
|
* @return File|void |
166
|
|
|
* @throws \Exception |
167
|
|
|
*/ |
168
|
|
|
protected function uploadFile($formData, $field) |
169
|
|
|
{ |
170
|
|
|
if (!empty($formData['Value']['name'])) { |
171
|
|
|
$foldername = $field->getFormField()->getFolderName(); |
172
|
|
|
|
173
|
|
|
// create the file from post data |
174
|
|
|
$upload = Upload::create(); |
175
|
|
|
$file = File::create(); |
176
|
|
|
$file->ShowInSearch = 0; |
177
|
|
|
try { |
178
|
|
|
$upload->loadIntoFile($formData['Value'], $file, $foldername); |
179
|
|
|
} catch (ValidationException $e) { |
180
|
|
|
$validationResult = $e->getResult(); |
181
|
|
|
foreach ($validationResult->getMessages() as $message) { |
182
|
|
|
$field->Parent()->sessionMessage($message['message'], ValidationResult::TYPE_ERROR); |
183
|
|
|
} |
184
|
|
|
Controller::curr()->redirectBack(); |
185
|
|
|
return; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $file; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|