‪TYPO3CMS  ‪main
DataMapItem.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 
26 {
27  public const ‪TYPE_PARENT = 'parent';
28  public const ‪TYPE_DIRECT_CHILD = 'directChild';
29  public const ‪TYPE_GRAND_CHILD = 'grandChild';
30 
33  public const ‪SCOPE_EXCLUDE = 'exclude';
34 
38  protected ‪$tableName;
39 
43  protected ‪$id;
44 
48  protected ‪$suggestedValues;
49 
54 
59 
63  protected ‪$new;
64 
68  protected ‪$type;
69 
73  protected ‪$state;
74 
78  protected ‪$language;
79 
83  protected ‪$parent;
84 
88  protected ‪$source;
89 
93  protected ‪$dependencies = [];
94 
102  public static function ‪build(
103  string ‪$tableName,
104  ‪$id,
105  array ‪$suggestedValues,
106  array ‪$persistedValues,
108  ) {
109  $item = GeneralUtility::makeInstance(
110  static::class,
112  ‪$id,
116  );
117 
118  $item->language = (int)(‪$suggestedValues[$item->getLanguageFieldName()] ?? ‪$persistedValues[$item->getLanguageFieldName()] ?? 0);
119  $item->setParent(‪$suggestedValues[$item->getParentFieldName()] ?? ‪$persistedValues[$item->getParentFieldName()] ?? '');
120  if ($item->getSourceFieldName() !== null) {
121  $item->setSource(‪$suggestedValues[$item->getSourceFieldName()] ?? ‪$persistedValues[$item->getSourceFieldName()] ?? '');
122  }
123 
124  return $item;
125  }
126 
130  public function ‪__construct(
131  string ‪$tableName,
132  ‪$id,
133  array ‪$suggestedValues,
134  array ‪$persistedValues,
136  ) {
137  $this->tableName = ‪$tableName;
138  $this->id = ‪$id;
139 
140  $this->suggestedValues = ‪$suggestedValues;
141  $this->persistedValues = ‪$persistedValues;
142  $this->configurationFieldNames = ‪$configurationFieldNames;
143 
145  }
146 
150  public function ‪getTableName(): string
151  {
152  return ‪$this->tableName;
153  }
154 
160  public function ‪getId()
161  {
162  return ‪$this->id;
163  }
164 
169  public function ‪getSuggestedValues(): array
170  {
172  }
173 
179  public function ‪getPersistedValues(): array
180  {
182  }
183 
184  public function ‪getConfigurationFieldNames(): array
185  {
187  }
188 
189  public function ‪getLanguageFieldName(): string
190  {
191  return $this->configurationFieldNames['language'];
192  }
193 
194  public function ‪getParentFieldName(): string
195  {
196  return $this->configurationFieldNames['parent'];
197  }
198 
202  public function ‪getSourceFieldName()
203  {
204  return $this->configurationFieldNames['source'] ?? null;
205  }
206 
207  public function ‪isNew(): bool
208  {
209  return ‪$this->new;
210  }
211 
212  public function ‪getType(): string
213  {
214  if ($this->type === null) {
215  // implicit: default language, it's a parent
216  if ($this->language === 0) {
217  $this->type = static::TYPE_PARENT;
218  } elseif (
219  // implicit: having source value different to parent value, it's a 2nd or higher level translation
220  $this->source !== 0
221  && $this->source !== null
222  && $this->source !== $this->parent
223  ) {
224  $this->type = static::TYPE_GRAND_CHILD;
225  } else {
226  // implicit: otherwise, it's a 1st level translation
227  $this->type = static::TYPE_DIRECT_CHILD;
228  }
229  }
230  return ‪$this->type;
231  }
232 
233  public function ‪isParentType(): bool
234  {
235  return $this->‪getType() === static::TYPE_PARENT;
236  }
237 
238  public function ‪isDirectChildType(): bool
239  {
240  return $this->‪getType() === static::TYPE_DIRECT_CHILD;
241  }
242 
243  public function ‪isGrandChildType(): bool
244  {
245  return $this->‪getType() === static::TYPE_GRAND_CHILD;
246  }
247 
248  public function ‪getState(): ?‪State
249  {
250  if ($this->state === null && !$this->‪isParentType()) {
251  $this->state = $this->‪buildState();
252  }
253  return ‪$this->state;
254  }
255 
259  public function ‪getLanguage()
260  {
261  return ‪$this->language;
262  }
263 
267  public function ‪setLanguage(‪$language)
268  {
269  $this->language = ‪$language;
270  }
271 
275  public function ‪getParent()
276  {
277  return ‪$this->parent;
278  }
279 
283  public function ‪setParent(‪$parent)
284  {
285  $this->parent = $this->‪extractId(‪$parent);
286  }
287 
291  public function ‪getSource()
292  {
293  return ‪$this->source;
294  }
295 
299  public function ‪setSource(‪$source)
300  {
301  $this->source = $this->‪extractId(‪$source);
302  }
303 
308  public function ‪getIdForScope(string $scope)
309  {
310  if (
311  $scope === static::SCOPE_PARENT
312  || $scope === static::SCOPE_EXCLUDE
313  ) {
314  return $this->‪getParent();
315  }
316  if ($scope === static::SCOPE_SOURCE) {
317  return $this->‪getSource();
318  }
319  throw new \RuntimeException('Invalid scope', 1486325248);
320  }
321 
325  public function ‪getDependencies(): array
326  {
327  return ‪$this->dependencies;
328  }
329 
333  public function ‪setDependencies(array ‪$dependencies)
334  {
335  $this->dependencies = ‪$dependencies;
336  }
337 
341  public function ‪findDependencies(string $scope): array
342  {
343  return $this->dependencies[$scope] ?? [];
344  }
345 
349  public function ‪getApplicableScopes(): array
350  {
351  $scopes = [];
352  if (!empty($this->‪getSourceFieldName())) {
353  $scopes[] = static::SCOPE_SOURCE;
354  }
355  $scopes[] = static::SCOPE_PARENT;
356  $scopes[] = static::SCOPE_EXCLUDE;
357  return $scopes;
358  }
359 
364  protected function ‪extractId(int|string $idValue): int|string
365  {
367  return $idValue;
368  }
369  $idValue = (string)$idValue;
370  if (str_starts_with($idValue, 'NEW')) {
371  return $idValue;
372  }
373  // @todo Handle if $tableName does not match $this->tableName
374  ‪$id = BackendUtility::splitTable_Uid($idValue)[1];
375  return ‪$id;
376  }
377 
378  protected function ‪buildState(): ?State
379  {
380  // build from persisted states
381  if (!$this->‪isNew()) {
383  $this->tableName,
384  $this->persistedValues['l10n_state'] ?? null
385  );
386  } elseif (is_string($this->suggestedValues['l10n_state'] ?? null)) {
387  // use provided states for a new and copied element
389  $this->tableName,
390  $this->suggestedValues['l10n_state']
391  );
392  } else {
393  // provide the default states
394  ‪$state = ‪State::create($this->tableName);
395  }
396  // switch "custom" to "source" state for 2nd level translations
397  if ($this->‪isNew() && $this->‪isGrandChildType()) {
399  }
400  // apply any provided updates to the states
401  if (is_array($this->suggestedValues['l10n_state'] ?? null)) {
402  ‪$state->‪update($this->suggestedValues['l10n_state']);
403  }
404  return ‪$state;
405  }
406 }
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$parent
‪string int $parent
Definition: DataMapItem.php:73
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\TYPE_PARENT
‪const TYPE_PARENT
Definition: DataMapItem.php:27
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getType
‪getType()
Definition: DataMapItem.php:200
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getPersistedValues
‪getPersistedValues()
Definition: DataMapItem.php:167
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\extractId
‪extractId(int|string $idValue)
Definition: DataMapItem.php:352
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getParentFieldName
‪getParentFieldName()
Definition: DataMapItem.php:182
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$new
‪bool $new
Definition: DataMapItem.php:57
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\isParentType
‪isParentType()
Definition: DataMapItem.php:221
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\findDependencies
‪DataMapItem[] findDependencies(string $scope)
Definition: DataMapItem.php:329
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\build
‪static DataMapItem build(string $tableName, $id, array $suggestedValues, array $persistedValues, array $configurationFieldNames)
Definition: DataMapItem.php:90
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\buildState
‪buildState()
Definition: DataMapItem.php:366
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_PARENT
‪const STATE_PARENT
Definition: State.php:28
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\setSource
‪setSource($source)
Definition: DataMapItem.php:287
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_CUSTOM
‪const STATE_CUSTOM
Definition: State.php:27
‪TYPO3\CMS\Core\DataHandling\Localization\State\create
‪static create(string $tableName)
Definition: State.php:31
‪TYPO3\CMS\Core\DataHandling\Localization\State
Definition: State.php:26
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$suggestedValues
‪array $suggestedValues
Definition: DataMapItem.php:45
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$persistedValues
‪array $persistedValues
Definition: DataMapItem.php:49
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\SCOPE_EXCLUDE
‪const SCOPE_EXCLUDE
Definition: DataMapItem.php:33
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$configurationFieldNames
‪array $configurationFieldNames
Definition: DataMapItem.php:53
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\TYPE_GRAND_CHILD
‪const TYPE_GRAND_CHILD
Definition: DataMapItem.php:29
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\TYPE_DIRECT_CHILD
‪const TYPE_DIRECT_CHILD
Definition: DataMapItem.php:28
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getApplicableScopes
‪string[] getApplicableScopes()
Definition: DataMapItem.php:337
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getDependencies
‪DataMapItem[][] getDependencies()
Definition: DataMapItem.php:313
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getLanguageFieldName
‪getLanguageFieldName()
Definition: DataMapItem.php:177
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\__construct
‪__construct(string $tableName, $id, array $suggestedValues, array $persistedValues, array $configurationFieldNames)
Definition: DataMapItem.php:118
‪TYPO3\CMS\Core\DataHandling\Localization\State\updateStates
‪updateStates(string $currentState, string $targetState)
Definition: State.php:134
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$state
‪State null $state
Definition: DataMapItem.php:65
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$dependencies
‪DataMapItem[][] $dependencies
Definition: DataMapItem.php:81
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\setParent
‪setParent($parent)
Definition: DataMapItem.php:271
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getIdForScope
‪int string getIdForScope(string $scope)
Definition: DataMapItem.php:296
‪TYPO3\CMS\Core\DataHandling\Localization\State\fromJSON
‪static fromJSON(string $tableName, string $json=null)
Definition: State.php:43
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$tableName
‪string $tableName
Definition: DataMapItem.php:37
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\setLanguage
‪setLanguage($language)
Definition: DataMapItem.php:255
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\isNew
‪isNew()
Definition: DataMapItem.php:195
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\isDirectChildType
‪isDirectChildType()
Definition: DataMapItem.php:226
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getState
‪getState()
Definition: DataMapItem.php:236
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getSourceFieldName
‪string null getSourceFieldName()
Definition: DataMapItem.php:190
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getTableName
‪getTableName()
Definition: DataMapItem.php:138
‪TYPO3\CMS\Core\DataHandling\Localization
Definition: DataMapItem.php:16
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getSource
‪string int getSource()
Definition: DataMapItem.php:279
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getConfigurationFieldNames
‪getConfigurationFieldNames()
Definition: DataMapItem.php:172
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\isGrandChildType
‪isGrandChildType()
Definition: DataMapItem.php:231
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$id
‪string int $id
Definition: DataMapItem.php:41
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem
Definition: DataMapItem.php:26
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\setDependencies
‪setDependencies(array $dependencies)
Definition: DataMapItem.php:321
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_SOURCE
‪const STATE_SOURCE
Definition: State.php:29
‪TYPO3\CMS\Core\DataHandling\Localization\State\update
‪update(array $states)
Definition: State.php:123
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$language
‪string int $language
Definition: DataMapItem.php:69
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getLanguage
‪string int getLanguage()
Definition: DataMapItem.php:247
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getSuggestedValues
‪getSuggestedValues()
Definition: DataMapItem.php:157
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\SCOPE_SOURCE
‪const SCOPE_SOURCE
Definition: DataMapItem.php:32
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getParent
‪string int getParent()
Definition: DataMapItem.php:263
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$type
‪string $type
Definition: DataMapItem.php:61
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$source
‪string int $source
Definition: DataMapItem.php:77
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getId
‪int string getId()
Definition: DataMapItem.php:148
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\SCOPE_PARENT
‪const SCOPE_PARENT
Definition: DataMapItem.php:31