Completed
Push — development ( f488fc...915d6a )
by Romain
04:48 queued 02:48
created

FieldsActivationJavaScriptAssetHandler::getConditionTreeForField()   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
 * 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 TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
/**
20
 * This asset handler generates the JavaScript code used the activate the
21
 * validation rules of a field. Indeed, when a field is hidden, it is probably
22
 * not useful to let JavaScript run the validation rules.
23
 */
24
class FieldsActivationJavaScriptAssetHandler extends AbstractAssetHandler
25
{
26
27
    /**
28
     * Main function of this asset handler.
29
     *
30
     * @return string
31
     */
32
    public function getFieldsActivationJavaScriptCode()
33
    {
34
        $javaScriptBlocks = [];
35
        $formConfiguration = $this->getFormObject()->getConfiguration();
36
37
        foreach ($formConfiguration->getFields() as $fieldName => $field) {
38
            $fieldConditionExpression = [];
39
            $javaScriptTree = $this->conditionProcessor
40
                ->getActivationConditionTreeForField($field)
41
                ->getJavaScriptConditions();
42
43
            if (false === empty($javaScriptTree)) {
44
                foreach ($javaScriptTree as $node) {
45
                    $fieldConditionExpression[] = 'flag = flag || (' . $node . ');';
46
                }
47
48
                $javaScriptBlocks[] = $this->getSingleFieldActivationConditionFunction($fieldName, $fieldConditionExpression);
49
            }
50
        }
51
52
        $javaScriptBlocks = implode(CRLF, $javaScriptBlocks);
53
        $formName = GeneralUtility::quoteJSvalue($this->getFormObject()->getName());
54
55
        return <<<JS
56
(function() {
57
    Formz.Form.get(
58
        $formName,
59
        function(form) {
60
            var field = null;
61
62
$javaScriptBlocks
63
        }
64
    );
65
})();
66
JS;
67
    }
68
69
    /**
70
     * This function is just here to make the class more readable.
71
     *
72
     * @param string $fieldName                Name of the field.
73
     * @param array  $fieldConditionExpression Array containing the JavaScript condition expression for the field.
74
     * @return string
75
     */
76
    protected function getSingleFieldActivationConditionFunction($fieldName, $fieldConditionExpression)
77
    {
78
        $fieldName = GeneralUtility::quoteJSvalue($fieldName);
79
        $fieldConditionExpression = implode(CRLF . str_repeat(' ', 20), $fieldConditionExpression);
80
81
        return <<<JS
82
            field = form.getFieldByName($fieldName);
83
84
            if (null !== field) {
85
                field.addActivationCondition(
86
                    '__auto',
87
                    function (field, continueValidation) {
88
                        var flag = false;
89
                        $fieldConditionExpression
90
                        continueValidation(flag);
91
                    }
92
                );
93
            }
94
JS;
95
    }
96
}
97