Completed
Push — feature/version-2 ( a1aace...47461d )
by Romain
03:02
created

FieldsValidationActivationJavaScriptAssetHandler::getConditionTreeForValidator()   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\Tree\ConditionTree;
18
use Romm\Formz\Form\Definition\Field\Validation\Validator;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * This asset handler generates the JavaScript code used the activate specific
23
 * validation rules for a given field. For example, you may want to enable the
24
 * rule "required" of a given field only under certain circumstances.
25
 */
26
class FieldsValidationActivationJavaScriptAssetHandler extends AbstractAssetHandler
27
{
28
29
    /**
30
     * Main function of this asset handler.
31
     *
32
     * @return string
33
     */
34
    public function getFieldsValidationActivationJavaScriptCode()
35
    {
36
        $javaScriptBlocks = [];
37
        $formConfiguration = $this->getFormObject()->getDefinition();
38
39
        foreach ($formConfiguration->getFields() as $field) {
40
            foreach ($field->getValidators() as $validator) {
41
                $fieldConditionExpression = [];
42
                $javaScriptTree = $this->getConditionTreeForValidator($validator)->getJavaScriptConditions();
43
44
                if (false === empty($javaScriptTree)) {
45
                    foreach ($javaScriptTree as $node) {
46
                        $fieldConditionExpression[] = 'flag = flag || (' . $node . ');';
47
                    }
48
49
                    $javaScriptBlocks[] = $this->getSingleFieldActivationConditionFunction($validator, $fieldConditionExpression);
50
                }
51
            }
52
        }
53
54
        $javaScriptBlocks = implode(CRLF, $javaScriptBlocks);
55
        $formName = GeneralUtility::quoteJSvalue($this->getFormObject()->getName());
56
57
        return <<<JS
58
(function() {
59
    Fz.Form.get(
60
        $formName,
61
        function(form) {
62
            var field = null;
63
64
$javaScriptBlocks
65
66
            form.refreshAllFields();
67
        }
68
    );
69
})();
70
JS;
71
    }
72
73
    /**
74
     * This function is just here to make the class more readable.
75
     *
76
     * @param Validator $validation
77
     * @param array     $fieldConditionExpression Array containing the JavaScript condition expression for the field.
78
     * @return string
79
     */
80
    protected function getSingleFieldActivationConditionFunction(Validator $validation, $fieldConditionExpression)
81
    {
82
        $fieldName = GeneralUtility::quoteJSvalue($validation->getParentField()->getName());
83
        $validationName = GeneralUtility::quoteJSvalue($validation->getName());
84
        $fieldConditionExpression = implode(CRLF . str_repeat(' ', 20), $fieldConditionExpression);
85
86
        return <<<JS
87
            field = form.getFieldByName($fieldName);
88
89
            if (null !== field) {
90
                field.addActivationConditionForValidator(
91
                    '__auto',
92
                    $validationName,
93
                    function (field, continueValidation) {
94
                        var flag = false;
95
                        $fieldConditionExpression
96
                        continueValidation(flag);
97
                    }
98
                );
99
            }
100
JS;
101
    }
102
103
    /**
104
     * @param Validator $validator
105
     * @return ConditionTree
106
     */
107
    protected function getConditionTreeForValidator(Validator $validator)
108
    {
109
        return $this->conditionProcessor->getActivationConditionTreeForValidator($validator);
110
    }
111
}
112