@@ -7,221 +7,221 @@ |
||
7 | 7 | |
8 | 8 | class FormComponent Extends BaseComponent |
9 | 9 | { |
10 | - /** |
|
11 | - * @var null|string |
|
12 | - */ |
|
13 | - protected $documentType = null; |
|
14 | - /** |
|
15 | - * @var null|string |
|
16 | - */ |
|
17 | - protected $responseFolder = null; |
|
18 | - /** |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - protected $subTemplate = 'cms/documents/document-form-form'; |
|
22 | - /** |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - protected $formParameterName = 'form'; |
|
26 | - /** |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $thankYouMessage = 'Thank you for sending us your response.'; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - private $formId; |
|
35 | - /** |
|
36 | - * @var null|string |
|
37 | - */ |
|
38 | - private $getPathBackup = null; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var null|\stdClass |
|
42 | - */ |
|
43 | - private $userSessionBackup = null; |
|
44 | - |
|
45 | - /** |
|
46 | - * @param Storage $storage |
|
47 | - * @return void |
|
48 | - * @throws \Exception |
|
49 | - */ |
|
50 | - public function run(Storage $storage) |
|
51 | - { |
|
52 | - parent::run($storage); |
|
53 | - |
|
54 | - $this->checkParameters(); |
|
55 | - |
|
56 | - if ($this->documentType === null || $this->responseFolder === null) { |
|
57 | - throw new \Exception('Parameters `documentType` and `responseFolder` are required for usage with this form'); |
|
58 | - } |
|
59 | - |
|
60 | - $this->setFormId(); |
|
61 | - $this->initialize($storage); |
|
62 | - $this->checkSubmit($storage); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @param null|Application $application |
|
67 | - * @throws \Exception |
|
68 | - */ |
|
69 | - public function render($application = null) |
|
70 | - { |
|
71 | - $request = $this->request; |
|
72 | - if (isset($request::$get['path'])) { |
|
73 | - $this->getPathBackup = $request::$get['path']; |
|
74 | - } |
|
75 | - $request::$get['path'] = $this->responseFolder; |
|
76 | - $form = $this->renderTemplate($this->subTemplate); |
|
77 | - if ($this->getPathBackup !== null) { |
|
78 | - $request::$get['path'] = $this->getPathBackup; |
|
79 | - } else { |
|
80 | - unset($request::$get['path']); |
|
81 | - } |
|
82 | - if ($this->isFormSubmitted($this->request)) { |
|
83 | - $this->parameters[$this->formParameterName] = '<a name="' . $this->formId . '"></a>' . $this->thankYouMessage; |
|
84 | - } else { |
|
85 | - $this->parameters[$this->formParameterName] = $form; |
|
86 | - } |
|
87 | - |
|
88 | - parent::render($application); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Checks if parameters were given in the CMS configuration and |
|
93 | - * sets them to their respective fields |
|
94 | - */ |
|
95 | - private function checkParameters() |
|
96 | - { |
|
97 | - if (isset($this->parameters['documentType'])) { |
|
98 | - $this->documentType = $this->parameters['documentType']; |
|
99 | - unset($this->parameters['documentType']); |
|
100 | - } |
|
101 | - |
|
102 | - if (isset($this->parameters['responseFolder'])) { |
|
103 | - $this->responseFolder = $this->parameters['responseFolder']; |
|
104 | - unset($this->parameters['responseFolder']); |
|
105 | - } |
|
106 | - |
|
107 | - if (isset($this->parameters['subTemplate'])) { |
|
108 | - $this->subTemplate = $this->parameters['subTemplate']; |
|
109 | - unset($this->parameters['subTemplate']); |
|
110 | - } |
|
111 | - |
|
112 | - if (isset($this->parameters['formParameterName'])) { |
|
113 | - $this->formParameterName = $this->parameters['formParameterName']; |
|
114 | - unset($this->parameters['formParameterName']); |
|
115 | - } |
|
116 | - |
|
117 | - if (isset($this->parameters['thankYouMessage'])) { |
|
118 | - $this->thankYouMessage = $this->parameters['thankYouMessage']; |
|
119 | - unset($this->parameters['thankYouMessage']); |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Sets variables needed for rendering the form template |
|
125 | - * @param $storage |
|
126 | - */ |
|
127 | - private function initialize($storage) |
|
128 | - { |
|
129 | - $this->parameters['smallestImage'] = $storage->getSmallestImageSet()->slug; |
|
130 | - $this->parameters['cmsPrefix'] = ''; |
|
131 | - |
|
132 | - $this->parameters['documentType'] = $this->storage->getDocumentTypeBySlug($this->documentType, true); |
|
133 | - $this->parameters['documentTypes'] = $this->storage->getDocumentTypes(); |
|
134 | - $this->parameters['hideTitleAndState'] = true; |
|
135 | - $this->parameters['formId'] = $this->formId; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * If the form has been submitted, save the document |
|
140 | - * Calls $this->postSubmit() afterwards |
|
141 | - * |
|
142 | - * @param Storage $storage |
|
143 | - */ |
|
144 | - private function checkSubmit($storage) |
|
145 | - { |
|
146 | - if ($this->isFormSubmitted($this->request)) { |
|
147 | - $postValues = $this->getPostValues($this->request); |
|
148 | - $this->setUserSessionBackup(); |
|
149 | - $storage->addDocument($postValues); |
|
150 | - $this->restoreUserSessionBackup(); |
|
151 | - $this->postSubmit($postValues, $storage) |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Hook for derived classes to take actions after |
|
157 | - * submitting the form |
|
158 | - * |
|
159 | - * @param $postValues |
|
160 | - * @param $storage |
|
161 | - */ |
|
162 | - protected function postSubmit($postValues, $storage) |
|
163 | - {} |
|
164 | - |
|
165 | - /** |
|
166 | - * Sets a unique id for this particular form, so it can recognize |
|
167 | - * it when a submit occurs |
|
168 | - */ |
|
169 | - private function setFormId() |
|
170 | - { |
|
171 | - if (isset($_SESSION['FormComponent'][$this->formParameterName])) { |
|
172 | - $this->formId = $_SESSION['FormComponent'][$this->formParameterName]; |
|
173 | - } else { |
|
174 | - $_SESSION['FormComponent'][$this->formParameterName] = (string) microtime(true); |
|
175 | - $this->formId = $_SESSION['FormComponent'][$this->formParameterName]; |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Checks if this form has been submitted |
|
181 | - * |
|
182 | - * @param $request |
|
183 | - * @return bool |
|
184 | - */ |
|
185 | - private function isFormSubmitted($request) |
|
186 | - { |
|
187 | - return !empty($request::$post) && isset($request::$post['formId']) && $request::$post['formId'] === $this->formId && isset($_SESSION['FormComponent'][$this->formParameterName]) && $_SESSION['FormComponent'][$this->formParameterName] === $this->formId; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * |
|
192 | - * |
|
193 | - * @param $request |
|
194 | - */ |
|
195 | - private function getPostValues($request) |
|
196 | - { |
|
197 | - $postValues = $request::$post; |
|
198 | - $postValues['documentType'] = $this->documentType; |
|
199 | - $postValues['path'] = $this->responseFolder; |
|
200 | - $postValues['title'] = date('r') . ' - From: ' . $request::$requestUri; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Temporarily stores the current user session in a backup variable |
|
205 | - * and sets a fake user instead |
|
206 | - */ |
|
207 | - private function setUserSessionBackup() |
|
208 | - { |
|
209 | - $this->userSessionBackup = isset($_SESSION['cloudcontrol']) ? $_SESSION['cloudcontrol'] : null; |
|
210 | - $fakeUser = new \stdClass(); |
|
211 | - $fakeUser->username = 'FormComponent'; |
|
212 | - $_SESSION['cloudcontrol'] = $fakeUser; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Removes the fake user and restores the existing user |
|
217 | - * session if it was there |
|
218 | - */ |
|
219 | - private function restoreUserSessionBackup() |
|
220 | - { |
|
221 | - if ($this->userSessionBackup === null) { |
|
222 | - unset($_SESSION['cloudcontrol']); |
|
223 | - } else { |
|
224 | - $_SESSION['cloudcontrol'] = $this->userSessionBackup; |
|
225 | - } |
|
226 | - } |
|
10 | + /** |
|
11 | + * @var null|string |
|
12 | + */ |
|
13 | + protected $documentType = null; |
|
14 | + /** |
|
15 | + * @var null|string |
|
16 | + */ |
|
17 | + protected $responseFolder = null; |
|
18 | + /** |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + protected $subTemplate = 'cms/documents/document-form-form'; |
|
22 | + /** |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + protected $formParameterName = 'form'; |
|
26 | + /** |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $thankYouMessage = 'Thank you for sending us your response.'; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + private $formId; |
|
35 | + /** |
|
36 | + * @var null|string |
|
37 | + */ |
|
38 | + private $getPathBackup = null; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var null|\stdClass |
|
42 | + */ |
|
43 | + private $userSessionBackup = null; |
|
44 | + |
|
45 | + /** |
|
46 | + * @param Storage $storage |
|
47 | + * @return void |
|
48 | + * @throws \Exception |
|
49 | + */ |
|
50 | + public function run(Storage $storage) |
|
51 | + { |
|
52 | + parent::run($storage); |
|
53 | + |
|
54 | + $this->checkParameters(); |
|
55 | + |
|
56 | + if ($this->documentType === null || $this->responseFolder === null) { |
|
57 | + throw new \Exception('Parameters `documentType` and `responseFolder` are required for usage with this form'); |
|
58 | + } |
|
59 | + |
|
60 | + $this->setFormId(); |
|
61 | + $this->initialize($storage); |
|
62 | + $this->checkSubmit($storage); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @param null|Application $application |
|
67 | + * @throws \Exception |
|
68 | + */ |
|
69 | + public function render($application = null) |
|
70 | + { |
|
71 | + $request = $this->request; |
|
72 | + if (isset($request::$get['path'])) { |
|
73 | + $this->getPathBackup = $request::$get['path']; |
|
74 | + } |
|
75 | + $request::$get['path'] = $this->responseFolder; |
|
76 | + $form = $this->renderTemplate($this->subTemplate); |
|
77 | + if ($this->getPathBackup !== null) { |
|
78 | + $request::$get['path'] = $this->getPathBackup; |
|
79 | + } else { |
|
80 | + unset($request::$get['path']); |
|
81 | + } |
|
82 | + if ($this->isFormSubmitted($this->request)) { |
|
83 | + $this->parameters[$this->formParameterName] = '<a name="' . $this->formId . '"></a>' . $this->thankYouMessage; |
|
84 | + } else { |
|
85 | + $this->parameters[$this->formParameterName] = $form; |
|
86 | + } |
|
87 | + |
|
88 | + parent::render($application); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Checks if parameters were given in the CMS configuration and |
|
93 | + * sets them to their respective fields |
|
94 | + */ |
|
95 | + private function checkParameters() |
|
96 | + { |
|
97 | + if (isset($this->parameters['documentType'])) { |
|
98 | + $this->documentType = $this->parameters['documentType']; |
|
99 | + unset($this->parameters['documentType']); |
|
100 | + } |
|
101 | + |
|
102 | + if (isset($this->parameters['responseFolder'])) { |
|
103 | + $this->responseFolder = $this->parameters['responseFolder']; |
|
104 | + unset($this->parameters['responseFolder']); |
|
105 | + } |
|
106 | + |
|
107 | + if (isset($this->parameters['subTemplate'])) { |
|
108 | + $this->subTemplate = $this->parameters['subTemplate']; |
|
109 | + unset($this->parameters['subTemplate']); |
|
110 | + } |
|
111 | + |
|
112 | + if (isset($this->parameters['formParameterName'])) { |
|
113 | + $this->formParameterName = $this->parameters['formParameterName']; |
|
114 | + unset($this->parameters['formParameterName']); |
|
115 | + } |
|
116 | + |
|
117 | + if (isset($this->parameters['thankYouMessage'])) { |
|
118 | + $this->thankYouMessage = $this->parameters['thankYouMessage']; |
|
119 | + unset($this->parameters['thankYouMessage']); |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Sets variables needed for rendering the form template |
|
125 | + * @param $storage |
|
126 | + */ |
|
127 | + private function initialize($storage) |
|
128 | + { |
|
129 | + $this->parameters['smallestImage'] = $storage->getSmallestImageSet()->slug; |
|
130 | + $this->parameters['cmsPrefix'] = ''; |
|
131 | + |
|
132 | + $this->parameters['documentType'] = $this->storage->getDocumentTypeBySlug($this->documentType, true); |
|
133 | + $this->parameters['documentTypes'] = $this->storage->getDocumentTypes(); |
|
134 | + $this->parameters['hideTitleAndState'] = true; |
|
135 | + $this->parameters['formId'] = $this->formId; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * If the form has been submitted, save the document |
|
140 | + * Calls $this->postSubmit() afterwards |
|
141 | + * |
|
142 | + * @param Storage $storage |
|
143 | + */ |
|
144 | + private function checkSubmit($storage) |
|
145 | + { |
|
146 | + if ($this->isFormSubmitted($this->request)) { |
|
147 | + $postValues = $this->getPostValues($this->request); |
|
148 | + $this->setUserSessionBackup(); |
|
149 | + $storage->addDocument($postValues); |
|
150 | + $this->restoreUserSessionBackup(); |
|
151 | + $this->postSubmit($postValues, $storage) |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Hook for derived classes to take actions after |
|
157 | + * submitting the form |
|
158 | + * |
|
159 | + * @param $postValues |
|
160 | + * @param $storage |
|
161 | + */ |
|
162 | + protected function postSubmit($postValues, $storage) |
|
163 | + {} |
|
164 | + |
|
165 | + /** |
|
166 | + * Sets a unique id for this particular form, so it can recognize |
|
167 | + * it when a submit occurs |
|
168 | + */ |
|
169 | + private function setFormId() |
|
170 | + { |
|
171 | + if (isset($_SESSION['FormComponent'][$this->formParameterName])) { |
|
172 | + $this->formId = $_SESSION['FormComponent'][$this->formParameterName]; |
|
173 | + } else { |
|
174 | + $_SESSION['FormComponent'][$this->formParameterName] = (string) microtime(true); |
|
175 | + $this->formId = $_SESSION['FormComponent'][$this->formParameterName]; |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Checks if this form has been submitted |
|
181 | + * |
|
182 | + * @param $request |
|
183 | + * @return bool |
|
184 | + */ |
|
185 | + private function isFormSubmitted($request) |
|
186 | + { |
|
187 | + return !empty($request::$post) && isset($request::$post['formId']) && $request::$post['formId'] === $this->formId && isset($_SESSION['FormComponent'][$this->formParameterName]) && $_SESSION['FormComponent'][$this->formParameterName] === $this->formId; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * |
|
192 | + * |
|
193 | + * @param $request |
|
194 | + */ |
|
195 | + private function getPostValues($request) |
|
196 | + { |
|
197 | + $postValues = $request::$post; |
|
198 | + $postValues['documentType'] = $this->documentType; |
|
199 | + $postValues['path'] = $this->responseFolder; |
|
200 | + $postValues['title'] = date('r') . ' - From: ' . $request::$requestUri; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Temporarily stores the current user session in a backup variable |
|
205 | + * and sets a fake user instead |
|
206 | + */ |
|
207 | + private function setUserSessionBackup() |
|
208 | + { |
|
209 | + $this->userSessionBackup = isset($_SESSION['cloudcontrol']) ? $_SESSION['cloudcontrol'] : null; |
|
210 | + $fakeUser = new \stdClass(); |
|
211 | + $fakeUser->username = 'FormComponent'; |
|
212 | + $_SESSION['cloudcontrol'] = $fakeUser; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Removes the fake user and restores the existing user |
|
217 | + * session if it was there |
|
218 | + */ |
|
219 | + private function restoreUserSessionBackup() |
|
220 | + { |
|
221 | + if ($this->userSessionBackup === null) { |
|
222 | + unset($_SESSION['cloudcontrol']); |
|
223 | + } else { |
|
224 | + $_SESSION['cloudcontrol'] = $this->userSessionBackup; |
|
225 | + } |
|
226 | + } |
|
227 | 227 | } |
228 | 228 | \ No newline at end of file |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | unset($request::$get['path']); |
81 | 81 | } |
82 | 82 | if ($this->isFormSubmitted($this->request)) { |
83 | - $this->parameters[$this->formParameterName] = '<a name="' . $this->formId . '"></a>' . $this->thankYouMessage; |
|
83 | + $this->parameters[$this->formParameterName] = '<a name="'.$this->formId.'"></a>'.$this->thankYouMessage; |
|
84 | 84 | } else { |
85 | 85 | $this->parameters[$this->formParameterName] = $form; |
86 | 86 | } |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | $postValues = $request::$post; |
198 | 198 | $postValues['documentType'] = $this->documentType; |
199 | 199 | $postValues['path'] = $this->responseFolder; |
200 | - $postValues['title'] = date('r') . ' - From: ' . $request::$requestUri; |
|
200 | + $postValues['title'] = date('r').' - From: '.$request::$requestUri; |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | /** |