‪TYPO3CMS  10.4
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 
21 
26 {
27  const ‪TYPE_PARENT = 'parent';
28  const ‪TYPE_DIRECT_CHILD = 'directChild';
29  const ‪TYPE_GRAND_CHILD = 'grandChild';
30 
33  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 
106  public static function ‪build(
107  string ‪$tableName,
108  ‪$id,
109  array ‪$suggestedValues,
110  array ‪$persistedValues,
112  ) {
113  $item = GeneralUtility::makeInstance(
114  static::class,
116  ‪$id,
120  );
121 
122  $item->language = (int)(‪$suggestedValues[$item->getLanguageFieldName()] ?? ‪$persistedValues[$item->getLanguageFieldName()]);
123  $item->setParent(‪$suggestedValues[$item->getParentFieldName()] ?? ‪$persistedValues[$item->getParentFieldName()]);
124  if ($item->getSourceFieldName() !== null) {
125  $item->setSource(‪$suggestedValues[$item->getSourceFieldName()] ?? ‪$persistedValues[$item->getSourceFieldName()]);
126  }
127 
128  return $item;
129  }
130 
138  public function ‪__construct(
139  string ‪$tableName,
140  ‪$id,
141  array ‪$suggestedValues,
142  array ‪$persistedValues,
144  ) {
145  $this->tableName = ‪$tableName;
146  $this->id = ‪$id;
147 
148  $this->suggestedValues = ‪$suggestedValues;
149  $this->persistedValues = ‪$persistedValues;
150  $this->configurationFieldNames = ‪$configurationFieldNames;
151 
153  }
154 
160  public function ‪getTableName(): string
161  {
162  return ‪$this->tableName;
163  }
164 
170  public function ‪getId()
171  {
172  return ‪$this->id;
173  }
174 
181  public function ‪getSuggestedValues(): array
182  {
184  }
185 
193  public function ‪getPersistedValues(): array
194  {
196  }
197 
201  public function ‪getConfigurationFieldNames(): array
202  {
204  }
205 
209  public function ‪getLanguageFieldName(): string
210  {
211  return $this->configurationFieldNames['language'];
212  }
213 
217  public function ‪getParentFieldName(): string
218  {
219  return $this->configurationFieldNames['parent'];
220  }
221 
225  public function ‪getSourceFieldName()
226  {
227  return $this->configurationFieldNames['source'];
228  }
229 
233  public function ‪isNew(): bool
234  {
235  return ‪$this->new;
236  }
237 
241  public function ‪getType(): string
242  {
243  if ($this->type === null) {
244  // implicit: default language, it's a parent
245  if ($this->language === 0) {
246  $this->type = static::TYPE_PARENT;
247  } elseif (
248  // implicit: having source value different to parent value, it's a 2nd or higher level translation
249  $this->source !== null
250  && $this->source !== $this->parent
251  ) {
252  $this->type = static::TYPE_GRAND_CHILD;
253  } else {
254  // implicit: otherwise, it's a 1st level translation
255  $this->type = static::TYPE_DIRECT_CHILD;
256  }
257  }
258  return ‪$this->type;
259  }
260 
264  public function ‪isParentType(): bool
265  {
266  return $this->‪getType() === static::TYPE_PARENT;
267  }
268 
272  public function ‪isDirectChildType(): bool
273  {
274  return $this->‪getType() === static::TYPE_DIRECT_CHILD;
275  }
276 
280  public function ‪isGrandChildType(): bool
281  {
282  return $this->‪getType() === static::TYPE_GRAND_CHILD;
283  }
284 
288  public function ‪getState(): ‪State
289  {
290  if ($this->state === null && !$this->‪isParentType()) {
291  $this->state = $this->‪buildState();
292  }
293  return ‪$this->state;
294  }
295 
299  public function ‪getLanguage()
300  {
301  return ‪$this->language;
302  }
303 
307  public function ‪setLanguage(‪$language)
308  {
309  $this->language = ‪$language;
310  }
311 
315  public function ‪getParent()
316  {
317  return ‪$this->parent;
318  }
319 
323  public function ‪setParent(‪$parent)
324  {
325  $this->parent = $this->‪extractId(‪$parent);
326  }
327 
331  public function ‪getSource()
332  {
333  return ‪$this->source;
334  }
335 
339  public function ‪setSource(‪$source)
340  {
341  $this->source = $this->‪extractId(‪$source);
342  }
343 
348  public function ‪getIdForScope($scope)
349  {
350  if (
351  $scope === static::SCOPE_PARENT
352  || $scope === static::SCOPE_EXCLUDE
353  ) {
354  return $this->‪getParent();
355  }
356  if ($scope === static::SCOPE_SOURCE) {
357  return $this->‪getSource();
358  }
359  throw new \RuntimeException('Invalid scope', 1486325248);
360  }
361 
365  public function ‪getDependencies(): array
366  {
367  return ‪$this->dependencies;
368  }
369 
373  public function ‪setDependencies(array ‪$dependencies)
374  {
375  $this->dependencies = ‪$dependencies;
376  }
377 
382  public function ‪findDependencies(string $scope)
383  {
384  return $this->dependencies[$scope] ?? [];
385  }
386 
390  public function ‪getApplicableScopes()
391  {
392  $scopes = [];
393  if (!empty($this->‪getSourceFieldName())) {
394  $scopes[] = static::SCOPE_SOURCE;
395  }
396  $scopes[] = static::SCOPE_PARENT;
397  $scopes[] = static::SCOPE_EXCLUDE;
398  return $scopes;
399  }
400 
408  protected function ‪extractId($idValue)
409  {
411  return $idValue;
412  }
413  if (strpos($idValue, 'NEW') === 0) {
414  return $idValue;
415  }
416  // @todo Handle if $tableName does not match $this->tableName
418  return ‪$id;
419  }
420 
424  protected function ‪buildState()
425  {
426  // build from persisted states
427  if (!$this->‪isNew()) {
429  $this->tableName,
430  $this->persistedValues['l10n_state'] ?? null
431  );
432  } elseif (is_string($this->suggestedValues['l10n_state'] ?? null)) {
433  // use provided states for a new and copied element
435  $this->tableName,
436  $this->suggestedValues['l10n_state']
437  );
438  } else {
439  // provide the default states
440  ‪$state = ‪State::create($this->tableName);
441  }
442  // switch "custom" to "source" state for 2nd level translations
443  if ($this->‪isNew() && $this->‪isGrandChildType()) {
445  }
446  // apply any provided updates to the states
447  if (is_array($this->suggestedValues['l10n_state'] ?? null)) {
449  $this->suggestedValues['l10n_state'] ?? []
450  );
451  }
452  return ‪$state;
453  }
454 }
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getType
‪string getType()
Definition: DataMapItem.php:229
‪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\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$new
‪bool $new
Definition: DataMapItem.php:57
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\findDependencies
‪DataMapItem[] findDependencies(string $scope)
Definition: DataMapItem.php:370
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\buildState
‪State null buildState()
Definition: DataMapItem.php:412
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getConfigurationFieldNames
‪array getConfigurationFieldNames()
Definition: DataMapItem.php:189
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_PARENT
‪const STATE_PARENT
Definition: State.php:26
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\setSource
‪setSource($source)
Definition: DataMapItem.php:327
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_CUSTOM
‪const STATE_CUSTOM
Definition: State.php:25
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\extractId
‪int string extractId($idValue)
Definition: DataMapItem.php:396
‪TYPO3\CMS\Core\DataHandling\Localization\State
Definition: State.php:24
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$suggestedValues
‪array $suggestedValues
Definition: DataMapItem.php:45
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getTableName
‪string getTableName()
Definition: DataMapItem.php:148
‪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\DataHandling\Localization\DataMapItem\isParentType
‪bool isParentType()
Definition: DataMapItem.php:252
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\TYPE_DIRECT_CHILD
‪const TYPE_DIRECT_CHILD
Definition: DataMapItem.php:28
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$state
‪State $state
Definition: DataMapItem.php:65
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getApplicableScopes
‪string[] getApplicableScopes()
Definition: DataMapItem.php:378
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getDependencies
‪DataMapItem[][] getDependencies()
Definition: DataMapItem.php:353
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\__construct
‪__construct(string $tableName, $id, array $suggestedValues, array $persistedValues, array $configurationFieldNames)
Definition: DataMapItem.php:126
‪TYPO3\CMS\Core\DataHandling\Localization\State\updateStates
‪updateStates(string $currentState, string $targetState)
Definition: State.php:179
‪TYPO3\CMS\Core\DataHandling\Localization\State\create
‪static State null create(string $tableName)
Definition: State.php:33
‪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:311
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\isNew
‪bool isNew()
Definition: DataMapItem.php:221
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getSuggestedValues
‪array getSuggestedValues()
Definition: DataMapItem.php:169
‪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:295
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getSourceFieldName
‪string null getSourceFieldName()
Definition: DataMapItem.php:213
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\build
‪static object DataMapItem build(string $tableName, $id, array $suggestedValues, array $persistedValues, array $configurationFieldNames)
Definition: DataMapItem.php:94
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getPersistedValues
‪array getPersistedValues()
Definition: DataMapItem.php:181
‪TYPO3\CMS\Core\DataHandling\Localization
Definition: DataMapItem.php:16
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getSource
‪string int getSource()
Definition: DataMapItem.php:319
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪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:361
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_SOURCE
‪const STATE_SOURCE
Definition: State.php:27
‪TYPO3\CMS\Core\DataHandling\Localization\State\update
‪update(array $states)
Definition: State.php:165
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$language
‪string int $language
Definition: DataMapItem.php:69
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getLanguageFieldName
‪string getLanguageFieldName()
Definition: DataMapItem.php:197
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getState
‪State getState()
Definition: DataMapItem.php:276
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getParentFieldName
‪string getParentFieldName()
Definition: DataMapItem.php:205
‪TYPO3\CMS\Core\DataHandling\Localization\State\fromJSON
‪static State null fromJSON(string $tableName, string $json=null)
Definition: State.php:50
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getLanguage
‪string int getLanguage()
Definition: DataMapItem.php:287
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\SCOPE_SOURCE
‪const SCOPE_SOURCE
Definition: DataMapItem.php:32
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getParent
‪string int getParent()
Definition: DataMapItem.php:303
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$type
‪string $type
Definition: DataMapItem.php:61
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\isDirectChildType
‪bool isDirectChildType()
Definition: DataMapItem.php:260
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getId
‪mixed getId()
Definition: DataMapItem.php:158
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$source
‪string int $source
Definition: DataMapItem.php:77
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getIdForScope
‪int string getIdForScope($scope)
Definition: DataMapItem.php:336
‪TYPO3\CMS\Backend\Utility\BackendUtility\splitTable_Uid
‪static array splitTable_Uid($str)
Definition: BackendUtility.php:208
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\isGrandChildType
‪bool isGrandChildType()
Definition: DataMapItem.php:268
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\SCOPE_PARENT
‪const SCOPE_PARENT
Definition: DataMapItem.php:31