‪TYPO3CMS  10.4
State.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 
19 
23 class ‪State
24 {
25  const ‪STATE_CUSTOM = 'custom';
26  const ‪STATE_PARENT = 'parent';
27  const ‪STATE_SOURCE = 'source';
28 
33  public static function ‪create(string ‪$tableName)
34  {
35  if (!static::isApplicable(‪$tableName)) {
36  return null;
37  }
38 
39  return GeneralUtility::makeInstance(
40  static::class,
42  );
43  }
44 
50  public static function ‪fromJSON(string ‪$tableName, string $json = null)
51  {
52  if (!static::isApplicable(‪$tableName)) {
53  return null;
54  }
55 
56  ‪$states = json_decode($json ?? '', true);
57  return GeneralUtility::makeInstance(
58  static::class,
60  ‪$states ?? []
61  );
62  }
63 
68  public static function ‪isApplicable(string ‪$tableName)
69  {
70  return
71  static::hasColumns(‪$tableName)
72  && static::hasLanguageFieldName(‪$tableName)
73  && static::hasTranslationParentFieldName(‪$tableName)
74  && count(static::getFieldNames(‪$tableName)) > 0
75  ;
76  }
77 
82  public static function ‪getFieldNames(string ‪$tableName)
83  {
84  return array_keys(
85  array_filter(
86  ‪$GLOBALS['TCA'][‪$tableName]['columns'] ?? [],
87  function (array $fieldConfiguration) {
88  return !empty(
89  $fieldConfiguration['config']
90  ['behaviour']['allowLanguageSynchronization']
91  );
92  }
93  )
94  );
95  }
96 
101  protected static function ‪hasColumns(string ‪$tableName)
102  {
103  return
104  !empty(‪$GLOBALS['TCA'][‪$tableName]['columns'])
105  && is_array(‪$GLOBALS['TCA'][‪$tableName]['columns'])
106  ;
107  }
108 
113  protected static function ‪hasLanguageFieldName(string ‪$tableName)
114  {
115  return !empty(‪$GLOBALS['TCA'][‪$tableName]['ctrl']['languageField']);
116  }
117 
122  protected static function ‪hasTranslationParentFieldName(string ‪$tableName)
123  {
124  return !empty(‪$GLOBALS['TCA'][‪$tableName]['ctrl']['transOrigPointerField']);
125  }
126 
130  protected ‪$tableName;
131 
135  protected ‪$states;
136 
140  protected ‪$originalStates;
141 
145  protected ‪$validStates = [
149  ];
150 
155  public function ‪__construct(string ‪$tableName, array ‪$states = [])
156  {
157  $this->tableName = ‪$tableName;
158  $this->states = ‪$states;
159  $this->originalStates = ‪$states;
160 
161  $this->states = $this->‪enrich(
162  $this->‪sanitize(‪$states)
163  );
164  }
165 
169  public function ‪update(array ‪$states)
170  {
171  $this->states = array_merge(
172  $this->states,
173  $this->‪sanitize($states)
174  );
175  }
176 
183  public function ‪updateStates(string $currentState, string $targetState)
184  {
185  ‪$states = [];
186  foreach ($this->‪filterFieldNames($currentState) as $fieldName) {
187  ‪$states[$fieldName] = $targetState;
188  }
189  if (!empty(‪$states)) {
190  $this->‪update(‪$states);
191  }
192  }
193 
197  public function ‪export()
198  {
199  if (empty($this->states)) {
200  return null;
201  }
202  return json_encode($this->states);
203  }
204 
208  public function ‪toArray(): array
209  {
210  return $this->states ?? [];
211  }
212 
216  public function ‪getModifiedFieldNames()
217  {
218  return array_keys(
219  array_diff_assoc(
220  $this->states,
221  $this->originalStates
222  )
223  );
224  }
225 
229  public function ‪isModified()
230  {
231  return !empty($this->‪getModifiedFieldNames());
232  }
233 
238  public function ‪isUndefined(string $fieldName)
239  {
240  return !isset($this->states[$fieldName]);
241  }
242 
247  public function ‪isCustomState(string $fieldName)
248  {
249  return ($this->states[$fieldName] ?? null) === static::STATE_CUSTOM;
250  }
251 
256  public function ‪isParentState(string $fieldName)
257  {
258  return ($this->states[$fieldName] ?? null) === static::STATE_PARENT;
259  }
260 
265  public function ‪isSourceState(string $fieldName)
266  {
267  return ($this->states[$fieldName] ?? null) === static::STATE_SOURCE;
268  }
269 
274  public function ‪getState(string $fieldName)
275  {
276  return $this->states[$fieldName] ?? null;
277  }
278 
286  public function ‪filterFieldNames(string $desiredState, bool $modified = false)
287  {
288  if (!$modified) {
289  $fieldNames = array_keys($this->states);
290  } else {
291  $fieldNames = $this->‪getModifiedFieldNames();
292  }
293  return array_filter(
294  $fieldNames,
295  function ($fieldName) use ($desiredState) {
296  return $this->states[$fieldName] === $desiredState;
297  }
298  );
299  }
300 
307  protected function ‪sanitize(array ‪$states)
308  {
309  $fieldNames = static::getFieldNames($this->tableName);
310  return array_intersect_key(
311  ‪$states,
312  array_combine($fieldNames, $fieldNames)
313  );
314  }
315 
322  protected function ‪enrich(array ‪$states)
323  {
324  foreach (static::getFieldNames($this->tableName) as $fieldName) {
325  $isValid = in_array(
326  ‪$states[$fieldName] ?? null,
327  $this->validStates,
328  true
329  );
330  if ($isValid) {
331  continue;
332  }
333  ‪$states[$fieldName] = static::STATE_PARENT;
334  }
335  return ‪$states;
336  }
337 }
‪TYPO3\CMS\Core\DataHandling\Localization\State\hasTranslationParentFieldName
‪static bool hasTranslationParentFieldName(string $tableName)
Definition: State.php:122
‪TYPO3\CMS\Core\DataHandling\Localization\State\__construct
‪__construct(string $tableName, array $states=[])
Definition: State.php:151
‪TYPO3\CMS\Core\DataHandling\Localization\State\enrich
‪array enrich(array $states)
Definition: State.php:318
‪TYPO3\CMS\Core\DataHandling\Localization\State\filterFieldNames
‪string[] filterFieldNames(string $desiredState, bool $modified=false)
Definition: State.php:282
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_PARENT
‪const STATE_PARENT
Definition: State.php:26
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_CUSTOM
‪const STATE_CUSTOM
Definition: State.php:25
‪TYPO3\CMS\Core\DataHandling\Localization\State
Definition: State.php:24
‪TYPO3\CMS\Core\DataHandling\Localization\State\$originalStates
‪array $originalStates
Definition: State.php:137
‪TYPO3\CMS\Core\DataHandling\Localization\State\hasColumns
‪static bool hasColumns(string $tableName)
Definition: State.php:101
‪TYPO3\CMS\Core\DataHandling\Localization\State\isSourceState
‪bool isSourceState(string $fieldName)
Definition: State.php:261
‪TYPO3\CMS\Core\DataHandling\Localization\State\isApplicable
‪static bool isApplicable(string $tableName)
Definition: State.php:68
‪TYPO3\CMS\Core\DataHandling\Localization\State\getFieldNames
‪static array getFieldNames(string $tableName)
Definition: State.php:82
‪TYPO3\CMS\Core\DataHandling\Localization\State\getState
‪string null getState(string $fieldName)
Definition: State.php:270
‪TYPO3\CMS\Core\DataHandling\Localization\State\$tableName
‪string $tableName
Definition: State.php:129
‪TYPO3\CMS\Core\DataHandling\Localization\State\updateStates
‪updateStates(string $currentState, string $targetState)
Definition: State.php:179
‪TYPO3\CMS\Core\DataHandling\Localization\State\$validStates
‪array $validStates
Definition: State.php:141
‪TYPO3\CMS\Core\DataHandling\Localization\State\getModifiedFieldNames
‪string[] getModifiedFieldNames()
Definition: State.php:212
‪TYPO3\CMS\Core\DataHandling\Localization\State\create
‪static State null create(string $tableName)
Definition: State.php:33
‪TYPO3\CMS\Core\DataHandling\Localization\State\sanitize
‪array sanitize(array $states)
Definition: State.php:303
‪TYPO3\CMS\Core\DataHandling\Localization\State\hasLanguageFieldName
‪static bool hasLanguageFieldName(string $tableName)
Definition: State.php:113
‪TYPO3\CMS\Core\DataHandling\Localization\State\isCustomState
‪bool isCustomState(string $fieldName)
Definition: State.php:243
‪TYPO3\CMS\Core\DataHandling\Localization
Definition: DataMapItem.php:16
‪TYPO3\CMS\Core\DataHandling\Localization\State\isParentState
‪bool isParentState(string $fieldName)
Definition: State.php:252
‪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\State\fromJSON
‪static State null fromJSON(string $tableName, string $json=null)
Definition: State.php:50
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\DataHandling\Localization\State\isUndefined
‪bool isUndefined(string $fieldName)
Definition: State.php:234
‪TYPO3\CMS\Core\DataHandling\Localization\State\$states
‪array $states
Definition: State.php:133
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\DataHandling\Localization\State\isModified
‪bool isModified()
Definition: State.php:225
‪TYPO3\CMS\Core\DataHandling\Localization\State\export
‪string null export()
Definition: State.php:193
‪TYPO3\CMS\Core\DataHandling\Localization\State\toArray
‪array toArray()
Definition: State.php:204