1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\PartialUserforms\Controllers; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Firesphere\PartialUserforms\Models\PartialFormSubmission; |
7
|
|
|
use Page; |
8
|
|
|
use SilverStripe\Control\HTTPRequest; |
9
|
|
|
use SilverStripe\Control\HTTPResponse_Exception; |
10
|
|
|
use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; |
11
|
|
|
use SilverStripe\Forms\Form; |
12
|
|
|
use SilverStripe\Forms\HiddenField; |
13
|
|
|
use SilverStripe\ORM\DataObject; |
14
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
15
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
16
|
|
|
use SilverStripe\UserForms\Control\UserDefinedFormController; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class PartialUserFormController |
20
|
|
|
* |
21
|
|
|
* @package Firesphere\PartialUserforms\Controllers |
22
|
|
|
*/ |
23
|
|
|
class PartialUserFormController extends UserDefinedFormController |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $url_handlers = [ |
|
|
|
|
29
|
|
|
'$Key/$Token' => 'partial', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private static $allowed_actions = [ |
|
|
|
|
36
|
|
|
'partial', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Partial form |
41
|
|
|
* |
42
|
|
|
* @param HTTPRequest $request |
43
|
|
|
* @return DBHTMLText|void |
44
|
|
|
* @throws HTTPResponse_Exception |
45
|
|
|
* @throws Exception |
46
|
|
|
*/ |
47
|
5 |
|
public function partial(HTTPRequest $request) |
48
|
|
|
{ |
49
|
|
|
// Ensure this URL doesn't get picked up by HTTP caches |
50
|
5 |
|
HTTPCacheControlMiddleware::singleton()->disableCache(); |
51
|
|
|
|
52
|
5 |
|
$key = $request->param('Key'); |
53
|
5 |
|
$token = $request->param('Token'); |
54
|
5 |
|
if (!$key || !$token) { |
55
|
1 |
|
return $this->httpError(404); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
$partial = PartialFormSubmission::validateKeyToken($key, $token); |
59
|
4 |
|
if ($partial === false) { |
60
|
3 |
|
return $this->httpError(404); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Set data record and load the form |
64
|
2 |
|
$record = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
|
|
|
|
65
|
2 |
|
$controller = parent::create($record); |
|
|
|
|
66
|
2 |
|
$controller->doInit(); |
67
|
|
|
|
68
|
|
|
// Set the session if the last session has expired or another submission has started |
69
|
2 |
|
$sessionID = $request->getSession()->get(PartialSubmissionController::SESSION_KEY); |
70
|
2 |
|
if (!$sessionID || $sessionID !== $partial->ID) { |
71
|
2 |
|
$request->getSession()->set(PartialSubmissionController::SESSION_KEY, $partial->ID); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$form = $controller->Form(); |
75
|
2 |
|
$form->loadDataFrom($partial->getFields()); |
|
|
|
|
76
|
2 |
|
$this->populateData($form, $partial); |
|
|
|
|
77
|
|
|
|
78
|
|
|
// Copied from {@link UserDefinedFormController} |
79
|
2 |
|
if ($controller->Content && $form && !$controller->config()->disable_form_content_shortcode) { |
|
|
|
|
80
|
2 |
|
$hasLocation = stristr($controller->Content, '$UserDefinedForm'); |
|
|
|
|
81
|
2 |
|
if ($hasLocation) { |
82
|
|
|
/** @see Requirements_Backend::escapeReplacement */ |
83
|
2 |
|
$formEscapedForRegex = addcslashes($form->forTemplate(), '\\$'); |
|
|
|
|
84
|
2 |
|
$content = preg_replace( |
85
|
2 |
|
'/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', |
86
|
2 |
|
$formEscapedForRegex, |
87
|
2 |
|
$controller->Content |
|
|
|
|
88
|
|
|
); |
89
|
|
|
|
90
|
2 |
|
return $controller->customise([ |
91
|
2 |
|
'Content' => DBField::create_field('HTMLText', $content), |
92
|
2 |
|
'Form' => '', |
93
|
2 |
|
'PartialLink' => $partial->getPartialLink() |
94
|
2 |
|
])->renderWith([static::class, Page::class]); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $controller->customise([ |
99
|
|
|
'Content' => DBField::create_field('HTMLText', $controller->Content), |
|
|
|
|
100
|
|
|
'Form' => $form, |
101
|
|
|
'PartialLink' => $partial->getPartialLink() |
102
|
|
|
])->renderWith([static::class, Page::class]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Add partial submission and set the uploaded filenames as right title of the file fields |
107
|
|
|
* |
108
|
|
|
* @param Form $form |
109
|
|
|
* @param PartialFormSubmission $partial |
110
|
|
|
*/ |
111
|
2 |
|
protected function populateData($form, $partial) |
112
|
|
|
{ |
113
|
2 |
|
$fields = $form->Fields(); |
114
|
|
|
// Add partial submission ID |
115
|
2 |
|
$fields->push( |
116
|
2 |
|
HiddenField::create( |
117
|
2 |
|
'PartialID', |
118
|
2 |
|
null, |
119
|
2 |
|
$partial->ID |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
// Populate files |
124
|
2 |
|
$uploads = $partial->PartialUploads()->filter([ |
125
|
2 |
|
'UploadedFileID:not'=> null |
126
|
|
|
]); |
127
|
|
|
|
128
|
2 |
|
if (!$uploads->exists()) { |
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
|
132
|
2 |
|
foreach ($uploads as $upload) { |
133
|
2 |
|
$fields->dataFieldByName($upload->Name) |
134
|
2 |
|
->setRightTitle( |
135
|
2 |
|
sprintf( |
136
|
2 |
|
'Uploaded: %s (Attach a new file to replace the uploaded file)', |
137
|
2 |
|
$upload->UploadedFile()->Name |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
} |
141
|
2 |
|
} |
142
|
|
|
} |
143
|
|
|
|