‪TYPO3CMS  9.5
DataMapItem.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
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.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 
25 {
26  const ‪TYPE_PARENT = 'parent';
27  const ‪TYPE_DIRECT_CHILD = 'directChild';
28  const ‪TYPE_GRAND_CHILD = 'grandChild';
29 
32  const ‪SCOPE_EXCLUDE = 'exclude';
33 
37  protected ‪$tableName;
38 
42  protected ‪$id;
43 
47  protected ‪$suggestedValues;
48 
53 
58 
62  protected ‪$new;
63 
67  protected ‪$type;
68 
72  protected ‪$state;
73 
77  protected ‪$language;
78 
82  protected ‪$parent;
83 
87  protected ‪$source;
88 
92  protected ‪$dependencies = [];
93 
105  public static function ‪build(
106  string ‪$tableName,
107  ‪$id,
108  array ‪$suggestedValues,
109  array ‪$persistedValues,
111  ) {
112  $item = GeneralUtility::makeInstance(
113  static::class,
115  ‪$id,
119  );
120 
121  $item->language = (int)(‪$suggestedValues[$item->getLanguageFieldName()] ?? ‪$persistedValues[$item->getLanguageFieldName()]);
122  $item->setParent(‪$suggestedValues[$item->getParentFieldName()] ?? ‪$persistedValues[$item->getParentFieldName()]);
123  if ($item->getSourceFieldName() !== null) {
124  $item->setSource(‪$suggestedValues[$item->getSourceFieldName()] ?? ‪$persistedValues[$item->getSourceFieldName()]);
125  }
126 
127  return $item;
128  }
129 
137  public function ‪__construct(
138  string ‪$tableName,
139  ‪$id,
140  array ‪$suggestedValues,
141  array ‪$persistedValues,
143  ) {
144  $this->tableName = ‪$tableName;
145  $this->id = ‪$id;
146 
147  $this->suggestedValues = ‪$suggestedValues;
148  $this->persistedValues = ‪$persistedValues;
149  $this->configurationFieldNames = ‪$configurationFieldNames;
150 
152  }
153 
159  public function ‪getTableName(): string
160  {
161  return ‪$this->tableName;
162  }
163 
169  public function ‪getId()
170  {
171  return ‪$this->id;
172  }
173 
180  public function ‪getSuggestedValues(): array
181  {
183  }
184 
192  public function ‪getPersistedValues(): array
193  {
195  }
196 
200  public function ‪getConfigurationFieldNames(): array
201  {
203  }
204 
208  public function ‪getLanguageFieldName(): string
209  {
210  return $this->configurationFieldNames['language'];
211  }
212 
216  public function ‪getParentFieldName(): string
217  {
218  return $this->configurationFieldNames['parent'];
219  }
220 
224  public function ‪getSourceFieldName()
225  {
226  return $this->configurationFieldNames['source'];
227  }
228 
232  public function ‪isNew(): bool
233  {
234  return ‪$this->new;
235  }
236 
240  public function ‪getType(): string
241  {
242  if ($this->type === null) {
243  // implicit: default language, it's a parent
244  if ($this->language === 0) {
245  $this->type = static::TYPE_PARENT;
246  } elseif (
247  // implicit: having source value different to parent value, it's a 2nd or higher level translation
248  $this->source !== null
249  && $this->source !== $this->parent
250  ) {
251  $this->type = static::TYPE_GRAND_CHILD;
252  } else {
253  // implicit: otherwise, it's a 1st level translation
254  $this->type = static::TYPE_DIRECT_CHILD;
255  }
256  }
257  return ‪$this->type;
258  }
259 
263  public function ‪isParentType(): bool
264  {
265  return $this->‪getType() === static::TYPE_PARENT;
266  }
267 
271  public function ‪isDirectChildType(): bool
272  {
273  return $this->‪getType() === static::TYPE_DIRECT_CHILD;
274  }
275 
279  public function ‪isGrandChildType(): bool
280  {
281  return $this->‪getType() === static::TYPE_GRAND_CHILD;
282  }
283 
287  public function ‪getState(): ‪State
288  {
289  if ($this->state === null && !$this->‪isParentType()) {
290  $this->state = $this->‪buildState();
291  }
292  return ‪$this->state;
293  }
294 
298  public function ‪getLanguage()
299  {
300  return ‪$this->language;
301  }
302 
306  public function ‪setLanguage(‪$language)
307  {
308  $this->language = ‪$language;
309  }
310 
314  public function ‪getParent()
315  {
316  return ‪$this->parent;
317  }
318 
322  public function ‪setParent(‪$parent)
323  {
324  $this->parent = $this->‪extractId(‪$parent);
325  }
326 
330  public function ‪getSource()
331  {
332  return ‪$this->source;
333  }
334 
338  public function ‪setSource(‪$source)
339  {
340  $this->source = $this->‪extractId(‪$source);
341  }
342 
347  public function ‪getIdForScope($scope)
348  {
349  if (
350  $scope === static::SCOPE_PARENT
351  || $scope === static::SCOPE_EXCLUDE
352  ) {
353  return $this->‪getParent();
354  }
355  if ($scope === static::SCOPE_SOURCE) {
356  return $this->‪getSource();
357  }
358  throw new \RuntimeException('Invalid scope', 1486325248);
359  }
360 
364  public function ‪getDependencies(): array
365  {
366  return ‪$this->dependencies;
367  }
368 
372  public function ‪setDependencies(array ‪$dependencies)
373  {
374  $this->dependencies = ‪$dependencies;
375  }
376 
381  public function ‪findDependencies(string $scope)
382  {
383  return $this->dependencies[$scope] ?? [];
384  }
385 
389  public function ‪getApplicableScopes()
390  {
391  $scopes = [];
392  if (!empty($this->‪getSourceFieldName())) {
393  $scopes[] = static::SCOPE_SOURCE;
394  }
395  $scopes[] = static::SCOPE_PARENT;
396  $scopes[] = static::SCOPE_EXCLUDE;
397  return $scopes;
398  }
399 
407  protected function ‪extractId($idValue)
408  {
410  return $idValue;
411  }
412  if (strpos($idValue, 'NEW') === 0) {
413  return $idValue;
414  }
415  // @todo Handle if $tableName does not match $this->tableName
417  return ‪$id;
418  }
419 
423  protected function ‪buildState()
424  {
425  // build from persisted states
426  if (!$this->‪isNew()) {
428  $this->tableName,
429  $this->persistedValues['l10n_state'] ?? null
430  );
431  } elseif (is_string($this->suggestedValues['l10n_state'] ?? null)) {
432  // use provided states for a new and copied element
434  $this->tableName,
435  $this->suggestedValues['l10n_state']
436  );
437  } else {
438  // provide the default states
439  ‪$state = ‪State::create($this->tableName);
440  }
441  // switch "custom" to "source" state for 2nd level translations
442  if ($this->‪isNew() && $this->‪isGrandChildType()) {
444  }
445  // apply any provided updates to the states
446  if (is_array($this->suggestedValues['l10n_state'] ?? null)) {
448  $this->suggestedValues['l10n_state'] ?? []
449  );
450  }
451  return ‪$state;
452  }
453 }
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getType
‪string getType()
Definition: DataMapItem.php:228
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$parent
‪string int $parent
Definition: DataMapItem.php:72
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\TYPE_PARENT
‪const TYPE_PARENT
Definition: DataMapItem.php:26
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$new
‪bool $new
Definition: DataMapItem.php:56
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\findDependencies
‪DataMapItem[] findDependencies(string $scope)
Definition: DataMapItem.php:369
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\buildState
‪State null buildState()
Definition: DataMapItem.php:411
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getConfigurationFieldNames
‪array getConfigurationFieldNames()
Definition: DataMapItem.php:188
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_PARENT
‪const STATE_PARENT
Definition: State.php:25
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\setSource
‪setSource($source)
Definition: DataMapItem.php:326
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_CUSTOM
‪const STATE_CUSTOM
Definition: State.php:24
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\extractId
‪int string extractId($idValue)
Definition: DataMapItem.php:395
‪TYPO3\CMS\Core\DataHandling\Localization\State
Definition: State.php:23
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$suggestedValues
‪array $suggestedValues
Definition: DataMapItem.php:44
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getTableName
‪string getTableName()
Definition: DataMapItem.php:147
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$persistedValues
‪array $persistedValues
Definition: DataMapItem.php:48
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\SCOPE_EXCLUDE
‪const SCOPE_EXCLUDE
Definition: DataMapItem.php:32
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$configurationFieldNames
‪array $configurationFieldNames
Definition: DataMapItem.php:52
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\TYPE_GRAND_CHILD
‪const TYPE_GRAND_CHILD
Definition: DataMapItem.php:28
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\isParentType
‪bool isParentType()
Definition: DataMapItem.php:251
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\TYPE_DIRECT_CHILD
‪const TYPE_DIRECT_CHILD
Definition: DataMapItem.php:27
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$state
‪State $state
Definition: DataMapItem.php:64
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getApplicableScopes
‪string[] getApplicableScopes()
Definition: DataMapItem.php:377
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getDependencies
‪DataMapItem[][] getDependencies()
Definition: DataMapItem.php:352
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\__construct
‪__construct(string $tableName, $id, array $suggestedValues, array $persistedValues, array $configurationFieldNames)
Definition: DataMapItem.php:125
‪TYPO3\CMS\Core\DataHandling\Localization\State\updateStates
‪updateStates(string $currentState, string $targetState)
Definition: State.php:178
‪TYPO3\CMS\Core\DataHandling\Localization\State\create
‪static State null create(string $tableName)
Definition: State.php:32
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$dependencies
‪DataMapItem[][] $dependencies
Definition: DataMapItem.php:80
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\setParent
‪setParent($parent)
Definition: DataMapItem.php:310
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\isNew
‪bool isNew()
Definition: DataMapItem.php:220
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getSuggestedValues
‪array getSuggestedValues()
Definition: DataMapItem.php:168
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$tableName
‪string $tableName
Definition: DataMapItem.php:36
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\setLanguage
‪setLanguage($language)
Definition: DataMapItem.php:294
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getSourceFieldName
‪string null getSourceFieldName()
Definition: DataMapItem.php:212
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\build
‪static object DataMapItem build(string $tableName, $id, array $suggestedValues, array $persistedValues, array $configurationFieldNames)
Definition: DataMapItem.php:93
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getPersistedValues
‪array getPersistedValues()
Definition: DataMapItem.php:180
‪TYPO3\CMS\Core\DataHandling\Localization
Definition: DataMapItem.php:2
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getSource
‪string int getSource()
Definition: DataMapItem.php:318
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$id
‪string int $id
Definition: DataMapItem.php:40
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem
Definition: DataMapItem.php:25
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\setDependencies
‪setDependencies(array $dependencies)
Definition: DataMapItem.php:360
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_SOURCE
‪const STATE_SOURCE
Definition: State.php:26
‪TYPO3\CMS\Core\DataHandling\Localization\State\update
‪update(array $states)
Definition: State.php:164
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$language
‪string int $language
Definition: DataMapItem.php:68
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getLanguageFieldName
‪string getLanguageFieldName()
Definition: DataMapItem.php:196
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getState
‪State getState()
Definition: DataMapItem.php:275
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getParentFieldName
‪string getParentFieldName()
Definition: DataMapItem.php:204
‪TYPO3\CMS\Core\DataHandling\Localization\State\fromJSON
‪static State null fromJSON(string $tableName, string $json=null)
Definition: State.php:49
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getLanguage
‪string int getLanguage()
Definition: DataMapItem.php:286
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\SCOPE_SOURCE
‪const SCOPE_SOURCE
Definition: DataMapItem.php:31
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getParent
‪string int getParent()
Definition: DataMapItem.php:302
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$type
‪string $type
Definition: DataMapItem.php:60
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\isDirectChildType
‪bool isDirectChildType()
Definition: DataMapItem.php:259
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getId
‪mixed getId()
Definition: DataMapItem.php:157
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\$source
‪string int $source
Definition: DataMapItem.php:76
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\getIdForScope
‪int string getIdForScope($scope)
Definition: DataMapItem.php:335
‪TYPO3\CMS\Backend\Utility\BackendUtility\splitTable_Uid
‪static array splitTable_Uid($str)
Definition: BackendUtility.php:240
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\isGrandChildType
‪bool isGrandChildType()
Definition: DataMapItem.php:267
‪TYPO3\CMS\Core\DataHandling\Localization\DataMapItem\SCOPE_PARENT
‪const SCOPE_PARENT
Definition: DataMapItem.php:30