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\Model\EditableFormField; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PartialSubmissionController |
15
|
|
|
* @package Firesphere\PartialUserforms\Controllers |
16
|
|
|
*/ |
17
|
|
|
class PartialSubmissionController extends ContentController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Session key name |
21
|
|
|
*/ |
22
|
|
|
const SESSION_KEY = 'PartialSubmissionID'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private static $url_handlers = [ |
|
|
|
|
28
|
|
|
'save' => 'savePartialSubmission', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private static $allowed_actions = [ |
|
|
|
|
35
|
|
|
'savePartialSubmission', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param HTTPRequest $request |
40
|
|
|
* @return bool|void |
41
|
|
|
* @throws ValidationException |
42
|
|
|
* @throws \SilverStripe\Control\HTTPResponse_Exception |
43
|
|
|
*/ |
44
|
|
|
public function savePartialSubmission(HTTPRequest $request) |
45
|
|
|
{ |
46
|
|
|
if (!$request->isPOST()) { |
47
|
|
|
return $this->httpError(404); |
48
|
|
|
} |
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
|
|
|
// TODO: Set the Parent ID and Parent Class before write, this issue will create new submissions |
63
|
|
|
// every time the session expires when the user proceeds to the next step. |
64
|
|
|
// Also, saving a new submission without a parent creates an "AccordionItems" as parent class (first DataObject found) |
65
|
|
|
$submissionID = $partialSubmission->write(); |
66
|
|
|
} |
67
|
|
|
$request->getSession()->set(self::SESSION_KEY, $submissionID); |
68
|
|
|
foreach ($postVars as $field => $value) { |
69
|
|
|
/** @var EditableFormField $editableField */ |
70
|
|
|
$editableField = $this->createOrUpdateSubmission([ |
71
|
|
|
'Name' => $field, |
72
|
|
|
'Value' => $value, |
73
|
|
|
'SubmittedFormID' => $submissionID |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($editableField instanceof EditableFormField && !$partialSubmission->UserDefinedFormID) { |
78
|
|
|
// Updates parent class to the correct DataObject |
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 $partialSubmission->exists(); |
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
|
|
|
|