‪TYPO3CMS  10.4
CollectionService.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
27 
32 {
36  protected ‪$dataHandler;
37 
42 
46  protected ‪$dependencyResolver;
47 
51  protected ‪$dataArray;
52 
57 
61  public function ‪getDependencyResolver()
62  {
63  if (!isset($this->dependencyResolver)) {
64  $this->dependencyResolver = GeneralUtility::makeInstance(DependencyResolver::class);
65  $this->dependencyResolver->setOuterMostParentsRequireReferences(true);
66  $this->dependencyResolver->setWorkspace($this->‪getWorkspace());
67 
68  $this->dependencyResolver->setEventCallback(
70  $this->‪getDependencyCallback('createNewDependentElementCallback')
71  );
72 
73  $this->dependencyResolver->setEventCallback(
75  $this->‪getDependencyCallback('createNewDependentElementChildReferenceCallback')
76  );
77 
78  $this->dependencyResolver->setEventCallback(
80  $this->‪getDependencyCallback('createNewDependentElementParentReferenceCallback')
81  );
82  }
83 
85  }
86 
94  protected function ‪getDependencyCallback($method, array $targetArguments = [])
95  {
96  return GeneralUtility::makeInstance(
97  EventCallback::class,
99  $method,
100  $targetArguments
101  );
102  }
103 
109  protected function ‪getElementEntityProcessor()
110  {
111  if (!isset($this->elementEntityProcessor)) {
112  $this->elementEntityProcessor = GeneralUtility::makeInstance(ElementEntityProcessor::class);
113  $this->elementEntityProcessor->setWorkspace($this->‪getWorkspace());
114  }
116  }
117 
123  protected function ‪getWorkspace()
124  {
125  return (int)‪$GLOBALS['BE_USER']->workspace;
126  }
127 
134  public function ‪process(array ‪$dataArray)
135  {
136  $collection = 0;
137  $this->dataArray = ‪$dataArray;
138  $this->nestedDataArray = [];
139 
140  $outerMostParents = $this->‪getDependencyResolver()->‪getOuterMostParents();
141 
142  if (empty($outerMostParents)) {
143  return ‪$this->dataArray;
144  }
145 
146  // For each outer most parent, get all nested child elements:
147  foreach ($outerMostParents as $outerMostParent) {
149  $outerMostParent,
150  ++$collection
151  );
152  }
153 
154  $processedDataArray = $this->‪finalize($this->dataArray);
155 
156  unset($this->dataArray);
157  unset($this->nestedDataArray);
158 
159  return $processedDataArray;
160  }
161 
169  protected function ‪finalize(array ‪$dataArray)
170  {
171  $processedDataArray = [];
172  foreach (‪$dataArray as $dataElement) {
173  $dataElementIdentifier = $dataElement['id'];
174  $processedDataArray[] = $dataElement;
175  // Insert children (if any)
176  if (!empty($this->nestedDataArray[$dataElementIdentifier])) {
177  $processedDataArray = array_merge(
178  $processedDataArray,
179  $this->‪finalize($this->nestedDataArray[$dataElementIdentifier])
180  );
181  unset($this->nestedDataArray[$dataElementIdentifier]);
182  }
183  }
184 
185  return $processedDataArray;
186  }
187 
196  protected function ‪resolveDataArrayChildDependencies(ElementEntity $parent, $collection, $nextParentIdentifier = '', $collectionLevel = 0)
197  {
198  $parentIdentifier = $parent->__toString();
199  $parentIsSet = isset($this->dataArray[$parentIdentifier]);
200 
201  if ($parentIsSet) {
202  $this->dataArray[$parentIdentifier][‪GridDataService::GridColumn_Collection] = $collection;
203  $this->dataArray[$parentIdentifier][‪GridDataService::GridColumn_CollectionLevel] = $collectionLevel;
204  $this->dataArray[$parentIdentifier][‪GridDataService::GridColumn_CollectionCurrent] = md5($parentIdentifier);
205  $this->dataArray[$parentIdentifier][‪GridDataService::GridColumn_CollectionChildren] = $this->‪getCollectionChildrenCount($parent->getChildren());
206  $nextParentIdentifier = $parentIdentifier;
207  $collectionLevel++;
208  }
209 
210  foreach ($parent->getChildren() as $child) {
212  $child->getElement(),
213  $collection,
214  $nextParentIdentifier,
215  $collectionLevel
216  );
217 
218  $childIdentifier = $child->getElement()->__toString();
219  if (!empty($nextParentIdentifier) && isset($this->dataArray[$childIdentifier])) {
220  // Remove from dataArray, but collect to process later
221  // and add it just next to the accordant parent element
222  $this->dataArray[$childIdentifier][‪GridDataService::GridColumn_CollectionParent] = md5($nextParentIdentifier);
223  $this->nestedDataArray[$nextParentIdentifier][] = $this->dataArray[$childIdentifier];
224  unset($this->dataArray[$childIdentifier]);
225  }
226  }
227  }
228 
235  protected function ‪getCollectionChildrenCount(array $children): int
236  {
237  return count(
238  array_filter($children, function (ReferenceEntity $child) {
239  return isset($this->dataArray[$child->getElement()->__toString()]);
240  })
241  );
242  }
243 }
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\getCollectionChildrenCount
‪int getCollectionChildrenCount(array $children)
Definition: CollectionService.php:230
‪TYPO3\CMS\Workspaces\Dependency\ReferenceEntity
Definition: ReferenceEntity.php:22
‪TYPO3\CMS\Workspaces\Dependency\ReferenceEntity\getElement
‪ElementEntity getElement()
Definition: ReferenceEntity.php:48
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\__toString
‪string __toString()
Definition: ElementEntity.php:192
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\$dataArray
‪array $dataArray
Definition: CollectionService.php:47
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity
Definition: ElementEntity.php:25
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\resolveDataArrayChildDependencies
‪resolveDataArrayChildDependencies(ElementEntity $parent, $collection, $nextParentIdentifier='', $collectionLevel=0)
Definition: CollectionService.php:191
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\$dataHandler
‪TYPO3 CMS Core DataHandling DataHandler $dataHandler
Definition: CollectionService.php:35
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\$nestedDataArray
‪array $nestedDataArray
Definition: CollectionService.php:51
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver
Definition: DependencyResolver.php:24
‪TYPO3\CMS\Workspaces\Dependency\EventCallback
Definition: EventCallback.php:22
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_Construct
‪const EVENT_Construct
Definition: ElementEntity.php:28
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\getElementEntityProcessor
‪Dependency ElementEntityProcessor getElementEntityProcessor()
Definition: CollectionService.php:104
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\getDependencyResolver
‪Dependency DependencyResolver getDependencyResolver()
Definition: CollectionService.php:56
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\getWorkspace
‪int getWorkspace()
Definition: CollectionService.php:118
‪TYPO3\CMS\Workspaces\Service\GridDataService\GridColumn_CollectionCurrent
‪const GridColumn_CollectionCurrent
Definition: GridDataService.php:65
‪TYPO3\CMS\Workspaces\Dependency
Definition: DependencyEntityFactory.php:16
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService
Definition: CollectionService.php:32
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\$elementEntityProcessor
‪Dependency ElementEntityProcessor $elementEntityProcessor
Definition: CollectionService.php:39
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_CreateChildReference
‪const EVENT_CreateChildReference
Definition: ElementEntity.php:29
‪TYPO3\CMS\Workspaces\Service\Dependency
Definition: CollectionService.php:16
‪TYPO3\CMS\Workspaces\Service\GridDataService\GridColumn_CollectionLevel
‪const GridColumn_CollectionLevel
Definition: GridDataService.php:63
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getOuterMostParents
‪array ElementEntity[] getOuterMostParents()
Definition: DependencyResolver.php:133
‪TYPO3\CMS\Workspaces\Dependency\ElementEntityProcessor
Definition: ElementEntityProcessor.php:27
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_CreateParentReference
‪const EVENT_CreateParentReference
Definition: ElementEntity.php:30
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\getDependencyCallback
‪Dependency EventCallback getDependencyCallback($method, array $targetArguments=[])
Definition: CollectionService.php:89
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getChildren
‪array ReferenceEntity[] getChildren()
Definition: ElementEntity.php:212
‪TYPO3\CMS\Workspaces\Service\GridDataService\GridColumn_Collection
‪const GridColumn_Collection
Definition: GridDataService.php:62
‪TYPO3\CMS\Workspaces\Service\GridDataService\GridColumn_CollectionParent
‪const GridColumn_CollectionParent
Definition: GridDataService.php:64
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\process
‪array process(array $dataArray)
Definition: CollectionService.php:129
‪TYPO3\CMS\Workspaces\Service\GridDataService\GridColumn_CollectionChildren
‪const GridColumn_CollectionChildren
Definition: GridDataService.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\finalize
‪array finalize(array $dataArray)
Definition: CollectionService.php:164
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\$dependencyResolver
‪Dependency DependencyResolver $dependencyResolver
Definition: CollectionService.php:43
‪TYPO3\CMS\Workspaces\Service\GridDataService
Definition: GridDataService.php:42