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