TYPO3 CMS  TYPO3_8-7
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  */
18 
23 {
24  const REFERENCES_ChildOf = 'childOf';
25  const REFERENCES_ParentOf = 'parentOf';
26  const EVENT_Construct = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::construct';
27  const EVENT_CreateChildReference = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::createChildReference';
28  const EVENT_CreateParentReference = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::createParentReference';
29  const RESPONSE_Skip = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity->skip';
30 
34  protected $invalid = false;
35 
39  protected $table;
40 
44  protected $id;
45 
49  protected $data;
50 
54  protected $record;
55 
59  protected $dependency;
60 
64  protected $children;
65 
69  protected $parents;
70 
74  protected $traversingParents = false;
75 
79  protected $outerMostParent;
80 
84  protected $nestedChildren;
85 
94  public function __construct($table, $id, array $data = [], \TYPO3\CMS\Version\Dependency\DependencyResolver $dependency)
95  {
96  $this->table = $table;
97  $this->id = (int)$id;
98  $this->data = $data;
99  $this->dependency = $dependency;
100  $this->dependency->executeEventCallback(self::EVENT_Construct, $this);
101  }
102 
106  public function setInvalid($invalid)
107  {
108  $this->invalid = (bool)$invalid;
109  }
110 
114  public function isInvalid()
115  {
116  return $this->invalid;
117  }
118 
124  public function getTable()
125  {
126  return $this->table;
127  }
128 
134  public function getId()
135  {
136  return $this->id;
137  }
138 
144  public function setId($id)
145  {
146  $this->id = (int)$id;
147  }
148 
154  public function getData()
155  {
156  return $this->data;
157  }
158 
165  public function getDataValue($key)
166  {
167  $result = null;
168  if ($this->hasDataValue($key)) {
169  $result = $this->data[$key];
170  }
171  return $result;
172  }
173 
180  public function setDataValue($key, $value)
181  {
182  $this->data[$key] = $value;
183  }
184 
191  public function hasDataValue($key)
192  {
193  return isset($this->data[$key]);
194  }
195 
201  public function __toString()
202  {
203  return self::getIdentifier($this->table, $this->id);
204  }
205 
211  public function getDependency()
212  {
213  return $this->dependency;
214  }
215 
221  public function getChildren()
222  {
223  if (!isset($this->children)) {
224  $this->children = [];
225 
226  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
227  ->getQueryBuilderForTable('sys_refindex');
228 
229  $result = $queryBuilder
230  ->select('*')
231  ->from('sys_refindex')
232  ->where(
233  $queryBuilder->expr()->eq(
234  'tablename',
235  $queryBuilder->createNamedParameter($this->table, \PDO::PARAM_STR)
236  ),
237  $queryBuilder->expr()->eq(
238  'recuid',
239  $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)
240  ),
241  $queryBuilder->expr()->eq(
242  'workspace',
243  $queryBuilder->createNamedParameter($this->dependency->getWorkspace(), \PDO::PARAM_INT)
244  )
245  )
246  ->orderBy('sorting')
247  ->execute();
248 
249  while ($row = $result->fetch()) {
250  if ($row['ref_table'] !== '_FILE' && $row['ref_table'] !== '_STRING') {
251  $arguments = [
252  'table' => $row['ref_table'],
253  'id' => $row['ref_uid'],
254  'field' => $row['field'],
255  'scope' => self::REFERENCES_ChildOf
256  ];
257 
258  $callbackResponse = $this->dependency->executeEventCallback(
259  self::EVENT_CreateChildReference,
260  $this,
261  $arguments
262  );
263  if ($callbackResponse !== self::RESPONSE_Skip) {
264  $this->children[] = $this->getDependency()->getFactory()->getReferencedElement(
265  $row['ref_table'],
266  $row['ref_uid'],
267  $row['field'],
268  [],
269  $this->getDependency()
270  );
271  }
272  }
273  }
274  }
275  return $this->children;
276  }
277 
283  public function getParents()
284  {
285  if (!isset($this->parents)) {
286  $this->parents = [];
287 
288  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
289  ->getQueryBuilderForTable('sys_refindex');
290 
291  $result = $queryBuilder
292  ->select('*')
293  ->from('sys_refindex')
294  ->where(
295  $queryBuilder->expr()->eq(
296  'deleted',
297  $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)
298  ),
299  $queryBuilder->expr()->eq(
300  'ref_table',
301  $queryBuilder->createNamedParameter($this->table, \PDO::PARAM_STR)
302  ),
303  $queryBuilder->expr()->eq(
304  'ref_uid',
305  $queryBuilder->createNamedParameter($this->id, \PDO::PARAM_INT)
306  ),
307  $queryBuilder->expr()->eq(
308  'workspace',
309  $queryBuilder->createNamedParameter($this->dependency->getWorkspace(), \PDO::PARAM_INT)
310  )
311  )
312  ->orderBy('sorting')
313  ->execute();
314 
315  while ($row = $result->fetch()) {
316  $arguments = [
317  'table' => $row['tablename'],
318  'id' => $row['recuid'],
319  'field' => $row['field'],
320  'scope' => self::REFERENCES_ParentOf
321  ];
322  $callbackResponse = $this->dependency->executeEventCallback(
323  self::EVENT_CreateParentReference,
324  $this,
325  $arguments
326  );
327  if ($callbackResponse !== self::RESPONSE_Skip) {
328  $this->parents[] = $this->getDependency()->getFactory()->getReferencedElement(
329  $row['tablename'],
330  $row['recuid'],
331  $row['field'],
332  [],
333  $this->getDependency()
334  );
335  }
336  }
337  }
338  return $this->parents;
339  }
340 
346  public function hasReferences()
347  {
348  return !empty($this->getChildren()) || !empty($this->getParents());
349  }
350 
356  public function getOuterMostParent()
357  {
358  if (!isset($this->outerMostParent)) {
359  $parents = $this->getParents();
360  if (empty($parents)) {
361  $this->outerMostParent = $this;
362  } else {
363  $this->outerMostParent = false;
365  foreach ($parents as $parent) {
366  $outerMostParent = $parent->getElement()->getOuterMostParent();
367  if ($outerMostParent instanceof \TYPO3\CMS\Version\Dependency\ElementEntity) {
368  $this->outerMostParent = $outerMostParent;
369  break;
370  }
371  if ($outerMostParent === false) {
372  break;
373  }
374  }
375  }
376  }
377  return $this->outerMostParent;
378  }
379 
385  public function getNestedChildren()
386  {
387  if (!isset($this->nestedChildren)) {
388  $this->nestedChildren = [];
389  $children = $this->getChildren();
391  foreach ($children as $child) {
392  $this->nestedChildren = array_merge($this->nestedChildren, [$child->getElement()->__toString() => $child->getElement()], $child->getElement()->getNestedChildren());
393  }
394  }
395  return $this->nestedChildren;
396  }
397 
405  public static function getIdentifier($table, $id)
406  {
407  return $table . ':' . $id;
408  }
409 
415  public function getRecord()
416  {
417  if (empty($this->record['uid']) || (int)$this->record['uid'] !== $this->getId()) {
418  $this->record = [];
419 
420  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
421  ->getQueryBuilderForTable($this->getTable());
422  $queryBuilder->getRestrictions()->removeAll();
423 
424  $row = $queryBuilder
425  ->select('uid', 'pid', 't3ver_wsid', 't3ver_state', 't3ver_oid')
426  ->from($this->getTable())
427  ->where(
428  $queryBuilder->expr()->eq(
429  'uid',
430  $queryBuilder->createNamedParameter($this->getId(), \PDO::PARAM_INT)
431  )
432  )
433  ->execute()
434  ->fetch();
435 
436  if (is_array($row)) {
437  $this->record = $row;
438  }
439  }
440 
441  return $this->record;
442  }
443 }
static makeInstance($className,... $constructorArguments)
__construct($table, $id, array $data=[], \TYPO3\CMS\Version\Dependency\DependencyResolver $dependency)