[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / version / Classes / Dependency / ElementEntity.php
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 */
16 /**
17 * Object to hold information on a dependent database element in abstract.
18 */
19 class ElementEntity {
20
21 const REFERENCES_ChildOf = 'childOf';
22 const REFERENCES_ParentOf = 'parentOf';
23 const EVENT_Construct = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::construct';
24 const EVENT_CreateChildReference = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::createChildReference';
25 const EVENT_CreateParentReference = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity::createParentReference';
26 const RESPONSE_Skip = 'TYPO3\\CMS\\Version\\Dependency\\ElementEntity->skip';
27
28 /**
29 * @var bool
30 */
31 protected $invalid = FALSE;
32
33 /**
34 * @var string
35 */
36 protected $table;
37
38 /**
39 * @var integer
40 */
41 protected $id;
42
43 /**
44 * @var array
45 */
46 protected $data;
47
48 /**
49 * @var array
50 */
51 protected $record;
52
53 /**
54 * @var \TYPO3\CMS\Version\Dependency\DependencyResolver
55 */
56 protected $dependency;
57
58 /**
59 * @var array
60 */
61 protected $children;
62
63 /**
64 * @var array
65 */
66 protected $parents;
67
68 /**
69 * @var boolean
70 */
71 protected $traversingParents = FALSE;
72
73 /**
74 * @var \TYPO3\CMS\Version\Dependency\ElementEntity
75 */
76 protected $outerMostParent;
77
78 /**
79 * @var array
80 */
81 protected $nestedChildren;
82
83 /**
84 * Creates this object.
85 *
86 * @param string $table
87 * @param integer $id
88 * @param array $data (optional)
89 * @param \TYPO3\CMS\Version\Dependency\DependencyResolver $dependency
90 */
91 public function __construct($table, $id, array $data = array(), \TYPO3\CMS\Version\Dependency\DependencyResolver $dependency) {
92 $this->table = $table;
93 $this->id = (int)$id;
94 $this->data = $data;
95 $this->dependency = $dependency;
96 $this->dependency->executeEventCallback(self::EVENT_Construct, $this);
97 }
98
99 /**
100 * @param bool $invalid
101 */
102 public function setInvalid($invalid) {
103 $this->invalid = (bool)$invalid;
104 }
105
106 /**
107 * @return bool
108 */
109 public function isInvalid() {
110 return $this->invalid;
111 }
112
113 /**
114 * Gets the table.
115 *
116 * @return string
117 */
118 public function getTable() {
119 return $this->table;
120 }
121
122 /**
123 * Gets the id.
124 *
125 * @return integer
126 */
127 public function getId() {
128 return $this->id;
129 }
130
131 /**
132 * Sets the id.
133 *
134 * @param int $id
135 */
136 public function setId($id) {
137 $this->id = (int)$id;
138 }
139
140 /**
141 * Gets the data.
142 *
143 * @return array
144 */
145 public function getData() {
146 return $this->data;
147 }
148
149 /**
150 * Gets a value for a particular key from the data.
151 *
152 * @param string $key
153 * @return mixed
154 */
155 public function getDataValue($key) {
156 $result = NULL;
157 if ($this->hasDataValue($key)) {
158 $result = $this->data[$key];
159 }
160 return $result;
161 }
162
163 /**
164 * Sets a value for a particular key in the data.
165 *
166 * @param string $key
167 * @param mixed $value
168 * @return void
169 */
170 public function setDataValue($key, $value) {
171 $this->data[$key] = $value;
172 }
173
174 /**
175 * Determines whether a particular key holds data.
176 *
177 * @param string $key
178 * @return bool
179 */
180 public function hasDataValue($key) {
181 return isset($this->data[$key]);
182 }
183
184 /**
185 * Converts this object for string representation.
186 *
187 * @return string
188 */
189 public function __toString() {
190 return self::getIdentifier($this->table, $this->id);
191 }
192
193 /**
194 * Gets the parent dependency object.
195 *
196 * @return \TYPO3\CMS\Version\Dependency\DependencyResolver
197 */
198 public function getDependency() {
199 return $this->dependency;
200 }
201
202 /**
203 * Gets all child references.
204 *
205 * @return array|ReferenceEntity[]
206 */
207 public function getChildren() {
208 if (!isset($this->children)) {
209 $this->children = array();
210 $where = 'tablename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->table, 'sys_refindex') . ' AND recuid='
211 . $this->id . ' AND workspace=' . $this->dependency->getWorkspace();
212 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_refindex', $where, '', 'sorting');
213 if (is_array($rows)) {
214 foreach ($rows as $row) {
215 $arguments = array('table' => $row['ref_table'], 'id' => $row['ref_uid'], 'field' => $row['field'], 'scope' => self::REFERENCES_ChildOf);
216 $callbackResponse = $this->dependency->executeEventCallback(self::EVENT_CreateChildReference, $this, $arguments);
217 if ($callbackResponse !== self::RESPONSE_Skip) {
218 $this->children[] = $this->getDependency()->getFactory()->getReferencedElement(
219 $row['ref_table'],
220 $row['ref_uid'],
221 $row['field'],
222 array(),
223 $this->getDependency()
224 );
225 }
226 }
227 }
228 }
229 return $this->children;
230 }
231
232 /**
233 * Gets all parent references.
234 *
235 * @return array|ReferenceEntity[]
236 */
237 public function getParents() {
238 if (!isset($this->parents)) {
239 $this->parents = array();
240 $where = 'ref_table=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->table, 'sys_refindex')
241 . ' AND deleted=0 AND ref_uid=' . $this->id . ' AND workspace=' . $this->dependency->getWorkspace();
242 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_refindex', $where, '', 'sorting');
243 if (is_array($rows)) {
244 foreach ($rows as $row) {
245 $arguments = array('table' => $row['tablename'], 'id' => $row['recuid'], 'field' => $row['field'], 'scope' => self::REFERENCES_ParentOf);
246 $callbackResponse = $this->dependency->executeEventCallback(self::EVENT_CreateParentReference, $this, $arguments);
247 if ($callbackResponse !== self::RESPONSE_Skip) {
248 $this->parents[] = $this->getDependency()->getFactory()->getReferencedElement(
249 $row['tablename'],
250 $row['recuid'],
251 $row['field'],
252 array(),
253 $this->getDependency()
254 );
255 }
256 }
257 }
258 }
259 return $this->parents;
260 }
261
262 /**
263 * Determines whether there are child or parent references.
264 *
265 * @return boolean
266 */
267 public function hasReferences() {
268 return count($this->getChildren()) > 0 || count($this->getParents()) > 0;
269 }
270
271 /**
272 * Gets the outermost parent element.
273 *
274 * @return ElementEntity
275 */
276 public function getOuterMostParent() {
277 if (!isset($this->outerMostParent)) {
278 $parents = $this->getParents();
279 if (count($parents) === 0) {
280 $this->outerMostParent = $this;
281 } else {
282 $this->outerMostParent = FALSE;
283 /** @var $parent \TYPO3\CMS\Version\Dependency\ReferenceEntity */
284 foreach ($parents as $parent) {
285 $outerMostParent = $parent->getElement()->getOuterMostParent();
286 if ($outerMostParent instanceof \TYPO3\CMS\Version\Dependency\ElementEntity) {
287 $this->outerMostParent = $outerMostParent;
288 break;
289 } elseif ($outerMostParent === FALSE) {
290 break;
291 }
292 }
293 }
294 }
295 return $this->outerMostParent;
296 }
297
298 /**
299 * Gets nested children accumulated.
300 *
301 * @return array|ReferenceEntity[]
302 */
303 public function getNestedChildren() {
304 if (!isset($this->nestedChildren)) {
305 $this->nestedChildren = array();
306 $children = $this->getChildren();
307 /** @var $child \TYPO3\CMS\Version\Dependency\ReferenceEntity */
308 foreach ($children as $child) {
309 $this->nestedChildren = array_merge($this->nestedChildren, array($child->getElement()->__toString() => $child->getElement()), $child->getElement()->getNestedChildren());
310 }
311 }
312 return $this->nestedChildren;
313 }
314
315 /**
316 * Converts the object for string representation.
317 *
318 * @param string $table
319 * @param integer $id
320 * @return string
321 */
322 static public function getIdentifier($table, $id) {
323 return $table . ':' . $id;
324 }
325
326 /**
327 * Gets the database record of this element.
328 *
329 * @return array
330 */
331 public function getRecord() {
332 if (empty($this->record['uid']) || (int)$this->record['uid'] !== $this->id) {
333 $this->record = array();
334 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid,pid,t3ver_wsid,t3ver_state,t3ver_oid', $this->getTable(), 'uid=' . $this->getId());
335 if (is_array($rows)) {
336 $this->record = $rows[0];
337 }
338 }
339 return $this->record;
340 }
341
342 }