TYPO3 CMS  TYPO3_8-7
CollectionService.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 
20 
25 {
29  protected $dataHandler;
30 
35 
40 
44  protected $dataArray;
45 
49  protected $nestedDataArray;
50 
54  public function getDependencyResolver()
55  {
56  if (!isset($this->dependencyResolver)) {
57  $this->dependencyResolver = GeneralUtility::makeInstance(\TYPO3\CMS\Version\Dependency\DependencyResolver::class);
58  $this->dependencyResolver->setOuterMostParentsRequireReferences(true);
59  $this->dependencyResolver->setWorkspace($this->getWorkspace());
60 
61  $this->dependencyResolver->setEventCallback(
63  $this->getDependencyCallback('createNewDependentElementCallback')
64  );
65 
66  $this->dependencyResolver->setEventCallback(
68  $this->getDependencyCallback('createNewDependentElementChildReferenceCallback')
69  );
70 
71  $this->dependencyResolver->setEventCallback(
73  $this->getDependencyCallback('createNewDependentElementParentReferenceCallback')
74  );
75  }
76 
78  }
79 
87  protected function getDependencyCallback($method, array $targetArguments = [])
88  {
90  \TYPO3\CMS\Version\Dependency\EventCallback::class,
92  $method,
93  $targetArguments
94  );
95  }
96 
102  protected function getElementEntityProcessor()
103  {
104  if (!isset($this->elementEntityProcessor)) {
105  $this->elementEntityProcessor = GeneralUtility::makeInstance(Dependency\ElementEntityProcessor::class);
106  $this->elementEntityProcessor->setWorkspace($this->getWorkspace());
107  }
109  }
110 
116  protected function getWorkspace()
117  {
118  return (int)$GLOBALS['BE_USER']->workspace;
119  }
120 
127  public function process(array $dataArray)
128  {
129  $collection = 0;
130  $this->dataArray = $dataArray;
131  $this->nestedDataArray = [];
132 
133  $outerMostParents = $this->getDependencyResolver()->getOuterMostParents();
134 
135  if (empty($outerMostParents)) {
136  return $this->dataArray;
137  }
138 
139  // For each outer most parent, get all nested child elements:
140  foreach ($outerMostParents as $outerMostParent) {
142  $outerMostParent,
143  ++$collection
144  );
145  }
146 
147  $processedDataArray = $this->finalize($this->dataArray);
148 
149  unset($this->dataArray);
150  unset($this->nestedDataArray);
151 
152  return $processedDataArray;
153  }
154 
162  protected function finalize(array $dataArray)
163  {
164  $processedDataArray = [];
165  foreach ($dataArray as $dataElement) {
166  $dataElementIdentifier = $dataElement['id'];
167  $processedDataArray[] = $dataElement;
168  // Insert children (if any)
169  if (!empty($this->nestedDataArray[$dataElementIdentifier])) {
170  $processedDataArray = array_merge(
171  $processedDataArray,
172  $this->finalize($this->nestedDataArray[$dataElementIdentifier])
173  );
174  unset($this->nestedDataArray[$dataElementIdentifier]);
175  }
176  }
177 
178  return $processedDataArray;
179  }
180 
189  protected function resolveDataArrayChildDependencies(Dependency\ElementEntity $parent, $collection, $nextParentIdentifier = '', $collectionLevel = 0)
190  {
191  $parentIdentifier = $parent->__toString();
192  $parentIsSet = isset($this->dataArray[$parentIdentifier]);
193 
194  if ($parentIsSet) {
195  $this->dataArray[$parentIdentifier][GridDataService::GridColumn_Collection] = $collection;
196  $this->dataArray[$parentIdentifier][GridDataService::GridColumn_CollectionLevel] = $collectionLevel;
197  $this->dataArray[$parentIdentifier][GridDataService::GridColumn_CollectionCurrent] = md5($parentIdentifier);
198  $this->dataArray[$parentIdentifier][GridDataService::GridColumn_CollectionChildren] = count($parent->getChildren());
199  $nextParentIdentifier = $parentIdentifier;
200  $collectionLevel++;
201  }
202 
203  foreach ($parent->getChildren() as $child) {
205  $child->getElement(),
206  $collection,
207  $nextParentIdentifier,
208  $collectionLevel
209  );
210 
211  $childIdentifier = $child->getElement()->__toString();
212  if (!empty($nextParentIdentifier) && isset($this->dataArray[$childIdentifier])) {
213  // Remove from dataArray, but collect to process later
214  // and add it just next to the accordant parent element
215  $this->dataArray[$childIdentifier][GridDataService::GridColumn_CollectionParent] = md5($nextParentIdentifier);
216  $this->nestedDataArray[$nextParentIdentifier][] = $this->dataArray[$childIdentifier];
217  unset($this->dataArray[$childIdentifier]);
218  }
219  }
220  }
221 }
getDependencyCallback($method, array $targetArguments=[])
static makeInstance($className,... $constructorArguments)
resolveDataArrayChildDependencies(Dependency\ElementEntity $parent, $collection, $nextParentIdentifier='', $collectionLevel=0)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']