Passed
Pull Request — dev (#35)
by Romain
03:20
created

SlotFlexFormService::getNotificationFlexFormList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2017
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\View\Slot\Service;
18
19
use CuyZ\Notiz\Definition\Tree\EventGroup\Event\EventDefinition;
20
use CuyZ\Notiz\Service\CacheService;
21
use CuyZ\Notiz\View\Slot\Application\Slot;
22
use CuyZ\Notiz\View\Slot\SlotView;
23
use CuyZ\Notiz\View\ViewPathsAware;
24
use TYPO3\CMS\Core\SingletonInterface;
25
26
class SlotFlexFormService implements SingletonInterface
27
{
28
    /**
29
     * @var SlotViewService
30
     */
31
    protected $slotViewService;
32
33
    /**
34
     * @var CacheService
35
     */
36
    protected $cacheService;
37
38
    /**
39
     * @param SlotViewService $slotViewService
40
     * @param CacheService $cacheService
41
     */
42
    public function __construct(SlotViewService $slotViewService, CacheService $cacheService)
43
    {
44
        $this->slotViewService = $slotViewService;
45
        $this->cacheService = $cacheService;
46
    }
47
48
    /**
49
     * Builds a list of FlexForms, based on the slots of every event.
50
     *
51
     * Events with no slots are ignored.
52
     *
53
     * @param ViewPathsAware $viewPaths
54
     * @return array
55
     */
56
    public function getNotificationFlexFormList(ViewPathsAware $viewPaths)
57
    {
58
        $flexFormList = ['default' => $this->getDefaultFlexForm()];
59
60
        foreach ($this->slotViewService->getEventsWithSlots($viewPaths) as $event => $view) {
61
            $flexForm = $this->getSlotViewFlexForm($view);
62
63
            /** @var EventDefinition $event */
64
            $flexFormList[$event->getFullIdentifier()] = $flexForm;
65
        }
66
67
        return $flexFormList;
68
    }
69
70
    /**
71
     * @param SlotView $view
72
     * @return string
73
     */
74
    public function getSlotViewFlexForm(SlotView $view)
75
    {
76
        $hash = 'slots-' . sha1(serialize([
77
                $view->getTemplatePathAndFilename(),
78
                $view->getLayoutRootPaths(),
79
                $view->getPartialRootPaths(),
80
                $view->getEventDefinition()->getFullIdentifier()
81
            ]));
82
83
        if ($this->cacheService->has($hash)) {
84
            $flexForm = $this->cacheService->get($hash);
85
        } else {
86
            $flexForm = $this->buildSlotViewFlexForm($view);
87
            $this->cacheService->set($hash, $flexForm);
88
        }
89
90
        return $flexForm;
91
    }
92
93
    /**
94
     * Builds a complete FlexForm XML, based on the slots of the given event
95
     * view.
96
     *
97
     * For each slot, a text field will be added to the FlexForm.
98
     *
99
     * @param SlotView $view
100
     * @return string
101
     */
102
    protected function buildSlotViewFlexForm(SlotView $view)
103
    {
104
        $slots = $view->getSlots()->getList();
105
106
        if (empty($slots)) {
107
            return null;
108
        }
109
110
        $slotsFlexForm = '';
111
112
        foreach ($slots as $slot) {
113
            $slotsFlexForm .= $this->getSlot($slot);
114
        }
115
116
        $sheet = $this->getSheet('sDEF', $slotsFlexForm);
117
118
        return $this->getBase($sheet);
119
    }
120
121
    /**
122
     * @param string $sheets
123
     * @return string
124
     */
125
    protected function getBase($sheets)
126
    {
127
        return <<<XML
128
<T3DataStructure>
129
    <meta>
130
        <langDisable>1</langDisable>
131
    </meta>
132
    <sheets>
133
        $sheets
134
    </sheets>
135
</T3DataStructure>
136
XML;
137
    }
138
139
    /**
140
     * @param string $identifier
141
     * @param string $slots
142
     * @return string
143
     */
144
    protected function getSheet($identifier, $slots)
145
    {
146
        return <<<XML
147
<$identifier>
148
    <ROOT>
149
        <TCEforms>
150
            <sheetTitle>$identifier</sheetTitle>
151
        </TCEforms>
152
        <type>array</type>
153
        <el>
154
            $slots
155
        </el>
156
    </ROOT>
157
</$identifier>
158
XML;
159
    }
160
161
    /**
162
     * @param Slot $slot
163
     * @return string
164
     */
165
    protected function getSlot(Slot $slot)
166
    {
167
        $label = htmlspecialchars($slot->getLabel());
168
169
        return <<<XML
170
<{$slot->getName()}>
171
    <TCEforms>
172
        <label>$label</label>
173
        <config>
174
            {$slot->getFlexFormConfiguration()}
175
        </config>
176
    </TCEforms>
177
</{$slot->getName()}>
178
XML;
179
    }
180
181
    /**
182
     * @return string
183
     */
184
    protected function getDefaultFlexForm()
185
    {
186
        return <<<XML
187
<T3DataStructure>
188
    <meta>
189
        <langDisable>1</langDisable>
190
    </meta>
191
    <sheets>
192
        <sDEF>
193
            <ROOT>
194
                <TCEforms>
195
                    <sheetTitle>default</sheetTitle>
196
                </TCEforms>
197
                <type>array</type>
198
                <el>
199
                </el>
200
            </ROOT>
201
        </sDEF>
202
    </sheets>
203
</T3DataStructure>
204
XML;
205
    }
206
}
207