-
Notifications
You must be signed in to change notification settings - Fork 3
/
CookieBarAdministrationController.php
310 lines (275 loc) · 9.01 KB
/
CookieBarAdministrationController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
declare(strict_types=1);
namespace Pixelant\PxaCookieBar\Controller;
/***************************************************************
* Copyright notice
*
* (c) 2017
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use Pixelant\PxaCookieBar\Domain\Repository\CookieWarningRepository;
use Pixelant\PxaCookieBar\Utility\BackendTranslateUtility;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\BackendTemplateView;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use TYPO3\CMS\Recordlist\RecordList\DatabaseRecordList;
/**
* Class CookieBarAdministrationController
* @package Pixelant\PxaCookieBar\Controller
*/
class CookieBarAdministrationController extends ActionController
{
/**
* Backend Template Container
*
* @var BackendTemplateView
*/
protected $defaultViewObjectName = BackendTemplateView::class;
/**
* BackendTemplateContainer
*
* @var BackendTemplateView
*/
protected $view = null;
/**
* @var \Pixelant\PxaCookieBar\Domain\Repository\CookieWarningRepository
*/
protected $cookieWarningRepository = null;
/**
* @param CookieWarningRepository $cookieWarningRepository
*/
public function injectCookieWarningRepository(CookieWarningRepository $cookieWarningRepository)
{
$this->cookieWarningRepository = $cookieWarningRepository;
}
/**
* Array of db row of current selected page
*
* @var array
*/
protected $currentPageInfo = [];
/**
* Current page
*
* @var int
*/
protected $pageUid = 0;
/**
* Current page row
*
* @var array
*/
protected $pageRow = [];
/**
* Determinate if current page is site root
*
* @var bool
*/
protected $isSiteRoot = false;
/**
* Set up the doc header properly here
*
* @param ViewInterface $view
* @return void
*/
protected function initializeView(ViewInterface $view)
{
/** @var BackendTemplateView $view */
parent::initializeView($view);
// Need to run before menu is created to know if new button is visible
$this->initialize();
$pageRenderer = $this->view->getModuleTemplate()->getPageRenderer();
$pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/AjaxDataHandler');
// Add JavaScript functions to the page:
$this->view->getModuleTemplate()->addJavaScriptCode(
'RecordListInlineJS',
'
function jumpExt(URL,anchor) {
var anc = anchor?anchor:"";
window.location.href = URL+(T3_THIS_LOCATION?"&returnUrl="+T3_THIS_LOCATION:"")+anc;
return false;
};
'
);
if ($this->isSiteRoot && $this->isNewCookieBarAllowed()) {
$this->createButtons();
}
}
/**
* Initialize
*
* @return void
*/
protected function initialize()
{
$this->pageUid = (int)GeneralUtility::_GET('id');
if ($this->pageUid) {
$page = BackendUtility::getRecord('pages', $this->pageUid);
if (is_array($page)) {
$this->isSiteRoot = (bool)$page['is_siteroot'];
$this->pageRow = $page;
}
}
}
/**
* Main action
*/
public function indexAction()
{
if ($this->pageUid) {
$countRecords = $this->cookieWarningRepository->countByPidRespectDisabled($this->pageUid);
if ($countRecords > 1) {
$this->addFlashMessage(
BackendTranslateUtility::translate('be.error.too_many'),
BackendTranslateUtility::translate('be.error'),
FlashMessage::ERROR
);
}
if ($countRecords > 0) {
$this->view->assign('htmlDbList', $this->getDbListHTML());
if (version_compare(TYPO3_version, '9.0', '>')) {
// Need to add labels manually
$this->includeLanguageFile('EXT:core/Resources/Private/Language/locallang_mod_web_list.xlf');
}
} else {
$this->view->assign('newRecordUrl', $this->buildNewCookieWarningUrl());
}
$this->view->assignMultiple([
'isRoot' => $this->isSiteRoot,
'pid' => $this->pageUid,
'requestUri' => GeneralUtility::quoteJSvalue(rawurlencode(GeneralUtility::getIndpEnv('REQUEST_URI')))
]);
}
}
/**
* Add menu buttons for specific actions
*
* @return void
*/
protected function createButtons()
{
if ($this->view->getModuleTemplate() !== null) {
/** @var IconFactory $iconFactory */
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar();
$button = $buttonBar
->makeLinkButton()
->setHref($this->buildNewCookieWarningUrl())
->setTitle(BackendTranslateUtility::translate('be.create_new'))
->setIcon($iconFactory->getIcon('actions-document-new', Icon::SIZE_SMALL));
$buttonBar->addButton($button, ButtonBar::BUTTON_POSITION_LEFT);
}
}
/**
* Records list html
*
* @return string
*/
protected function getDbListHTML(): string
{
/** @var DatabaseRecordList $dbList */
$dbList = GeneralUtility::makeInstance(DatabaseRecordList::class);
$dbList->script = GeneralUtility::getIndpEnv('REQUEST_URI');
$dbList->thumbs = $this->getBackendUser()->uc['thumbnailsByDefault'];
$dbList->allFields = 1;
$dbList->localizationView = 1;
$dbList->clickTitleMode = 'edit';
$dbList->calcPerms = $this->getBackendUser()->calcPerms($this->pageRow);
$dbList->showClipboard = 0;
$dbList->disableSingleTableView = 1;
$dbList->pageRow = $this->pageRow;
$dbList->displayFields = [];
$dbList->dontShowClipControlPanels = true;
$dbList->counter++;
$pointer = MathUtility::forceIntegerInRange(GeneralUtility::_GP('pointer'), 0);
$dbList->start(
$this->pageUid,
'tx_pxacookiebar_domain_model_cookiewarning',
$pointer
);
$dbList->generateList();
return $dbList->HTMLcode;
}
/**
* Generate url to create new survey
*
* @return string
*/
protected function buildNewCookieWarningUrl(): string
{
$url = (string)GeneralUtility::makeInstance(UriBuilder::class)->buildUriFromRoute(
'record_edit',
[
'edit[tx_pxacookiebar_domain_model_cookiewarning][' . $this->pageUid . ']' => 'new',
'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
]
);
return $url;
}
/**
* Add js labels from file
*
* @param $file
* @return void
*/
protected function includeLanguageFile(string $file)
{
$this->getPageRenderer()->addInlineLanguageLabelFile(
GeneralUtility::getFileAbsFileName($file)
);
}
/**
* Check if it's allowed to create new cookie warnings
*
* @return bool
*/
protected function isNewCookieBarAllowed(): bool
{
return $this->pageUid !== 0
&& ($this->cookieWarningRepository->countByPidRespectDisabled($this->pageUid) === 0);
}
/**
* Wrapper for class
*
* @return PageRenderer
*/
protected function getPageRenderer(): PageRenderer
{
return GeneralUtility::makeInstance(PageRenderer::class);
}
/**
* Get backend user
*
* @return BackendUserAuthentication
*/
protected function getBackendUser(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}
}