Completed
Push — middleware ( 9c572f...78eddc )
by Romain
02:34
created

FormRequestDataJavaScriptAssetHandler::getFieldsExistingMessages()   C

Complexity

Conditions 7
Paths 2

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 2
nop 0
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Formz project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\AssetHandler\JavaScript;
15
16
use Romm\Formz\AssetHandler\AbstractAssetHandler;
17
use Romm\Formz\Error\FormzMessageInterface;
18
use Romm\Formz\Service\ArrayService;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * Will handle the "one time run" data needed by JavaScript: the submitted
23
 * values, and others.
24
 */
25
class FormRequestDataJavaScriptAssetHandler extends AbstractAssetHandler
26
{
27
28
    /**
29
     * See class description.
30
     *
31
     * @return string
32
     */
33
    public function getFormRequestDataJavaScriptCode()
34
    {
35
        $submittedFormValues = [];
36
        $fieldsExistingMessages = [];
37
        $deactivatedFields = [];
38
        $formWasSubmitted = 'false';
39
40
        if ($this->getFormObject()->formWasSubmitted()) {
41
            $formWasSubmitted = 'true';
42
            $submittedFormValues = $this->getSubmittedFormValues();
43
            $fieldsExistingMessages = $this->getFieldsExistingMessages();
44
            $deactivatedFields = $this->getDeactivatedFields();
45
        }
46
47
        $submittedFormValues = ArrayService::get()->arrayToJavaScriptJson($submittedFormValues);
48
        $fieldsExistingMessages = ArrayService::get()->arrayToJavaScriptJson($fieldsExistingMessages);
49
        $deactivatedFields = ArrayService::get()->arrayToJavaScriptJson($deactivatedFields);
50
51
        $formName = GeneralUtility::quoteJSvalue($this->getFormObject()->getName());
52
53
        $javaScriptCode = <<<JS
54
(function() {
55
    Formz.Form.beforeInitialization($formName, function(form) {
56
        form.injectRequestData($submittedFormValues, $fieldsExistingMessages, $formWasSubmitted, $deactivatedFields)
57
    });
58
})();
59
JS;
60
61
        return $javaScriptCode;
62
    }
63
64
    /**
65
     * Will fetch the values of the fields of the submitted form, if a form has
66
     * been submitted.
67
     *
68
     * @return array
69
     */
70
    protected function getSubmittedFormValues()
71
    {
72
        $result = [];
73
        $formName = $this->getFormObject()->getName();
74
        $originalRequest = $this->getControllerContext()
75
            ->getRequest()
76
            ->getOriginalRequest();
77
78
        if (null !== $originalRequest
79
            && $originalRequest->hasArgument($formName)
80
        ) {
81
            $result = $originalRequest->getArgument($formName);
82
        }
83
84
        return $result;
85
    }
86
87
    /**
88
     * This function checks every message which may exist on every property of
89
     * the form (used to tell to JavaScript which messages already exist).
90
     *
91
     * @return array
92
     */
93
    protected function getFieldsExistingMessages()
94
    {
95
        $fieldsMessages = [];
96
        $formObject = $this->getFormObject();
97
98
        if ($formObject->formWasSubmitted()
99
            && $formObject->hasFormResult()
100
        ) {
101
            $formResult = $this->getFormObject()->getFormResult();
102
103
            foreach ($this->getFormObject()->getProperties() as $fieldName) {
104
                $result = $formResult->forProperty($fieldName);
105
                $messages = [];
106
107
                if ($result->hasErrors()) {
108
                    $messages['errors'] = $this->formatMessages($result->getErrors());
0 ignored issues
show
Documentation introduced by
$result->getErrors() is of type array<integer,object<TYP...S\Extbase\Error\Error>>, but the function expects a array<integer,object<Rom...FormzMessageInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109
                }
110
111
                if ($result->hasWarnings()) {
112
                    $messages['warnings'] = $this->formatMessages($result->getWarnings());
0 ignored issues
show
Documentation introduced by
$result->getWarnings() is of type array<integer,object<TYP...Extbase\Error\Warning>>, but the function expects a array<integer,object<Rom...FormzMessageInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113
                }
114
115
                if ($result->hasNotices()) {
116
                    $messages['notices'] = $this->formatMessages($result->getNotices());
0 ignored issues
show
Documentation introduced by
$result->getNotices() is of type array<integer,object<TYP...\Extbase\Error\Notice>>, but the function expects a array<integer,object<Rom...FormzMessageInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
                }
118
119
                $fieldsMessages[$fieldName] = $messages;
120
            }
121
        }
122
123
        return $fieldsMessages;
124
    }
125
126
    /**
127
     * @param FormzMessageInterface[] $messages
128
     * @return array
129
     */
130
    protected function formatMessages(array $messages)
131
    {
132
        $sortedMessages = [];
133
134
        foreach ($messages as $message) {
135
            $validationName = $message->getValidationName();
136
            $messageKey = $message->getMessageKey();
137
138
            if (false === isset($sortedMessages[$validationName])) {
139
                $sortedMessages[$validationName] = [];
140
            }
141
142
            $sortedMessages[$validationName][$messageKey] = $message->render();
143
        }
144
145
        return $sortedMessages;
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    protected function getDeactivatedFields()
152
    {
153
        return ($this->getFormObject()->hasFormResult())
154
            ? array_keys($this->getFormObject()->getFormResult()->getDeactivatedFields())
155
            : [];
156
    }
157
}
158