/
ElementInformationController.php
876 lines (787 loc) · 33.3 KB
/
ElementInformationController.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Backend\Controller\ContentElement;
use Doctrine\DBAL\Connection;
use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Backend\Avatar\Avatar;
use TYPO3\CMS\Backend\Form\FormDataCompiler;
use TYPO3\CMS\Backend\Form\FormDataGroup\TcaDatabaseRecord;
use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Resource\AbstractFile;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\Folder;
use TYPO3\CMS\Core\Resource\Index\MetaDataRepository;
use TYPO3\CMS\Core\Resource\Rendering\RendererRegistry;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
/**
* Script Class for showing information about an item.
* @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
*/
class ElementInformationController
{
/**
* Record table name
*
* @var string
*/
protected $table;
/**
* Record uid
*
* @var int
*/
protected $uid;
/**
* @var string
*/
protected $permsClause;
/**
* @var bool
*/
protected $access = false;
/**
* Which type of element:
* - "file"
* - "db"
*
* @var string
*/
protected $type = '';
/**
* @var ModuleTemplate
*/
protected $moduleTemplate;
/**
* For type "db": Set to page record of the parent page of the item set
* (if type="db")
*
* @var array
*/
protected $pageInfo;
/**
* Database records identified by table/uid
*
* @var array
*/
protected $row;
/**
* @var \TYPO3\CMS\Core\Resource\File|null
*/
protected $fileObject;
/**
* @var Folder
*/
protected $folderObject;
/**
* @var IconFactory
*/
protected $iconFactory;
/**
* Constructor
*/
public function __construct()
{
$this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
}
/**
* Injects the request object for the current request or subrequest
* As this controller goes only through the main() method, it is rather simple for now
*
* @param ServerRequestInterface $request the current request
* @return ResponseInterface the response with the content
*/
public function mainAction(ServerRequestInterface $request): ResponseInterface
{
$this->init($request);
$this->main($request);
return new HtmlResponse($this->moduleTemplate->renderContent());
}
/**
* Determines if table/uid point to database record or file and
* if user has access to view information
*
* @param ServerRequestInterface $request
*/
protected function init(ServerRequestInterface $request): void
{
$queryParams = $request->getQueryParams();
$this->table = $queryParams['table'] ?? null;
$this->uid = $queryParams['uid'] ?? null;
$this->permsClause = $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW);
$this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
$this->moduleTemplate->getDocHeaderComponent()->disable();
if (isset($GLOBALS['TCA'][$this->table])) {
$this->initDatabaseRecord();
} elseif ($this->table === '_FILE' || $this->table === '_FOLDER' || $this->table === 'sys_file') {
$this->initFileOrFolderRecord();
}
}
/**
* Init database records (table)
*/
protected function initDatabaseRecord(): void
{
$this->type = 'db';
$this->uid = (int)$this->uid;
// Check permissions and uid value:
if ($this->uid && $this->getBackendUser()->check('tables_select', $this->table)) {
if ((string)$this->table === 'pages') {
$this->pageInfo = BackendUtility::readPageAccess($this->uid, $this->permsClause);
$this->access = is_array($this->pageInfo);
$this->row = $this->pageInfo;
} else {
$this->row = BackendUtility::getRecordWSOL($this->table, $this->uid);
if ($this->row) {
if (!empty($this->row['t3ver_oid'])) {
// Make $this->uid the uid of the versioned record, while $this->row['uid'] is live record uid
$this->uid = (int)$this->row['_ORIG_uid'];
}
$this->pageInfo = BackendUtility::readPageAccess((int)$this->row['pid'], $this->permsClause);
$this->access = is_array($this->pageInfo);
}
}
}
}
/**
* Init file/folder parameters
*/
protected function initFileOrFolderRecord(): void
{
$fileOrFolderObject = GeneralUtility::makeInstance(ResourceFactory::class)->retrieveFileOrFolderObject($this->uid);
if ($fileOrFolderObject instanceof Folder) {
$this->folderObject = $fileOrFolderObject;
$this->access = $this->folderObject->checkActionPermission('read');
$this->type = 'folder';
} elseif ($fileOrFolderObject instanceof File) {
$this->fileObject = $fileOrFolderObject;
$this->access = $this->fileObject->checkActionPermission('read');
$this->type = 'file';
$this->table = 'sys_file';
try {
$this->row = BackendUtility::getRecordWSOL($this->table, $fileOrFolderObject->getUid());
} catch (Exception $e) {
$this->row = [];
}
}
}
/**
* Compiles the whole content to be outputted, which is then set as content to the moduleTemplate
* There is a hook to do a custom rendering of a record.
*
* @param ServerRequestInterface $request
*/
protected function main(ServerRequestInterface $request): void
{
$content = '';
// Rendering of the output via fluid
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setTemplateRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates')]);
$view->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials')]);
$view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
'EXT:backend/Resources/Private/Templates/ContentElement/ElementInformation.html'
));
if ($this->access) {
// render type by user func
$typeRendered = false;
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/show_item.php']['typeRendering'] ?? [] as $className) {
$typeRenderObj = GeneralUtility::makeInstance($className);
if (is_object($typeRenderObj) && method_exists($typeRenderObj, 'isValid') && method_exists($typeRenderObj, 'render')) {
if ($typeRenderObj->isValid($this->type, $this)) {
$content .= $typeRenderObj->render($this->type, $this);
$typeRendered = true;
break;
}
}
}
if (!$typeRendered) {
$view->assign('accessAllowed', true);
$view->assignMultiple($this->getPageTitle());
$view->assignMultiple($this->getPreview());
$view->assignMultiple($this->getPropertiesForTable());
$view->assignMultiple($this->getReferences($request));
$view->assign('returnUrl', GeneralUtility::sanitizeLocalUrl($request->getQueryParams()['returnUrl']));
$view->assign('maxTitleLength', $this->getBackendUser()->uc['titleLen'] ?? 20);
$content .= $view->render();
}
} else {
$content .= $view->render();
}
$this->moduleTemplate->setContent($content);
}
/**
* Get page title with icon, table title and record title
*
* @return array
*/
protected function getPageTitle(): array
{
$pageTitle = [
'title' => BackendUtility::getRecordTitle($this->table, $this->row)
];
if ($this->type === 'folder') {
$pageTitle['table'] = $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:folder');
$pageTitle['icon'] = $this->iconFactory->getIconForResource($this->folderObject, Icon::SIZE_SMALL)->render();
} elseif ($this->type === 'file') {
$pageTitle['table'] = $this->getLanguageService()->sL($GLOBALS['TCA'][$this->table]['ctrl']['title']);
$pageTitle['icon'] = $this->iconFactory->getIconForResource($this->fileObject, Icon::SIZE_SMALL)->render();
} else {
$pageTitle['table'] = $this->getLanguageService()->sL($GLOBALS['TCA'][$this->table]['ctrl']['title']);
$pageTitle['icon'] = $this->iconFactory->getIconForRecord($this->table, $this->row, Icon::SIZE_SMALL);
}
$this->moduleTemplate->setTitle($pageTitle['table'] . ': ' . $pageTitle['title']);
return $pageTitle;
}
/**
* Get preview for current record
*
* @return array
*/
protected function getPreview(): array
{
$preview = [];
// Perhaps @todo in future: Also display preview for records - without fileObject
if (!$this->fileObject) {
return $preview;
}
// check if file is marked as missing
if ($this->fileObject->isMissing()) {
$preview['missingFile'] = $this->fileObject->getName();
} else {
$rendererRegistry = GeneralUtility::makeInstance(RendererRegistry::class);
$fileRenderer = $rendererRegistry->getRenderer($this->fileObject);
$preview['url'] = $this->fileObject->getPublicUrl(true);
$width = '590m';
$height = '400m';
// Check if there is a FileRenderer
if ($fileRenderer !== null) {
$preview['fileRenderer'] = $fileRenderer->render(
$this->fileObject,
$width,
$height,
[],
true
);
// else check if we can create an Image preview
} elseif ($this->fileObject->isImage()) {
$preview['fileObject'] = $this->fileObject;
$preview['width'] = $width;
$preview['height'] = $height;
}
}
return $preview;
}
/**
* Get property array for html table
*
* @return array
*/
protected function getPropertiesForTable(): array
{
$lang = $this->getLanguageService();
$propertiesForTable = [];
$propertiesForTable['extraFields'] = $this->getExtraFields();
// Traverse the list of fields to display for the record:
$fieldList = $this->getFieldList($this->table, (int)$this->row['uid']);
foreach ($fieldList as $name) {
$name = trim($name);
$uid = $this->row['uid'];
if (!isset($GLOBALS['TCA'][$this->table]['columns'][$name])) {
continue;
}
// not a real field -> skip
if ($this->type === 'file' && $name === 'fileinfo') {
continue;
}
$isExcluded = !(!$GLOBALS['TCA'][$this->table]['columns'][$name]['exclude'] || $this->getBackendUser()->check('non_exclude_fields', $this->table . ':' . $name));
if ($isExcluded) {
continue;
}
$label = $lang->sL(BackendUtility::getItemLabel($this->table, $name));
$label = $label ?: $name;
$propertiesForTable['fields'][] = [
'fieldValue' => BackendUtility::getProcessedValue($this->table, $name, $this->row[$name], 0, 0, false, $uid),
'fieldLabel' => htmlspecialchars($label)
];
}
// additional information for folders and files
if ($this->folderObject instanceof Folder || $this->fileObject instanceof File) {
// storage
if ($this->folderObject instanceof Folder) {
$propertiesForTable['fields']['storage'] = [
'fieldValue' => $this->folderObject->getStorage()->getName(),
'fieldLabel' => htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_file.storage'))
];
}
// folder
$resourceObject = $this->fileObject ?: $this->folderObject;
$propertiesForTable['fields']['folder'] = [
'fieldValue' => $resourceObject->getParentFolder()->getReadablePath(),
'fieldLabel' => htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:folder'))
];
if ($this->fileObject instanceof File) {
// show file dimensions for images
if ($this->fileObject->getType() === AbstractFile::FILETYPE_IMAGE) {
$propertiesForTable['fields']['width'] = [
'fieldValue' => $this->fileObject->getProperty('width') . 'px',
'fieldLabel' => htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.width'))
];
$propertiesForTable['fields']['height'] = [
'fieldValue' => $this->fileObject->getProperty('height') . 'px',
'fieldLabel' => htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.height'))
];
}
// file size
$propertiesForTable['fields']['size'] = [
'fieldValue' => GeneralUtility::formatSize((int)$this->fileObject->getProperty('size'), htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:byteSizeUnits'))),
'fieldLabel' => $lang->sL(BackendUtility::getItemLabel($this->table, 'size'))
];
// show the metadata of a file as well
$table = 'sys_file_metadata';
$metaDataRepository = GeneralUtility::makeInstance(MetaDataRepository::class);
$metaData = $metaDataRepository->findByFileUid($this->row['uid']);
$allowedFields = $this->getFieldList($table, (int)$metaData['uid']);
foreach ($metaData as $name => $value) {
if (in_array($name, $allowedFields, true)) {
if (!isset($GLOBALS['TCA'][$table]['columns'][$name])) {
continue;
}
$isExcluded = !(!$GLOBALS['TCA'][$table]['columns'][$name]['exclude'] || $this->getBackendUser()->check('non_exclude_fields', $table . ':' . $name));
if ($isExcluded) {
continue;
}
$label = $lang->sL(BackendUtility::getItemLabel($table, $name));
$label = $label ?: $name;
$propertiesForTable['fields'][] = [
'fieldValue' => BackendUtility::getProcessedValue($table, $name, $metaData[$name], 0, 0, false, $metaData['uid']),
'fieldLabel' => htmlspecialchars($label)
];
}
}
}
}
return $propertiesForTable;
}
/**
* Get the list of fields that should be shown for the given table
*
* @param string $table
* @param int $uid
* @return array
*/
protected function getFieldList(string $table, int $uid): array
{
$formDataGroup = GeneralUtility::makeInstance(TcaDatabaseRecord::class);
$formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
$formDataCompilerInput = [
'command' => 'edit',
'tableName' => $table,
'vanillaUid' => $uid,
];
try {
$result = $formDataCompiler->compile($formDataCompilerInput);
$fieldList = array_unique(array_values($result['columnsToProcess']));
$ctrlKeysOfUnneededFields = ['origUid', 'transOrigPointerField', 'transOrigDiffSourceField'];
foreach ($ctrlKeysOfUnneededFields as $field) {
if (($key = array_search($GLOBALS['TCA'][$table]['ctrl'][$field], $fieldList, true)) !== false) {
unset($fieldList[$key]);
}
}
} catch (Exception $exception) {
$fieldList = [];
}
$searchFields = GeneralUtility::trimExplode(',', $GLOBALS['TCA'][$table]['ctrl']['searchFields']);
return array_unique(array_merge($fieldList, $searchFields));
}
/**
* Get the extra fields (uid, timestamps, creator) for the table
*
* @return array
*/
protected function getExtraFields(): array
{
$lang = $this->getLanguageService();
$keyLabelPair = [];
$extraFields = [
'uid' => htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:show_item.php.uid'))
];
if (in_array($this->type, ['folder', 'file'], true)) {
if ($this->type === 'file') {
$extraFields['creation_date'] = htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.creationDate'));
$extraFields['modification_date'] = htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.timestamp'));
}
} else {
foreach (['crdate' => 'creationDate', 'tstamp' => 'timestamp', 'cruser_id' => 'creationUserId'] as $field => $label) {
if (isset($GLOBALS['TCA'][$this->table]['ctrl'][$field])) {
$extraFields[$GLOBALS['TCA'][$this->table]['ctrl'][$field]] = htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.' . $label));
}
}
}
foreach ($extraFields as $name => $fieldLabel) {
if (in_array($name, ['creation_date', 'modification_date', 'tstamp', 'crdate'], true)) {
$rowValue = BackendUtility::datetime($this->row[$name]);
$keyLabelPair[$name] = [
'value' => $rowValue,
'fieldLabel' => rtrim($fieldLabel, ':'),
'isDatetime' => true,
];
} else {
$rowValue = BackendUtility::getProcessedValueExtra($this->table, $name, $this->row[$name]);
// show the backend username who created the issue
if ($name === 'cruser_id' && $rowValue) {
$creatorRecord = BackendUtility::getRecord('be_users', $rowValue);
if ($creatorRecord) {
/** @var Avatar $avatar */
$avatar = GeneralUtility::makeInstance(Avatar::class);
$creatorRecord['icon'] = $avatar->render($creatorRecord);
$name = 'creatorRecord';
$rowValue = $creatorRecord;
}
}
$keyLabelPair[$name] = [
'value' => $rowValue,
'fieldLabel' => rtrim($fieldLabel, ':'),
];
}
}
return $keyLabelPair;
}
/**
* Get references section (references from and references to current record)
*
* @param ServerRequestInterface $request
* @return array
*/
protected function getReferences(ServerRequestInterface $request): array
{
$references = [];
switch ($this->type) {
case 'db': {
$references['refLines'] = $this->makeRef($this->table, $this->uid, $request);
$references['refFromLines'] = $this->makeRefFrom($this->table, $this->uid, $request);
break;
}
case 'file': {
if ($this->fileObject && $this->fileObject->isIndexed()) {
$references['refLines'] = $this->makeRef('_FILE', $this->fileObject, $request);
}
break;
}
}
return $references;
}
/**
* Get field name for specified table/column name
*
* @param string $tableName Table name
* @param string $fieldName Column name
* @return string label
*/
protected function getLabelForTableColumn($tableName, $fieldName): string
{
if ($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['label'] !== null) {
$field = $this->getLanguageService()->sL($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['label']);
if (trim($field) === '') {
$field = $fieldName;
}
} else {
$field = $fieldName;
}
return $field;
}
/**
* Returns the record actions
*
* @param string $table
* @param int $uid
* @param ServerRequestInterface $request
* @return array
* @throws RouteNotFoundException
*/
protected function getRecordActions($table, $uid, ServerRequestInterface $request): array
{
if ($table === '' || $uid < 0) {
return [];
}
$actions = [];
// Edit button
$urlParameters = [
'edit' => [
$table => [
$uid => 'edit'
]
],
'returnUrl' => $request->getAttribute('normalizedParams')->getRequestUri()
];
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$actions['recordEditUrl'] = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
// History button
$urlParameters = [
'element' => $table . ':' . $uid,
'returnUrl' => $request->getAttribute('normalizedParams')->getRequestUri()
];
$actions['recordHistoryUrl'] = (string)$uriBuilder->buildUriFromRoute('record_history', $urlParameters);
if ($table === 'pages') {
// Recordlist button
$actions['webListUrl'] = (string)$uriBuilder->buildUriFromRoute('web_list', ['id' => $uid, 'returnUrl' => $request->getAttribute('normalizedParams')->getRequestUri()]);
// View page button
$actions['viewOnClick'] = BackendUtility::viewOnClick($uid, '', BackendUtility::BEgetRootLine($uid));
}
return $actions;
}
/**
* Make reference display
*
* @param string $table Table name
* @param string|\TYPO3\CMS\Core\Resource\File $ref Filename or uid
* @param ServerRequestInterface $request
* @return array
* @throws RouteNotFoundException
*/
protected function makeRef($table, $ref, ServerRequestInterface $request): array
{
$refLines = [];
$lang = $this->getLanguageService();
// Files reside in sys_file table
if ($table === '_FILE') {
$selectTable = 'sys_file';
$selectUid = $ref->getUid();
} else {
$selectTable = $table;
$selectUid = $ref;
}
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_refindex');
$predicates = [
$queryBuilder->expr()->eq(
'ref_table',
$queryBuilder->createNamedParameter($selectTable, \PDO::PARAM_STR)
),
$queryBuilder->expr()->eq(
'ref_uid',
$queryBuilder->createNamedParameter($selectUid, \PDO::PARAM_INT)
),
$queryBuilder->expr()->eq(
'deleted',
$queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
)
];
$backendUser = $this->getBackendUser();
if (!$backendUser->isAdmin()) {
$allowedSelectTables = GeneralUtility::trimExplode(',', $backendUser->groupData['tables_select']);
$predicates[] = $queryBuilder->expr()->in(
'tablename',
$queryBuilder->createNamedParameter($allowedSelectTables, Connection::PARAM_STR_ARRAY)
);
}
$rows = $queryBuilder
->select('*')
->from('sys_refindex')
->where(...$predicates)
->execute()
->fetchAll();
// Compile information for title tag:
foreach ($rows as $row) {
if ($row['tablename'] === 'sys_file_reference') {
$row = $this->transformFileReferenceToRecordReference($row);
if ($row['tablename'] === null || $row['recuid'] === null) {
return [];
}
}
$line = [];
$record = BackendUtility::getRecord($row['tablename'], $row['recuid']);
if ($record) {
BackendUtility::fixVersioningPid($row['tablename'], $record);
if (!$this->canAccessPage($row['tablename'], $record)) {
continue;
}
$parentRecord = BackendUtility::getRecord('pages', $record['pid']);
$parentRecordTitle = is_array($parentRecord)
? BackendUtility::getRecordTitle('pages', $parentRecord)
: '';
$urlParameters = [
'edit' => [
$row['tablename'] => [
$row['recuid'] => 'edit'
]
],
'returnUrl' => $request->getAttribute('normalizedParams')->getRequestUri()
];
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
$line['url'] = $url;
$line['icon'] = $this->iconFactory->getIconForRecord($row['tablename'], $record, Icon::SIZE_SMALL)->render();
$line['row'] = $row;
$line['record'] = $record;
$line['recordTitle'] = BackendUtility::getRecordTitle($row['tablename'], $record, false, true);
$line['parentRecordTitle'] = $parentRecordTitle;
$line['title'] = $lang->sL($GLOBALS['TCA'][$row['tablename']]['ctrl']['title']);
$line['labelForTableColumn'] = $this->getLabelForTableColumn($row['tablename'], $row['field']);
$line['path'] = BackendUtility::getRecordPath($record['pid'], '', 0, 0);
$line['actions'] = $this->getRecordActions($row['tablename'], $row['recuid'], $request);
} else {
$line['row'] = $row;
$line['title'] = $lang->sL($GLOBALS['TCA'][$row['tablename']]['ctrl']['title']) ?: $row['tablename'];
$line['labelForTableColumn'] = $this->getLabelForTableColumn($row['tablename'], $row['field']);
}
$refLines[] = $line;
}
return $refLines;
}
/**
* Make reference display (what this elements points to)
*
* @param string $table Table name
* @param string $ref Filename or uid
* @param ServerRequestInterface $request
* @return array
*/
protected function makeRefFrom($table, $ref, ServerRequestInterface $request): array
{
$refFromLines = [];
$lang = $this->getLanguageService();
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_refindex');
$predicates = [
$queryBuilder->expr()->eq(
'tablename',
$queryBuilder->createNamedParameter($table, \PDO::PARAM_STR)
),
$queryBuilder->expr()->eq(
'recuid',
$queryBuilder->createNamedParameter($ref, \PDO::PARAM_INT)
)
];
$backendUser = $this->getBackendUser();
if (!$backendUser->isAdmin()) {
$allowedSelectTables = GeneralUtility::trimExplode(',', $backendUser->groupData['tables_select']);
$predicates[] = $queryBuilder->expr()->in(
'ref_table',
$queryBuilder->createNamedParameter($allowedSelectTables, Connection::PARAM_STR_ARRAY)
);
}
$rows = $queryBuilder
->select('*')
->from('sys_refindex')
->where(...$predicates)
->execute()
->fetchAll();
// Compile information for title tag:
foreach ($rows as $row) {
$line = [];
$record = BackendUtility::getRecord($row['ref_table'], $row['ref_uid']);
if ($record) {
BackendUtility::fixVersioningPid($row['ref_table'], $record);
if (!$this->canAccessPage($row['ref_table'], $record)) {
continue;
}
$urlParameters = [
'edit' => [
$row['ref_table'] => [
$row['ref_uid'] => 'edit'
]
],
'returnUrl' => $request->getAttribute('normalizedParams')->getRequestUri()
];
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
$line['url'] = $url;
$line['icon'] = $this->iconFactory->getIconForRecord($row['tablename'], $record, Icon::SIZE_SMALL)->render();
$line['row'] = $row;
$line['record'] = $record;
$line['recordTitle'] = BackendUtility::getRecordTitle($row['ref_table'], $record, false, true);
$line['title'] = $lang->sL($GLOBALS['TCA'][$row['ref_table']]['ctrl']['title']);
$line['labelForTableColumn'] = $this->getLabelForTableColumn($table, $row['field']);
$line['path'] = BackendUtility::getRecordPath($record['pid'], '', 0, 0);
$line['actions'] = $this->getRecordActions($row['ref_table'], $row['ref_uid'], $request);
} else {
$line['row'] = $row;
$line['title'] = $lang->sL($GLOBALS['TCA'][$row['ref_table']]['ctrl']['title']);
$line['labelForTableColumn'] = $this->getLabelForTableColumn($table, $row['field']);
}
$refFromLines[] = $line;
}
return $refFromLines;
}
/**
* Convert FAL file reference (sys_file_reference) to reference index (sys_refindex) table format
*
* @param array $referenceRecord
* @return array
*/
protected function transformFileReferenceToRecordReference(array $referenceRecord): array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_file_reference');
$queryBuilder->getRestrictions()->removeAll();
$fileReference = $queryBuilder
->select('*')
->from('sys_file_reference')
->where(
$queryBuilder->expr()->eq(
'uid',
$queryBuilder->createNamedParameter($referenceRecord['recuid'], \PDO::PARAM_INT)
)
)
->execute()
->fetch();
return [
'recuid' => $fileReference['uid_foreign'],
'tablename' => $fileReference['tablenames'],
'field' => $fileReference['fieldname'],
'flexpointer' => '',
'softref_key' => '',
'sorting' => $fileReference['sorting_foreign']
];
}
/**
* @param string $tableName Name of the table
* @param array $record Record to be checked (ensure pid is resolved for workspaces)
* @return bool
*/
protected function canAccessPage(string $tableName, array $record): bool
{
$recordPid = (int)($tableName === 'pages' ? $record['uid'] : $record['pid']);
return $this->getBackendUser()->isInWebMount($tableName === 'pages' ? $record : $record['pid'])
|| $recordPid === 0 && !empty($GLOBALS['TCA'][$tableName]['ctrl']['security']['ignoreRootLevelRestriction']);
}
/**
* @return LanguageService
*/
protected function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}
/**
* @return BackendUserAuthentication
*/
protected function getBackendUser(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}
}