TYPO3 CMS  TYPO3_8-7
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 
39  static::class,
40  $tableName
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);
57  static::class,
58  $tableName,
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 = [
145  self::STATE_CUSTOM,
146  self::STATE_SOURCE,
147  self::STATE_PARENT,
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 }
static create(string $tableName)
Definition: State.php:32
static hasTranslationParentFieldName(string $tableName)
Definition: State.php:121
static hasLanguageFieldName(string $tableName)
Definition: State.php:112
filterFieldNames(string $desiredState, bool $modified=false)
Definition: State.php:285
static getFieldNames(string $tableName)
Definition: State.php:81
__construct(string $tableName, array $states=[])
Definition: State.php:154
static makeInstance($className,... $constructorArguments)
static fromJSON(string $tableName, string $json=null)
Definition: State.php:49
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
updateStates(string $currentState, string $targetState)
Definition: State.php:182
static isApplicable(string $tableName)
Definition: State.php:67
static hasColumns(string $tableName)
Definition: State.php:100