‪TYPO3CMS  ‪main
ElementEntity.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 
23 
30 {
31  public const ‪REFERENCES_ChildOf = 'childOf';
32  public const ‪REFERENCES_ParentOf = 'parentOf';
33  public const ‪EVENT_Construct = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::construct';
34  public const ‪EVENT_CreateChildReference = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::createChildReference';
35  public const ‪EVENT_CreateParentReference = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::createParentReference';
36  public const ‪RESPONSE_Skip = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity->skip';
37 
38  protected bool ‪$invalid = false;
39  protected string ‪$table;
40  protected int ‪$id;
41  protected array ‪$data;
42  protected array ‪$record;
44  protected ?array ‪$children;
45  protected ?array ‪$parents;
46  protected ‪ElementEntity|false|null ‪$outerMostParent;
47 
48  protected ?array ‪$nestedChildren;
49 
51  {
52  $this->table = ‪$table;
53  $this->id = ‪$id;
54  $this->data = ‪$data;
55  $this->dependency = ‪$dependency;
56  $this->dependency->‪executeEventCallback(self::EVENT_Construct, $this);
57  }
58 
59  public function ‪setInvalid(bool ‪$invalid): void
60  {
61  $this->invalid = ‪$invalid;
62  }
63 
64  public function ‪isInvalid(): bool
65  {
66  return ‪$this->invalid;
67  }
68 
72  public function ‪getTable(): string
73  {
74  return ‪$this->table;
75  }
76 
80  public function ‪getId(): int
81  {
82  return ‪$this->id;
83  }
84 
88  public function ‪setId(int ‪$id): void
89  {
90  $this->id = ‪$id;
91  }
92 
96  public function ‪getData(): array
97  {
98  return ‪$this->data;
99  }
100 
104  public function ‪getDataValue(string $key): mixed
105  {
106  $result = null;
107  if ($this->‪hasDataValue($key)) {
108  $result = $this->data[$key];
109  }
110  return $result;
111  }
112 
116  public function ‪setDataValue(string $key, mixed $value): void
117  {
118  $this->data[$key] = $value;
119  }
120 
124  public function ‪hasDataValue(string $key): bool
125  {
126  return isset($this->data[$key]);
127  }
128 
132  public function ‪__toString(): string
133  {
134  return ‪self::getIdentifier($this->table, $this->id);
135  }
136 
141  {
142  return ‪$this->dependency;
143  }
144 
150  public function ‪getChildren(): array
151  {
152  if (!isset($this->children)) {
153  $this->children = [];
154 
155  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
156  ->getQueryBuilderForTable('sys_refindex');
157 
158  $result = $queryBuilder
159  ->select('*')
160  ->from('sys_refindex')
161  ->where(
162  $queryBuilder->expr()->eq(
163  'tablename',
164  $queryBuilder->createNamedParameter($this->table)
165  ),
166  $queryBuilder->expr()->eq(
167  'recuid',
168  $queryBuilder->createNamedParameter($this->id, ‪Connection::PARAM_INT)
169  ),
170  $queryBuilder->expr()->eq(
171  'workspace',
172  $queryBuilder->createNamedParameter($this->dependency->getWorkspace(), ‪Connection::PARAM_INT)
173  )
174  )
175  ->orderBy('sorting')
176  ->executeQuery();
177 
178  while ($row = $result->fetchAssociative()) {
179  if ($row['ref_table'] !== '_FILE' && $row['ref_table'] !== '_STRING') {
180  $arguments = [
181  'table' => $row['ref_table'],
182  'id' => $row['ref_uid'],
183  'field' => $row['field'],
184  'scope' => ‪self::REFERENCES_ChildOf,
185  ];
186 
187  $callbackResponse = $this->dependency->executeEventCallback(
188  self::EVENT_CreateChildReference,
189  $this,
190  $arguments
191  );
192  if ($callbackResponse !== self::RESPONSE_Skip) {
193  $this->children[] = $this->‪getDependency()->getFactory()->getReferencedElement(
194  $row['ref_table'],
195  $row['ref_uid'],
196  $row['field'],
197  [],
198  $this->‪getDependency()
199  );
200  }
201  }
202  }
203  }
204  return ‪$this->children;
205  }
206 
212  public function ‪getParents(): array
213  {
214  if (!isset($this->parents)) {
215  $this->parents = [];
216 
217  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
218  ->getQueryBuilderForTable('sys_refindex');
219 
220  $result = $queryBuilder
221  ->select('*')
222  ->from('sys_refindex')
223  ->where(
224  $queryBuilder->expr()->eq(
225  'ref_table',
226  $queryBuilder->createNamedParameter($this->table)
227  ),
228  $queryBuilder->expr()->eq(
229  'ref_uid',
230  $queryBuilder->createNamedParameter($this->id, ‪Connection::PARAM_INT)
231  ),
232  $queryBuilder->expr()->eq(
233  'workspace',
234  $queryBuilder->createNamedParameter($this->dependency->getWorkspace(), ‪Connection::PARAM_INT)
235  )
236  )
237  ->orderBy('sorting')
238  ->executeQuery();
239 
240  while ($row = $result->fetchAssociative()) {
241  $arguments = [
242  'table' => $row['tablename'],
243  'id' => $row['recuid'],
244  'field' => $row['field'],
245  'scope' => ‪self::REFERENCES_ParentOf,
246  ];
247  $callbackResponse = $this->dependency->executeEventCallback(
248  self::EVENT_CreateParentReference,
249  $this,
250  $arguments
251  );
252  if ($callbackResponse !== self::RESPONSE_Skip) {
253  $this->parents[] = $this->‪getDependency()->getFactory()->getReferencedElement(
254  $row['tablename'],
255  $row['recuid'],
256  $row['field'],
257  [],
258  $this->‪getDependency()
259  );
260  }
261  }
262  }
263  return ‪$this->parents;
264  }
265 
269  public function ‪hasReferences(): bool
270  {
271  return !empty($this->‪getChildren()) || !empty($this->‪getParents());
272  }
273 
277  public function ‪getOuterMostParent(): false|‪ElementEntity
278  {
279  if (!isset($this->outerMostParent)) {
280  ‪$parents = $this->‪getParents();
281  if (empty(‪$parents)) {
282  $this->outerMostParent = $this;
283  } else {
284  $this->outerMostParent = false;
286  foreach (‪$parents as $parent) {
287  ‪$outerMostParent = $parent->getElement()->‪getOuterMostParent();
288  if (‪$outerMostParent instanceof ‪ElementEntity) {
289  $this->outerMostParent = ‪$outerMostParent;
290  break;
291  }
292  if (‪$outerMostParent === false) {
293  break;
294  }
295  }
296  }
297  }
299  }
300 
306  public function ‪getNestedChildren(): array
307  {
308  if (!isset($this->nestedChildren)) {
309  $this->nestedChildren = [];
310  ‪$children = $this->‪getChildren();
312  foreach ($children as $child) {
313  $this->nestedChildren = array_merge($this->nestedChildren, [$child->getElement()->__toString() => $child->getElement()], $child->getElement()->getNestedChildren());
314  }
315  }
317  }
318 
322  public static function ‪getIdentifier(string ‪$table, int ‪$id): string
323  {
324  return ‪$table . ':' . ‪$id;
325  }
326 
330  public function ‪getRecord(): array
331  {
332  if (empty($this->record['uid']) || (int)$this->record['uid'] !== $this->‪getId()) {
333  $this->record = [];
334 
335  $fieldNames = ['uid', 'pid', 't3ver_wsid', 't3ver_state', 't3ver_oid'];
336  if (!empty(‪$GLOBALS['TCA'][$this->‪getTable()]['ctrl']['delete'])) {
337  $fieldNames[] = ‪$GLOBALS['TCA'][$this->‪getTable()]['ctrl']['delete'];
338  }
339 
340  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
341  ->getQueryBuilderForTable($this->‪getTable());
342  $queryBuilder->getRestrictions()->removeAll();
343 
344  $row = $queryBuilder
345  ->select(...$fieldNames)
346  ->from($this->‪getTable())
347  ->where(
348  $queryBuilder->expr()->eq(
349  'uid',
350  $queryBuilder->createNamedParameter($this->getId(), ‪Connection::PARAM_INT)
351  )
352  )
353  ->executeQuery()
354  ->fetchAssociative();
355 
356  if (is_array($row)) {
357  $this->record = $row;
358  }
359  }
360 
361  return ‪$this->record;
362  }
363 }
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:52
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\RESPONSE_Skip
‪const RESPONSE_Skip
Definition: ElementEntity.php:36
‪TYPO3\CMS\Workspaces\Dependency\ReferenceEntity
Definition: ReferenceEntity.php:26
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getDependency
‪getDependency()
Definition: ElementEntity.php:140
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$table
‪string $table
Definition: ElementEntity.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\Dependency\ElementEntity\$children
‪array $children
Definition: ElementEntity.php:44
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$invalid
‪bool $invalid
Definition: ElementEntity.php:38
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getNestedChildren
‪ReferenceEntity[] getNestedChildren()
Definition: ElementEntity.php:306
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\executeEventCallback
‪executeEventCallback(string $eventName, object $caller, array $callerArguments=[])
Definition: DependencyResolver.php:64
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$dependency
‪DependencyResolver $dependency
Definition: ElementEntity.php:43
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver
Definition: DependencyResolver.php:28
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$outerMostParent
‪ElementEntity false null $outerMostParent
Definition: ElementEntity.php:46
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_Construct
‪const EVENT_Construct
Definition: ElementEntity.php:33
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getDataValue
‪getDataValue(string $key)
Definition: ElementEntity.php:104
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getOuterMostParent
‪getOuterMostParent()
Definition: ElementEntity.php:277
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$record
‪array $record
Definition: ElementEntity.php:42
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\REFERENCES_ParentOf
‪const REFERENCES_ParentOf
Definition: ElementEntity.php:32
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\setDataValue
‪setDataValue(string $key, mixed $value)
Definition: ElementEntity.php:116
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getIdentifier
‪static getIdentifier(string $table, int $id)
Definition: ElementEntity.php:322
‪TYPO3\CMS\Workspaces\Dependency
Definition: DependencyEntityFactory.php:18
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\setInvalid
‪setInvalid(bool $invalid)
Definition: ElementEntity.php:59
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$parents
‪array $parents
Definition: ElementEntity.php:45
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getParents
‪ReferenceEntity[] getParents()
Definition: ElementEntity.php:212
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_CreateChildReference
‪const EVENT_CreateChildReference
Definition: ElementEntity.php:34
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$nestedChildren
‪array $nestedChildren
Definition: ElementEntity.php:48
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$id
‪int $id
Definition: ElementEntity.php:40
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\__construct
‪__construct(string $table, int $id, array $data, DependencyResolver $dependency)
Definition: ElementEntity.php:50
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getData
‪getData()
Definition: ElementEntity.php:96
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_CreateParentReference
‪const EVENT_CreateParentReference
Definition: ElementEntity.php:35
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\setId
‪setId(int $id)
Definition: ElementEntity.php:88
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\hasReferences
‪hasReferences()
Definition: ElementEntity.php:269
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\REFERENCES_ChildOf
‪const REFERENCES_ChildOf
Definition: ElementEntity.php:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$data
‪array $data
Definition: ElementEntity.php:41
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getRecord
‪getRecord()
Definition: ElementEntity.php:330
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\isInvalid
‪isInvalid()
Definition: ElementEntity.php:64
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getTable
‪getTable()
Definition: ElementEntity.php:72
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getChildren
‪ReferenceEntity[] getChildren()
Definition: ElementEntity.php:150
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getId
‪getId()
Definition: ElementEntity.php:80
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\hasDataValue
‪hasDataValue(string $key)
Definition: ElementEntity.php:124