4fe4cffa90d5b9e9372d60c9fb915825086457c2
[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 t3lib_utility_Dependency
56 */
57 protected $dependency;
58
59 /**
60 * @var array
61 */
62 protected $children;
63
64 /**
65 * @var array
66 */
67 protected $parents;
68
69 /**
70 * @var boolean
71 */
72 protected $traversingParents = FALSE;
73
74 /**
75 * @var t3lib_utility_Dependency_Element
76 */
77 protected $outerMostParent;
78
79 /**
80 * @var array
81 */
82 protected $nestedChildren;
83
84 /**
85 * Creates this object.
86 *
87 * @param string $table
88 * @param integer $id
89 * @param array $data (optional)
90 * @param t3lib_utility_Dependency $dependency
91 */
92 public function __construct($table, $id, array $data = array(), t3lib_utility_Dependency $dependency) {
93 $this->table = $table;
94 $this->id = intval($id);
95 $this->data = $data;
96 $this->dependency = $dependency;
97
98 $this->dependency->executeEventCallback(self::EVENT_Construct, $this);
99 }
100
101 /**
102 * Gets the table.
103 *
104 * @return string
105 */
106 public function getTable() {
107 return $this->table;
108 }
109
110 /**
111 * Gets the id.
112 *
113 * @return integer
114 */
115 public function getId() {
116 return $this->id;
117 }
118
119 /**
120 * Gets the data.
121 *
122 * @return array
123 */
124 public function getData() {
125 return $this->data;
126 }
127
128 /**
129 * Gets a value for a particular key from the data.
130 *
131 * @param string $key
132 * @return mixed
133 */
134 public function getDataValue($key) {
135 $result = NULL;
136
137 if ($this->hasDataValue($key)) {
138 $result = $this->data[$key];
139 }
140
141 return $result;
142 }
143
144 /**
145 * Sets a value for a particular key in the data.
146 *
147 * @param string $key
148 * @param mixed $value
149 * @return void
150 */
151 public function setDataValue($key, $value) {
152 $this->data[$key] = $value;
153 }
154
155 /**
156 * Determines whether a particular key holds data.
157 *
158 * @param string $key
159 * @return
160 */
161 public function hasDataValue($key) {
162 return (isset($this->data[$key]));
163 }
164
165 /**
166 * Converts this object for string representation.
167 *
168 * @return string
169 */
170 public function __toString() {
171 return self::getIdentifier($this->table, $this->id);
172 }
173
174 /**
175 * Gets the parent dependency object.
176 *
177 * @return t3lib_utility_Dependency
178 */
179 public function getDependency() {
180 return $this->dependency;
181 }
182
183 /**
184 * Gets all child references.
185 *
186 * @return array
187 */
188 public function getChildren() {
189 if (!isset($this->children)) {
190 $this->children = array();
191 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
192 '*',
193 'sys_refindex',
194 'tablename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->table, 'sys_refindex') .
195 ' AND recuid=' . $this->id
196 );
197 if (is_array($rows)) {
198 foreach ($rows as $row) {
199 $reference = $this->getDependency()->getFactory()->getReferencedElement(
200 $row['ref_table'], $row['ref_uid'], $row['field'], array(), $this->getDependency()
201 );
202 $callbackResponse = $this->dependency->executeEventCallback(
203 self::EVENT_CreateChildReference,
204 $this, array('reference' => $reference)
205 );
206 if ($callbackResponse !== self::RESPONSE_Skip) {
207 $this->children[] = $reference;
208 }
209 }
210 }
211 }
212 return $this->children;
213 }
214
215 /**
216 * Gets all parent references.
217 *
218 * @return array
219 */
220 public function getParents() {
221 if (!isset($this->parents)) {
222 $this->parents = array();
223 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
224 '*',
225 'sys_refindex',
226 'ref_table=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->table, 'sys_refindex') .
227 ' AND deleted=0 AND ref_uid=' . $this->id
228 );
229 if (is_array($rows)) {
230 foreach ($rows as $row) {
231 $reference = $this->getDependency()->getFactory()->getReferencedElement(
232 $row['tablename'], $row['recuid'], $row['field'], array(), $this->getDependency()
233 );
234 $callbackResponse = $this->dependency->executeEventCallback(
235 self::EVENT_CreateParentReference,
236 $this, array('reference' => $reference)
237 );
238 if ($callbackResponse !== self::RESPONSE_Skip) {
239 $this->parents[] = $reference;
240 }
241 }
242 }
243 }
244 return $this->parents;
245 }
246
247 /**
248 * Determines whether there are child or parent references.
249 *
250 * @return boolean
251 */
252 public function hasReferences() {
253 return (count($this->getChildren()) > 0 || count($this->getParents()) > 0);
254 }
255
256 /**
257 * Gets the outermost parent element.
258 *
259 * @return t3lib_utility_Dependency_Element
260 */
261 public function getOuterMostParent() {
262 if (!isset($this->outerMostParent)) {
263 $parents = $this->getParents();
264 if (count($parents) === 0) {
265 $this->outerMostParent = $this;
266 } else {
267 $this->outerMostParent = FALSE;
268 /** @var $parent t3lib_utility_Dependency_Reference */
269 foreach ($parents as $parent) {
270 $outerMostParent = $parent->getElement()->getOuterMostParent();
271 if ($outerMostParent instanceof t3lib_utility_Dependency_Element) {
272 $this->outerMostParent = $outerMostParent;
273 break;
274 } elseif ($outerMostParent === FALSE) {
275 break;
276 }
277 }
278 }
279 }
280
281 return $this->outerMostParent;
282 }
283
284 /**
285 * Gets nested children accumulated.
286 *
287 * @return array
288 */
289 public function getNestedChildren() {
290 if (!isset($this->nestedChildren)) {
291 $this->nestedChildren = array();
292 $children = $this->getChildren();
293 /** @var $child t3lib_utility_Dependency_Reference */
294 foreach ($children as $child) {
295 $this->nestedChildren = array_merge(
296 $this->nestedChildren,
297 array($child->getElement()->__toString() => $child->getElement()),
298 $child->getElement()->getNestedChildren()
299 );
300 }
301 }
302
303 return $this->nestedChildren;
304 }
305
306 /**
307 * Converts the object for string representation.
308 *
309 * @param string $table
310 * @param integer $id
311 * @return string
312 */
313 public static function getIdentifier($table, $id) {
314 return $table . ':' . $id;
315 }
316 }
317 ?>