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