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

FieldsActivationCssAssetHandler::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\Css;
15
16
use Romm\Formz\AssetHandler\AbstractAssetHandler;
17
18
/**
19
 * This asset handler generates the CSS code which will automatically hide
20
 * certain fields of the form, depending on their activation conditions.
21
 *
22
 * Two steps are important:
23
 *  - First, the container of the field is hidden, no matter what.
24
 *  - Then, for each activation condition that can be reached, a CSS selector is
25
 *    generated, and used to display the container.
26
 */
27
class FieldsActivationCssAssetHandler extends AbstractAssetHandler
28
{
29
30
    /**
31
     * Main function of this asset handler.
32
     *
33
     * @return string
34
     */
35
    public function getFieldsActivationCss()
36
    {
37
        $cssBlocks = [];
38
        $formConfiguration = $this->getFormObject()->getConfiguration();
39
40
        foreach ($formConfiguration->getFields() as $fieldName => $field) {
41
            $formName = $this->getFormObject()->getName();
42
            $fieldContainerSelector = $field->getSettings()->getFieldContainerSelector();
43
44
            $cssTree = $this->conditionProcessor
45
                ->getActivationConditionTreeForField($field)
46
                ->getCssConditions();
47
48
            if (false === empty($cssTree)) {
49
                $fullNodeData = [];
50
51
                foreach ($cssTree as $node) {
52
                    $fullNodeData[] = 'form[name="' . $formName . '"]' . $node . ' ' . $fieldContainerSelector;
53
                }
54
55
                $nodesSelector = implode(',' . CRLF, $fullNodeData);
56
57
                $cssBlocks[] = $this->getSingleFieldCssBlock($formName, $fieldName, $fieldContainerSelector, $nodesSelector);
58
            }
59
        }
60
61
        return implode(CRLF, $cssBlocks);
62
    }
63
64
    /**
65
     * This function is just here to make the class more readable.
66
     *
67
     * @param string $formName               Name of the form.
68
     * @param string $fieldName              Name of the field.
69
     * @param string $fieldContainerSelector Selector for the field container.
70
     * @param string $nodesSelector          Nodes used to display the field container.
71
     * @return string
72
     */
73
    protected function getSingleFieldCssBlock($formName, $fieldName, $fieldContainerSelector, $nodesSelector)
74
    {
75
        return <<<CSS
76
/* Hiding the container of the field "$fieldName" by default */
77
form[name="$formName"] $fieldContainerSelector {
78
    display: none;
79
}
80
81
/* Showing the container of the field "$fieldName" */
82
$nodesSelector {
83
    display: block;
84
}
85
CSS;
86
    }
87
}
88