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