InlineRecord   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 4
dl 0
loc 133
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 3 1
A renderForeignRecordHeaderControl_preProcess() 0 9 1
C renderForeignRecordHeaderControl_postProcess() 0 64 10
A isValidRecord() 0 4 1
A arrayUnshiftAssoc() 0 6 1
1
<?php
2
3
/**
4
 * Hook into the inline icons.
5
 */
6
7
namespace HDNET\Focuspoint\Hooks;
8
9
use HDNET\Autoloader\Annotation\Hook;
10
use HDNET\Focuspoint\Service\WizardService;
11
use TYPO3\CMS\Backend\Form\Element\InlineElementHookInterface;
12
use TYPO3\CMS\Backend\Utility\BackendUtility;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
use TYPO3\CMS\Core\Utility\MathUtility;
15
16
/**
17
 * Hook into the inline icons.
18
 *
19
 * @Hook("TYPO3_CONF_VARS|SC_OPTIONS|t3lib/class.t3lib_tceforms_inline.php|tceformsInlineHook")
20
 */
21
class InlineRecord implements InlineElementHookInterface
22
{
23
    /**
24
     * Initializes this hook object.
25
     *
26
     * @param object $parentObject
27
     */
28
    public function init(&$parentObject)
0 ignored issues
show
Unused Code introduced by
The parameter $parentObject 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...
29
    {
30
    }
31
32
    /**
33
     * Pre-processing to define which control items are enabled or disabled.
34
     *
35
     * @param string $parentUid       The uid of the parent (embedding) record (uid or NEW...)
36
     * @param string $foreignTable    The table (foreign_table) we create control-icons for
37
     * @param array  $childRecord     The current record of that foreign_table
38
     * @param array  $childConfig     TCA configuration of the current field of the child record
39
     * @param bool   $isVirtual       Defines whether the current records is only virtually shown and not physically part of the parent record
40
     * @param array  $enabledControls (reference) Associative array with the enabled control items
41
     */
42
    public function renderForeignRecordHeaderControl_preProcess(
43
        $parentUid,
44
        $foreignTable,
45
        array $childRecord,
46
        array $childConfig,
47
        $isVirtual,
48
        array &$enabledControls
49
    ) {
50
    }
51
52
    /**
53
     * Post-processing to define which control items to show. Possibly own icons can be added here.
54
     *
55
     * @param string $parentUid    The uid of the parent (embedding) record (uid or NEW...)
56
     * @param string $foreignTable The table (foreign_table) we create control-icons for
57
     * @param array  $childRecord  The current record of that foreign_table
58
     * @param array  $childConfig  TCA configuration of the current field of the child record
59
     * @param bool   $isVirtual    Defines whether the current records is only virtually shown and not physically part of the parent record
60
     * @param array  $controlItems (reference) Associative array with the currently available control items
61
     */
62
    public function renderForeignRecordHeaderControl_postProcess(
63
        $parentUid,
64
        $foreignTable,
65
        array $childRecord,
66
        array $childConfig,
67
        $isVirtual,
68
        array &$controlItems
69
    ) {
70
        if ('sys_file_reference' !== $foreignTable) {
71
            return;
72
        }
73
74
        if (\is_array($childRecord['uid_local'])) {
75
            // Handling for TYPO3 > 8.x
76
            foreach ($childRecord['uid_local'] as $item) {
77
                if ('sys_file' !== $item['table']) {
78
                    return;
79
                }
80
                if (!MathUtility::canBeInterpretedAsInteger($childRecord['uid'])) {
81
                    return;
82
                }
83
            }
84
        } else {
85
            // Handling for TYPO3 < 8.x
86
            if (!GeneralUtility::isFirstPartOfStr($childRecord['uid_local'], 'sys_file_')) {
87
                return;
88
            }
89
90
            $parts = BackendUtility::splitTable_Uid($childRecord['uid_local']);
91
            if (!isset($parts[1])) {
92
                return;
93
            }
94
        }
95
96
        $table = $childRecord['tablenames'];
97
        $uid = $parentUid;
98
99
        if ($this->isValidRecord($table, $uid)) {
100
            $arguments = GeneralUtility::_GET();
101
            // The arguments array is different in case this is called by an AJAX request
102
            // via an IRRE inside an IRRE...
103
            if (!isset($arguments['edit'])) {
104
                $url = \parse_url(GeneralUtility::getIndpEnv('HTTP_REFERER'));
105
                \parse_str($url['query'], $arguments);
106
            }
107
            $returnUrl = [
108
                'edit' => $arguments['edit'],
109
                'returnUrl' => $arguments['returnUrl'],
110
            ];
111
112
            $wizardArguments = [
113
                'P' => [
114
                    'referenceUid' => $childRecord['uid'],
115
                    'returnUrl' => BackendUtility::getModuleUrl('record_edit', $returnUrl),
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Backend\Utilit...Utility::getModuleUrl() has been deprecated with message: since TYPO3 v9, will be removed in TYPO3 v10.0. Use UriBuilder instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
116
                ],
117
            ];
118
            $wizardUri = BackendUtility::getModuleUrl('focuspoint', $wizardArguments);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Backend\Utilit...Utility::getModuleUrl() has been deprecated with message: since TYPO3 v9, will be removed in TYPO3 v10.0. Use UriBuilder instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
119
        } else {
120
            $wizardUri = 'javascript:alert(\'Please save the base record first, because open this wizard will not save the changes in the current form!\');';
121
        }
122
        /** @var WizardService $wizardService */
123
        $wizardService = GeneralUtility::makeInstance(WizardService::class);
124
        $this->arrayUnshiftAssoc($controlItems, 'focuspoint', $wizardService->getWizardButton($wizardUri));
125
    }
126
127
    /**
128
     * Check if the record is valid.
129
     *
130
     * @param string $table
131
     * @param int    $uid
132
     *
133
     * @return bool
134
     */
135
    protected function isValidRecord($table, $uid)
136
    {
137
        return null !== BackendUtility::getRecord($table, $uid);
138
    }
139
140
    /**
141
     * Add a element with the given key in front of the array.
142
     *
143
     * @param $arr
144
     * @param string $key
145
     * @param string $val
146
     */
147
    protected function arrayUnshiftAssoc(&$arr, $key, $val)
148
    {
149
        $arr = \array_reverse($arr, true);
150
        $arr[$key] = $val;
151
        $arr = \array_reverse($arr, true);
152
    }
153
}
154