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 \Firesphere\PartialUserforms\Controllers\PartialUserFormController |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class PartialUserFormController 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
|
|
|
'' => 'savePartialSubmission' |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private static $allowed_actions = [ |
|
|
|
|
35
|
|
|
'savePartialSubmission' |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param HTTPRequest $request |
40
|
|
|
* @return int |
41
|
|
|
* @throws ValidationException |
42
|
|
|
*/ |
43
|
|
|
public function savePartialSubmission(HTTPRequest $request) |
44
|
|
|
{ |
45
|
|
|
$postVars = $request->postVars(); |
46
|
|
|
$editableField = null; |
47
|
|
|
|
48
|
|
|
// We don't want SecurityID and/or the process Action stored as a thing |
49
|
|
|
unset($postVars['SecurityID'], $postVars['action_process']); |
50
|
|
|
$submissionID = $request->getSession()->get(self::SESSION_KEY); |
51
|
|
|
|
52
|
|
|
/** @var PartialFormSubmission $partialSubmission */ |
53
|
|
|
$partialSubmission = PartialFormSubmission::get()->byID($submissionID); |
54
|
|
|
|
55
|
|
|
if (!$submissionID || !$partialSubmission) { |
56
|
|
|
$partialSubmission = PartialFormSubmission::create(); |
57
|
|
|
$submissionID = $partialSubmission->write(); |
58
|
|
|
} |
59
|
|
|
$request->getSession()->set(self::SESSION_KEY, $submissionID); |
60
|
|
|
foreach ($postVars as $field => $value) { |
61
|
|
|
/** @var EditableFormField $editableField */ |
62
|
|
|
$editableField = $this->createOrUpdateSubmission([ |
63
|
|
|
'Name' => $field, |
64
|
|
|
'Value' => $value, |
65
|
|
|
'SubmittedFormID' => $submissionID |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($editableField instanceof EditableFormField && !$partialSubmission->UserDefinedFormID) { |
70
|
|
|
$partialSubmission->update([ |
71
|
|
|
'UserDefinedFormID' => $editableField->Parent()->ID, |
72
|
|
|
'ParentID' => $editableField->Parent()->ID, |
73
|
|
|
'ParentClass' => $editableField->Parent()->ClassName, |
74
|
|
|
'UserDefinedFormClass' => $editableField->Parent()->ClassName |
75
|
|
|
]); |
76
|
|
|
$partialSubmission->write(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $submissionID; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $formData |
84
|
|
|
* @return DataObject|EditableFormField |
85
|
|
|
* @throws ValidationException |
86
|
|
|
*/ |
87
|
|
|
protected function createOrUpdateSubmission($formData) |
88
|
|
|
{ |
89
|
|
|
if (is_array($formData['Value'])) { |
90
|
|
|
$formData['Value'] = implode(', ', $formData['Value']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$filter = [ |
94
|
|
|
'Name' => $formData['Name'], |
95
|
|
|
'SubmittedFormID' => $formData['SubmittedFormID'], |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$exists = PartialFieldSubmission::get()->filter($filter)->first(); |
99
|
|
|
// Set the title |
100
|
|
|
$editableField = EditableFormField::get()->filter(['Name' => $formData['Name']])->first(); |
101
|
|
|
if ($editableField) { |
102
|
|
|
$formData['Title'] = $editableField->Title; |
103
|
|
|
$formData['ParentClass'] = $editableField->Parent()->ClassName; |
104
|
|
|
} |
105
|
|
|
if (!$exists) { |
106
|
|
|
$exists = PartialFieldSubmission::create($formData); |
107
|
|
|
$exists->write(); |
108
|
|
|
} else { |
109
|
|
|
$exists->update($formData); |
110
|
|
|
$exists->write(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Return the ParentID to link the PartialSubmission to it's proper thingy |
114
|
|
|
return $editableField; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|