Passed
Push — master ( fa28f5...070d6c )
by Jens
02:40
created

FormComponent::setSubmitToSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 2
c 1
b 1
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
namespace library\components;
3
4
5
use library\cc\Application;
6
use library\storage\Storage;
7
8
class FormComponent Extends BaseComponent
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 bool
33
     */
34
    protected $submitOncePerSession = false;
35
36
    /**
37
     * @var string
38
     */
39
    private $formId;
40
    /**
41
     * @var null|string
42
     */
43
    private $getPathBackup = null;
44
45
    /**
46
     * @var null|\stdClass
47
     */
48
    private $userSessionBackup = null;
49
50
    /**
51
     * @param Storage $storage
52
     * @return void
53
     * @throws \Exception
54
     */
55
    public function run(Storage $storage)
56
    {
57
        parent::run($storage);
58
59
        $this->checkParameters();
60
61
        if ($this->documentType === null || $this->responseFolder === null) {
62
            throw new \Exception('Parameters `documentType` and `responseFolder` are required for usage with this form');
63
        }
64
65
        $this->setFormId();
66
        $this->initialize($storage);
67
        $this->checkSubmit($storage);
68
    }
69
70
    /**
71
     * @param null|Application $application
72
     * @throws \Exception
73
     */
74
    public function render($application = null)
75
    {
76
        $request = $this->request;
77
        if (isset($request::$get['path'])) {
78
            $this->getPathBackup = $request::$get['path'];
79
        }
80
        $request::$get['path'] = $this->responseFolder;
81
        $form = $this->renderTemplate($this->subTemplate);
82
        if ($this->getPathBackup !== null) {
83
            $request::$get['path'] = $this->getPathBackup;
84
        } else {
85
            unset($request::$get['path']);
86
        }
87
        if ($this->isFormSubmitted($this->request) || $this->isSubmitAllowed() === false) {
88
            $this->parameters[$this->formParameterName] = '<a name="' . $this->formId . '"></a>' . $this->thankYouMessage;
89
        } else {
90
            $this->parameters[$this->formParameterName] = $form;
91
        }
92
93
        parent::render($application);
94
    }
95
96
    /**
97
     * Checks if parameters were given in the CMS configuration and
98
     * sets them to their respective fields
99
     */
100
    private function checkParameters()
101
    {
102
        if (isset($this->parameters['documentType'])) {
103
            $this->documentType = $this->parameters['documentType'];
104
            unset($this->parameters['documentType']);
105
        }
106
107
        if (isset($this->parameters['responseFolder'])) {
108
            $this->responseFolder = $this->parameters['responseFolder'];
109
            unset($this->parameters['responseFolder']);
110
        }
111
112
        if (isset($this->parameters['subTemplate'])) {
113
            $this->subTemplate = $this->parameters['subTemplate'];
114
            unset($this->parameters['subTemplate']);
115
        }
116
117
        if (isset($this->parameters['formParameterName'])) {
118
            $this->formParameterName = $this->parameters['formParameterName'];
119
            unset($this->parameters['formParameterName']);
120
        }
121
122
        if (isset($this->parameters['thankYouMessage'])) {
123
            $this->thankYouMessage = $this->parameters['thankYouMessage'];
124
            unset($this->parameters['thankYouMessage']);
125
        }
126
127
        if (isset($this->parameters['submitOncePerSession'])) {
128
            $this->submitOncePerSession = $this->parameters['submitOncePerSession'] === 'true' ? true : false;
129
            unset($this->parameters['submitOncePerSession']);
130
        }
131
    }
132
133
    /**
134
     * Sets variables needed for rendering the form template
135
     * @param $storage
136
     */
137
    private function initialize($storage)
138
    {
139
        $this->parameters['smallestImage'] = $storage->getSmallestImageSet()->slug;
140
        $this->parameters['cmsPrefix'] = '';
141
142
        $this->parameters['documentType'] = $this->storage->getDocumentTypeBySlug($this->documentType, true);
143
        $this->parameters['documentTypes'] = $this->storage->getDocumentTypes();
144
        $this->parameters['hideTitleAndState'] = true;
145
        $this->parameters['formId'] = $this->formId;
146
    }
147
148
    /**
149
     * If the form has been submitted, save the document
150
     * Calls $this->postSubmit() afterwards
151
     *
152
     * @param Storage $storage
153
     */
154
    private function checkSubmit($storage)
155
    {
156
        if ($this->isFormSubmitted($this->request) && $this->isSubmitAllowed()) {
157
            $postValues = $this->getPostValues($this->request);
158
            $this->setUserSessionBackup();
159
            $storage->addDocument($postValues);
160
            $this->restoreUserSessionBackup();
161
            $this->setSubmitToSession();
162
            $this->postSubmit($postValues, $storage);
163
        }
164
    }
165
166
    /**
167
     * Hook for derived classes to take actions after
168
     * submitting the form
169
     *
170
     * @param $postValues
171
     * @param $storage
172
     */
173
    protected function postSubmit($postValues, $storage)
0 ignored issues
show
Unused Code introduced by
The parameter $postValues is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $storage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
174
    {}
175
176
    /**
177
     * Sets a unique id for this particular form, so it can recognize
178
     * it when a submit occurs
179
     */
180
    private function setFormId()
181
    {
182
        if (isset($_SESSION['FormComponent'][$this->formParameterName]['formId'])) {
183
            $this->formId = $_SESSION['FormComponent'][$this->formParameterName]['formId'];
184
        } else {
185
            $_SESSION['FormComponent'][$this->formParameterName]['formId'] = (string) microtime(true);
186
            $_SESSION['FormComponent'][$this->formParameterName]['submitted'] = false;
187
            $this->formId = $_SESSION['FormComponent'][$this->formParameterName]['formId'];
188
        }
189
    }
190
191
    /**
192
     * Checks if this form has been submitted
193
     *
194
     * @param $request
195
     * @return bool
196
     */
197
    private function isFormSubmitted($request)
198
    {
199
        return !empty($request::$post) && isset($request::$post['formId']) && $request::$post['formId'] === $this->formId && isset($_SESSION['FormComponent'][$this->formParameterName]['formId']) && $_SESSION['FormComponent'][$this->formParameterName]['formId'] === $this->formId;
200
    }
201
202
    /**
203
     *
204
     *
205
     * @param $request
206
     */
207
    private function getPostValues($request)
208
    {
209
        $postValues = $request::$post;
210
        $postValues['documentType'] = $this->documentType;
211
        $postValues['path'] = $this->responseFolder;
212
        $postValues['title'] = date('r') . ' - From: ' . $request::$requestUri;
213
        return $postValues;
214
    }
215
216
    /**
217
     * Temporarily stores the current user session in a backup variable
218
     * and sets a fake user instead
219
     */
220
    private function setUserSessionBackup()
221
    {
222
        $this->userSessionBackup = isset($_SESSION['cloudcontrol']) ? $_SESSION['cloudcontrol'] : null;
223
        $fakeUser = new \stdClass();
224
        $fakeUser->username = 'FormComponent';
225
        $_SESSION['cloudcontrol'] = $fakeUser;
226
    }
227
228
    /**
229
     * Removes the fake user and restores the existing user
230
     * session if it was there
231
     */
232
    private function restoreUserSessionBackup()
233
    {
234
        if ($this->userSessionBackup === null) {
235
            unset($_SESSION['cloudcontrol']);
236
        } else {
237
            $_SESSION['cloudcontrol'] = $this->userSessionBackup;
238
        }
239
    }
240
241
    private function setSubmitToSession()
242
    {
243
        $_SESSION['FormComponent'][$this->formParameterName]['submitted'] = true;
244
    }
245
246
    private function isSubmitAllowed()
247
    {
248
        if ($this->submitOncePerSession === true && $_SESSION['FormComponent'][$this->formParameterName]['submitted'] === true) {
249
            return false;
250
        } else {
251
            return true;
252
        }
253
    }
254
}