Completed
Push — development ( 915d6a...01399d )
by Romain
02:22
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
use Romm\Formz\Condition\Parser\ConditionTree;
18
use Romm\Formz\Configuration\Form\Field\Field;
19
20
/**
21
 * This asset handler generates the CSS code which will automatically hide
22
 * certain fields of the form, depending on their activation conditions.
23
 *
24
 * Two steps are important:
25
 *  - First, the container of the field is hidden, no matter what.
26
 *  - Then, for each activation condition that can be reached, a CSS selector is
27
 *    generated, and used to display the container.
28
 */
29
class FieldsActivationCssAssetHandler extends AbstractAssetHandler
30
{
31
32
    /**
33
     * Main function of this asset handler.
34
     *
35
     * @return string
36
     */
37
    public function getFieldsActivationCss()
38
    {
39
        $cssBlocks = [];
40
        $formConfiguration = $this->getFormObject()->getConfiguration();
41
42
        foreach ($formConfiguration->getFields() as $field) {
43
            $formName = $this->getFormObject()->getName();
44
            $fieldContainerSelector = $field->getSettings()->getFieldContainerSelector();
45
46
            $cssTree = $this->getConditionTreeForField($field)->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, $field, $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 Field  $field                  Field instance.
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, $field, $fieldContainerSelector, $nodesSelector)
74
    {
75
        return <<<CSS
76
/* Hiding the container of the field "{$field->getFieldName()}" by default */
77
form[name="$formName"] $fieldContainerSelector {
78
    display: none;
79
}
80
81
/* Showing the container of the field "{$field->getFieldName()}" */
82
$nodesSelector {
83
    display: block;
84
}
85
CSS;
86
    }
87
88
    /**
89
     * @param Field $field
90
     * @return ConditionTree
91
     */
92
    protected function getConditionTreeForField(Field $field)
93
    {
94
        return $this->conditionProcessor->getActivationConditionTreeForField($field);
95
    }
96
}
97