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\ORM\DataObject; |
12
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
13
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
14
|
|
|
use SilverStripe\UserForms\Control\UserDefinedFormController; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class PartialUserFormController |
18
|
|
|
* |
19
|
|
|
* @package Firesphere\PartialUserforms\Controllers |
20
|
|
|
*/ |
21
|
|
|
class PartialUserFormController extends UserDefinedFormController |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private static $url_handlers = [ |
|
|
|
|
27
|
|
|
'$Key/$Token' => 'partial', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private static $allowed_actions = [ |
|
|
|
|
34
|
|
|
'partial', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Partial form |
39
|
|
|
* |
40
|
|
|
* @param HTTPRequest $request |
41
|
|
|
* @return DBHTMLText|void |
42
|
|
|
* @throws HTTPResponse_Exception |
43
|
|
|
* @throws Exception |
44
|
|
|
*/ |
45
|
3 |
|
public function partial(HTTPRequest $request) |
46
|
|
|
{ |
47
|
|
|
// Ensure this URL doesn't get picked up by HTTP caches |
48
|
3 |
|
HTTPCacheControlMiddleware::singleton()->disableCache(); |
49
|
|
|
|
50
|
3 |
|
$key = $request->param('Key'); |
51
|
3 |
|
$token = $request->param('Token'); |
52
|
3 |
|
if (!$key || !$token) { |
53
|
1 |
|
return $this->httpError(404); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @var PartialFormSubmission $partial */ |
57
|
2 |
|
$partial = PartialFormSubmission::get()->find('Token', $token); |
58
|
2 |
|
if (!$partial || |
59
|
|
|
!$partial->UserDefinedFormID || |
60
|
2 |
|
!hash_equals($partial->generateKey($token), $key) |
61
|
|
|
) { |
62
|
2 |
|
return $this->httpError(404); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Set the session if the last session has expired |
66
|
|
|
if (!$request->getSession()->get(PartialSubmissionController::SESSION_KEY)) { |
67
|
|
|
$request->getSession()->set(PartialSubmissionController::SESSION_KEY, $partial->ID); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// TODO: Recognize visitor with the password |
71
|
|
|
// Set data record and load the form |
72
|
|
|
$this->dataRecord = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
|
|
|
|
73
|
|
|
$this->setFailover($this->dataRecord); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$form = $this->Form(); |
76
|
|
|
$fields = $partial->PartialFields()->map('Name', 'Value')->toArray(); |
77
|
|
|
$form->loadDataFrom($fields); |
|
|
|
|
78
|
|
|
|
79
|
|
|
// Copied from {@link UserDefinedFormController} |
80
|
|
|
if ($this->Content && $form && !$this->config()->disable_form_content_shortcode) { |
|
|
|
|
81
|
|
|
$hasLocation = stristr($this->Content, '$UserDefinedForm'); |
|
|
|
|
82
|
|
|
if ($hasLocation) { |
83
|
|
|
/** @see Requirements_Backend::escapeReplacement */ |
84
|
|
|
$formEscapedForRegex = addcslashes($form->forTemplate(), '\\$'); |
|
|
|
|
85
|
|
|
$content = preg_replace( |
86
|
|
|
'/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', |
87
|
|
|
$formEscapedForRegex, |
88
|
|
|
$this->Content |
|
|
|
|
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return $this->customise([ |
92
|
|
|
'Content' => DBField::create_field('HTMLText', $content), |
93
|
|
|
'Form' => '', |
94
|
|
|
'PartialLink' => $partial->getPartialLink() |
95
|
|
|
])->renderWith([static::class, Page::class]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->customise([ |
100
|
|
|
'Content' => DBField::create_field('HTMLText', $this->Content), |
|
|
|
|
101
|
|
|
'Form' => $form, |
102
|
|
|
'PartialLink' => $partial->getPartialLink() |
103
|
|
|
])->renderWith([static::class, Page::class]); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|