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