TYPO3 CMS  TYPO3_7-6
PlainDataResolver.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 
19 
28 {
32  protected $tableName;
33 
37  protected $liveIds;
38 
42  protected $sortingStatement;
43 
47  protected $workspaceId;
48 
52  protected $keepLiveIds = false;
53 
57  protected $keepDeletePlaceholder = false;
58 
62  protected $keepMovePlaceholder = true;
63 
67  protected $resolvedIds;
68 
74  public function __construct($tableName, array $liveIds, $sortingStatement = null)
75  {
76  $this->tableName = $tableName;
77  $this->liveIds = $this->reindex($liveIds);
78  $this->sortingStatement = $sortingStatement;
79  }
80 
86  public function setWorkspaceId($workspaceId)
87  {
88  $this->workspaceId = (int)$workspaceId;
89  }
90 
97  public function setKeepLiveIds($keepLiveIds)
98  {
99  $this->keepLiveIds = (bool)$keepLiveIds;
100  return $this;
101  }
102 
110  {
111  $this->keepDeletePlaceholder = (bool)$keepDeletePlaceholder;
112  return $this;
113  }
114 
122  {
123  $this->keepMovePlaceholder = (bool)$keepMovePlaceholder;
124  return $this;
125  }
126 
130  public function get()
131  {
132  if (isset($this->resolvedIds)) {
133  return $this->resolvedIds;
134  }
135 
136  $this->resolvedIds = $this->processVersionOverlays($this->liveIds);
137  if ($this->resolvedIds !== $this->liveIds) {
138  $this->resolvedIds = $this->reindex($this->resolvedIds);
139  }
140 
141  $tempIds = $this->processSorting($this->resolvedIds);
142  if ($tempIds !== $this->resolvedIds) {
143  $this->resolvedIds = $this->reindex($tempIds);
144  }
145 
146  $tempIds = $this->applyLiveIds($this->resolvedIds);
147  if ($tempIds !== $this->resolvedIds) {
148  $this->resolvedIds = $this->reindex($tempIds);
149  }
150 
151  return $this->resolvedIds;
152  }
153 
161  public function processVersionOverlays(array $ids)
162  {
163  if (empty($this->workspaceId) || !$this->isWorkspaceEnabled() || empty($ids)) {
164  return $ids;
165  }
166 
167  $ids = $this->reindex(
168  $this->processVersionMovePlaceholders($ids)
169  );
170  $versions = $this->getDatabaseConnection()->exec_SELECTgetRows(
171  'uid,t3ver_oid,t3ver_state',
172  $this->tableName,
173  'pid=-1 AND t3ver_oid IN (' . $this->intImplode(',', $ids) . ')'
174  . ' AND t3ver_wsid=' . $this->workspaceId
175  );
176 
177  if (!empty($versions)) {
178  foreach ($versions as $version) {
179  $liveReferenceId = $version['t3ver_oid'];
180  $versionId = $version['uid'];
181  if (isset($ids[$liveReferenceId])) {
182  if (!$this->keepDeletePlaceholder && VersionState::cast($version['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)) {
183  unset($ids[$liveReferenceId]);
184  } else {
185  $ids[$liveReferenceId] = $versionId;
186  }
187  }
188  }
189  }
190 
191  return $ids;
192  }
193 
201  public function processVersionMovePlaceholders(array $ids)
202  {
203  // Early return on insufficient data-set
204  if (empty($this->workspaceId) || !$this->isWorkspaceEnabled() || empty($ids)) {
205  return $ids;
206  }
207 
208  $movePlaceholders = $this->getDatabaseConnection()->exec_SELECTgetRows(
209  'uid,t3ver_move_id',
210  $this->tableName,
211  'pid<>-1 AND t3ver_state=' . VersionState::MOVE_PLACEHOLDER
212  . ' AND t3ver_wsid=' . $this->workspaceId
213  . ' AND t3ver_move_id IN (' . $this->intImplode(',', $ids) . ')'
214  );
215 
216  if (!empty($movePlaceholders)) {
217  foreach ($movePlaceholders as $movePlaceholder) {
218  $liveReferenceId = $movePlaceholder['t3ver_move_id'];
219  $movePlaceholderId = $movePlaceholder['uid'];
220  // Substitute MOVE_PLACEHOLDER and purge live reference
221  if (isset($ids[$movePlaceholderId])) {
222  $ids[$movePlaceholderId] = $liveReferenceId;
223  unset($ids[$liveReferenceId]);
224  // Just purge live reference
225  } elseif (!$this->keepMovePlaceholder) {
226  unset($ids[$liveReferenceId]);
227  }
228  }
229  }
230 
231  return $ids;
232  }
233 
242  public function processSorting(array $ids)
243  {
244  // Early return on missing sorting statement or insufficient data-set
245  if (empty($this->sortingStatement) || count($ids) < 2) {
246  return $ids;
247  }
248 
249  $records = $this->getDatabaseConnection()->exec_SELECTgetRows(
250  'uid',
251  $this->tableName,
252  'uid IN (' . $this->intImplode(',', $ids) . ')',
253  '',
254  $this->sortingStatement,
255  '',
256  'uid'
257  );
258 
259  if (!is_array($records)) {
260  return [];
261  }
262 
263  $ids = array_keys($records);
264  return $ids;
265  }
266 
276  public function applyLiveIds(array $ids)
277  {
278  if (!$this->keepLiveIds || !$this->isWorkspaceEnabled() || empty($ids)) {
279  return $ids;
280  }
281 
282  $records = $this->getDatabaseConnection()->exec_SELECTgetRows(
283  'uid,t3ver_oid',
284  $this->tableName,
285  'uid IN (' . $this->intImplode(',', $ids) . ')',
286  '',
287  '',
288  '',
289  'uid'
290  );
291 
292  if (!is_array($records)) {
293  return [];
294  }
295 
296  foreach ($ids as $id) {
297  if (!empty($records[$id]['t3ver_oid'])) {
298  $ids[$id] = $records[$id]['t3ver_oid'];
299  }
300  }
301 
302  return $ids;
303  }
304 
311  protected function reindex(array $ids)
312  {
313  if (empty($ids)) {
314  return $ids;
315  }
316  $ids = array_values($ids);
317  $ids = array_combine($ids, $ids);
318  return $ids;
319  }
320 
324  protected function isWorkspaceEnabled()
325  {
326  return BackendUtility::isTableWorkspaceEnabled($this->tableName);
327  }
328 
332  protected function isLocalizationEnabled()
333  {
334  return BackendUtility::isTableLocalizable($this->tableName);
335  }
336 
344  protected function intImplode($delimiter, array $values)
345  {
346  return implode($delimiter, $this->getDatabaseConnection()->cleanIntArray($values));
347  }
348 
352  protected function getDatabaseConnection()
353  {
354  return $GLOBALS['TYPO3_DB'];
355  }
356 }
__construct($tableName, array $liveIds, $sortingStatement=null)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']