‪TYPO3CMS  10.4
ElementEntity.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 
20 
25 {
26  const ‪REFERENCES_ChildOf = 'childOf';
27  const ‪REFERENCES_ParentOf = 'parentOf';
28  const ‪EVENT_Construct = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::construct';
29  const ‪EVENT_CreateChildReference = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::createChildReference';
30  const ‪EVENT_CreateParentReference = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::createParentReference';
31  const ‪RESPONSE_Skip = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity->skip';
32 
36  protected ‪$invalid = false;
37 
41  protected ‪$table;
42 
46  protected ‪$id;
47 
51  protected ‪$data;
52 
56  protected ‪$record;
57 
61  protected ‪$dependency;
62 
66  protected ‪$children;
67 
71  protected ‪$parents;
72 
76  protected ‪$traversingParents = false;
77 
81  protected ‪$outerMostParent;
82 
86  protected ‪$nestedChildren;
87 
97  {
98  $this->table = ‪$table;
99  $this->id = (int)‪$id;
100  $this->data = ‪$data;
101  $this->dependency = ‪$dependency;
102  $this->dependency->‪executeEventCallback(self::EVENT_Construct, $this);
103  }
104 
108  public function ‪setInvalid(‪$invalid)
109  {
110  $this->invalid = (bool)‪$invalid;
111  }
112 
116  public function ‪isInvalid()
117  {
118  return ‪$this->invalid;
119  }
120 
126  public function ‪getTable()
127  {
128  return ‪$this->table;
129  }
130 
136  public function ‪getId()
137  {
138  return ‪$this->id;
139  }
140 
146  public function ‪setId(‪$id)
147  {
148  $this->id = (int)‪$id;
149  }
150 
156  public function ‪getData()
157  {
158  return ‪$this->data;
159  }
160 
167  public function ‪getDataValue($key)
168  {
169  $result = null;
170  if ($this->‪hasDataValue($key)) {
171  $result = $this->data[$key];
172  }
173  return $result;
174  }
175 
182  public function ‪setDataValue($key, $value)
183  {
184  $this->data[$key] = $value;
185  }
186 
193  public function ‪hasDataValue($key)
194  {
195  return isset($this->data[$key]);
196  }
197 
203  public function ‪__toString()
204  {
205  return ‪self::getIdentifier($this->table, $this->id);
206  }
207 
213  public function ‪getDependency()
214  {
215  return ‪$this->dependency;
216  }
217 
223  public function ‪getChildren()
224  {
225  if (!isset($this->children)) {
226  $this->children = [];
227 
228  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
229  ->getQueryBuilderForTable('sys_refindex');
230 
231  $result = $queryBuilder
232  ->select('*')
233  ->from('sys_refindex')
234  ->where(
235  $queryBuilder->expr()->eq(
236  'tablename',
237  $queryBuilder->createNamedParameter($this->table, \PDO::PARAM_STR)
238  ),
239  $queryBuilder->expr()->eq(
240  'recuid',
241  $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)
242  ),
243  $queryBuilder->expr()->eq(
244  'workspace',
245  $queryBuilder->createNamedParameter($this->dependency->getWorkspace(), \PDO::PARAM_INT)
246  )
247  )
248  ->orderBy('sorting')
249  ->execute();
250 
251  while ($row = $result->fetch()) {
252  if ($row['ref_table'] !== '_FILE' && $row['ref_table'] !== '_STRING') {
253  $arguments = [
254  'table' => $row['ref_table'],
255  'id' => $row['ref_uid'],
256  'field' => $row['field'],
257  'scope' => ‪self::REFERENCES_ChildOf
258  ];
259 
260  $callbackResponse = $this->dependency->executeEventCallback(
261  self::EVENT_CreateChildReference,
262  $this,
263  $arguments
264  );
265  if ($callbackResponse !== self::RESPONSE_Skip) {
266  $this->children[] = $this->‪getDependency()->‪getFactory()->‪getReferencedElement(
267  $row['ref_table'],
268  $row['ref_uid'],
269  $row['field'],
270  [],
271  $this->‪getDependency()
272  );
273  }
274  }
275  }
276  }
277  return ‪$this->children;
278  }
279 
285  public function ‪getParents()
286  {
287  if (!isset($this->parents)) {
288  $this->parents = [];
289 
290  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
291  ->getQueryBuilderForTable('sys_refindex');
292 
293  $result = $queryBuilder
294  ->select('*')
295  ->from('sys_refindex')
296  ->where(
297  $queryBuilder->expr()->eq(
298  'deleted',
299  $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
300  ),
301  $queryBuilder->expr()->eq(
302  'ref_table',
303  $queryBuilder->createNamedParameter($this->table, \PDO::PARAM_STR)
304  ),
305  $queryBuilder->expr()->eq(
306  'ref_uid',
307  $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)
308  ),
309  $queryBuilder->expr()->eq(
310  'workspace',
311  $queryBuilder->createNamedParameter($this->dependency->getWorkspace(), \PDO::PARAM_INT)
312  )
313  )
314  ->orderBy('sorting')
315  ->execute();
316 
317  while ($row = $result->fetch()) {
318  $arguments = [
319  'table' => $row['tablename'],
320  'id' => $row['recuid'],
321  'field' => $row['field'],
323  ];
324  $callbackResponse = $this->dependency->executeEventCallback(
325  self::EVENT_CreateParentReference,
326  $this,
327  $arguments
328  );
329  if ($callbackResponse !== self::RESPONSE_Skip) {
330  $this->parents[] = $this->‪getDependency()->‪getFactory()->‪getReferencedElement(
331  $row['tablename'],
332  $row['recuid'],
333  $row['field'],
334  [],
335  $this->‪getDependency()
336  );
337  }
338  }
339  }
340  return ‪$this->parents;
341  }
342 
348  public function ‪hasReferences()
349  {
350  return !empty($this->‪getChildren()) || !empty($this->‪getParents());
351  }
352 
358  public function ‪getOuterMostParent()
359  {
360  if (!isset($this->outerMostParent)) {
361  ‪$parents = $this->‪getParents();
362  if (empty(‪$parents)) {
363  $this->outerMostParent = $this;
364  } else {
365  $this->outerMostParent = false;
367  foreach (‪$parents as $parent) {
368  ‪$outerMostParent = $parent->getElement()->‪getOuterMostParent();
369  if (‪$outerMostParent instanceof ElementEntity) {
370  $this->outerMostParent = ‪$outerMostParent;
371  break;
372  }
373  if (‪$outerMostParent === false) {
374  break;
375  }
376  }
377  }
378  }
380  }
381 
387  public function ‪getNestedChildren()
388  {
389  if (!isset($this->nestedChildren)) {
390  $this->nestedChildren = [];
391  ‪$children = $this->‪getChildren();
393  foreach (‪$children as $child) {
394  $this->nestedChildren = array_merge($this->nestedChildren, [$child->getElement()->__toString() => $child->getElement()], $child->getElement()->getNestedChildren());
395  }
396  }
398  }
399 
407  public static function ‪getIdentifier(‪$table, ‪$id)
408  {
409  return ‪$table . ':' . ‪$id;
410  }
411 
417  public function ‪getRecord()
418  {
419  if (empty($this->record['uid']) || (int)$this->record['uid'] !== $this->‪getId()) {
420  $this->record = [];
421 
422  $fieldNames = ['uid', 'pid', 't3ver_wsid', 't3ver_state', 't3ver_oid'];
423  if (!empty(‪$GLOBALS['TCA'][$this->‪getTable()]['ctrl']['delete'])) {
424  $fieldNames[] = ‪$GLOBALS['TCA'][$this->‪getTable()]['ctrl']['delete'];
425  }
426 
427  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
428  ->getQueryBuilderForTable($this->‪getTable());
429  $queryBuilder->getRestrictions()->removeAll();
430 
431  $row = $queryBuilder
432  ->select(...$fieldNames)
433  ->from($this->‪getTable())
434  ->where(
435  $queryBuilder->expr()->eq(
436  'uid',
437  $queryBuilder->createNamedParameter($this->getId(), \PDO::PARAM_INT)
438  )
439  )
440  ->execute()
441  ->fetch();
442 
443  if (is_array($row)) {
444  $this->record = $row;
445  }
446  }
447 
448  return ‪$this->record;
449  }
450 }
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getDataValue
‪mixed getDataValue($key)
Definition: ElementEntity.php:156
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getId
‪int getId()
Definition: ElementEntity.php:125
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\RESPONSE_Skip
‪const RESPONSE_Skip
Definition: ElementEntity.php:31
‪TYPO3\CMS\Workspaces\Dependency\ReferenceEntity
Definition: ReferenceEntity.php:22
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getData
‪array getData()
Definition: ElementEntity.php:145
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getParents
‪array ReferenceEntity[] getParents()
Definition: ElementEntity.php:274
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\__toString
‪string __toString()
Definition: ElementEntity.php:192
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$table
‪string $table
Definition: ElementEntity.php:39
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity
Definition: ElementEntity.php:25
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$outerMostParent
‪ElementEntity $outerMostParent
Definition: ElementEntity.php:71
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$children
‪array $children
Definition: ElementEntity.php:59
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$invalid
‪bool $invalid
Definition: ElementEntity.php:35
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$traversingParents
‪bool $traversingParents
Definition: ElementEntity.php:67
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\hasDataValue
‪bool hasDataValue($key)
Definition: ElementEntity.php:182
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\setInvalid
‪setInvalid($invalid)
Definition: ElementEntity.php:97
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getRecord
‪array getRecord()
Definition: ElementEntity.php:406
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$dependency
‪DependencyResolver $dependency
Definition: ElementEntity.php:55
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\executeEventCallback
‪mixed executeEventCallback($eventName, $caller, array $callerArguments=[])
Definition: DependencyResolver.php:90
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver
Definition: DependencyResolver.php:24
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\__construct
‪__construct($table, $id, array $data, DependencyResolver $dependency)
Definition: ElementEntity.php:85
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_Construct
‪const EVENT_Construct
Definition: ElementEntity.php:28
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getFactory
‪DependencyEntityFactory getFactory()
Definition: DependencyResolver.php:195
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$record
‪array $record
Definition: ElementEntity.php:51
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\REFERENCES_ParentOf
‪const REFERENCES_ParentOf
Definition: ElementEntity.php:27
‪TYPO3\CMS\Workspaces\Dependency
Definition: DependencyEntityFactory.php:16
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$parents
‪array $parents
Definition: ElementEntity.php:63
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\isInvalid
‪bool isInvalid()
Definition: ElementEntity.php:105
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_CreateChildReference
‪const EVENT_CreateChildReference
Definition: ElementEntity.php:29
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$nestedChildren
‪array $nestedChildren
Definition: ElementEntity.php:75
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getDependency
‪DependencyResolver getDependency()
Definition: ElementEntity.php:202
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$id
‪int $id
Definition: ElementEntity.php:43
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getOuterMostParent
‪ElementEntity bool getOuterMostParent()
Definition: ElementEntity.php:347
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\setId
‪setId($id)
Definition: ElementEntity.php:135
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_CreateParentReference
‪const EVENT_CreateParentReference
Definition: ElementEntity.php:30
‪$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\Dependency\ElementEntity\setDataValue
‪setDataValue($key, $value)
Definition: ElementEntity.php:171
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\hasReferences
‪bool hasReferences()
Definition: ElementEntity.php:337
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\REFERENCES_ChildOf
‪const REFERENCES_ChildOf
Definition: ElementEntity.php:26
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getNestedChildren
‪array ReferenceEntity[] getNestedChildren()
Definition: ElementEntity.php:376
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getIdentifier
‪static string getIdentifier($table, $id)
Definition: ElementEntity.php:396
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$data
‪array $data
Definition: ElementEntity.php:47
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getTable
‪string getTable()
Definition: ElementEntity.php:115
‪TYPO3\CMS\Workspaces\Dependency\DependencyEntityFactory\getReferencedElement
‪ReferenceEntity getReferencedElement($table, $id, $field, array $data=[], DependencyResolver $dependency)
Definition: DependencyEntityFactory.php:80