‪TYPO3CMS  ‪main
State.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
25 class ‪State
26 {
27  public const ‪STATE_CUSTOM = 'custom';
28  public const ‪STATE_PARENT = 'parent';
29  public const ‪STATE_SOURCE = 'source';
30 
31  public static function ‪create(string ‪$tableName): ?‪State
32  {
33  if (!static::isApplicable(‪$tableName)) {
34  return null;
35  }
36 
37  return GeneralUtility::makeInstance(
38  static::class,
40  );
41  }
42 
43  public static function ‪fromJSON(string ‪$tableName, ?string $json = null): ?‪State
44  {
45  if (!static::isApplicable(‪$tableName)) {
46  return null;
47  }
48 
49  ‪$states = json_decode($json ?? '', true);
50  return GeneralUtility::makeInstance(
51  static::class,
53  ‪$states ?? []
54  );
55  }
56 
57  public static function ‪isApplicable(string ‪$tableName): bool
58  {
59  return
60  static::hasColumns(‪$tableName)
61  && static::hasLanguageFieldName(‪$tableName)
62  && static::hasTranslationParentFieldName(‪$tableName)
63  && count(static::getFieldNames(‪$tableName)) > 0
64  ;
65  }
66 
70  public static function ‪getFieldNames(string ‪$tableName): array
71  {
72  return array_keys(
73  array_filter(
74  ‪$GLOBALS['TCA'][‪$tableName]['columns'] ?? [],
75  static function (array $fieldConfiguration): bool {
76  return !empty(
77  $fieldConfiguration['config']
78  ['behaviour']['allowLanguageSynchronization']
79  );
80  }
81  )
82  );
83  }
84 
85  protected static function ‪hasColumns(string ‪$tableName): bool
86  {
87  return
88  !empty(‪$GLOBALS['TCA'][‪$tableName]['columns'])
89  && is_array(‪$GLOBALS['TCA'][‪$tableName]['columns'])
90  ;
91  }
92 
93  protected static function ‪hasLanguageFieldName(string ‪$tableName): bool
94  {
95  return !empty(‪$GLOBALS['TCA'][‪$tableName]['ctrl']['languageField']);
96  }
97 
98  protected static function ‪hasTranslationParentFieldName(string ‪$tableName): bool
99  {
100  return !empty(‪$GLOBALS['TCA'][‪$tableName]['ctrl']['transOrigPointerField']);
101  }
102 
103  protected string ‪$tableName;
104  protected array ‪$states;
105  protected array ‪$originalStates;
106  protected array ‪$validStates = [
110  ];
111 
112  public function ‪__construct(string ‪$tableName, array ‪$states = [])
113  {
114  $this->tableName = ‪$tableName;
115  $this->states = ‪$states;
116  $this->originalStates = ‪$states;
117 
118  $this->states = $this->‪enrich(
119  $this->‪sanitize($states)
120  );
121  }
122 
123  public function ‪update(array ‪$states): void
124  {
125  $this->states = array_merge(
126  $this->states,
127  $this->‪sanitize($states)
128  );
129  }
130 
134  public function ‪updateStates(string $currentState, string $targetState): void
135  {
136  ‪$states = [];
137  foreach ($this->‪filterFieldNames($currentState) as $fieldName) {
138  ‪$states[$fieldName] = $targetState;
139  }
140  if (!empty(‪$states)) {
141  $this->‪update($states);
142  }
143  }
144 
145  public function ‪export(): string|false|null
146  {
147  if (empty($this->states)) {
148  return null;
149  }
150  return json_encode($this->states);
151  }
152 
153  public function ‪toArray(): array
154  {
155  return $this->states ?? [];
156  }
157 
161  public function ‪getModifiedFieldNames(): array
162  {
163  return array_keys(
164  array_diff_assoc(
165  $this->states,
166  $this->originalStates
167  )
168  );
169  }
170 
171  public function ‪isModified(): bool
172  {
173  return !empty($this->‪getModifiedFieldNames());
174  }
175 
176  public function ‪isUndefined(string $fieldName): bool
177  {
178  return !isset($this->states[$fieldName]);
179  }
180 
181  public function ‪isCustomState(string $fieldName): bool
182  {
183  return ($this->states[$fieldName] ?? null) === static::STATE_CUSTOM;
184  }
185 
186  public function ‪isParentState(string $fieldName): bool
187  {
188  return ($this->states[$fieldName] ?? null) === static::STATE_PARENT;
189  }
190 
191  public function ‪isSourceState(string $fieldName): bool
192  {
193  return ($this->states[$fieldName] ?? null) === static::STATE_SOURCE;
194  }
195 
196  public function ‪getState(string $fieldName): ?string
197  {
198  return $this->states[$fieldName] ?? null;
199  }
200 
206  public function ‪filterFieldNames(string $desiredState, bool $modified = false): array
207  {
208  if (!$modified) {
209  $fieldNames = array_keys($this->states);
210  } else {
211  $fieldNames = $this->‪getModifiedFieldNames();
212  }
213  return array_filter(
214  $fieldNames,
215  function (string $fieldName) use ($desiredState): bool {
216  return $this->states[$fieldName] === $desiredState;
217  }
218  );
219  }
220 
226  protected function ‪sanitize(array ‪$states): array
227  {
228  $fieldNames = static::getFieldNames($this->tableName);
229  return array_intersect_key(
230  ‪$states,
231  array_combine($fieldNames, $fieldNames) ?: []
232  );
233  }
234 
238  protected function ‪enrich(array ‪$states): array
239  {
240  foreach (static::getFieldNames($this->tableName) as $fieldName) {
241  $isValid = in_array(
242  ‪$states[$fieldName] ?? null,
243  $this->validStates,
244  true
245  );
246  if ($isValid) {
247  continue;
248  }
249  ‪$states[$fieldName] = static::STATE_PARENT;
250  }
251  return ‪$states;
252  }
253 }
‪TYPO3\CMS\Core\DataHandling\Localization\State\isUndefined
‪isUndefined(string $fieldName)
Definition: State.php:176
‪TYPO3\CMS\Core\DataHandling\Localization\State\hasLanguageFieldName
‪static hasLanguageFieldName(string $tableName)
Definition: State.php:93
‪TYPO3\CMS\Core\DataHandling\Localization\State\isParentState
‪isParentState(string $fieldName)
Definition: State.php:186
‪TYPO3\CMS\Core\DataHandling\Localization\State\__construct
‪__construct(string $tableName, array $states=[])
Definition: State.php:112
‪TYPO3\CMS\Core\DataHandling\Localization\State\enrich
‪enrich(array $states)
Definition: State.php:238
‪TYPO3\CMS\Core\DataHandling\Localization\State\toArray
‪toArray()
Definition: State.php:153
‪TYPO3\CMS\Core\DataHandling\Localization\State\filterFieldNames
‪string[] filterFieldNames(string $desiredState, bool $modified=false)
Definition: State.php:206
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_PARENT
‪const STATE_PARENT
Definition: State.php:28
‪TYPO3\CMS\Core\DataHandling\Localization\State\hasTranslationParentFieldName
‪static hasTranslationParentFieldName(string $tableName)
Definition: State.php:98
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_CUSTOM
‪const STATE_CUSTOM
Definition: State.php:27
‪TYPO3\CMS\Core\DataHandling\Localization\State\create
‪static create(string $tableName)
Definition: State.php:31
‪TYPO3\CMS\Core\DataHandling\Localization\State
Definition: State.php:26
‪TYPO3\CMS\Core\DataHandling\Localization\State\getFieldNames
‪static string[] getFieldNames(string $tableName)
Definition: State.php:70
‪TYPO3\CMS\Core\DataHandling\Localization\State\$originalStates
‪array $originalStates
Definition: State.php:105
‪TYPO3\CMS\Core\DataHandling\Localization\State\getState
‪getState(string $fieldName)
Definition: State.php:196
‪TYPO3\CMS\Core\DataHandling\Localization\State\isSourceState
‪isSourceState(string $fieldName)
Definition: State.php:191
‪TYPO3\CMS\Core\DataHandling\Localization\State\$tableName
‪string $tableName
Definition: State.php:103
‪TYPO3\CMS\Core\DataHandling\Localization\State\updateStates
‪updateStates(string $currentState, string $targetState)
Definition: State.php:134
‪TYPO3\CMS\Core\DataHandling\Localization\State\$validStates
‪array $validStates
Definition: State.php:106
‪TYPO3\CMS\Core\DataHandling\Localization\State\getModifiedFieldNames
‪string[] getModifiedFieldNames()
Definition: State.php:161
‪TYPO3\CMS\Core\DataHandling\Localization\State\isCustomState
‪isCustomState(string $fieldName)
Definition: State.php:181
‪TYPO3\CMS\Core\DataHandling\Localization\State\isModified
‪isModified()
Definition: State.php:171
‪TYPO3\CMS\Core\DataHandling\Localization\State\isApplicable
‪static isApplicable(string $tableName)
Definition: State.php:57
‪TYPO3\CMS\Core\DataHandling\Localization
Definition: DataMapItem.php:16
‪TYPO3\CMS\Core\DataHandling\Localization\State\fromJSON
‪static fromJSON(string $tableName, ?string $json=null)
Definition: State.php:43
‪TYPO3\CMS\Core\DataHandling\Localization\State\STATE_SOURCE
‪const STATE_SOURCE
Definition: State.php:29
‪TYPO3\CMS\Core\DataHandling\Localization\State\update
‪update(array $states)
Definition: State.php:123
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\DataHandling\Localization\State\$states
‪array $states
Definition: State.php:104
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\DataHandling\Localization\State\export
‪export()
Definition: State.php:145
‪TYPO3\CMS\Core\DataHandling\Localization\State\hasColumns
‪static hasColumns(string $tableName)
Definition: State.php:85
‪TYPO3\CMS\Core\DataHandling\Localization\State\sanitize
‪string[] sanitize(array $states)
Definition: State.php:226