Passed
Pull Request — master (#214)
by Ralf
11:24
created

AbstractController::flashMessage()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
cc 3
nc 4
nop 4
1
<?php
2
namespace EWW\Dpf\Controller;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use EWW\Dpf\Domain\Model\Document;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
20
use TYPO3\CMS\Core\Log\LogManager;
21
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
22
23
24
abstract class AbstractController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
25
{
26
    /**
27
     * authorizationChecker
28
     *
29
     * @var \EWW\Dpf\Security\AuthorizationChecker
30
     * @TYPO3\CMS\Extbase\Annotation\Inject
31
     */
32
    protected $authorizationChecker = null;
33
34
    /**
35
     * security
36
     *
37
     * @var \EWW\Dpf\Security\Security
38
     * @TYPO3\CMS\Extbase\Annotation\Inject
39
     */
40
    protected $security = null;
41
42
    /**
43
     * clientRepository
44
     *
45
     * @var \EWW\Dpf\Domain\Repository\ClientRepository
46
     * @TYPO3\CMS\Extbase\Annotation\Inject
47
     */
48
    protected $clientRepository = null;
49
50
    /**
51
     * signalSlotDispatcher
52
     *
53
     * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
54
     * @TYPO3\CMS\Extbase\Annotation\Inject
55
     */
56
    protected $signalSlotDispatcher = null;
57
58
    /**
59
     * session
60
     *
61
     * @var \EWW\Dpf\Session\Session
62
     * @TYPO3\CMS\Extbase\Annotation\Inject
63
     */
64
    protected $session = null;
65
66
    /**
67
     * logger
68
     *
69
     * @var \TYPO3\CMS\Core\Log\Logger
70
     */
71
    protected $logger = null;
72
73
74
    public function __construct()
75
    {
76
        /** @var $logger \TYPO3\CMS\Core\Log\Logger */
77
        $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
78
    }
79
80
    protected function initializeView(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface $view)
81
    {
82
        parent::initializeView($view);
83
84
        $client = $this->clientRepository->findAll()->current();
85
86
        $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:manager.chooseClientMessage';
87
        $message = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf');
88
89
        if (!$client) {
90
            $this->addFlashMessage(
91
                $message,
92
                $messageTitle = '',
93
                $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::WARNING,
94
                $storeInSession = true
95
            );
96
        } else {
97
            $view->assign('client', $client);
98
        }
99
    }
100
101
    /**
102
     * Set session data
103
     *
104
     * @param string $key
105
     * @param string $data
106
     */
107
    public function setSessionData($key, $data)
108
    {
109
        if (!empty($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE'])) {
110
111
            $userGlobals = $GLOBALS['TSFE']->fe_user;
112
113
        } else if (!empty($GLOBALS['BE_USER']) && is_object($GLOBALS['BE_USER'])) {
114
115
            $userGlobals = $GLOBALS['BE_USER'];
116
117
        }
118
119
        $userGlobals->setAndSaveSessionData($key, $data);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userGlobals does not seem to be defined for all execution paths leading up to this point.
Loading history...
120
121
        return;
122
    }
123
124
    /**
125
     * Get session data
126
     *
127
     * @param string $key
128
     *
129
     * @return
130
     */
131
    public function getSessionData($key)
132
    {
133
        if (!empty($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE'])) {
134
135
            $userGlobals = $GLOBALS['TSFE']->fe_user;
136
137
        } else if (!empty($GLOBALS['BE_USER']) && is_object($GLOBALS['BE_USER'])) {
138
139
            $userGlobals = $GLOBALS['BE_USER'];
140
141
        }
142
        return $userGlobals->getSessionData($key);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userGlobals does not seem to be defined for all execution paths leading up to this point.
Loading history...
143
    }
144
145
    /**
146
     * Safely gets Parameters from request
147
     * if they exist
148
     *
149
     * @param string $parameterName
150
     *
151
     * @return null|string
152
     */
153
    protected function getParametersSafely($parameterName)
154
    {
155
        if ($this->request->hasArgument($parameterName)) {
156
            return $this->filterSafelyParameters($this->request->getArgument($parameterName));
157
        }
158
        return null;
159
    }
160
161
    /**
162
     * remove XSS stuff recursively
163
     *
164
     * @param mixed $param
165
     *
166
     * @return string
167
     */
168
    protected function filterSafelyParameters($param)
169
    {
170
        if (is_array($param)) {
171
            foreach ($param as $key => $item) {
172
                $param[$key] = $this->filterSafelyParameters($item);
173
            }
174
            return $param;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $param returns the type array which is incompatible with the documented return type string.
Loading history...
175
        } else {
176
            // FIXME: removeXSS is deprecated;
177
            //return GeneralUtility::removeXSS($param)
178
            return $param;
179
        }
180
    }
181
182
    public function initializeAction()
183
    {
184
        parent::initializeAction();
185
186
        $signalSlotDispatcher = GeneralUtility::makeInstance(Dispatcher::class);
187
        $signalSlotDispatcher->dispatch(get_class($this), 'actionChange', [$this->actionMethodName, get_class($this)]);
188
    }
189
190
191
    public function getCurrentAction()
192
    {
193
        return str_replace('Action', '', $this->actionMethodName);
194
    }
195
196
    public function getCurrentController()
197
    {
198
        $controllerName = end(explode('\\', get_Class($this)));
199
        return str_replace('Controller', '', $controllerName);
200
    }
201
202
203
    /**
204
     *
205
     * @param Document $document
206
     * @param string $key
207
     * @param int $severity
208
     * @param string $defaultMessage
209
     */
210
    protected function flashMessage(Document $document, string $key, int $severity, $defaultMessage = "")
211
    {
212
        // Show success or failure of the action in a flash message
213
        $args = [];
214
        if ($document) {
0 ignored issues
show
introduced by
$document is of type EWW\Dpf\Domain\Model\Document, thus it always evaluated to true.
Loading history...
215
            $args[] = $document->getTitle();
216
            $args[] = $document->getObjectIdentifier();
217
        }
218
219
        $message = LocalizationUtility::translate($key, 'dpf', $args);
220
        $message = empty($message) ? $defaultMessage : $message;
221
222
        $this->addFlashMessage(
223
            $message,
224
            '',
225
            $severity,
226
            true
227
        );
228
    }
229
230
231
}
232