‪TYPO3CMS  9.5
ElementEntity.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
24 {
25  const ‪REFERENCES_ChildOf = 'childOf';
26  const ‪REFERENCES_ParentOf = 'parentOf';
27  const ‪EVENT_Construct = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::construct';
28  const ‪EVENT_CreateChildReference = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::createChildReference';
29  const ‪EVENT_CreateParentReference = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::createParentReference';
30  const ‪RESPONSE_Skip = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity->skip';
31 
35  protected ‪$invalid = false;
36 
40  protected ‪$table;
41 
45  protected ‪$id;
46 
50  protected ‪$data;
51 
55  protected ‪$record;
56 
60  protected ‪$dependency;
61 
65  protected ‪$children;
66 
70  protected ‪$parents;
71 
75  protected ‪$traversingParents = false;
76 
80  protected ‪$outerMostParent;
81 
85  protected ‪$nestedChildren;
86 
96  {
97  $this->table = ‪$table;
98  $this->id = (int)‪$id;
99  $this->data = ‪$data;
100  $this->dependency = ‪$dependency;
101  $this->dependency->‪executeEventCallback(self::EVENT_Construct, $this);
102  }
103 
107  public function ‪setInvalid(‪$invalid)
108  {
109  $this->invalid = (bool)‪$invalid;
110  }
111 
115  public function ‪isInvalid()
116  {
117  return ‪$this->invalid;
118  }
119 
125  public function ‪getTable()
126  {
127  return ‪$this->table;
128  }
129 
135  public function ‪getId()
136  {
137  return ‪$this->id;
138  }
139 
145  public function ‪setId(‪$id)
146  {
147  $this->id = (int)‪$id;
148  }
149 
155  public function ‪getData()
156  {
157  return ‪$this->data;
158  }
159 
166  public function ‪getDataValue($key)
167  {
168  $result = null;
169  if ($this->‪hasDataValue($key)) {
170  $result = $this->data[$key];
171  }
172  return $result;
173  }
174 
181  public function ‪setDataValue($key, $value)
182  {
183  $this->data[$key] = $value;
184  }
185 
192  public function ‪hasDataValue($key)
193  {
194  return isset($this->data[$key]);
195  }
196 
202  public function ‪__toString()
203  {
204  return ‪self::getIdentifier($this->table, $this->id);
205  }
206 
212  public function ‪getDependency()
213  {
214  return ‪$this->dependency;
215  }
216 
222  public function ‪getChildren()
223  {
224  if (!isset($this->children)) {
225  $this->children = [];
226 
227  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
228  ->getQueryBuilderForTable('sys_refindex');
229 
230  $result = $queryBuilder
231  ->select('*')
232  ->from('sys_refindex')
233  ->where(
234  $queryBuilder->expr()->eq(
235  'tablename',
236  $queryBuilder->createNamedParameter($this->table, \PDO::PARAM_STR)
237  ),
238  $queryBuilder->expr()->eq(
239  'recuid',
240  $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)
241  ),
242  $queryBuilder->expr()->eq(
243  'workspace',
244  $queryBuilder->createNamedParameter($this->dependency->getWorkspace(), \PDO::PARAM_INT)
245  )
246  )
247  ->orderBy('sorting')
248  ->execute();
249 
250  while ($row = $result->fetch()) {
251  if ($row['ref_table'] !== '_FILE' && $row['ref_table'] !== '_STRING') {
252  $arguments = [
253  'table' => $row['ref_table'],
254  'id' => $row['ref_uid'],
255  'field' => $row['field'],
256  'scope' => ‪self::REFERENCES_ChildOf
257  ];
258 
259  $callbackResponse = $this->dependency->executeEventCallback(
260  self::EVENT_CreateChildReference,
261  $this,
262  $arguments
263  );
264  if ($callbackResponse !== self::RESPONSE_Skip) {
265  $this->children[] = $this->‪getDependency()->‪getFactory()->‪getReferencedElement(
266  $row['ref_table'],
267  $row['ref_uid'],
268  $row['field'],
269  [],
270  $this->‪getDependency()
271  );
272  }
273  }
274  }
275  }
276  return ‪$this->children;
277  }
278 
284  public function ‪getParents()
285  {
286  if (!isset($this->parents)) {
287  $this->parents = [];
288 
289  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
290  ->getQueryBuilderForTable('sys_refindex');
291 
292  $result = $queryBuilder
293  ->select('*')
294  ->from('sys_refindex')
295  ->where(
296  $queryBuilder->expr()->eq(
297  'deleted',
298  $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
299  ),
300  $queryBuilder->expr()->eq(
301  'ref_table',
302  $queryBuilder->createNamedParameter($this->table, \PDO::PARAM_STR)
303  ),
304  $queryBuilder->expr()->eq(
305  'ref_uid',
306  $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)
307  ),
308  $queryBuilder->expr()->eq(
309  'workspace',
310  $queryBuilder->createNamedParameter($this->dependency->getWorkspace(), \PDO::PARAM_INT)
311  )
312  )
313  ->orderBy('sorting')
314  ->execute();
315 
316  while ($row = $result->fetch()) {
317  $arguments = [
318  'table' => $row['tablename'],
319  'id' => $row['recuid'],
320  'field' => $row['field'],
322  ];
323  $callbackResponse = $this->dependency->executeEventCallback(
324  self::EVENT_CreateParentReference,
325  $this,
326  $arguments
327  );
328  if ($callbackResponse !== self::RESPONSE_Skip) {
329  $this->parents[] = $this->‪getDependency()->‪getFactory()->‪getReferencedElement(
330  $row['tablename'],
331  $row['recuid'],
332  $row['field'],
333  [],
334  $this->‪getDependency()
335  );
336  }
337  }
338  }
339  return ‪$this->parents;
340  }
341 
347  public function ‪hasReferences()
348  {
349  return !empty($this->‪getChildren()) || !empty($this->‪getParents());
350  }
351 
357  public function ‪getOuterMostParent()
358  {
359  if (!isset($this->outerMostParent)) {
360  ‪$parents = $this->‪getParents();
361  if (empty(‪$parents)) {
362  $this->outerMostParent = $this;
363  } else {
364  $this->outerMostParent = false;
366  foreach (‪$parents as $parent) {
367  ‪$outerMostParent = $parent->getElement()->‪getOuterMostParent();
368  if (‪$outerMostParent instanceof ElementEntity) {
369  $this->outerMostParent = ‪$outerMostParent;
370  break;
371  }
372  if (‪$outerMostParent === false) {
373  break;
374  }
375  }
376  }
377  }
379  }
380 
386  public function ‪getNestedChildren()
387  {
388  if (!isset($this->nestedChildren)) {
389  $this->nestedChildren = [];
390  ‪$children = $this->‪getChildren();
392  foreach (‪$children as $child) {
393  $this->nestedChildren = array_merge($this->nestedChildren, [$child->getElement()->__toString() => $child->getElement()], $child->getElement()->getNestedChildren());
394  }
395  }
397  }
398 
406  public static function ‪getIdentifier(‪$table, ‪$id)
407  {
408  return ‪$table . ':' . ‪$id;
409  }
410 
416  public function ‪getRecord()
417  {
418  if (empty($this->record['uid']) || (int)$this->record['uid'] !== $this->‪getId()) {
419  $this->record = [];
420 
421  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
422  ->getQueryBuilderForTable($this->‪getTable());
423  $queryBuilder->getRestrictions()->removeAll();
424 
425  $row = $queryBuilder
426  ->select('uid', 'pid', 't3ver_wsid', 't3ver_state', 't3ver_oid')
427  ->from($this->‪getTable())
428  ->where(
429  $queryBuilder->expr()->eq(
430  'uid',
431  $queryBuilder->createNamedParameter($this->getId(), \PDO::PARAM_INT)
432  )
433  )
434  ->execute()
435  ->fetch();
436 
437  if (is_array($row)) {
438  $this->record = $row;
439  }
440  }
441 
442  return ‪$this->record;
443  }
444 }
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getDataValue
‪mixed getDataValue($key)
Definition: ElementEntity.php:155
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getId
‪int getId()
Definition: ElementEntity.php:124
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\RESPONSE_Skip
‪const RESPONSE_Skip
Definition: ElementEntity.php:30
‪TYPO3\CMS\Workspaces\Dependency\ReferenceEntity
Definition: ReferenceEntity.php:21
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getData
‪array getData()
Definition: ElementEntity.php:144
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getParents
‪array ReferenceEntity[] getParents()
Definition: ElementEntity.php:273
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\__toString
‪string __toString()
Definition: ElementEntity.php:191
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$table
‪string $table
Definition: ElementEntity.php:38
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity
Definition: ElementEntity.php:24
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$outerMostParent
‪ElementEntity $outerMostParent
Definition: ElementEntity.php:70
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$children
‪array $children
Definition: ElementEntity.php:58
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$invalid
‪bool $invalid
Definition: ElementEntity.php:34
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$traversingParents
‪bool $traversingParents
Definition: ElementEntity.php:66
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\hasDataValue
‪bool hasDataValue($key)
Definition: ElementEntity.php:181
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\setInvalid
‪setInvalid($invalid)
Definition: ElementEntity.php:96
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getRecord
‪array getRecord()
Definition: ElementEntity.php:405
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$dependency
‪DependencyResolver $dependency
Definition: ElementEntity.php:54
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\executeEventCallback
‪mixed executeEventCallback($eventName, $caller, array $callerArguments=[])
Definition: DependencyResolver.php:89
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver
Definition: DependencyResolver.php:23
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_Construct
‪const EVENT_Construct
Definition: ElementEntity.php:27
‪TYPO3\CMS\Workspaces\Dependency\DependencyResolver\getFactory
‪DependencyEntityFactory getFactory()
Definition: DependencyResolver.php:194
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$record
‪array $record
Definition: ElementEntity.php:50
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\REFERENCES_ParentOf
‪const REFERENCES_ParentOf
Definition: ElementEntity.php:26
‪TYPO3\CMS\Workspaces\Dependency
Definition: DependencyEntityFactory.php:2
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\__construct
‪__construct($table, $id, array $data=[], DependencyResolver $dependency)
Definition: ElementEntity.php:84
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$parents
‪array $parents
Definition: ElementEntity.php:62
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\isInvalid
‪bool isInvalid()
Definition: ElementEntity.php:104
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_CreateChildReference
‪const EVENT_CreateChildReference
Definition: ElementEntity.php:28
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$nestedChildren
‪array $nestedChildren
Definition: ElementEntity.php:74
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getDependency
‪DependencyResolver getDependency()
Definition: ElementEntity.php:201
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$id
‪int $id
Definition: ElementEntity.php:42
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getOuterMostParent
‪ElementEntity bool getOuterMostParent()
Definition: ElementEntity.php:346
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\setId
‪setId($id)
Definition: ElementEntity.php:134
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\EVENT_CreateParentReference
‪const EVENT_CreateParentReference
Definition: ElementEntity.php:29
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getChildren
‪array ReferenceEntity[] getChildren()
Definition: ElementEntity.php:211
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\setDataValue
‪setDataValue($key, $value)
Definition: ElementEntity.php:170
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\hasReferences
‪bool hasReferences()
Definition: ElementEntity.php:336
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\REFERENCES_ChildOf
‪const REFERENCES_ChildOf
Definition: ElementEntity.php:25
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getNestedChildren
‪array ReferenceEntity[] getNestedChildren()
Definition: ElementEntity.php:375
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getIdentifier
‪static string getIdentifier($table, $id)
Definition: ElementEntity.php:395
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\$data
‪array $data
Definition: ElementEntity.php:46
‪TYPO3\CMS\Workspaces\Dependency\ElementEntity\getTable
‪string getTable()
Definition: ElementEntity.php:114
‪TYPO3\CMS\Workspaces\Dependency\DependencyEntityFactory\getReferencedElement
‪ReferenceEntity getReferencedElement($table, $id, $field, array $data=[], DependencyResolver $dependency)
Definition: DependencyEntityFactory.php:79