Passed
Push — develop ( bfc038...8eb1da )
by Jens
03:00
created

FormComponent::checkParameters()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 16
c 1
b 0
f 1
nc 32
nop 0
dl 0
loc 27
rs 8.439
1
<?php
2
/**
3
 * Created by IntelliJ IDEA.
4
 * User: Jens
5
 * Date: 11-5-2016
6
 * Time: 10:02
7
 */
8
9
namespace library\components;
10
11
12
use library\cc\Request;
13
use library\storage\Storage;
14
15
class FormComponent Extends BaseComponent
16
{
17
    protected $documentType = null;
18
    protected $responseFolder = null;
19
    protected $subTemplate = 'cms/documents/document-form-form';
20
    protected $formParameterName = 'form';
21
    protected $thankYouMessage = 'Thank you for sending us your response.';
22
23
    private $formId;
24
    private $getPathBackup = null;
25
26
    public function run(Storage $storage)
27
    {
28
        parent::run($storage);
29
30
        $this->checkParameters();
31
32
        if ($this->documentType === null || $this->responseFolder === null) {
33
            throw new \Exception('Parameters `documentType` and `responseFolder` are required for usage with this form');
34
        }
35
36
        $this->setFormId();
37
        $this->initialize($storage);
38
        $this->checkSubmit($storage);
39
    }
40
41
    public function render($application = null)
42
    {
43
        $request = $this->request;
44
        if (isset($request::$get['path'])) {
45
            $this->getPathBackup = $request::$get['path'];
46
        }
47
        $request::$get['path'] = $this->responseFolder;
48
        $form = $this->renderTemplate($this->subTemplate);
49
        if ($this->getPathBackup !== null) {
50
            $request::$get['path'] = $this->getPathBackup;
51
        } else {
52
            unset($request::$get['path']);
53
        }
54
        if (!empty($request::$post) && isset($request::$post['formId']) && $request::$post['formId'] === $this->formId && isset($_SESSION['FormComponent'][$this->formParameterName]) && $_SESSION['FormComponent'][$this->formParameterName] === $this->formId) {
55
            $this->parameters[$this->formParameterName] = $this->thankYouMessage;
56
        } else {
57
            $this->parameters[$this->formParameterName] = $form;
58
        }
59
60
        parent::render($application);
61
    }
62
63
    private function checkParameters()
64
    {
65
        if (isset($this->parameters['documentType'])) {
66
            $this->documentType = $this->parameters['documentType'];
67
            unset($this->parameters['documentType']);
68
        }
69
70
        if (isset($this->parameters['responseFolder'])) {
71
            $this->responseFolder = $this->parameters['responseFolder'];
72
            unset($this->parameters['responseFolder']);
73
        }
74
75
        if (isset($this->parameters['subTemplate'])) {
76
            $this->subTemplate = $this->parameters['subTemplate'];
77
            unset($this->parameters['subTemplate']);
78
        }
79
80
        if (isset($this->parameters['formParameterName'])) {
81
            $this->formParameterName = $this->parameters['formParameterName'];
82
            unset($this->parameters['formParameterName']);
83
        }
84
85
        if (isset($this->parameters['thankYouMessage'])) {
86
            $this->thankYouMessage = $this->parameters['thankYouMessage'];
87
            unset($this->parameters['thankYouMessage']);
88
        }
89
    }
90
91
    private function initialize($storage)
92
    {
93
        $this->parameters['smallestImage'] = $storage->getSmallestImageSet()->slug;
94
        $this->parameters['cmsPrefix'] = '';
95
96
        $this->parameters['documentType'] = $this->storage->getDocumentTypeBySlug($this->documentType, true);
97
        $this->parameters['documentTypes'] = $this->storage->getDocumentTypes();
98
        $this->parameters['hideTitleAndState'] = true;
99
        $this->parameters['formId'] = $this->formId;
100
    }
101
102
    private function checkSubmit($storage)
103
    {
104
        $request = $this->request;
105
        if (!empty($request::$post) && isset($request::$post['formId']) && $request::$post['formId'] === $this->formId && isset($_SESSION['FormComponent'][$this->formParameterName]) && $_SESSION['FormComponent'][$this->formParameterName] === $this->formId) {
106
            $postValues = $request::$post;
107
            $postValues['documentType'] = $this->documentType;
108
            $postValues['path'] = $this->responseFolder;
109
            $postValues['title'] = date('r') . ' - From: ' . $request::$requestUri;
110
111
            $backup = null;
112
            if (isset($_SESSION['cloudcontrol'])) {
113
                $backup = $_SESSION['cloudcontrol'];
114
            }
115
            $fakeUser = new \stdClass();
116
            $fakeUser->username = 'FormComponent';
117
            $_SESSION['cloudcontrol'] = $fakeUser;
118
119
            $storage->addDocument($postValues);
120
121
            if ($backup === null) {
122
                unset($_SESSION['cloudcontrol']);
123
            } else {
124
                $_SESSION['cloudcontrol'] = $backup;
125
            }
126
        }
127
    }
128
129
    private function setFormId()
130
    {
131
        if (isset($_SESSION['FormComponent'][$this->formParameterName])) {
132
            $this->formId = $_SESSION['FormComponent'][$this->formParameterName];
133
        } else {
134
            $_SESSION['FormComponent'][$this->formParameterName] = (string) microtime(true);
135
            $this->formId = $_SESSION['FormComponent'][$this->formParameterName];
136
        }
137
    }
138
}