‪TYPO3CMS  ‪main
CollectionService.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 
29 
36 {
39  protected array ‪$dataArray;
40  protected array ‪$nestedDataArray;
41 
43  {
44  if (!isset($this->dependencyResolver)) {
45  $this->dependencyResolver = GeneralUtility::makeInstance(DependencyResolver::class);
46  $this->dependencyResolver->setOuterMostParentsRequireReferences(true);
47  $this->dependencyResolver->setWorkspace($this->‪getBackendUser()->workspace);
48 
49  $this->dependencyResolver->setEventCallback(
51  $this->‪getDependencyCallback('createNewDependentElementCallback')
52  );
53 
54  $this->dependencyResolver->setEventCallback(
56  $this->‪getDependencyCallback('createNewDependentElementChildReferenceCallback')
57  );
58 
59  $this->dependencyResolver->setEventCallback(
61  $this->‪getDependencyCallback('createNewDependentElementParentReferenceCallback')
62  );
63  }
64 
66  }
67 
71  protected function ‪getDependencyCallback(string $method, array $targetArguments = []): ‪EventCallback
72  {
73  return GeneralUtility::makeInstance(
74  EventCallback::class,
76  $method,
77  $targetArguments
78  );
79  }
80 
85  {
86  if (!isset($this->elementEntityProcessor)) {
87  $this->elementEntityProcessor = GeneralUtility::makeInstance(ElementEntityProcessor::class);
88  $this->elementEntityProcessor->setWorkspace($this->‪getBackendUser()->workspace);
89  }
91  }
92 
96  public function ‪process(array ‪$dataArray): array
97  {
98  $collection = 0;
99  $this->dataArray = ‪$dataArray;
100  $this->nestedDataArray = [];
101 
102  $outerMostParents = $this->‪getDependencyResolver()->getOuterMostParents();
103 
104  if (empty($outerMostParents)) {
105  return ‪$this->dataArray;
106  }
107 
108  // For each outer most parent, get all nested child elements:
109  foreach ($outerMostParents as $outerMostParent) {
111  $outerMostParent,
112  ++$collection
113  );
114  }
115 
116  $processedDataArray = $this->‪finalize($this->dataArray);
117 
118  unset($this->dataArray);
119  unset($this->nestedDataArray);
120 
121  return $processedDataArray;
122  }
123 
128  protected function ‪finalize(array ‪$dataArray): array
129  {
130  $processedDataArray = [];
131  foreach (‪$dataArray as $dataElement) {
132  $dataElementIdentifier = $dataElement['id'];
133  $processedDataArray[] = $dataElement;
134  // Insert children (if any)
135  if (!empty($this->nestedDataArray[$dataElementIdentifier])) {
136  $processedDataArray = array_merge(
137  $processedDataArray,
138  $this->‪finalize($this->nestedDataArray[$dataElementIdentifier])
139  );
140  unset($this->nestedDataArray[$dataElementIdentifier]);
141  }
142  }
143 
144  return $processedDataArray;
145  }
146 
150  protected function ‪resolveDataArrayChildDependencies(‪ElementEntity $parent, int $collection, string $nextParentIdentifier = '', int $collectionLevel = 0): void
151  {
152  $parentIdentifier = $parent->‪__toString();
153  $parentIsSet = isset($this->dataArray[$parentIdentifier]);
154 
155  if ($parentIsSet) {
156  $this->dataArray[$parentIdentifier][‪GridDataService::GridColumn_Collection] = $collection;
157  $this->dataArray[$parentIdentifier][‪GridDataService::GridColumn_CollectionLevel] = $collectionLevel;
158  $this->dataArray[$parentIdentifier][‪GridDataService::GridColumn_CollectionCurrent] = md5($parentIdentifier);
159  $this->dataArray[$parentIdentifier][‪GridDataService::GridColumn_CollectionChildren] = $this->‪getCollectionChildrenCount($parent->‪getChildren());
160  $nextParentIdentifier = $parentIdentifier;
161  $collectionLevel++;
162  }
163 
164  foreach ($parent->‪getChildren() as $child) {
166  $child->getElement(),
167  $collection,
168  $nextParentIdentifier,
169  $collectionLevel
170  );
171 
172  $childIdentifier = $child->getElement()->__toString();
173  if (!empty($nextParentIdentifier) && isset($this->dataArray[$childIdentifier])) {
174  // Remove from dataArray, but collect to process later
175  // and add it just next to the accordant parent element
176  $this->dataArray[$childIdentifier][‪GridDataService::GridColumn_CollectionParent] = md5($nextParentIdentifier);
177  $this->nestedDataArray[$nextParentIdentifier][] = $this->dataArray[$childIdentifier];
178  unset($this->dataArray[$childIdentifier]);
179  }
180  }
181  }
182 
188  protected function ‪getCollectionChildrenCount(array $children): int
189  {
190  return count(
191  array_filter($children, function (‪ReferenceEntity $child) {
192  return isset($this->dataArray[$child->‪getElement()->__toString()]);
193  })
194  );
195  }
196 
198  {
199  return ‪$GLOBALS['BE_USER'];
200  }
201 }
‪TYPO3\CMS\Workspaces\Dependency\ReferenceEntity\getElement
‪getElement()
Definition: ReferenceEntity.php:39
‪TYPO3\CMS\Workspaces\Dependency\ReferenceEntity
Definition: ReferenceEntity.php:26
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\getCollectionChildrenCount
‪getCollectionChildrenCount(array $children)
Definition: CollectionService.php:188
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\$elementEntityProcessor
‪ElementEntityProcessor $elementEntityProcessor
Definition: CollectionService.php:37
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\$dataArray
‪array $dataArray
Definition: CollectionService.php:39
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity
Definition: ElementEntity.php:30
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\__toString
‪__toString()
Definition: ElementEntity.php:132
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\getDependencyCallback
‪getDependencyCallback(string $method, array $targetArguments=[])
Definition: CollectionService.php:71
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\$nestedDataArray
‪array $nestedDataArray
Definition: CollectionService.php:40
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver
Definition: DependencyResolver.php:28
‪TYPO3\CMS\Workspaces\Dependency\EventCallback
Definition: EventCallback.php:26
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_Construct
‪const EVENT_Construct
Definition: ElementEntity.php:33
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\getDependencyResolver
‪getDependencyResolver()
Definition: CollectionService.php:42
‪TYPO3\CMS\Workspaces\Service\GridDataService\GridColumn_CollectionCurrent
‪const GridColumn_CollectionCurrent
Definition: GridDataService.php:54
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\process
‪process(array $dataArray)
Definition: CollectionService.php:96
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService
Definition: CollectionService.php:36
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_CreateChildReference
‪const EVENT_CreateChildReference
Definition: ElementEntity.php:34
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\getBackendUser
‪getBackendUser()
Definition: CollectionService.php:197
‪TYPO3\CMS\Workspaces\Service\Dependency
Definition: CollectionService.php:18
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\getElementEntityProcessor
‪getElementEntityProcessor()
Definition: CollectionService.php:84
‪TYPO3\CMS\Workspaces\Service\GridDataService\GridColumn_CollectionLevel
‪const GridColumn_CollectionLevel
Definition: GridDataService.php:52
‪TYPO3\CMS\Workspaces\Dependency\ElementEntityProcessor
Definition: ElementEntityProcessor.php:31
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_CreateParentReference
‪const EVENT_CreateParentReference
Definition: ElementEntity.php:35
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\resolveDataArrayChildDependencies
‪resolveDataArrayChildDependencies(ElementEntity $parent, int $collection, string $nextParentIdentifier='', int $collectionLevel=0)
Definition: CollectionService.php:150
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Workspaces\Service\GridDataService\GridColumn_Collection
‪const GridColumn_Collection
Definition: GridDataService.php:51
‪TYPO3\CMS\Workspaces\Service\GridDataService\GridColumn_CollectionParent
‪const GridColumn_CollectionParent
Definition: GridDataService.php:53
‪TYPO3\CMS\Workspaces\Service\GridDataService\GridColumn_CollectionChildren
‪const GridColumn_CollectionChildren
Definition: GridDataService.php:55
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getChildren
‪ReferenceEntity[] getChildren()
Definition: ElementEntity.php:150
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\finalize
‪finalize(array $dataArray)
Definition: CollectionService.php:128
‪TYPO3\CMS\Workspaces\Service\GridDataService
Definition: GridDataService.php:48
‪TYPO3\CMS\Workspaces\Service\Dependency\CollectionService\$dependencyResolver
‪DependencyResolver $dependencyResolver
Definition: CollectionService.php:38