[BUGFIX] Removing child records in a workspace does not work
[Packages/TYPO3.CMS.git] / t3lib / utility / dependency / class.t3lib_utility_dependency_element.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Oliver Hader <oliver@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Object to hold information on a dependent database element in abstract.
30 */
31 class t3lib_utility_Dependency_Element {
32 const REFERENCES_ChildOf = 'childOf';
33 const REFERENCES_ParentOf = 'parentOf';
34 const EVENT_Construct = 't3lib_utility_Dependency_Element::construct';
35 const EVENT_CreateChildReference = 't3lib_utility_Dependency_Element::createChildReference';
36 const EVENT_CreateParentReference = 't3lib_utility_Dependency_Element::createParentReference';
37 const RESPONSE_Skip = 't3lib_utility_Dependency_Element->skip';
38
39 /**
40 * @var string
41 */
42 protected $table;
43
44 /**
45 * @var integer
46 */
47 protected $id;
48
49 /**
50 * @var array
51 */
52 protected $data;
53
54 /**
55 * @var array
56 */
57 protected $record;
58
59 /**
60 * @var t3lib_utility_Dependency
61 */
62 protected $dependency;
63
64 /**
65 * @var array
66 */
67 protected $children;
68
69 /**
70 * @var array
71 */
72 protected $parents;
73
74 /**
75 * @var boolean
76 */
77 protected $traversingParents = FALSE;
78
79 /**
80 * @var t3lib_utility_Dependency_Element
81 */
82 protected $outerMostParent;
83
84 /**
85 * @var array
86 */
87 protected $nestedChildren;
88
89 /**
90 * Creates this object.
91 *
92 * @param string $table
93 * @param integer $id
94 * @param array $data (optional)
95 * @param t3lib_utility_Dependency $dependency
96 */
97 public function __construct($table, $id, array $data = array(), t3lib_utility_Dependency $dependency) {
98 $this->table = $table;
99 $this->id = intval($id);
100 $this->data = $data;
101 $this->dependency = $dependency;
102
103 $this->dependency->executeEventCallback(self::EVENT_Construct, $this);
104 }
105
106 /**
107 * Gets the table.
108 *
109 * @return string
110 */
111 public function getTable() {
112 return $this->table;
113 }
114
115 /**
116 * Gets the id.
117 *
118 * @return integer
119 */
120 public function getId() {
121 return $this->id;
122 }
123
124 /**
125 * Gets the data.
126 *
127 * @return array
128 */
129 public function getData() {
130 return $this->data;
131 }
132
133 /**
134 * Gets a value for a particular key from the data.
135 *
136 * @param string $key
137 * @return mixed
138 */
139 public function getDataValue($key) {
140 $result = NULL;
141
142 if ($this->hasDataValue($key)) {
143 $result = $this->data[$key];
144 }
145
146 return $result;
147 }
148
149 /**
150 * Sets a value for a particular key in the data.
151 *
152 * @param string $key
153 * @param mixed $value
154 * @return void
155 */
156 public function setDataValue($key, $value) {
157 $this->data[$key] = $value;
158 }
159
160 /**
161 * Determines whether a particular key holds data.
162 *
163 * @param string $key
164 * @return
165 */
166 public function hasDataValue($key) {
167 return (isset($this->data[$key]));
168 }
169
170 /**
171 * Converts this object for string representation.
172 *
173 * @return string
174 */
175 public function __toString() {
176 return self::getIdentifier($this->table, $this->id);
177 }
178
179 /**
180 * Gets the parent dependency object.
181 *
182 * @return t3lib_utility_Dependency
183 */
184 public function getDependency() {
185 return $this->dependency;
186 }
187
188 /**
189 * Gets all child references.
190 *
191 * @return array
192 */
193 public function getChildren() {
194 if (!isset($this->children)) {
195 $this->children = array();
196 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
197 '*',
198 'sys_refindex',
199 'tablename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->table, 'sys_refindex') .
200 ' AND recuid=' . $this->id
201 );
202 if (is_array($rows)) {
203 foreach ($rows as $row) {
204 $reference = $this->getDependency()->getFactory()->getReferencedElement(
205 $row['ref_table'], $row['ref_uid'], $row['field'], array(), $this->getDependency()
206 );
207 $callbackResponse = $this->dependency->executeEventCallback(
208 self::EVENT_CreateChildReference,
209 $this, array('reference' => $reference)
210 );
211 if ($callbackResponse !== self::RESPONSE_Skip) {
212 $this->children[] = $reference;
213 }
214 }
215 }
216 }
217 return $this->children;
218 }
219
220 /**
221 * Gets all parent references.
222 *
223 * @return array
224 */
225 public function getParents() {
226 if (!isset($this->parents)) {
227 $this->parents = array();
228 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
229 '*',
230 'sys_refindex',
231 'ref_table=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->table, 'sys_refindex') .
232 ' AND deleted=0 AND ref_uid=' . $this->id
233 );
234 if (is_array($rows)) {
235 foreach ($rows as $row) {
236 $reference = $this->getDependency()->getFactory()->getReferencedElement(
237 $row['tablename'], $row['recuid'], $row['field'], array(), $this->getDependency()
238 );
239 $callbackResponse = $this->dependency->executeEventCallback(
240 self::EVENT_CreateParentReference,
241 $this, array('reference' => $reference)
242 );
243 if ($callbackResponse !== self::RESPONSE_Skip) {
244 $this->parents[] = $reference;
245 }
246 }
247 }
248 }
249 return $this->parents;
250 }
251
252 /**
253 * Determines whether there are child or parent references.
254 *
255 * @return boolean
256 */
257 public function hasReferences() {
258 return (count($this->getChildren()) > 0 || count($this->getParents()) > 0);
259 }
260
261 /**
262 * Gets the outermost parent element.
263 *
264 * @return t3lib_utility_Dependency_Element
265 */
266 public function getOuterMostParent() {
267 if (!isset($this->outerMostParent)) {
268 $parents = $this->getParents();
269 if (count($parents) === 0) {
270 $this->outerMostParent = $this;
271 } else {
272 $this->outerMostParent = FALSE;
273 /** @var $parent t3lib_utility_Dependency_Reference */
274 foreach ($parents as $parent) {
275 $outerMostParent = $parent->getElement()->getOuterMostParent();
276 if ($outerMostParent instanceof t3lib_utility_Dependency_Element) {
277 $this->outerMostParent = $outerMostParent;
278 break;
279 } elseif ($outerMostParent === FALSE) {
280 break;
281 }
282 }
283 }
284 }
285
286 return $this->outerMostParent;
287 }
288
289 /**
290 * Gets nested children accumulated.
291 *
292 * @return array
293 */
294 public function getNestedChildren() {
295 if (!isset($this->nestedChildren)) {
296 $this->nestedChildren = array();
297 $children = $this->getChildren();
298 /** @var $child t3lib_utility_Dependency_Reference */
299 foreach ($children as $child) {
300 $this->nestedChildren = array_merge(
301 $this->nestedChildren,
302 array($child->getElement()->__toString() => $child->getElement()),
303 $child->getElement()->getNestedChildren()
304 );
305 }
306 }
307
308 return $this->nestedChildren;
309 }
310
311 /**
312 * Converts the object for string representation.
313 *
314 * @param string $table
315 * @param integer $id
316 * @return string
317 */
318 public static function getIdentifier($table, $id) {
319 return $table . ':' . $id;
320 }
321
322 /**
323 * Gets the database record of this element.
324 *
325 * @return array
326 */
327 public function getRecord() {
328 if (!isset($this->record)) {
329 $this->record = array();
330 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
331 '*',
332 $this->getTable(),
333 'uid=' . $this->getId()
334 );
335 if (is_array($rows)) {
336 $this->record = $rows[0];
337 }
338 }
339 return $this->record;
340 }
341 }
342 ?>