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