TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
ElementEntity.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Version\Dependency;
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 
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'],
321  'scope' => self::REFERENCES_ParentOf
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 \TYPO3\CMS\Version\Dependency\ElementEntity) {
369  $this->outerMostParent = $outerMostParent;
370  break;
371  } elseif ($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)