Completed
Push — development ( 915d6a...01399d )
by Romain
02:22
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 Romm\Formz\Condition\Parser\ConditionTree;
18
use Romm\Formz\Configuration\Form\Field\Field;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * This asset handler generates the JavaScript code used the activate the
23
 * validation rules of a field. Indeed, when a field is hidden, it is probably
24
 * not useful to let JavaScript run the validation rules.
25
 */
26
class FieldsActivationJavaScriptAssetHandler extends AbstractAssetHandler
27
{
28
29
    /**
30
     * Main function of this asset handler.
31
     *
32
     * @return string
33
     */
34
    public function getFieldsActivationJavaScriptCode()
35
    {
36
        $javaScriptBlocks = [];
37
        $formConfiguration = $this->getFormObject()->getConfiguration();
38
39
        foreach ($formConfiguration->getFields() as $field) {
40
            $fieldConditionExpression = [];
41
            $javaScriptTree = $this->getConditionTreeForField($field)->getJavaScriptConditions();
42
43
            if (false === empty($javaScriptTree)) {
44
                foreach ($javaScriptTree as $node) {
45
                    $fieldConditionExpression[] = 'flag = flag || (' . $node . ');';
46
                }
47
48
                $javaScriptBlocks[] = $this->getSingleFieldActivationConditionFunction($field, $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 Field $field                    Field instance.
73
     * @param array $fieldConditionExpression Array containing the JavaScript condition expression for the field.
74
     * @return string
75
     */
76
    protected function getSingleFieldActivationConditionFunction(Field $field, $fieldConditionExpression)
77
    {
78
        $fieldName = GeneralUtility::quoteJSvalue($field->getFieldName());
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
    /**
98
     * @param Field $field
99
     * @return ConditionTree
100
     */
101
    protected function getConditionTreeForField(Field $field)
102
    {
103
        return $this->conditionProcessor->getActivationConditionTreeForField($field);
104
    }
105
}
106