TYPO3 CMS  TYPO3_8-7
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 
52  protected $persistedValues;
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  ) {
113  static::class,
114  $tableName,
115  $id,
116  $suggestedValues,
117  $persistedValues,
118  $configurationFieldNames
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 getFromTableName(): string
170  {
171  if ($this->tableName === 'pages_language_overlay') {
172  return 'pages';
173  }
174  return $this->tableName;
175  }
176 
182  public function getForTableName(): string
183  {
184  if ($this->tableName === 'pages') {
185  return 'pages_language_overlay';
186  }
187  return $this->tableName;
188  }
189 
195  public function getId()
196  {
197  return $this->id;
198  }
199 
206  public function getSuggestedValues(): array
207  {
208  return $this->suggestedValues;
209  }
210 
218  public function getPersistedValues(): array
219  {
220  return $this->persistedValues;
221  }
222 
226  public function getConfigurationFieldNames(): array
227  {
229  }
230 
234  public function getLanguageFieldName(): string
235  {
236  return $this->configurationFieldNames['language'];
237  }
238 
242  public function getParentFieldName(): string
243  {
244  return $this->configurationFieldNames['parent'];
245  }
246 
250  public function getSourceFieldName()
251  {
252  return $this->configurationFieldNames['source'];
253  }
254 
258  public function isNew(): bool
259  {
260  return $this->new;
261  }
262 
266  public function getType(): string
267  {
268  if ($this->type === null) {
269  // implicit: default language, it's a parent
270  if ($this->language === 0) {
271  $this->type = static::TYPE_PARENT;
272  } elseif (
273  // implicit: having source value different to parent value, it's a 2nd or higher level translation
274  $this->source !== null
275  && $this->source !== $this->parent
276  ) {
277  $this->type = static::TYPE_GRAND_CHILD;
278  } else {
279  // implicit: otherwise, it's a 1st level translation
280  $this->type = static::TYPE_DIRECT_CHILD;
281  }
282  }
283  return $this->type;
284  }
285 
289  public function isParentType(): bool
290  {
291  return $this->getType() === static::TYPE_PARENT;
292  }
293 
297  public function isDirectChildType(): bool
298  {
299  return $this->getType() === static::TYPE_DIRECT_CHILD;
300  }
301 
305  public function isGrandChildType(): bool
306  {
307  return $this->getType() === static::TYPE_GRAND_CHILD;
308  }
309 
313  public function getState(): State
314  {
315  if ($this->state === null && !$this->isParentType()) {
316  $this->state = $this->buildState();
317  }
318  return $this->state;
319  }
320 
324  public function getLanguage()
325  {
326  return $this->language;
327  }
328 
332  public function setLanguage($language)
333  {
334  $this->language = $language;
335  }
336 
340  public function getParent()
341  {
342  return $this->parent;
343  }
344 
348  public function setParent($parent)
349  {
350  $this->parent = $this->extractId($parent);
351  }
352 
356  public function getSource()
357  {
358  return $this->source;
359  }
360 
364  public function setSource($source)
365  {
366  $this->source = $this->extractId($source);
367  }
368 
373  public function getIdForScope($scope)
374  {
375  if (
376  $scope === static::SCOPE_PARENT
377  || $scope === static::SCOPE_EXCLUDE
378  ) {
379  return $this->getParent();
380  }
381  if ($scope === static::SCOPE_SOURCE) {
382  return $this->getSource();
383  }
384  throw new \RuntimeException('Invalid scope', 1486325248);
385  }
386 
390  public function getDependencies(): array
391  {
392  return $this->dependencies;
393  }
394 
398  public function setDependencies(array $dependencies)
399  {
400  $this->dependencies = $dependencies;
401  }
402 
407  public function findDependencies(string $scope)
408  {
409  return $this->dependencies[$scope] ?? [];
410  }
411 
415  public function getApplicableScopes()
416  {
417  $scopes = [];
418  if (!empty($this->getSourceFieldName())) {
419  $scopes[] = static::SCOPE_SOURCE;
420  }
421  $scopes[] = static::SCOPE_PARENT;
422  $scopes[] = static::SCOPE_EXCLUDE;
423  return $scopes;
424  }
425 
433  protected function extractId($idValue)
434  {
436  return $idValue;
437  }
438  if (strpos($idValue, 'NEW') === 0) {
439  return $idValue;
440  }
441  // @todo Handle if $tableName does not match $this->tableName
442  list($tableName, $id) = BackendUtility::splitTable_Uid($idValue);
443  return $id;
444  }
445 
449  protected function buildState()
450  {
451  // build from persisted states
452  if (!$this->isNew()) {
454  $this->tableName,
455  $this->persistedValues['l10n_state'] ?? null
456  );
457  } elseif (is_string($this->suggestedValues['l10n_state'] ?? null)) {
458  // use provided states for a new and copied element
460  $this->tableName,
461  $this->suggestedValues['l10n_state']
462  );
463  } else {
464  // provide the default states
465  $state = State::create($this->tableName);
466  }
467  // switch "custom" to "source" state for 2nd level translations
468  if ($this->isNew() && $this->isGrandChildType()) {
470  }
471  // apply any provided updates to the states
472  if (is_array($this->suggestedValues['l10n_state'] ?? null)) {
473  $state->update(
474  $this->suggestedValues['l10n_state'] ?? []
475  );
476  }
477  return $state;
478  }
479 }
static create(string $tableName)
Definition: State.php:32
static makeInstance($className,... $constructorArguments)
static fromJSON(string $tableName, string $json=null)
Definition: State.php:49
__construct(string $tableName, $id, array $suggestedValues, array $persistedValues, array $configurationFieldNames)
static build(string $tableName, $id, array $suggestedValues, array $persistedValues, array $configurationFieldNames)