1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class cTreeItem {
25:
26: 27: 28: 29: 30:
31: var $_subitems = array();
32:
33: 34: 35: 36: 37:
38: var $_collapsed;
39:
40: 41: 42: 43: 44:
45: var $_id;
46:
47: 48: 49: 50: 51:
52: var $_name;
53:
54: 55: 56: 57: 58:
59: var $_level;
60:
61: 62: 63: 64: 65:
66: var $_attributes = array();
67:
68: 69: 70: 71: 72:
73: var $_parent = false;
74:
75: 76: 77: 78: 79:
80: var $_next = false;
81:
82: 83: 84: 85: 86:
87: var $_previous = false;
88:
89: public function __construct($id = "", $name = "", $collapsed = false) {
90: $this->_id = $id;
91: $this->_name = $name;
92: $this->_collapsed = $collapsed;
93: }
94:
95: 96: 97: 98: 99:
100: public function getId() {
101: return $this->_id;
102: }
103:
104: 105: 106: 107: 108:
109: public function getName() {
110: return $this->_name;
111: }
112:
113: 114: 115: 116: 117:
118: public function getCollapsed() {
119: return $this->_collapsed;
120: }
121:
122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135:
136: public function importTable($flat_array) {
137: $lastobj[0] = $this->_id;
138: $currentlevel = 1;
139:
140: if (!is_array($flat_array)) {
141: return false;
142: }
143:
144: foreach ($flat_array as $item) {
145: $mitem[$item["id"]] = new cTreeItem($item["id"], $item["name"]);
146:
147: if ($item["level"] > $currentlevel) {
148: $currentlevel++;
149: }
150:
151: if ($item["level"] < $currentlevel) {
152: $currentlevel = $item["level"];
153: }
154:
155: if (is_array($item["attributes"])) {
156: $mitem[$item["id"]]->setAttributes($item["attributes"]);
157: }
158:
159: if (array_key_exists("collapsed", $item)) {
160: $mitem[$item["id"]]->setCollapsed($item["collapsed"]);
161: }
162:
163:
164: if (array_key_exists("payload", $item)) {
165: $mitem[$item["id"]]->setPayloadObject($item["payload"]);
166: }
167:
168: if (is_object($mitem[$lastobj[$currentlevel - 1]])) {
169: $mitem[$lastobj[$currentlevel - 1]]->addItem($mitem[$item["id"]]);
170: } else {
171: $this->addItemToID($lastobj[$currentlevel - 1], $mitem[$item["id"]]);
172: }
173:
174: $lastobj[$currentlevel] = $item["id"];
175: }
176: }
177:
178: public function importStructuredArray($array) {
179: $i = array();
180:
181: $lastid = 1;
182: $level = 1;
183:
184: $this->_flattenArray($array, $i, $lastid, $level);
185:
186: $this->importTable($i);
187: }
188:
189: protected function _flattenArray($sourcearray, &$destarray, &$lastid, &$level) {
190: if ($lastid == false) {
191: $lastid = 1;
192: }
193:
194: if ($level == false) {
195: $level = 1;
196: }
197:
198: if (!is_array($sourcearray)) {
199: return false;
200: }
201:
202: foreach ($sourcearray as $id => $item) {
203: $lastid++;
204: $destarray[$lastid]["id"] = $item["class"] . "." . $id;
205:
206:
207: $meta = $item["object"]->getMetaObject();
208:
209: if (is_object($meta)) {
210: $destarray[$lastid]["name"] = $meta->getName();
211: }
212:
213: $destarray[$lastid]["level"] = $level;
214: $destarray[$lastid]["payload"] = $item["object"];
215:
216: if (count($item["items"]) > 0) {
217: $level++;
218: $this->_flattenArray($item["items"], $destarray, $lastid, $level);
219: $level--;
220: }
221: }
222: }
223:
224: 225: 226: 227: 228: 229: 230:
231: public function addItem(&$item) {
232:
233: if (($lastitem = end($this->_subitems)) !== false) {
234: $this->_subitems[key($this->_subitems)]->_next = $item->_id;
235: }
236:
237: $this->_subitems[count($this->_subitems)] = &$item;
238: $item->_parent = $this->_id;
239: $item->_previous = $lastitem->_id;
240: }
241:
242: 243: 244: 245: 246: 247: 248: 249:
250: public function addItemToID($id, &$item) {
251: if ($this->_id == $id) {
252:
253: if ($lastitem = end($this->_subitems) !== false) {
254: $this->_subitems[key($this->_subitems)]->_next = $item->_id;
255: }
256:
257: $this->_subitems[count($this->_subitems)] = &$item;
258: $item->_parent = $this->_id;
259: $item->_previous = $lastitem->_id;
260: return true;
261: } else {
262: foreach (array_keys($this->_subitems) as $key) {
263: $result = $this->_subitems[$key]->addItemToID($id, $item);
264: if ($result == true) {
265: return true;
266: }
267: }
268: }
269:
270: return false;
271: }
272:
273: 274: 275: 276: 277: 278: 279: 280:
281: public function moveItem($targetItem, $itemToMove) {
282: }
283:
284: 285: 286: 287: 288: 289: 290:
291: public function deleteItem($id) {
292: foreach (array_keys($this->_subitems) as $key) {
293: if ($this->_subitems[$key]->_id == $id) {
294:
295: $nextitem = next($this->_subitems);
296: $nkey = key($this->_subitems);
297: prev($this->_subitems);
298:
299: $previtem = &prev($this->_subitems);
300: $pkey = key($this->_subitems);
301: next($this->_subitems);
302:
303: if ($nextitem !== false) {
304: if ($previtem !== false) {
305: $this->_subitems[$nkey]->_previous = $this->_subitems[$pkey]->_id;
306: }
307: }
308:
309: if ($previtem !== false) {
310: if ($nextitem !== false) {
311: $this->_subitems[$pkey]->_next = $this->_subitems[$nkey]->_id;
312: }
313: }
314:
315: $itemcopy = $this->_subitems[$key];
316: unset($this->_subitems[$key]);
317:
318: return ($itemcopy);
319: } else {
320: $this->_subitems[$key]->deleteItem($id);
321: }
322: }
323: }
324:
325: 326: 327: 328: 329: 330: 331: 332: 333:
334: public function &getItemByID($id) {
335: if ($this->_id == $id) {
336: return $this;
337: } else {
338: foreach (array_keys($this->_subitems) as $key) {
339: $retObj = &$this->_subitems[$key]->getItemByID($id);
340: if ($retObj->_id == $id) {
341: return $retObj;
342: }
343: }
344: }
345:
346: return false;
347: }
348:
349: 350: 351: 352: 353: 354: 355: 356:
357: public function setAttribute($attributeName, $attributeValue) {
358: $this->_attributes[$attributeName] = $attributeValue;
359: }
360:
361: 362: 363: 364: 365: 366: 367: 368:
369: public function setAttributes($aAttributeArray) {
370: $this->_attributes = array_merge($aAttributeArray, $this->_attributes);
371: }
372:
373: 374: 375: 376: 377: 378: 379:
380: public function getAttribute($attributeName) {
381: if (array_key_exists($attributeName, $this->_attributes)) {
382: return ($this->_attributes[$attributeName]);
383: } else {
384: return false;
385: }
386: }
387:
388: 389: 390: 391: 392: 393: 394:
395: public function deleteAttribute($attributeName) {
396: if (array_key_exists($attributeName, $this->_attributes)) {
397: unset($this->_attributes[$attributeName]);
398: return true;
399: } else {
400: return false;
401: }
402: }
403:
404: public function hasAttribute($attributeName, $bRecursive = false) {
405: if (array_key_exists($attributeName, $this->_attributes)) {
406: return true;
407: } else {
408: if ($bRecursive == true) {
409: if (count($this->_subitems) > 0) {
410: foreach ($this->_subitems as $oSubitem) {
411: $bFound = $oSubitem->hasAttribute($attributeName, true);
412: if ($bFound == true) {
413: return true;
414: }
415: }
416: }
417:
418: return false;
419: } else {
420: return false;
421: }
422: }
423: }
424:
425: 426: 427: 428: 429: 430:
431: public function setExpanded($id) {
432: if (is_array($id)) {
433: if (in_array($this->_id, $id, true)) {
434: $this->_collapsed = false;
435: }
436:
437: foreach (array_keys($this->_subitems) as $key) {
438: $this->_subitems[$key]->setExpanded($id);
439: }
440: } else {
441: if ($this->_id === $id) {
442: $this->_collapsed = false;
443: return true;
444: } else {
445: foreach (array_keys($this->_subitems) as $key) {
446: $this->_subitems[$key]->setExpanded($id);
447: }
448: }
449: }
450: }
451:
452: 453: 454: 455: 456: 457:
458: public function setCollapsed($id) {
459: if (is_array($id)) {
460: if (in_array($this->_id, $id, true)) {
461: $this->_collapsed = true;
462: }
463:
464: foreach (array_keys($this->_subitems) as $key) {
465: $this->_subitems[$key]->setCollapsed($id);
466: }
467: } else {
468: if ($this->_id === $id) {
469: $this->_collapsed = true;
470: return true;
471: } else {
472: foreach (array_keys($this->_subitems) as $key) {
473: $this->_subitems[$key]->setCollapsed($id);
474: }
475: }
476: }
477: }
478:
479: 480: 481: 482: 483: 484: 485:
486: protected function _expandBelowLevel($leveloffset) {
487: if ($leveloffset > 0) {
488: $leveloffset--;
489: } else {
490: $this->_collapsed = false;
491: }
492:
493: foreach (array_keys($this->_subitems) as $key) {
494: $this->_subitems[$key]->expandBelowLevel($leveloffset);
495: }
496: }
497:
498: 499: 500: 501: 502: 503: 504:
505: protected function _collapseBelowLevel($leveloffset) {
506: if ($leveloffset > 0) {
507: $leveloffset--;
508: } else {
509: $this->_collapsed = true;
510: }
511:
512: foreach (array_keys($this->_subitems) as $key) {
513: $this->_subitems[$key]->collapseBelowLevel($leveloffset);
514: }
515: }
516:
517: 518: 519: 520: 521: 522: 523:
524: protected function _expandBelowID($id, $found = false) {
525: if ($found === true) {
526: $this->_collapsed = false;
527: }
528:
529: if ($this->_id == $id) {
530: $found = true;
531: $this->_collapsed = false;
532: }
533:
534: foreach (array_keys($this->_subitems) as $key) {
535: $this->_subitems[$key]->expandBelowID($id, $found);
536: }
537: }
538:
539: 540: 541: 542: 543: 544: 545:
546: protected function _collapseBelowID($id, $found = false) {
547: if ($found === true) {
548: $this->_collapsed = true;
549: }
550:
551: if ($this->_id == $id) {
552: $found = true;
553: $this->_collapsed = true;
554: }
555:
556: foreach (array_keys($this->_subitems) as $key) {
557: $this->_subitems[$key]->collapseBelowID($id, $found);
558: }
559: }
560:
561: 562: 563: 564: 565: 566:
567: public function getCollapsedList(&$list) {
568: if (!is_array($list)) {
569: $list = array();
570: }
571:
572: if ($this->_collapsed == true) {
573: $list[] = $this->_id;
574: }
575:
576: foreach (array_keys($this->_subitems) as $key) {
577: $this->_subitems[$key]->getCollapsedList($list);
578: }
579: }
580:
581: 582: 583: 584: 585: 586:
587: public function getExpandedList(&$list) {
588: if (!is_array($list)) {
589: $list = array();
590: }
591:
592: if ($this->_collapsed == false && !in_array($this->_id, $list)) {
593: $list[] = $this->_id;
594: }
595:
596: foreach (array_keys($this->_subitems) as $key) {
597: $this->_subitems[$key]->getExpandedList($list);
598: }
599: }
600:
601: 602: 603: 604: 605: 606: 607:
608: public function setPayloadObject($payload) {
609: $this->payload = $payload;
610: }
611:
612: 613: 614: 615: 616: 617:
618: public function unsetPayloadObject() {
619: }
620:
621: 622: 623: 624: 625: 626: 627: 628:
629: public function traverse(&$objects, $level = 0) {
630: $objects[count($objects)] = &$this;
631: $this->_level = $level;
632:
633: if ($this->_collapsed == false) {
634: foreach (array_keys($this->_subitems) as $key) {
635: $this->_subitems[$key]->traverse($objects, $level + 1);
636: }
637: }
638: }
639:
640: 641: 642: 643: 644: 645: 646: 647:
648: public function flatTraverse($level = 0) {
649: $objects[] = &$this;
650: $this->_level = $level;
651:
652: if ($this->_collapsed == false) {
653: foreach (array_keys($this->_subitems) as $key) {
654: $objects = array_merge($objects, $this->_subitems[$key]->flatTraverse($level + 1));
655: }
656: }
657:
658: return $objects;
659: }
660:
661: 662: 663: 664: 665: 666: 667:
668: public function setName($name) {
669: $this->_name = $name;
670: }
671:
672: }
673: