‪TYPO3CMS  9.5
State.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 
18 
22 class ‪State
23 {
24  const ‪STATE_CUSTOM = 'custom';
25  const ‪STATE_PARENT = 'parent';
26  const ‪STATE_SOURCE = 'source';
27 
32  public static function ‪create(string ‪$tableName)
33  {
34  if (!static::isApplicable(‪$tableName)) {
35  return null;
36  }
37 
38  return GeneralUtility::makeInstance(
39  static::class,
41  );
42  }
43 
49  public static function ‪fromJSON(string ‪$tableName, string $json = null)
50  {
51  if (!static::isApplicable(‪$tableName)) {
52  return null;
53  }
54 
55  ‪$states = json_decode($json ?? '', true);
56  return GeneralUtility::makeInstance(
57  static::class,
59  ‪$states ?? []
60  );
61  }
62 
67  public static function ‪isApplicable(string ‪$tableName)
68  {
69  return
70  static::hasColumns(‪$tableName)
71  && static::hasLanguageFieldName(‪$tableName)
72  && static::hasTranslationParentFieldName(‪$tableName)
73  && count(static::getFieldNames(‪$tableName)) > 0
74  ;
75  }
76 
81  public static function ‪getFieldNames(string ‪$tableName)
82  {
83  return array_keys(
84  array_filter(
85  ‪$GLOBALS['TCA'][‪$tableName]['columns'] ?? [],
86  function (array $fieldConfiguration) {
87  return !empty(
88  $fieldConfiguration['config']
89  ['behaviour']['allowLanguageSynchronization']
90  );
91  }
92  )
93  );
94  }
95 
100  protected static function ‪hasColumns(string ‪$tableName)
101  {
102  return
103  !empty(‪$GLOBALS['TCA'][‪$tableName]['columns'])
104  && is_array(‪$GLOBALS['TCA'][‪$tableName]['columns'])
105  ;
106  }
107 
112  protected static function ‪hasLanguageFieldName(string ‪$tableName)
113  {
114  return !empty(‪$GLOBALS['TCA'][‪$tableName]['ctrl']['languageField']);
115  }
116 
121  protected static function ‪hasTranslationParentFieldName(string ‪$tableName)
122  {
123  return !empty(‪$GLOBALS['TCA'][‪$tableName]['ctrl']['transOrigPointerField']);
124  }
125 
129  protected ‪$tableName;
130 
134  protected ‪$states;
135 
139  protected ‪$originalStates;
140 
144  protected ‪$validStates = [
148  ];
149 
154  public function ‪__construct(string ‪$tableName, array ‪$states = [])
155  {
156  $this->tableName = ‪$tableName;
157  $this->states = ‪$states;
158  $this->originalStates = ‪$states;
159 
160  $this->states = $this->‪enrich(
161  $this->‪sanitize(‪$states)
162  );
163  }
164 
168  public function ‪update(array ‪$states)
169  {
170  $this->states = array_merge(
171  $this->states,
172  $this->‪sanitize($states)
173  );
174  }
175 
182  public function ‪updateStates(string $currentState, string $targetState)
183  {
184  ‪$states = [];
185  foreach ($this->‪filterFieldNames($currentState) as $fieldName) {
186  ‪$states[$fieldName] = $targetState;
187  }
188  if (!empty(‪$states)) {
189  $this->‪update(‪$states);
190  }
191  }
192 
196  public function ‪export()
197  {
198  if (empty($this->states)) {
199  return null;
200  }
201  return json_encode($this->states);
202  }
203 
207  public function ‪toArray(): array
208  {
209  return $this->states ?? [];
210  }
211 
215  public function ‪getModifiedFieldNames()
216  {
217  return array_keys(
218  array_diff_assoc(
219  $this->states,
220  $this->originalStates
221  )
222  );
223  }
224 
228  public function ‪isModified()
229  {
230  return !empty($this->‪getModifiedFieldNames());
231  }
232 
237  public function ‪isUndefined(string $fieldName)
238  {
239  return !isset($this->states[$fieldName]);
240  }
241 
246  public function ‪isCustomState(string $fieldName)
247  {
248  return ($this->states[$fieldName] ?? null) === static::STATE_CUSTOM;
249  }
250 
255  public function ‪isParentState(string $fieldName)
256  {
257  return ($this->states[$fieldName] ?? null) === static::STATE_PARENT;
258  }
259 
264  public function ‪isSourceState(string $fieldName)
265  {
266  return ($this->states[$fieldName] ?? null) === static::STATE_SOURCE;
267  }
268 
273  public function ‪getState(string $fieldName)
274  {
275  return $this->states[$fieldName] ?? null;
276  }
277 
285  public function ‪filterFieldNames(string $desiredState, bool $modified = false)
286  {
287  if (!$modified) {
288  $fieldNames = array_keys($this->states);
289  } else {
290  $fieldNames = $this->‪getModifiedFieldNames();
291  }
292  return array_filter(
293  $fieldNames,
294  function ($fieldName) use ($desiredState) {
295  return $this->states[$fieldName] === $desiredState;
296  }
297  );
298  }
299 
306  protected function ‪sanitize(array ‪$states)
307  {
308  $fieldNames = static::getFieldNames($this->tableName);
309  return array_intersect_key(
310  ‪$states,
311  array_combine($fieldNames, $fieldNames)
312  );
313  }
314 
321  protected function ‪enrich(array ‪$states)
322  {
323  foreach (static::getFieldNames($this->tableName) as $fieldName) {
324  $isValid = in_array(
325  ‪$states[$fieldName] ?? null,
326  $this->validStates,
327  true
328  );
329  if ($isValid) {
330  continue;
331  }
332  ‪$states[$fieldName] = static::STATE_PARENT;
333  }
334  return ‪$states;
335  }
336 }
‪TYPO3\CMS\Core\DataHandling\Localization\State\hasTranslationParentFieldName
‪static bool hasTranslationParentFieldName(string $tableName)
Definition: State.php:121
‪TYPO3\CMS\Core\DataHandling\Localization\State\__construct
‪__construct(string $tableName, array $states=[])
Definition: State.php:150
‪TYPO3\CMS\Core\DataHandling\Localization\State\enrich
‪array enrich(array $states)
Definition: State.php:317
‪TYPO3\CMS\Core\DataHandling\Localization\State\filterFieldNames
‪string[] filterFieldNames(string $desiredState, bool $modified=false)
Definition: State.php:281
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_PARENT
‪const STATE_PARENT
Definition: State.php:25
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_CUSTOM
‪const STATE_CUSTOM
Definition: State.php:24
‪TYPO3\CMS\Core\DataHandling\Localization\State
Definition: State.php:23
‪TYPO3\CMS\Core\DataHandling\Localization\State\$originalStates
‪array $originalStates
Definition: State.php:136
‪TYPO3\CMS\Core\DataHandling\Localization\State\hasColumns
‪static bool hasColumns(string $tableName)
Definition: State.php:100
‪TYPO3\CMS\Core\DataHandling\Localization\State\isSourceState
‪bool isSourceState(string $fieldName)
Definition: State.php:260
‪TYPO3\CMS\Core\DataHandling\Localization\State\isApplicable
‪static bool isApplicable(string $tableName)
Definition: State.php:67
‪TYPO3\CMS\Core\DataHandling\Localization\State\getFieldNames
‪static array getFieldNames(string $tableName)
Definition: State.php:81
‪TYPO3\CMS\Core\DataHandling\Localization\State\getState
‪string null getState(string $fieldName)
Definition: State.php:269
‪TYPO3\CMS\Core\DataHandling\Localization\State\$tableName
‪string $tableName
Definition: State.php:128
‪TYPO3\CMS\Core\DataHandling\Localization\State\updateStates
‪updateStates(string $currentState, string $targetState)
Definition: State.php:178
‪TYPO3\CMS\Core\DataHandling\Localization\State\$validStates
‪array $validStates
Definition: State.php:140
‪TYPO3\CMS\Core\DataHandling\Localization\State\getModifiedFieldNames
‪string[] getModifiedFieldNames()
Definition: State.php:211
‪TYPO3\CMS\Core\DataHandling\Localization\State\create
‪static State null create(string $tableName)
Definition: State.php:32
‪TYPO3\CMS\Core\DataHandling\Localization\State\sanitize
‪array sanitize(array $states)
Definition: State.php:302
‪TYPO3\CMS\Core\DataHandling\Localization\State\hasLanguageFieldName
‪static bool hasLanguageFieldName(string $tableName)
Definition: State.php:112
‪TYPO3\CMS\Core\DataHandling\Localization\State\isCustomState
‪bool isCustomState(string $fieldName)
Definition: State.php:242
‪TYPO3\CMS\Core\DataHandling\Localization
Definition: DataMapItem.php:2
‪TYPO3\CMS\Core\DataHandling\Localization\State\isParentState
‪bool isParentState(string $fieldName)
Definition: State.php:251
‪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\State\fromJSON
‪static State null fromJSON(string $tableName, string $json=null)
Definition: State.php:49
‪$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:233
‪TYPO3\CMS\Core\DataHandling\Localization\State\$states
‪array $states
Definition: State.php:132
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\DataHandling\Localization\State\isModified
‪bool isModified()
Definition: State.php:224
‪TYPO3\CMS\Core\DataHandling\Localization\State\export
‪string null export()
Definition: State.php:192
‪TYPO3\CMS\Core\DataHandling\Localization\State\toArray
‪array toArray()
Definition: State.php:203