-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathTcaColPosItems.php
More file actions
132 lines (113 loc) · 5.01 KB
/
TcaColPosItems.php
File metadata and controls
132 lines (113 loc) · 5.01 KB
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
<?php
declare(strict_types=1);
namespace IchHabRecht\ContentDefender\Form\FormDataProvider;
/*
* This file is part of the TYPO3 extension content_defender.
*
* (c) Nicole Cordes <typo3@cordes.co>
*
* 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 file that was distributed with this source code.
*/
use IchHabRecht\ContentDefender\BackendLayout\BackendLayoutConfiguration;
use IchHabRecht\ContentDefender\Form\Exception\AccessDeniedColPosException;
use IchHabRecht\ContentDefender\Repository\ContentRepository;
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class TcaColPosItems implements FormDataProviderInterface
{
/**
* @var ContentRepository
*/
protected $contentRepository;
/**
* @param ContentRepository $contentRepository
*/
public function __construct(ContentRepository $contentRepository = null)
{
$this->contentRepository = $contentRepository ?? GeneralUtility::makeInstance(ContentRepository::class);
}
/**
* @param array $result
* @throws AccessDeniedColPosException
* @return array
*/
public function addData(array $result)
{
// colPos items (e.g. from backend layout) are processed by an itemsProcFunc only
// This function only gets called for config type IN (checkbox, radio, select)
if ('tt_content' !== $result['tableName']
|| empty($result['processedTca']['columns']['colPos']['config']['type'])
|| !in_array($result['processedTca']['columns']['colPos']['config']['type'], ['checkbox', 'radio', 'select'], true)
) {
return $result;
}
$pageId = !empty($result['effectivePid']) ? (int)$result['effectivePid'] : (int)$result['databaseRow']['pid'];
$backendLayoutConfiguration = BackendLayoutConfiguration::createFromPageId($pageId);
$record = $result['databaseRow'];
$record['pid'] = $pageId;
foreach ($result['processedTca']['columns']['colPos']['config']['items'] as $key => $item) {
$colPos = (int)($item['value'] ?? $item[1]);
$columnConfiguration = $backendLayoutConfiguration->getConfigurationByColPos($colPos, $record['uid']);
if (empty($columnConfiguration)) {
continue;
}
$record['colPos'] = $colPos;
$allowedConfiguration = array_intersect_key($columnConfiguration['allowed.'] ?? [], $result['processedTca']['columns']);
foreach ($allowedConfiguration as $field => $value) {
if (!isset($record[$field])) {
continue;
}
$allowedValues = GeneralUtility::trimExplode(',', $value);
if ($this->fieldContainsDisallowedValues($record[$field], $allowedValues)) {
unset($result['processedTca']['columns']['colPos']['config']['items'][$key]);
}
}
$disallowedConfiguration = array_intersect_key($columnConfiguration['disallowed.'] ?? [], $result['processedTca']['columns']);
foreach ($disallowedConfiguration as $field => $value) {
if (!isset($record[$field])) {
continue;
}
$disallowedValues = GeneralUtility::trimExplode(',', $value);
if ($this->fieldContainsDisallowedValues($record[$field], $disallowedValues, false)) {
unset($result['processedTca']['columns']['colPos']['config']['items'][$key]);
}
}
if (!empty($columnConfiguration['maxitems'])
&& $columnConfiguration['maxitems'] <= $this->contentRepository->countColPosByRecord($record)
) {
$isCurrentColPos = $colPos === (int)$result['databaseRow']['colPos'][0];
if ($isCurrentColPos && !$this->contentRepository->isRecordInColPos($record)) {
throw new AccessDeniedColPosException(
'Maximum number of allowed content elements (' . $columnConfiguration['maxitems'] . ') reached.',
1494605357
);
} elseif (!$isCurrentColPos) {
unset($result['processedTca']['columns']['colPos']['config']['items'][$key]);
}
}
}
return $result;
}
/**
* @param string|array $fieldValue
* @param array $values
* @param bool $allowed
* @return bool
*/
protected function fieldContainsDisallowedValues($fieldValue, array $values, $allowed = true)
{
foreach ((array)$fieldValue as $item) {
if (($allowed && !in_array($item, $values, true))
|| (!$allowed && in_array($item, $values, true))
) {
return true;
}
}
return false;
}
}