Completed
Push — development ( d6c8c1...1ddb4f )
by Torben
11s
created

DataHandlerHooks   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 2
dl 0
loc 112
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A clearCachePostProc() 0 17 6
C processDatamap_postProcessFieldArray() 0 68 10
1
<?php
2
namespace DERHANSEN\SfEventMgt\Hooks;
3
4
/*
5
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
12
use TYPO3\CMS\Core\Cache\CacheManager;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
15
/**
16
 * Hooks for DataHandler
17
 *
18
 * @author Torben Hansen <[email protected]>
19
 */
20
class DataHandlerHooks
21
{
22
23
    /**
24
     * Flushes the cache if a event record was edited.
25
     * This happens on two levels: by UID and by PID.
26
     *
27
     * @param array $params
28
     */
29
    public function clearCachePostProc(array $params)
30
    {
31
        if (isset($params['table']) && $params['table'] === 'tx_sfeventmgt_domain_model_event') {
32
            $cacheTagsToFlush = [];
33
            if (isset($params['uid'])) {
34
                $cacheTagsToFlush[] = 'tx_sfeventmgt_uid_' . $params['uid'];
35
            }
36
            if (isset($params['uid_page'])) {
37
                $cacheTagsToFlush[] = 'tx_sfeventmgt_pid_' . $params['uid_page'];
38
            }
39
40
            $cacheManager = GeneralUtility::makeInstance(CacheManager::class);
41
            foreach ($cacheTagsToFlush as $cacheTag) {
42
                $cacheManager->flushCachesInGroupByTag('pages', $cacheTag);
43
            }
44
        }
45
    }
46
47
    /**
48
     * Checks if the fields defined in $checkFields are set in the data-array of pi_flexform.
49
     * If a field is present and contains an empty value, the field is unset.
50
     *
51
     * Structure of the checkFields array:
52
     *
53
     * array('sheet' => array('field1', 'field2'));
54
     *
55
     * @param string $status
56
     * @param string $table
57
     * @param string $id
58
     * @param array $fieldArray
59
     * @param \TYPO3\CMS\Core\DataHandling\DataHandler $reference
60
     *
61
     * @return void
62
     */
63
    public function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray, &$reference)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from 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 $reference is not used and could be removed.

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

Loading history...
Coding Style introduced by
Method name "DataHandlerHooks::processDatamap_postProcessFieldArray" is not in camel caps format
Loading history...
64
    {
65
        if ($table === 'tt_content' && $status == 'update' && isset($fieldArray['pi_flexform'])) {
66
            $checkFields = [
67
                'notification' => [
68
                    'settings.notification.senderEmail',
69
                    'settings.notification.senderName',
70
                    'settings.notification.adminEmail',
71
                    'settings.notification.registrationNew.userSubject',
72
                    'settings.notification.registrationWaitlistNew.userSubject',
73
                    'settings.notification.registrationNew.adminSubject',
74
                    'settings.notification.registrationWaitlistNew.adminSubject',
75
                    'settings.notification.registrationConfirmed.userSubject',
76
                    'settings.notification.registrationWaitlistConfirmed.userSubject',
77
                    'settings.notification.registrationConfirmed.adminSubject',
78
                    'settings.notification.registrationWaitlistConfirmed.adminSubject',
79
                    'settings.notification.registrationCancelled.userSubject',
80
                    'settings.notification.registrationCancelled.adminSubject',
81
                ],
82
                'sDEF' => [
83
                    'settings.displayMode',
84
                    'settings.orderField',
85
                    'settings.orderDirection',
86
                    'settings.topEventRestriction',
87
                    'settings.queryLimit',
88
                    'settings.category',
89
                    'settings.storagePage',
90
                    'settings.registration.requiredFields',
91
                    'settings.userRegistration.displayMode',
92
                    'settings.userRegistration.orderField',
93
                    'settings.userRegistration.orderDirection',
94
                    'settings.userRegistration.storagePage',
95
                    'settings.userRegistration.recursive'
96
                ],
97
                'additional' => [
98
                    'settings.detailPid',
99
                    'settings.listPid',
100
                    'settings.registrationPid',
101
                    'settings.paymentPid',
102
                    'settings.restrictForeignRecordsToStoragePage',
103
                    'settings.disableOverrideDemand'
104
                ],
105
                'template' => [
106
                    'settings.templateLayout'
107
                ]
108
            ];
109
110
            $flexformData = GeneralUtility::xml2array($fieldArray['pi_flexform']);
111
            foreach ($checkFields as $sheet => $fields) {
112
                foreach ($fields as $field) {
113
                    if (isset($flexformData['data'][$sheet]['lDEF'][$field]['vDEF']) &&
114
                        $flexformData['data'][$sheet]['lDEF'][$field]['vDEF'] === ''
115
                    ) {
116
                        unset($flexformData['data'][$sheet]['lDEF'][$field]);
117
                    }
118
                }
119
120
                // If remaining sheet does not contain fields, then remove the sheet
121
                if (isset($flexformData['data'][$sheet]['lDEF']) && $flexformData['data'][$sheet]['lDEF'] === []) {
122
                    unset($flexformData['data'][$sheet]);
123
                }
124
            }
125
126
            /** @var \TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools $flexFormTools */
127
            $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
128
            $fieldArray['pi_flexform'] = $flexFormTools->flexArray2Xml($flexformData, true);
129
        }
130
    }
131
}
132