-
Notifications
You must be signed in to change notification settings - Fork 26
/
DrawItem.php
1490 lines (1417 loc) · 68.3 KB
/
DrawItem.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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace GridElementsTeam\Gridelements\Hooks;
/***************************************************************
* Copyright notice
* (c) 2013 Jo Hasenau <info@cybercraft.de>
* 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 2 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 GridElementsTeam\Gridelements\Backend\LayoutSetup;
use GridElementsTeam\Gridelements\Helper\Helper;
use PDO;
use TYPO3\CMS\Backend\Controller\PageLayoutController;
use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Backend\View\PageLayoutView;
use TYPO3\CMS\Backend\View\PageLayoutViewDrawFooterHookInterface;
use TYPO3\CMS\Backend\View\PageLayoutViewDrawItemHookInterface;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\EndTimeRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\StartTimeRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction;
use TYPO3\CMS\Core\Database\QueryGenerator;
use TYPO3\CMS\Core\Exception;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\CMS\Core\Versioning\VersionState;
use UnexpectedValueException;
/**
* Class/Function which manipulates the rendering of item example content and replaces it with a grid of child elements.
*
* @author Jo Hasenau <info@cybercraft.de>
*/
class DrawItem implements PageLayoutViewDrawItemHookInterface, SingletonInterface
{
/**
* @var array
*/
protected $extentensionConfiguration;
/**
* @var Helper
*/
protected Helper $helper;
/**
* @var IconFactory
*/
protected $iconFactory;
/**
* @var LanguageService
*/
protected LanguageService $languageService;
/**
* Stores whether a certain language has translations in it
*
* @var array
*/
protected array $languageHasTranslationsCache = [];
/**
* @var QueryGenerator
*/
protected QueryGenerator $tree;
/**
* @var bool
*/
protected bool $showHidden;
/**
* @var string
*/
protected string $backPath = '';
public function __construct()
{
$this->extentensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('gridelements');
$this->setLanguageService($GLOBALS['LANG']);
$this->helper = Helper::getInstance();
$this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$this->cleanupCollapsedStatesInUC();
}
/**
* Processes the collapsed states of Gridelements columns and removes columns with 0 values
*/
public function cleanupCollapsedStatesInUC()
{
$backendUser = $this->getBackendUser();
if (is_array($backendUser->uc['moduleData']['page']['gridelementsCollapsedColumns'])) {
$collapsedGridelementColumns = $backendUser->uc['moduleData']['page']['gridelementsCollapsedColumns'];
foreach ($collapsedGridelementColumns as $item => $collapsed) {
if (empty($collapsed)) {
unset($collapsedGridelementColumns[$item]);
}
}
$backendUser->uc['moduleData']['page']['gridelementsCollapsedColumns'] = $collapsedGridelementColumns;
$backendUser->writeUC($backendUser->uc);
}
}
/**
* @return BackendUserAuthentication
*/
public function getBackendUser(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}
/**
* Processes the item to be rendered before the actual example content gets rendered
* Deactivates the original example content output
*
* @param PageLayoutView $parentObject : The parent object that triggered this hook
* @param bool $drawItem : A switch to tell the parent object, if the item still must be drawn
* @param string $headerContent : The content of the item header
* @param string $itemContent : The content of the item itself
* @param array $row : The current data row for this item
*/
public function preProcess(PageLayoutView &$parentObject, &$drawItem, &$headerContent, &$itemContent, array &$row)
{
if ($row['CType']) {
$this->showHidden = (bool)$parentObject->tt_contentConfig['showHidden'];
if ($this->helper->getBackendUser()->uc['hideContentPreview']) {
$itemContent = '';
$drawItem = false;
}
switch ($row['CType']) {
case 'gridelements_pi1':
$drawItem = false;
$itemContent .= $this->renderCTypeGridelements($parentObject, $row);
break;
case 'shortcut':
$drawItem = false;
$itemContent .= $this->renderCTypeShortcut($parentObject, $row);
break;
}
}
$listType = $row['list_type'] && $row['CType'] === 'list' ? ' data-list_type="' . htmlspecialchars($row['list_type']) . '"' : '';
$gridType = $row['tx_gridelements_backend_layout'] && $row['CType'] === 'gridelements_pi1' ? ' data-tx_gridelements_backend_layout="' . htmlspecialchars($row['tx_gridelements_backend_layout']) . '"' : '';
$headerContent = '<div id="element-tt_content-' . (int)$row['uid'] . '" class="t3-ctype-identifier " data-ctype="' . htmlspecialchars($row['CType']) . '" ' . $listType . $gridType . '>' . $headerContent . '</div>';
}
/**
* renders the HTML output for elements of the CType gridelements_pi1
*
* @param PageLayoutView $parentObject : The parent object that triggered this hook
* @param array $row : The current data row for this item
*
* @return string $itemContent: The HTML output for elements of the CType gridelements_pi1
*/
protected function renderCTypeGridelements(PageLayoutView $parentObject, array &$row): string
{
$head = [];
$gridContent = [];
$editUidList = [];
$colPosValues = [];
$singleColumn = false;
// get the layout record for the selected backend layout if any
$gridContainerId = $row['uid'];
if ($row['pid'] < 0) {
$originalRecord = BackendUtility::getRecord('tt_content', $row['t3ver_oid']);
} else {
$originalRecord = $row;
}
/** @var LayoutSetup $layoutSetup */
$layoutSetup = GeneralUtility::makeInstance(LayoutSetup::class)->init($originalRecord['pid']);
$gridElement = $layoutSetup->cacheCurrentParent($gridContainerId, true);
$layoutUid = $gridElement['tx_gridelements_backend_layout'];
$layout = $layoutSetup->getLayoutSetup($layoutUid);
$parserRows = null;
if (isset($layout['config']) && isset($layout['config']['rows.'])) {
$parserRows = $layout['config']['rows.'];
}
// if there is anything to parse, lets check for existing columns in the layout
if (is_array($parserRows) && !empty($parserRows)) {
$this->setMultipleColPosValues($parserRows, $colPosValues, $layout);
} else {
$singleColumn = true;
$this->setSingleColPosItems($parentObject, $colPosValues, $gridElement);
}
// if there are any columns, lets build the content for them
$outerTtContentDataArray = $parentObject->tt_contentData['nextThree'];
if (!empty($colPosValues)) {
$this->renderGridColumns(
$parentObject,
$colPosValues,
$gridContent,
$gridElement,
$editUidList,
$singleColumn,
$head
);
}
$parentObject->tt_contentData['nextThree'] = $outerTtContentDataArray;
// if we got a selected backend layout, we have to create the layout table now
if ($layoutUid && isset($layout['config'])) {
$itemContent = $this->renderGridLayoutTable($layout, $gridElement, $head, $gridContent, $parentObject);
} else {
$itemContent = '<div class="t3-grid-container t3-grid-element-container">';
$itemContent .= '<table border="0" cellspacing="0" cellpadding="0" width="100%" class="t3-page-columns t3-grid-table">';
$itemContent .= '<tr><td valign="top" class="t3-grid-cell t3-page-column t3-page-column-0">' . $gridContent[0] . '</td></tr>';
$itemContent .= '</table></div>';
}
return $itemContent;
}
/**
* Sets column positions based on a selected gridelement layout
*
* @param array $parserRows : The parsed rows of the gridelement layout
* @param array $colPosValues : The column positions that have been found for that layout
* @param array $layout
*/
protected function setMultipleColPosValues(array $parserRows, array &$colPosValues, array $layout)
{
foreach ($parserRows as $parserRow) {
if (is_array($parserRow['columns.']) && !empty($parserRow['columns.'])) {
foreach ($parserRow['columns.'] as $parserColumns) {
$name = $this->languageService->sL($parserColumns['name']);
if (isset($parserColumns['colPos']) && $parserColumns['colPos'] !== '') {
$columnKey = (int)$parserColumns['colPos'];
$colPosValues[$columnKey] = [
'name' => htmlspecialchars($name),
'allowed' => $layout['allowed'][$columnKey],
'disallowed' => $layout['disallowed'][$columnKey],
'maxitems' => (int)$layout['maxitems'][$columnKey],
];
} else {
$colPosValues[32768] = [
'name' => htmlspecialchars($this->languageService->getLL('notAssigned')),
'allowed' => '',
'disallowed' => '*',
'maxitems' => 0,
];
}
}
}
}
}
/**
* Directly returns the items for a single column if the rendering mode is set to single columns only
*
* @param PageLayoutView $parentObject : The parent object that triggered this hook
* @param array $colPosValues : The column positions that have been found for that layout
* @param array $row : The current data row for the container item
*
* @return array collected items for this column
* @throws \Doctrine\DBAL\DBALException
*/
protected function setSingleColPosItems(PageLayoutView $parentObject, array &$colPosValues, array &$row): array
{
$specificIds = $this->helper->getSpecificIds($row);
/** @var ExpressionBuilder $expressionBuilder */
$expressionBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tt_content')
->expr();
$queryBuilder = $parentObject->getQueryBuilder(
'tt_content',
$specificIds['pid'],
[
$expressionBuilder->eq('colPos', -1),
$expressionBuilder->in('tx_gridelements_container', [(int)$row['uid'], $specificIds['uid']]),
]
);
$restrictions = $queryBuilder->getRestrictions();
if ($this->showHidden) {
$restrictions->removeByType(HiddenRestriction::class);
}
$restrictions->removeByType(StartTimeRestriction::class);
$restrictions->removeByType(EndTimeRestriction::class);
$queryBuilder->setRestrictions($restrictions);
$colPosValues[] = [0, ''];
return $parentObject->getResult($queryBuilder->execute());
}
/**
* renders the columns of a grid layout
*
* @param PageLayoutView $parentObject : The parent object that triggered this hook
* @param array $colPosValues : The column positions we want to get the content for
* @param array $gridContent : The rendered content data of the grid columns
* @param array $row : The current data row for the container item
* @param array $editUidList : determines if we will get edit icons or not
* @param bool $singleColumn : Determines if we are in single column mode or not
* @param array $head : An array of headers for each of the columns
*/
protected function renderGridColumns(
PageLayoutView $parentObject,
array &$colPosValues,
array &$gridContent,
array &$row,
array &$editUidList,
bool &$singleColumn,
array &$head
) {
$collectedItems = $this->collectItemsForColumns($parentObject, $colPosValues, $row);
$workspace = $this->helper->getBackendUser()->workspace;
if ($workspace > 0) {
$workspacePreparedItems = [];
$moveUids = [];
foreach ($collectedItems as $item) {
if ($item['t3ver_state'] === 3) {
$moveUids[] = (int)$item['t3ver_move_id'];
$item = BackendUtility::getRecordWSOL('tt_content', (int)$item['uid']);
$movePlaceholder = BackendUtility::getMovePlaceholder(
'tt_content',
(int)$item['uid'],
'*',
$workspace
);
if (!empty($movePlaceholder)) {
$item['sorting'] = $movePlaceholder['sorting'];
$item['tx_gridelements_columns'] = $movePlaceholder['tx_gridelements_columns'];
$item['tx_gridelements_container'] = $movePlaceholder['tx_gridelements_container'];
}
} else {
$item = BackendUtility::getRecordWSOL('tt_content', (int)$item['uid']);
if ($item['t3ver_state'] === 4) {
$movePlaceholder = BackendUtility::getMovePlaceholder(
'tt_content',
(int)$item['uid'],
'*',
$workspace
);
if (!empty($movePlaceholder)) {
$item['sorting'] = $movePlaceholder['sorting'];
$item['tx_gridelements_columns'] = $movePlaceholder['tx_gridelements_columns'];
$item['tx_gridelements_container'] = $movePlaceholder['tx_gridelements_container'];
}
}
}
$workspacePreparedItems[] = $item;
}
$moveUids = array_flip($moveUids);
$collectedItems = $workspacePreparedItems;
foreach ($collectedItems as $key => $item) {
if (isset($moveUids[$item['uid']]) && !$item['_MOVE_PLH']) {
unset($collectedItems[$key]);
}
}
} else {
foreach ($collectedItems as $key => $item) {
$item = BackendUtility::getRecordWSOL('tt_content', (int)$item['uid']);
if ($item['t3ver_state'] > 0) {
unset($collectedItems[$key]);
}
}
}
foreach ($colPosValues as $colPos => $values) {
// first we have to create the column content separately for each column
// so we can check for the first and the last element to provide proper sorting
$counter = 0;
$items = [];
if ($singleColumn === false) {
foreach ($collectedItems as $item) {
if ((int)$item['tx_gridelements_columns'] === $colPos && (int)$item['tx_gridelements_container'] === (int)$row['uid']) {
if (
$row['sys_language_uid'] === $item['sys_language_uid'] ||
($row['sys_language_uid'] === -1 && $item['sys_language_uid'] === 0)
) {
$counter++;
}
$items[] = $item;
}
}
}
usort($items, function ($a, $b) {
if ($a['sorting'] === $b['sorting']) {
return 0;
}
return $a['sorting'] > $b['sorting'] ? 1 : -1;
});
// if there are any items, we can create the HTML for them just like in the original TCEform
$gridContent['numberOfItems'][$colPos] = $counter;
$this->renderSingleGridColumn($parentObject, $items, $colPos, $values, $gridContent, $row, $editUidList);
// we will need a header for each of the columns to activate mass editing for elements of that column
$expanded = !$this->helper->getBackendUser()->uc['moduleData']['page']['gridelementsCollapsedColumns'][$row['uid'] . '_' . $colPos];
$this->setColumnHeader($parentObject, $head, $colPos, $values['name'], $editUidList, $expanded);
}
}
/**
* Collects tt_content data from a single tt_content element
*
* @param PageLayoutView $parentObject : The paren object that triggered this hook
* @param array $colPosValues : The column position to collect the items for
* @param array $row : The current data row for the container item
*
* @return mixed[] collected items for the given column
* @throws \Doctrine\DBAL\DBALException
*/
protected function collectItemsForColumns(PageLayoutView $parentObject, array &$colPosValues, array &$row)
{
$colPosList = array_keys($colPosValues);
$specificIds = $this->helper->getSpecificIds($row);
$queryBuilder = $this->getQueryBuilder();
$constraints = [
$queryBuilder->expr()->in(
'pid',
$queryBuilder->createNamedParameter(
[(int)$row['pid'], $specificIds['pid']],
Connection::PARAM_INT_ARRAY
)
),
$queryBuilder->expr()->eq('colPos', $queryBuilder->createNamedParameter(-1, PDO::PARAM_INT)),
$queryBuilder->expr()->notIn(
'uid',
$queryBuilder->createNamedParameter(
[(int)$row['uid'], $specificIds['uid']],
Connection::PARAM_INT_ARRAY
)
),
$queryBuilder->expr()->in(
'tx_gridelements_container',
$queryBuilder->createNamedParameter(
[(int)$row['uid'], $specificIds['uid']],
Connection::PARAM_INT_ARRAY
)
),
$queryBuilder->expr()->in(
'tx_gridelements_columns',
$queryBuilder->createNamedParameter($colPosList, Connection::PARAM_INT_ARRAY)
),
];
if (!$parentObject->tt_contentConfig['languageMode']) {
$constraints[] = $queryBuilder->expr()->orX(
$queryBuilder->expr()->eq('sys_language_uid', $queryBuilder->createNamedParameter(-1, PDO::PARAM_INT)),
$queryBuilder->expr()->eq(
'sys_language_uid',
$queryBuilder->createNamedParameter(
(int)$parentObject->tt_contentConfig['sys_language_uid'],
PDO::PARAM_INT
)
)
);
} elseif ($row['sys_language_uid'] > 0) {
$constraints[] = $queryBuilder->expr()->eq(
'sys_language_uid',
$queryBuilder->createNamedParameter((int)$row['sys_language_uid'], PDO::PARAM_INT)
);
}
$queryBuilder
->select('*')
->from('tt_content')
->where(
...$constraints
)
->orderBy('sorting');
$restrictions = $queryBuilder->getRestrictions();
if ($this->showHidden) {
$restrictions->removeByType(HiddenRestriction::class);
}
$restrictions->removeByType(StartTimeRestriction::class);
$restrictions->removeByType(EndTimeRestriction::class);
$workspaceRestriction = GeneralUtility::makeInstance(
WorkspaceRestriction::class,
$this->helper->getBackendUser()->workspace
);
$restrictions->add($workspaceRestriction);
$queryBuilder->setRestrictions($restrictions);
return $queryBuilder->execute()->fetchAll();
}
/**
* getter for queryBuilder
*
* @return QueryBuilder
*/
public function getQueryBuilder(): QueryBuilder
{
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tt_content');
return $queryBuilder;
}
/**
* renders a single column of a grid layout and sets the edit uid list
*
* @param PageLayoutView $parentObject : The parent object that triggered this hook
* @param array $items : The content data of the column to be rendered
* @param int $colPos : The column position we want to get the content for
* @param array $values : The layout configuration values for the grid column
* @param array $gridContent : The rendered content data of the grid column
* @param array $row
* @param array $editUidList : determines if we will get edit icons or not
*/
protected function renderSingleGridColumn(
PageLayoutView $parentObject,
array &$items,
int &$colPos,
array $values,
array &$gridContent,
array $row,
array &$editUidList
) {
$specificIds = $this->helper->getSpecificIds($row);
$allowed = base64_encode(json_encode($values['allowed']));
$disallowed = base64_encode(json_encode($values['disallowed']));
$maxItems = (int)$values['maxitems'];
$url = '';
$pageinfo = BackendUtility::readPageAccess($parentObject->id, '');
$contentIsNotLockedForEditors = $this->contentIsNotLockedForEditors($parentObject->id);
if ($colPos < 32768) {
try {
if ($contentIsNotLockedForEditors
&& $this->getBackendUser()->doesUserHaveAccess($pageinfo, Permission::CONTENT_EDIT)
&& (!$this->checkIfTranslationsExistInLanguage($items, $row['sys_language_uid'], $parentObject))
) {
if ($parentObject->option_newWizard) {
$urlParameters = [
'id' => $parentObject->id,
'sys_language_uid' => $row['sys_language_uid'],
'tx_gridelements_allowed' => $allowed,
'tx_gridelements_disallowed' => $disallowed,
'tx_gridelements_container' => $specificIds['uid'],
'tx_gridelements_columns' => $colPos,
'colPos' => -1,
'uid_pid' => $parentObject->id,
'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI'),
];
$routeName = BackendUtility::getPagesTSconfig($parentObject->id)['mod.']['newContentElementWizard.']['override']
?? 'new_content_element_wizard';
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
try {
$url = (string)$uriBuilder->buildUriFromRoute($routeName, $urlParameters);
} catch (RouteNotFoundException $e) {
}
} else {
$urlParameters = [
'edit' => [
'tt_content' => [
$parentObject->id => 'new',
],
],
'defVals' => [
'tt_content' => [
'sys_language_uid' => $row['sys_language_uid'],
'tx_gridelements_allowed' => $allowed,
'tx_gridelements_disallowed' => $disallowed,
'tx_gridelements_container' => $specificIds['uid'],
'tx_gridelements_columns' => $colPos,
'colPos' => -1,
],
],
'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI'),
];
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
try {
$url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
} catch (RouteNotFoundException $e) {
}
}
}
} catch (Exception $e) {
}
}
$iconsArray = [];
if ((string)$colPos !== '' && $colPos < 32768 && $url) {
$iconsArray = [
'new' => '<a
href="' . htmlspecialchars($url) . '"
data-title="' . htmlspecialchars($this->getLanguageService()->getLL('newContentElement')) . '"
title="' . htmlspecialchars($this->getLanguageService()->getLL('newContentElement')) . '"
class="btn btn-default btn-sm t3js-toggle-new-content-element-wizard">' .
$this->iconFactory->getIcon('actions-add', 'small') . ' ' .
$this->languageService->getLL('content') .
'</a>',
];
}
$gridContent[$colPos] .= '<div class="t3-page-ce gridelements-collapsed-column-marker">' .
$this->languageService->sL('LLL:EXT:gridelements/Resources/Private/Language/locallang_db.xlf:tx_gridelements_contentcollapsed') .
'</div>';
$gridContent[$colPos] .= '
<div data-colpos="' . htmlspecialchars((string)$colPos) . '"
data-language-uid="' . (int)$row['sys_language_uid'] . '"
class="t3js-sortable t3js-sortable-lang t3js-sortable-lang-' . (int)$row['sys_language_uid'] . ' t3-page-ce-wrapper ui-sortable">
<div class="t3-page-ce t3js-page-ce"
data-container="' . (int)$row['uid'] . '"
id="' . str_replace('.', '', uniqid('', true)) . '">
<div class="t3js-page-new-ce t3js-page-new-ce-allowed t3-page-ce-wrapper-new-ce btn-group btn-group-sm"
id="colpos-' . htmlspecialchars((string)$colPos) . '-' . str_replace('.', '', uniqid('', true)) . '">' .
implode('', $iconsArray) . '
</div>
<div class="t3-page-ce-dropzone-available t3js-page-ce-dropzone-available"></div>
</div>';
if (!empty($items)) {
$counter = 0;
foreach ($items as $item) {
if (
$row['sys_language_uid'] === $item['sys_language_uid'] ||
($row['sys_language_uid'] === -1 && $item['sys_language_uid'] === 0)
) {
$counter++;
}
if ((int)$item['t3ver_state'] === VersionState::DELETE_PLACEHOLDER) {
continue;
}
if (is_array($item)) {
$uid = (int)$item['uid'];
$pid = (int)$item['pid'];
$container = (int)$item['tx_gridelements_container'];
$gridColumn = (int)$item['tx_gridelements_columns'];
$language = (int)$item['sys_language_uid'];
$statusHidden = $parentObject->isDisabled('tt_content', $item) ? ' t3-page-ce-hidden' : '';
$maxItemsReached = $counter > $maxItems && $maxItems > 0 ? ' t3-page-ce-danger' : '';
$highlightHeader = '';
try {
if ($this->checkIfTranslationsExistInLanguage(
[],
(int)$item['sys_language_uid'],
$parentObject
) && (int)$item['l18n_parent'] === 0) {
$highlightHeader = ' t3-page-ce-danger';
}
} catch (Exception $e) {
}
$gridContent[$colPos] .= '
<div class="t3-page-ce t3js-page-ce t3js-page-ce-sortable' . $statusHidden . $maxItemsReached . $highlightHeader . '"
data-table="tt_content" id="element-tt_content-' . $uid . '"
data-uid="' . $uid . '"
data-container="' . $container . '"
data-ctype="' . htmlspecialchars($item['CType']) . '">' .
$this->renderSingleElementHTML($parentObject, $item) .
'</div>';
try {
if ($contentIsNotLockedForEditors
&& $this->getBackendUser()->doesUserHaveAccess($pageinfo, Permission::CONTENT_EDIT)
&& (!$this->checkIfTranslationsExistInLanguage(
$items,
$row['sys_language_uid'],
$parentObject
))
) {
// New content element:
$specificIds = $this->helper->getSpecificIds($item);
if ($parentObject->option_newWizard) {
$urlParameters = [
'id' => $parentObject->id,
'sys_language_uid' => $language,
'tx_gridelements_allowed' => $allowed,
'tx_gridelements_disallowed' => $disallowed,
'tx_gridelements_container' => $container,
'tx_gridelements_columns' => $gridColumn,
'colPos' => -1,
'uid_pid' => -$specificIds['uid'],
'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI'),
];
$routeName = BackendUtility::getPagesTSconfig($pid)['mod.']['newContentElementWizard.']['override']
?? 'new_content_element_wizard';
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
try {
$url = (string)$uriBuilder->buildUriFromRoute($routeName, $urlParameters);
} catch (RouteNotFoundException $e) {
}
} else {
$urlParameters = [
'edit' => [
'tt_content' => [
-$specificIds['uid'] => 'new',
],
],
'defVals' => [
'tt_content' => [
'sys_language_uid' => $language,
'tx_gridelements_allowed' => $allowed,
'tx_gridelements_disallowed' => $disallowed,
'tx_gridelements_container' => $container,
'tx_gridelements_columns' => $gridColumn,
'colPos' => -1,
],
],
'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI'),
];
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
try {
$url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
} catch (RouteNotFoundException $e) {
}
}
$iconsArray = [
'new' => '<a
href="' . htmlspecialchars($url) . '"
data-title="' . htmlspecialchars($this->getLanguageService()->getLL('newContentElement')) . '"
title="' . htmlspecialchars($this->getLanguageService()->getLL('newContentElement')) . '"
class="btn btn-default btn-sm btn t3js-toggle-new-content-element-wizard">' .
$this->iconFactory->getIcon('actions-add', 'small') . ' ' .
$this->languageService->getLL('content') .
'</a>',
];
}
} catch (Exception $e) {
}
$gridContent[$colPos] .= '
<div class="t3-page-ce">
<div class="t3js-page-new-ce t3js-page-new-ce-allowed t3-page-ce-wrapper-new-ce btn-group btn-group-sm"
id="colpos-' . $gridColumn .
'-page-' . $pid .
'-gridcontainer-' . $container .
'-' . str_replace('.', '', uniqid('', true)) . '">' .
implode('', $iconsArray) . '
</div>
</div>
<div class="t3-page-ce-dropzone-available t3js-page-ce-dropzone-available"></div>
</div>
';
$editUidList[$colPos] .= $editUidList[$colPos] ? ',' . $uid : $uid;
}
}
}
$gridContent[$colPos] .= '</div>';
}
/**
* Check if content can be edited by current user
*
* @param int $id
* @return bool
*/
protected function contentIsNotLockedForEditors(int $id): bool
{
if (!empty($this->getPageLayoutController()) && get_class($this->getPageLayoutController()) === PageLayoutController::class) {
$perms_clause = $this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW);
$pageinfo = BackendUtility::readPageAccess($id, $perms_clause);
return $this->isContentEditable($pageinfo);
}
return true;
}
/**
* @return PageLayoutController
*/
public function getPageLayoutController(): PageLayoutController
{
return $GLOBALS['SOBE'];
}
/**
* Check if content can be edited by current user
*
* @param array $pageinfo
* @return bool
*/
protected function isContentEditable(array $pageinfo): bool
{
if ($this->getBackendUser()->isAdmin()) {
return true;
}
return !$pageinfo['editlock'] && $this->getBackendUser()->doesUserHaveAccess(
$pageinfo,
Permission::CONTENT_EDIT
);
}
/**
* Checks whether translated Content Elements exist in the desired language
* If so, deny creating new ones via the UI
*
* @param array $contentElements
* @param int $language
* @param PageLayoutView $parentObject
*
* @return bool
* @throws Exception
*/
protected function checkIfTranslationsExistInLanguage(
array $contentElements,
int $language,
PageLayoutView $parentObject
): bool {
// If in default language, you may always create new entries
// Also, you may override this strict behavior via user TS Config
// If you do so, you're on your own and cannot rely on any support by the TYPO3 core
// We jump out here since we don't need to do the expensive loop operations
$allowInconsistentLanguageHandling = (bool)BackendUtility::getPagesTSconfig($parentObject->id)['mod.']['web_layout.']['allowInconsistentLanguageHandling'];
if ($language === 0 || $language === -1 || $allowInconsistentLanguageHandling === true) {
return false;
}
/**
* Build up caches
*/
if (!isset($this->languageHasTranslationsCache[$language])) {
foreach ($contentElements as $contentElement) {
if ((int)$contentElement['l18n_parent'] === 0) {
$this->languageHasTranslationsCache[$language]['hasStandAloneContent'] = true;
}
if ((int)$contentElement['l18n_parent'] > 0) {
$this->languageHasTranslationsCache[$language]['hasTranslations'] = true;
}
}
// Check whether we have a mix of both
if ($this->languageHasTranslationsCache[$language]['hasStandAloneContent']
&& $this->languageHasTranslationsCache[$language]['hasTranslations']
) {
/** @var FlashMessage $message */
$message = GeneralUtility::makeInstance(
FlashMessage::class,
sprintf(
$this->getLanguageService()->getLL('staleTranslationWarning'),
''
// $parentObject->languageIconTitles[$language]['title']
),
sprintf(
$this->getLanguageService()->getLL('staleTranslationWarningTitle'),
''
// $parentObject->languageIconTitles[$language]['title']
),
FlashMessage::WARNING
);
$service = GeneralUtility::makeInstance(FlashMessageService::class);
/** @var FlashMessageQueue $queue */
$queue = $service->getMessageQueueByIdentifier();
$queue->enqueue($message);
}
}
if ($this->languageHasTranslationsCache[$language]['hasTranslations']) {
return true;
}
return false;
}
/**
* getter for LanguageService
*
* @return LanguageService $languageService
*/
public function getLanguageService(): LanguageService
{
return $this->languageService;
}
/**
* setter for LanguageService object
*
* @param LanguageService $languageService
*/
public function setLanguageService(LanguageService $languageService)
{
$this->languageService = $languageService;
}
/**
* Renders the HTML code for a single tt_content element
*
* @param PageLayoutView $parentObject : The parent object that triggered this hook
* @param array $item : The data row to be rendered as HTML
*
* @return string
*/
protected function renderSingleElementHTML(PageLayoutView $parentObject, array $item): string
{
$singleElementHTML = '';
$unset = false;
if (!isset($parentObject->tt_contentData['nextThree'][$item['uid']])) {
$unset = true;
$parentObject->tt_contentData['nextThree'][$item['uid']] = $item['uid'];
}
if (!$parentObject->tt_contentConfig['languageMode']) {
$singleElementHTML .= '<div class="t3-page-ce-dragitem" id="' . StringUtility::getUniqueId() . '">';
}
$singleElementHTML .= $parentObject->tt_content_drawHeader(
$item,
$parentObject->tt_contentConfig['showInfo'] ? 15 : 5,
$parentObject->defLangBinding,
true,
true
);
$singleElementHTML .= (!empty($item['_ORIG_uid']) ? '<div class="ver-element">' : '')
. '<div class="t3-page-ce-body-inner t3-page-ce-body-inner-' . htmlspecialchars($item['CType']) . '">'
. $parentObject->tt_content_drawItem($item)
. '</div>'
. (!empty($item['_ORIG_uid']) ? '</div>' : '');
$singleElementHTML .= $this->tt_content_drawFooter($parentObject, $item);
if (!$parentObject->tt_contentConfig['languageMode']) {
$singleElementHTML .= '</div>';
}
if ($unset) {
unset($parentObject->tt_contentData['nextThree'][$item['uid']]);
}
return $singleElementHTML;
}
/**
* Draw the footer for a single tt_content element
*
* @param PageLayoutView $parentObject : The parent object that triggered this hook
* @param array $row Record array
* @return string HTML of the footer
* @throws UnexpectedValueException
*/
protected function tt_content_drawFooter(PageLayoutView $parentObject, array $row): string
{
$content = '';
// Get processed values:
$info = [];
$parentObject->getProcessedValue(
'tt_content',
'starttime,endtime,fe_group,space_before_class,space_after_class',
$row,
$info
);
// Content element annotation
if (!empty($GLOBALS['TCA']['tt_content']['ctrl']['descriptionColumn']) && !empty($row[$GLOBALS['TCA']['tt_content']['ctrl']['descriptionColumn']])) {
$info[] = htmlspecialchars($row[$GLOBALS['TCA']['tt_content']['ctrl']['descriptionColumn']]);
}
// Call drawFooter hooks
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['tt_content_drawFooter'] ?? [] as $className) {
$hookObject = GeneralUtility::makeInstance($className);
if (!$hookObject instanceof PageLayoutViewDrawFooterHookInterface) {
throw new UnexpectedValueException($className . ' must implement interface ' . PageLayoutViewDrawFooterHookInterface::class, 1404378171);
}
$hookObject->preProcess($parentObject, $info, $row);
}
// Display info from records fields:
if (!empty($info)) {
$content = '<div class="t3-page-ce-info">
' . implode('<br>', $info) . '
</div>';
}
// Wrap it
if (!empty($content)) {
$content = '<div class="t3-page-ce-footer">' . $content . '</div>';
}
return $content;
}
/**
* Sets the headers for a grid before content and headers are put together
*
* @param PageLayoutView $parentObject : The parent object that triggered this hook
* @param array $head : The collected item data rows
* @param int $colPos : The column position we want to get a header for
* @param string $name : The name of the header
* @param array $editUidList : determines if we will get edit icons or not
* @param bool $expanded
*
* @internal param array $row : The current data row for the container item
*/