‪TYPO3CMS  11.5
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 
21 
26 {
27  public const ‪REFERENCES_ChildOf = 'childOf';
28  public const ‪REFERENCES_ParentOf = 'parentOf';
29  public const ‪EVENT_Construct = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::construct';
30  public const ‪EVENT_CreateChildReference = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::createChildReference';
31  public const ‪EVENT_CreateParentReference = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::createParentReference';
32  public const ‪RESPONSE_Skip = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity->skip';
33 
37  protected ‪$invalid = false;
38 
42  protected ‪$table;
43 
47  protected ‪$id;
48 
52  protected ‪$data;
53 
57  protected ‪$record;
58 
62  protected ‪$dependency;
63 
67  protected ‪$children;
68 
72  protected ‪$parents;
73 
77  protected ‪$traversingParents = false;
78 
82  protected ‪$outerMostParent;
83 
87  protected ‪$nestedChildren;
88 
98  {
99  $this->table = ‪$table;
100  $this->id = (int)‪$id;
101  $this->data = ‪$data;
102  $this->dependency = ‪$dependency;
103  $this->dependency->‪executeEventCallback(self::EVENT_Construct, $this);
104  }
105 
109  public function ‪setInvalid(‪$invalid)
110  {
111  $this->invalid = (bool)‪$invalid;
112  }
113 
117  public function ‪isInvalid()
118  {
119  return ‪$this->invalid;
120  }
121 
127  public function ‪getTable()
128  {
129  return ‪$this->table;
130  }
131 
137  public function ‪getId()
138  {
139  return ‪$this->id;
140  }
141 
147  public function ‪setId(‪$id)
148  {
149  $this->id = (int)‪$id;
150  }
151 
157  public function ‪getData()
158  {
159  return ‪$this->data;
160  }
161 
168  public function ‪getDataValue($key)
169  {
170  $result = null;
171  if ($this->‪hasDataValue($key)) {
172  $result = $this->data[$key];
173  }
174  return $result;
175  }
176 
183  public function ‪setDataValue($key, $value)
184  {
185  $this->data[$key] = $value;
186  }
187 
194  public function ‪hasDataValue($key)
195  {
196  return isset($this->data[$key]);
197  }
198 
204  public function ‪__toString()
205  {
206  return ‪self::getIdentifier($this->table, $this->id);
207  }
208 
214  public function ‪getDependency()
215  {
216  return ‪$this->dependency;
217  }
218 
224  public function ‪getChildren()
225  {
226  if (!isset($this->children)) {
227  $this->children = [];
228 
229  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
230  ->getQueryBuilderForTable('sys_refindex');
231 
232  $result = $queryBuilder
233  ->select('*')
234  ->from('sys_refindex')
235  ->where(
236  $queryBuilder->expr()->eq(
237  'tablename',
238  $queryBuilder->createNamedParameter($this->table, ‪Connection::PARAM_STR)
239  ),
240  $queryBuilder->expr()->eq(
241  'recuid',
242  $queryBuilder->createNamedParameter($this->id, ‪Connection::PARAM_INT)
243  ),
244  $queryBuilder->expr()->eq(
245  'workspace',
246  $queryBuilder->createNamedParameter($this->dependency->getWorkspace(), ‪Connection::PARAM_INT)
247  )
248  )
249  ->orderBy('sorting')
250  ->executeQuery();
251 
252  while ($row = $result->fetchAssociative()) {
253  if ($row['ref_table'] !== '_FILE' && $row['ref_table'] !== '_STRING') {
254  $arguments = [
255  'table' => $row['ref_table'],
256  'id' => $row['ref_uid'],
257  'field' => $row['field'],
258  'scope' => ‪self::REFERENCES_ChildOf,
259  ];
260 
261  $callbackResponse = $this->dependency->executeEventCallback(
262  self::EVENT_CreateChildReference,
263  $this,
264  $arguments
265  );
266  if ($callbackResponse !== self::RESPONSE_Skip) {
267  $this->children[] = $this->‪getDependency()->‪getFactory()->‪getReferencedElement(
268  $row['ref_table'],
269  $row['ref_uid'],
270  $row['field'],
271  [],
272  $this->‪getDependency()
273  );
274  }
275  }
276  }
277  }
278  return ‪$this->children;
279  }
280 
286  public function ‪getParents()
287  {
288  if (!isset($this->parents)) {
289  $this->parents = [];
290 
291  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
292  ->getQueryBuilderForTable('sys_refindex');
293 
294  $result = $queryBuilder
295  ->select('*')
296  ->from('sys_refindex')
297  ->where(
298  $queryBuilder->expr()->eq(
299  'ref_table',
300  $queryBuilder->createNamedParameter($this->table, ‪Connection::PARAM_STR)
301  ),
302  $queryBuilder->expr()->eq(
303  'ref_uid',
304  $queryBuilder->createNamedParameter($this->id, ‪Connection::PARAM_INT)
305  ),
306  $queryBuilder->expr()->eq(
307  'workspace',
308  $queryBuilder->createNamedParameter($this->dependency->getWorkspace(), ‪Connection::PARAM_INT)
309  )
310  )
311  ->orderBy('sorting')
312  ->executeQuery();
313 
314  while ($row = $result->fetchAssociative()) {
315  $arguments = [
316  'table' => $row['tablename'],
317  'id' => $row['recuid'],
318  'field' => $row['field'],
319  'scope' => ‪self::REFERENCES_ParentOf,
320  ];
321  $callbackResponse = $this->dependency->executeEventCallback(
322  self::EVENT_CreateParentReference,
323  $this,
324  $arguments
325  );
326  if ($callbackResponse !== self::RESPONSE_Skip) {
327  $this->parents[] = $this->‪getDependency()->‪getFactory()->‪getReferencedElement(
328  $row['tablename'],
329  $row['recuid'],
330  $row['field'],
331  [],
332  $this->‪getDependency()
333  );
334  }
335  }
336  }
337  return ‪$this->parents;
338  }
339 
345  public function ‪hasReferences()
346  {
347  return !empty($this->‪getChildren()) || !empty($this->‪getParents());
348  }
349 
355  public function ‪getOuterMostParent()
356  {
357  if (!isset($this->outerMostParent)) {
358  ‪$parents = $this->‪getParents();
359  if (empty(‪$parents)) {
360  $this->outerMostParent = $this;
361  } else {
362  $this->outerMostParent = false;
364  foreach (‪$parents as $parent) {
365  ‪$outerMostParent = $parent->getElement()->‪getOuterMostParent();
366  if (‪$outerMostParent instanceof ElementEntity) {
367  $this->outerMostParent = ‪$outerMostParent;
368  break;
369  }
370  if (‪$outerMostParent === false) {
371  break;
372  }
373  }
374  }
375  }
377  }
378 
384  public function ‪getNestedChildren()
385  {
386  if (!isset($this->nestedChildren)) {
387  $this->nestedChildren = [];
388  ‪$children = $this->‪getChildren();
390  foreach (‪$children as $child) {
391  $this->nestedChildren = array_merge($this->nestedChildren, [$child->getElement()->__toString() => $child->getElement()], $child->getElement()->getNestedChildren());
392  }
393  }
395  }
396 
404  public static function ‪getIdentifier(‪$table, ‪$id)
405  {
406  return ‪$table . ':' . ‪$id;
407  }
408 
414  public function ‪getRecord()
415  {
416  if (empty($this->record['uid']) || (int)$this->record['uid'] !== $this->‪getId()) {
417  $this->record = [];
418 
419  $fieldNames = ['uid', 'pid', 't3ver_wsid', 't3ver_state', 't3ver_oid'];
420  if (!empty(‪$GLOBALS['TCA'][$this->‪getTable()]['ctrl']['delete'])) {
421  $fieldNames[] = ‪$GLOBALS['TCA'][$this->‪getTable()]['ctrl']['delete'];
422  }
423 
424  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
425  ->getQueryBuilderForTable($this->‪getTable());
426  $queryBuilder->getRestrictions()->removeAll();
427 
428  $row = $queryBuilder
429  ->select(...$fieldNames)
430  ->from($this->‪getTable())
431  ->where(
432  $queryBuilder->expr()->eq(
433  'uid',
434  $queryBuilder->createNamedParameter($this->getId(), ‪Connection::PARAM_INT)
435  )
436  )
437  ->executeQuery()
438  ->fetchAssociative();
439 
440  if (is_array($row)) {
441  $this->record = $row;
442  }
443  }
444 
445  return ‪$this->record;
446  }
447 }
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getDataValue
‪mixed getDataValue($key)
Definition: ElementEntity.php:157
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getId
‪int getId()
Definition: ElementEntity.php:126
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\RESPONSE_Skip
‪const RESPONSE_Skip
Definition: ElementEntity.php:32
‪TYPO3\CMS\Workspaces\Dependency\ReferenceEntity
Definition: ReferenceEntity.php:22
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getData
‪array getData()
Definition: ElementEntity.php:146
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getParents
‪array ReferenceEntity[] getParents()
Definition: ElementEntity.php:275
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\__toString
‪string __toString()
Definition: ElementEntity.php:193
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$table
‪string $table
Definition: ElementEntity.php:40
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity
Definition: ElementEntity.php:26
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$invalid
‪bool $invalid
Definition: ElementEntity.php:36
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$traversingParents
‪bool $traversingParents
Definition: ElementEntity.php:68
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\hasDataValue
‪bool hasDataValue($key)
Definition: ElementEntity.php:183
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\setInvalid
‪setInvalid($invalid)
Definition: ElementEntity.php:98
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getRecord
‪array getRecord()
Definition: ElementEntity.php:403
‪TYPO3\CMS\Core\Database\Connection\PARAM_STR
‪const PARAM_STR
Definition: Connection.php:54
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$dependency
‪DependencyResolver $dependency
Definition: ElementEntity.php:56
‪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\$outerMostParent
‪ElementEntity false null $outerMostParent
Definition: ElementEntity.php:72
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\__construct
‪__construct($table, $id, array $data, DependencyResolver $dependency)
Definition: ElementEntity.php:86
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_Construct
‪const EVENT_Construct
Definition: ElementEntity.php:29
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getFactory
‪DependencyEntityFactory getFactory()
Definition: DependencyResolver.php:195
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$record
‪array $record
Definition: ElementEntity.php:52
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\REFERENCES_ParentOf
‪const REFERENCES_ParentOf
Definition: ElementEntity.php:28
‪TYPO3\CMS\Workspaces\Dependency
Definition: DependencyEntityFactory.php:16
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$parents
‪array null $parents
Definition: ElementEntity.php:64
‪TYPO3\CMS\Workspaces\Dependency\DependencyEntityFactory\getReferencedElement
‪ReferenceEntity getReferencedElement($table, $id, $field, array $data, DependencyResolver $dependency)
Definition: DependencyEntityFactory.php:80
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\isInvalid
‪bool isInvalid()
Definition: ElementEntity.php:106
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_CreateChildReference
‪const EVENT_CreateChildReference
Definition: ElementEntity.php:30
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getDependency
‪DependencyResolver getDependency()
Definition: ElementEntity.php:203
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$id
‪int $id
Definition: ElementEntity.php:44
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getOuterMostParent
‪ElementEntity bool getOuterMostParent()
Definition: ElementEntity.php:344
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\setId
‪setId($id)
Definition: ElementEntity.php:136
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_CreateParentReference
‪const EVENT_CreateParentReference
Definition: ElementEntity.php:31
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getChildren
‪array ReferenceEntity[] getChildren()
Definition: ElementEntity.php:213
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\setDataValue
‪setDataValue($key, $value)
Definition: ElementEntity.php:172
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\hasReferences
‪bool hasReferences()
Definition: ElementEntity.php:334
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$children
‪array null $children
Definition: ElementEntity.php:60
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\REFERENCES_ChildOf
‪const REFERENCES_ChildOf
Definition: ElementEntity.php:27
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getNestedChildren
‪array ReferenceEntity[] getNestedChildren()
Definition: ElementEntity.php:373
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getIdentifier
‪static string getIdentifier($table, $id)
Definition: ElementEntity.php:393
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$data
‪array $data
Definition: ElementEntity.php:48
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getTable
‪string getTable()
Definition: ElementEntity.php:116
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$nestedChildren
‪array null $nestedChildren
Definition: ElementEntity.php:76