|
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
|
3 |
|
*/ |
|
46
|
|
|
public function partial(HTTPRequest $request) |
|
47
|
|
|
{ |
|
48
|
3 |
|
// Ensure this URL doesn't get picked up by HTTP caches |
|
49
|
|
|
HTTPCacheControlMiddleware::singleton()->disableCache(); |
|
50
|
3 |
|
|
|
51
|
3 |
|
$key = $request->param('Key'); |
|
52
|
3 |
|
$token = $request->param('Token'); |
|
53
|
1 |
|
if (!$key || !$token) { |
|
54
|
|
|
return $this->httpError(404); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
/** @var PartialFormSubmission $partial */ |
|
58
|
2 |
|
$partial = PartialFormSubmission::get()->find('Token', $token); |
|
59
|
|
|
if (!$partial || |
|
60
|
2 |
|
!$partial->UserDefinedFormID || |
|
61
|
|
|
!hash_equals($partial->generateKey($token), $key) |
|
62
|
2 |
|
) { |
|
63
|
|
|
return $this->httpError(404); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Set the session if the last session has expired or from another submission |
|
67
|
|
|
$session = $request->getSession()->get(PartialSubmissionController::SESSION_KEY); |
|
68
|
|
|
if (!$session || $session !== $partial->ID) { |
|
69
|
|
|
$request->getSession()->set(PartialSubmissionController::SESSION_KEY, $partial->ID); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Set data record and load the form |
|
73
|
|
|
$record = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
|
|
|
|
|
|
74
|
|
|
$controller = parent::create($record); |
|
|
|
|
|
|
75
|
|
|
$controller->doInit(); |
|
76
|
|
|
|
|
77
|
|
|
$form = $controller->Form(); |
|
78
|
|
|
$form->loadDataFrom($partial->getFields()); |
|
|
|
|
|
|
79
|
|
|
$this->populateFileData($form, $partial); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
// Copied from {@link UserDefinedFormController} |
|
82
|
|
|
if ($controller->Content && $form && !$controller->config()->disable_form_content_shortcode) { |
|
|
|
|
|
|
83
|
|
|
$hasLocation = stristr($controller->Content, '$UserDefinedForm'); |
|
|
|
|
|
|
84
|
|
|
if ($hasLocation) { |
|
85
|
|
|
/** @see Requirements_Backend::escapeReplacement */ |
|
86
|
|
|
$formEscapedForRegex = addcslashes($form->forTemplate(), '\\$'); |
|
|
|
|
|
|
87
|
|
|
$content = preg_replace( |
|
88
|
|
|
'/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', |
|
89
|
|
|
$formEscapedForRegex, |
|
90
|
|
|
$controller->Content |
|
|
|
|
|
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
return $controller->customise([ |
|
94
|
|
|
'Content' => DBField::create_field('HTMLText', $content), |
|
95
|
|
|
'Form' => '', |
|
96
|
|
|
'PartialLink' => $partial->getPartialLink() |
|
97
|
|
|
])->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
|
|
|
protected function populateFileData($form, $partialSubmission) |
|
115
|
|
|
{ |
|
116
|
|
|
$uploads = $partialSubmission->PartialUploads()->filter([ |
|
117
|
|
|
'UploadedFileID:not'=> null |
|
118
|
|
|
]); |
|
119
|
|
|
|
|
120
|
|
|
if (!$uploads->exists()) { |
|
121
|
|
|
return; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$fields = $form->Fields(); |
|
125
|
|
|
foreach ($uploads as $upload) { |
|
126
|
|
|
$fields->dataFieldByName($upload->Name) |
|
127
|
|
|
->setRightTitle( |
|
128
|
|
|
sprintf( |
|
129
|
|
|
'Uploaded: %s (Attach a new file to replace the uploaded file)', |
|
130
|
|
|
$upload->UploadedFile()->Name |
|
131
|
|
|
) |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|