Completed
Push — development ( 762b81...ba51da )
by Torben
01:40
created

injectPropertyMapper()   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
namespace DERHANSEN\SfEventMgt\ViewHelpers\Registration\Field;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use DERHANSEN\SfEventMgt\Domain\Model\Registration\FieldValue;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use TYPO3\CMS\Extbase\Property\PropertyMapper;
20
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
21
22
/**
23
 * PrefillMultiValueField ViewHelper for registration fields
24
 *
25
 * @author Torben Hansen <[email protected]>
26
 */
27
class PrefillMultiValueFieldViewHelper extends AbstractViewHelper
28
{
29
    /**
30
     * @var \TYPO3\CMS\Extbase\Property\PropertyMapper
31
     */
32
    protected $propertyMapper = null;
33
34
    /**
35
     * @param PropertyMapper $propertyMapper
36
     */
37
    public function injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
38
    {
39
        $this->propertyMapper = $propertyMapper;
40
    }
41
42
    /**
43
     * Returns, if the given $currentValue is selected/checked for the given registration field
44
     * If no originalRequest exist (form is not submitted), true is returned if the given $currentValue
45
     * matches the default value of the field
46
     *
47
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration\Field $registrationField
48
     * @param string $currentValue
49
     * @return bool
50
     */
51
    public function render($registrationField, $currentValue)
52
    {
53
        // If mapping errors occured for form, return value that has been submitted
54
        $originalRequest = $this->controllerContext->getRequest()->getOriginalRequest();
55
        if ($originalRequest) {
56
            return $this->getFieldValueFromArguments(
57
                $originalRequest->getArguments(),
58
                $registrationField->getUid(),
59
                $currentValue
60
            );
61
        } else {
62
            return $this->getFieldValueFromDefaultProperty($registrationField, $currentValue);
63
        }
64
    }
65
66
    /**
67
     * Returns the submitted value for the given field uid
68
     *
69
     * @param array $submittedValues
70
     * @param int $fieldUid
71
     * @param string $currentValue
72
     * @return bool
73
     */
74
    protected function getFieldValueFromArguments($submittedValues, $fieldUid, $currentValue)
75
    {
76
        $result = false;
77
        foreach ($submittedValues['registration']['fieldValues'] as $fieldValueArray) {
78
            /** @var FieldValue $fieldValue */
79
            $fieldValue = $this->propertyMapper->convert($fieldValueArray, FieldValue::class);
80
            if ($fieldValue->getField()->getUid() === $fieldUid) {
81
                $result = $this->isGivenValueSelected($fieldValue, $currentValue);
82
            }
83
        }
84
        return $result;
85
    }
86
87
    /**
88
     * @param FieldValue $fieldValue
89
     * @param string $currentValue
90
     * @return bool
91
     */
92
    protected function isGivenValueSelected($fieldValue, $currentValue)
93
    {
94
        $fieldValue = $fieldValue->getValue();
95
        if (is_array($fieldValue)) {
96
            return in_array($currentValue, $fieldValue);
97
        } else {
98
            return $currentValue === $fieldValue;
99
        }
100
    }
101
102
    /**
103
     * @param \DERHANSEN\SfEventMgt\Domain\Model\Registration\Field $registrationField
104
     * @param $currentValue
105
     * @return bool
106
     */
107
    protected function getFieldValueFromDefaultProperty($registrationField, $currentValue)
108
    {
109
        $defaultValues = GeneralUtility::trimExplode(',', $registrationField->getDefaultValue());
110
        return in_array($currentValue, $defaultValues);
111
    }
112
}
113