2 namespace TYPO3\CMS\Core\DataHandling\Localization
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Core\Utility\GeneralUtility
;
18 use TYPO3\CMS\Core\Utility\MathUtility
;
21 * Entity for data-map item.
25 const TYPE_PARENT
= 'parent';
26 const TYPE_DIRECT_CHILD
= 'directChild';
27 const TYPE_GRAND_CHILD
= 'grandChild';
29 const SCOPE_PARENT
= State
::STATE_PARENT
;
30 const SCOPE_SOURCE
= State
::STATE_SOURCE
;
31 const SCOPE_EXCLUDE
= 'exclude';
46 protected $suggestedValues;
51 protected $persistedValues;
56 protected $configurationFieldNames;
89 * @var DataMapItem[][]
91 protected $dependencies = [];
94 * Builds a data-map item. In addition to the constructor, the values
95 * for language, parent and source record pointers are assigned as well.
97 * @param string $tableName
98 * @param string|int $id
99 * @param array $suggestedValues
100 * @param array $persistedValues
101 * @param array $configurationFieldNames
102 * @return object|DataMapItem
104 public static function build(
107 array $suggestedValues,
108 array $persistedValues,
109 array $configurationFieldNames
111 $item = GeneralUtility
::makeInstance(
117 $configurationFieldNames
120 $item->language
= (int)($suggestedValues[$item->getLanguageFieldName()] ??
$persistedValues[$item->getLanguageFieldName()]);
121 $item->setParent($suggestedValues[$item->getParentFieldName()] ??
$persistedValues[$item->getParentFieldName()]);
122 if ($item->getSourceFieldName() !== null) {
123 $item->setSource($suggestedValues[$item->getSourceFieldName()] ??
$persistedValues[$item->getSourceFieldName()]);
130 * @param string $tableName
131 * @param string|int $id
132 * @param array $suggestedValues
133 * @param array $persistedValues
134 * @param array $configurationFieldNames
136 public function __construct(
139 array $suggestedValues,
140 array $persistedValues,
141 array $configurationFieldNames
143 $this->tableName
= $tableName;
146 $this->suggestedValues
= $suggestedValues;
147 $this->persistedValues
= $persistedValues;
148 $this->configurationFieldNames
= $configurationFieldNames;
150 $this->new = !MathUtility
::canBeInterpretedAsInteger($id);
154 * Gets the current table name of this data-map item.
158 public function getTableName(): string
160 return $this->tableName
;
164 * Gets the table name used to resolve the language parent record.
168 public function getFromTableName(): string
170 if ($this->tableName
=== 'pages_language_overlay') {
173 return $this->tableName
;
177 * Gets the table name used to resolve any kind of translations.
181 public function getForTableName(): string
183 if ($this->tableName
=== 'pages') {
184 return 'pages_language_overlay';
186 return $this->tableName
;
190 * Gets the id of this data-map item.
194 public function getId()
200 * Gets the suggested values that were initially
201 * submitted as the whole data-map to the DataHandler.
205 public function getSuggestedValues(): array
207 return $this->suggestedValues
;
211 * Gets the persisted values that represent the persisted state
212 * of the record this data-map item is a surrogate for - does only
213 * contain relevant field values.
217 public function getPersistedValues(): array
219 return $this->persistedValues
;
225 public function getConfigurationFieldNames(): array
227 return $this->configurationFieldNames
;
233 public function getLanguageFieldName(): string
235 return $this->configurationFieldNames
['language'];
241 public function getParentFieldName(): string
243 return $this->configurationFieldNames
['parent'];
247 * @return null|string
249 public function getSourceFieldName()
251 return $this->configurationFieldNames
['source'];
257 public function isNew(): bool
265 public function getType(): string
267 if ($this->type
=== null) {
268 // implicit: default language, it's a parent
269 if ($this->language
=== 0) {
270 $this->type
= static::TYPE_PARENT
;
271 // implicit: having source value different to parent value, it's a 2nd or higher level translation
273 $this->source
!== null
274 && $this->source
!== $this->parent
276 $this->type
= static::TYPE_GRAND_CHILD
;
277 // implicit: otherwise, it's a 1st level translation
279 $this->type
= static::TYPE_DIRECT_CHILD
;
288 public function isParentType(): bool
290 return $this->getType() === static::TYPE_PARENT
;
296 public function isDirectChildType(): bool
298 return $this->getType() === static::TYPE_DIRECT_CHILD
;
304 public function isGrandChildType(): bool
306 return $this->getType() === static::TYPE_GRAND_CHILD
;
312 public function getState(): State
314 if ($this->state
=== null && !$this->isParentType()) {
315 $this->state
= $this->buildState();
323 public function getLanguage()
325 return $this->language
;
329 * @param string|int $language
331 public function setLanguage($language)
333 $this->language
= $language;
339 public function getParent()
341 return $this->parent
;
345 * @param string|int $parent
347 public function setParent($parent)
349 $this->parent
= $parent;
355 public function getSource()
357 return $this->source
;
361 * @param string|int $source
363 public function setSource($source)
365 $this->source
= $source;
369 * @param string $scope
372 public function getIdForScope($scope)
375 $scope === static::SCOPE_PARENT
376 ||
$scope === static::SCOPE_EXCLUDE
378 return $this->getParent();
380 if ($scope === static::SCOPE_SOURCE
) {
381 return $this->getSource();
383 throw new \
RuntimeException('Invalid scope', 1486325248);
387 * @return DataMapItem[][]
389 public function getDependencies(): array
391 return $this->dependencies
;
395 * @param DataMapItem[][] $dependencies
397 public function setDependencies(array $dependencies)
399 $this->dependencies
= $dependencies;
403 * @param string $scope
404 * @return DataMapItem[]
406 public function findDependencies(string $scope)
408 return $this->dependencies
[$scope] ??
[];
414 public function getApplicableScopes()
417 if (!empty($this->getSourceFieldName())) {
418 $scopes[] = static::SCOPE_SOURCE
;
420 $scopes[] = static::SCOPE_PARENT
;
421 $scopes[] = static::SCOPE_EXCLUDE
;
428 protected function buildState()
430 // build from persisted states
431 if (!$this->isNew()) {
432 $state = State
::fromJSON(
434 $this->persistedValues
['l10n_state'] ??
null
436 // use provided states for a new and copied element
437 } elseif (is_string($this->suggestedValues
['l10n_state'] ??
null)) {
438 $state = State
::fromJSON(
440 $this->suggestedValues
['l10n_state']
442 // provide the default states
444 $state = State
::create($this->tableName
);
446 // switch "custom" to "source" state for 2nd level translations
447 if ($this->isNew() && $this->isGrandChildType()) {
448 $state->updateStates(State
::STATE_CUSTOM
, State
::STATE_SOURCE
);
450 // apply any provided updates to the states
451 if (is_array($this->suggestedValues
['l10n_state'] ??
null)) {
453 $this->suggestedValues
['l10n_state'] ??
[]