Passed
Pull Request — master (#146)
by Romain
03:13
created

EventConfigurationProvider::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 17
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Backend\FormEngine\DataProvider;
18
19
use CuyZ\Notiz\Core\Notification\Service\LegacyNotificationTcaService;
20
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
21
22
/**
23
 * Builds the TCA array for the event configuration.
24
 *
25
 * It is a FlexForm field, with as many definition sheets as there are events
26
 * using FlexForm. Display conditions are dynamically assigned to show the
27
 * correct definition depending on the selected event.
28
 */
29
class EventConfigurationProvider extends GracefulProvider
30
{
31
    const COLUMN = '__eventConfiguration';
32
33
    /**
34
     * @param array $result
35
     * @return array
36
     */
37
    public function process(array $result)
38
    {
39
        $tableName = $result['tableName'];
40
41
        if (!isset($GLOBALS['TCA'][$tableName]['ctrl'][self::COLUMN])) {
42
            return $result;
43
        }
44
45
        $columnName = $GLOBALS['TCA'][$tableName]['ctrl'][self::COLUMN];
46
47
        if (!isset($GLOBALS['TCA'][$tableName]['columns'][$columnName])) {
48
            return $result;
49
        }
50
51
        $this->fillEventConfigurationTca($tableName, $columnName);
52
53
        return $result;
54
    }
55
56
    /**
57
     * @param string $tableName
58
     * @param string $columnName
59
     */
60
    private function fillEventConfigurationTca($tableName, $columnName)
61
    {
62
        $flexFormDs = [];
63
        $displayConditions = [];
64
65
        foreach ($this->definitionService->getDefinition()->getEvents() as $event) {
66
            $provider = $event->getConfiguration()->getFlexFormProvider();
67
68
            if ($provider->hasFlexForm()) {
69
                $identifier = $event->getFullIdentifier();
70
71
                $flexFormDs[$identifier] = $provider->getFlexFormValue();
72
                $displayConditions[] = $identifier;
73
            }
74
        }
75
76
        // If no definition is found, the field is not shown at all.
77
        if (empty($flexFormDs)) {
78
            $GLOBALS['TCA'][$tableName]['columns'][$columnName]['config'] = ['type' => 'passthrough'];
79
        }
80
81
        /**
82
         * @deprecated Value can be empty when TYPO3 v7 is not supported anymore.
83
         */
84
        $flexFormDs['default'] = 'FILE:EXT:notiz/Configuration/FlexForm/Event/DefaultEventFlexForm.xml';
85
86
        $GLOBALS['TCA'][$tableName]['columns'][$columnName]['config']['ds'] = $flexFormDs;
87
88
        $GLOBALS['TCA'][$tableName]['columns'][$columnName]['displayCond'] = version_compare(VersionNumberUtility::getCurrentTypo3Version(), '8.0.0', '<')
89
            ? 'USER:' . LegacyNotificationTcaService::class . '->displayEventFlexForm:' . $tableName . ':' . implode(',', $displayConditions)
90
            : 'FIELD:event:IN:' . implode(',', $displayConditions);
91
    }
92
}
93