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