Completed
Push — master ( 3e70d8...0fa5e9 )
by
unknown
03:54
created

FormInitializationJavaScriptAssetHandler::handleFormConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * 2016 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\Core\Core;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
/**
20
 * This asset handler generates the JavaScript code which will initialize the
21
 * form and its whole configuration.
22
 */
23
class FormInitializationJavaScriptAssetHandler extends AbstractJavaScriptAssetHandler
24
{
25
26
    /**
27
     * Generates and returns JavaScript code to instantiate a new form.
28
     *
29
     * @return string
30
     */
31
    public function getFormInitializationJavaScriptCode()
32
    {
33
        $formName = GeneralUtility::quoteJSvalue($this->getFormObject()->getName());
34
        $formConfigurationJson = $this->handleFormConfiguration($this->getFormConfiguration());
35
36
        $javaScriptCode = <<<JS
37
(function() {
38
    Formz.Form.register($formName, $formConfigurationJson);
39
})();
40
JS;
41
42
        return $javaScriptCode;
43
    }
44
45
    /**
46
     * This function is here to help unit tests mocking.
47
     *
48
     * @param string $formConfiguration
49
     * @return string
50
     */
51
    protected function handleFormConfiguration($formConfiguration)
52
    {
53
        return $formConfiguration;
54
    }
55
56
    /**
57
     * Returns a JSON array containing the form configuration.
58
     *
59
     * @return string
60
     */
61
    protected function getFormConfiguration()
62
    {
63
        $formConfigurationArray = $this->getFormObject()->getConfiguration()->toArray();
64
        $this->removeFieldsValidationConfiguration($formConfigurationArray)
65
            ->addClassNameProperty($formConfigurationArray);
66
67
        return Core::get()->arrayToJavaScriptJson($formConfigurationArray);
68
    }
69
70
    /**
71
     * To lower the length of the JavaScript code, we remove useless fields
72
     * validation configuration.
73
     *
74
     * @param array $formConfiguration
75
     * @return $this
76
     */
77
    protected function removeFieldsValidationConfiguration(array &$formConfiguration)
78
    {
79
        foreach ($formConfiguration['fields'] as $fieldName => $fieldConfiguration) {
80
            if (true === isset($fieldConfiguration['validation'])) {
81
                unset($fieldConfiguration['validation']);
82
                unset($fieldConfiguration['activation']);
83
                $formConfiguration['fields'][$fieldName] = $fieldConfiguration;
84
            }
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * Adds the "model" property to the form configuration, which can then be
92
     * used by JavaScript.
93
     *
94
     * @param array $formConfiguration
95
     */
96
    protected function addClassNameProperty(array &$formConfiguration)
97
    {
98
        $formConfiguration['className'] = $this->getFormObject()->getClassName();
99
    }
100
}
101