DataHandlerHooks   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 118
dl 0
loc 224
rs 10
c 5
b 0
f 0
wmc 29

7 Methods

Rating   Name   Duplication   Size   Complexity  
A clearCachePostProc() 0 8 5
A deleteCustomNotificationsByEvent() 0 10 1
C processDatamap_postProcessFieldArray() 0 102 14
A processCmdmap_preProcess() 0 4 3
A processCmdmap_postProcess() 0 10 4
A unhideRegistrationsAfterCopyAndLocalize() 0 12 1
A hideRegistrationsBeforeCopyAndLocalize() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Hooks;
13
14
use DERHANSEN\SfEventMgt\Service\EventCacheService;
15
use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
16
use TYPO3\CMS\Core\Database\Connection;
17
use TYPO3\CMS\Core\Database\ConnectionPool;
18
use TYPO3\CMS\Core\DataHandling\DataHandler;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * Hooks for DataHandler
23
 */
24
class DataHandlerHooks
25
{
26
    public const EVENT_TABLE = 'tx_sfeventmgt_domain_model_event';
27
    public const REGISTRATION_TABLE = 'tx_sfeventmgt_domain_model_registration';
28
    public const CUSTOMNOTIFICATIONLOG_TABLE = 'tx_sfeventmgt_domain_model_customnotificationlog';
29
30
    /**
31
     * Flushes the cache if a event record was edited.
32
     * This happens on two levels: by UID and by PID.
33
     */
34
    public function clearCachePostProc(array $params): void
35
    {
36
        if (isset($params['table']) && $params['table'] === self::EVENT_TABLE) {
37
            $eventUid = $params['uid'] ?? 0;
38
            $pageUid = $params['uid_page'] ?? 0;
39
            if ($eventUid > 0 || $pageUid > 0) {
40
                $eventCacheService = GeneralUtility::makeInstance(EventCacheService::class);
41
                $eventCacheService->flushEventCache($eventUid, $pageUid);
42
            }
43
        }
44
    }
45
46
    /**
47
     * Checks if the fields defined in $checkFields are set in the data-array of pi_flexform.
48
     * If a field is present and contains an empty value, the field is unset. This way empty plugin settings
49
     * don't overwrite TypoScript settings.
50
     *
51
     * Structure of the checkFields array:
52
     *
53
     * array('sheet' => array('field1', 'field2'));
54
     */
55
    public function processDatamap_postProcessFieldArray(
56
        string $status,
57
        string $table,
58
        string|int $id,
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
        /** @scrutinizer ignore-unused */ string|int $id,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
        array &$fieldArray,
60
        DataHandler $dataHandler
61
    ): void {
62
        if ($table === 'tt_content' &&
63
            ($status === 'update' || $status === 'new') &&
64
            isset($fieldArray['pi_flexform']) &&
65
            in_array(
66
                $dataHandler->checkValue_currentRecord['CType'],
67
                [
68
                    'sfeventmgt_pieventlist',
69
                    'sfeventmgt_pieventdetail',
70
                    'sfeventmgt_pieventregistration',
71
                    'sfeventmgt_pieventsearch',
72
                    'sfeventmgt_pieventcalendar',
73
                    'sfeventmgt_piuserreg',
74
                    'sfeventmgt_pipayment',
75
                ],
76
                true
77
            )
78
        ) {
79
            $checkFields = [
80
                'notification' => [
81
                    'settings.notification.senderEmail',
82
                    'settings.notification.senderName',
83
                    'settings.notification.adminEmail',
84
                    'settings.notification.replyToEmail',
85
                    'settings.notification.registrationNew.userSubject',
86
                    'settings.notification.registrationWaitlistNew.userSubject',
87
                    'settings.notification.registrationNew.adminSubject',
88
                    'settings.notification.registrationWaitlistNew.adminSubject',
89
                    'settings.notification.registrationConfirmed.userSubject',
90
                    'settings.notification.registrationWaitlistConfirmed.userSubject',
91
                    'settings.notification.registrationConfirmed.adminSubject',
92
                    'settings.notification.registrationWaitlistConfirmed.adminSubject',
93
                    'settings.notification.registrationCancelled.userSubject',
94
                    'settings.notification.registrationCancelled.adminSubject',
95
                    'settings.notification.registrationWaitlistMoveUp.userSubject',
96
                    'settings.notification.registrationWaitlistMoveUp.adminSubject',
97
                ],
98
                'sDEF' => [
99
                    'settings.displayMode',
100
                    'settings.orderField',
101
                    'settings.orderDirection',
102
                    'settings.topEventRestriction',
103
                    'settings.timeRestrictionLow',
104
                    'settings.timeRestrictionHigh',
105
                    'settings.includeCurrent',
106
                    'settings.queryLimit',
107
                    'settings.category',
108
                    'settings.storagePage',
109
                    'settings.registration.requiredFields',
110
                    'settings.userRegistration.displayMode',
111
                    'settings.userRegistration.orderField',
112
                    'settings.userRegistration.orderDirection',
113
                    'settings.userRegistration.storagePage',
114
                    'settings.userRegistration.recursive',
115
                    'settings.singleEvent',
116
                ],
117
                'additional' => [
118
                    'settings.detailPid',
119
                    'settings.listPid',
120
                    'settings.registrationPid',
121
                    'settings.paymentPid',
122
                    'settings.restrictForeignRecordsToStoragePage',
123
                ],
124
                'pagination' => [
125
                    'settings.pagination.enablePagination',
126
                    'settings.pagination.itemsPerPage',
127
                    'settings.pagination.maxNumPages',
128
                ],
129
                'template' => [
130
                    'settings.templateLayout',
131
                ],
132
            ];
133
134
            $flexformData = GeneralUtility::xml2array($fieldArray['pi_flexform']);
135
            if (!is_array($flexformData)) {
136
                return;
137
            }
138
139
            foreach ($checkFields as $sheet => $fields) {
140
                foreach ($fields as $field) {
141
                    if (isset($flexformData['data'][$sheet]['lDEF'][$field]['vDEF']) &&
142
                        ($flexformData['data'][$sheet]['lDEF'][$field]['vDEF'] === '' ||
143
                            $flexformData['data'][$sheet]['lDEF'][$field]['vDEF'] === '0')
144
                    ) {
145
                        unset($flexformData['data'][$sheet]['lDEF'][$field]);
146
                    }
147
                }
148
149
                // If remaining sheet does not contain fields, then remove the sheet
150
                if (isset($flexformData['data'][$sheet]['lDEF']) && $flexformData['data'][$sheet]['lDEF'] === []) {
151
                    unset($flexformData['data'][$sheet]);
152
                }
153
            }
154
155
            $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
156
            $fieldArray['pi_flexform'] = $flexFormTools->flexArray2Xml($flexformData);
157
        }
158
    }
159
160
    /**
161
     * Hides non deleted event registrations before copy and localize in order to prevent registrations from being
162
     * copied or localized.
163
     *
164
     * @todo: Try to extend TYPO3 core to allow 3rd party extension to modifying `$excludeFields` in
165
     *        `DataHandler->copyRecord()` function.
166
     *
167
     * @param string $command
168
     * @param string $table
169
     * @param int $id
170
     * @param mixed $value
171
     * @param DataHandler $pObj
172
     * @param bool $pasteUpdate
173
     */
174
    public function processCmdmap_preProcess($command, $table, $id, $value, $pObj, $pasteUpdate): void
0 ignored issues
show
Unused Code introduced by
The parameter $pObj is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

174
    public function processCmdmap_preProcess($command, $table, $id, $value, /** @scrutinizer ignore-unused */ $pObj, $pasteUpdate): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

174
    public function processCmdmap_preProcess($command, $table, $id, /** @scrutinizer ignore-unused */ $value, $pObj, $pasteUpdate): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pasteUpdate is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

174
    public function processCmdmap_preProcess($command, $table, $id, $value, $pObj, /** @scrutinizer ignore-unused */ $pasteUpdate): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
    {
176
        if ($table === self::EVENT_TABLE && in_array($command, ['copy', 'localize'])) {
177
            $this->hideRegistrationsBeforeCopyAndLocalize($id);
178
        }
179
    }
180
181
    /**
182
     * (1) Unhides non deleted event registrations after copy and localize
183
     * (2) Handles deletion of custom notifications when deleting an event
184
     *
185
     * @param string $command
186
     * @param string $table
187
     * @param int $id
188
     * @param string $value
189
     * @param DataHandler $pObj
190
     * @param bool $pasteUpdate
191
     * @param array $pasteDatamap
192
     */
193
    public function processCmdmap_postProcess($command, $table, $id, $value, $pObj, $pasteUpdate, $pasteDatamap): void
0 ignored issues
show
Unused Code introduced by
The parameter $pObj is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

193
    public function processCmdmap_postProcess($command, $table, $id, $value, /** @scrutinizer ignore-unused */ $pObj, $pasteUpdate, $pasteDatamap): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pasteDatamap is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

193
    public function processCmdmap_postProcess($command, $table, $id, $value, $pObj, $pasteUpdate, /** @scrutinizer ignore-unused */ $pasteDatamap): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

193
    public function processCmdmap_postProcess($command, $table, $id, /** @scrutinizer ignore-unused */ $value, $pObj, $pasteUpdate, $pasteDatamap): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pasteUpdate is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

193
    public function processCmdmap_postProcess($command, $table, $id, $value, $pObj, /** @scrutinizer ignore-unused */ $pasteUpdate, $pasteDatamap): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
194
    {
195
        if ($table !== self::EVENT_TABLE) {
196
            return;
197
        }
198
199
        if (in_array($command, ['copy', 'localize'])) {
200
            $this->unhideRegistrationsAfterCopyAndLocalize($id);
201
        } elseif ($command === 'delete') {
202
            $this->deleteCustomNotificationsByEvent($id);
203
        }
204
    }
205
206
    /**
207
     * Removes all custom notification log entries for the given event UID
208
     */
209
    protected function deleteCustomNotificationsByEvent(int $eventUid): void
210
    {
211
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
212
            ->getQueryBuilderForTable(self::CUSTOMNOTIFICATIONLOG_TABLE);
213
        $queryBuilder
214
            ->delete(self::CUSTOMNOTIFICATIONLOG_TABLE)
215
            ->where(
216
                $queryBuilder->expr()->eq('event', $queryBuilder->createNamedParameter($eventUid, Connection::PARAM_INT))
217
            )
218
            ->executeStatement();
219
    }
220
221
    protected function hideRegistrationsBeforeCopyAndLocalize(int $eventUid): void
222
    {
223
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
224
            ->getQueryBuilderForTable(self::CUSTOMNOTIFICATIONLOG_TABLE);
225
        $queryBuilder
226
            ->update(self::REGISTRATION_TABLE)
227
            ->set('deleted', 1)
228
            ->set('temp_event_uid', $eventUid)
229
            ->where(
230
                $queryBuilder->expr()->eq('event', $queryBuilder->createNamedParameter($eventUid, Connection::PARAM_INT)),
231
                $queryBuilder->expr()->eq('deleted', $queryBuilder->createNamedParameter(0, Connection::PARAM_INT))
232
            )
233
            ->executeStatement();
234
    }
235
236
    protected function unhideRegistrationsAfterCopyAndLocalize(int $eventUid): void
237
    {
238
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
239
            ->getQueryBuilderForTable(self::CUSTOMNOTIFICATIONLOG_TABLE);
240
        $queryBuilder
241
            ->update(self::REGISTRATION_TABLE)
242
            ->set('deleted', 0)
243
            ->set('temp_event_uid', 0)
244
            ->where(
245
                $queryBuilder->expr()->eq('temp_event_uid', $queryBuilder->createNamedParameter($eventUid, Connection::PARAM_INT))
246
            )
247
            ->executeStatement();
248
    }
249
}
250