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: public function importTable($flat_array) {
135: $lastobj[0] = $this->_id;
136: $currentlevel = 1;
137:
138: if (!is_array($flat_array)) {
139: return false;
140: }
141:
142: foreach ($flat_array as $item) {
143: $mitem[$item["id"]] = new cTreeItem($item["id"], $item["name"]);
144:
145: if ($item["level"] > $currentlevel) {
146: $currentlevel++;
147: }
148:
149: if ($item["level"] < $currentlevel) {
150: $currentlevel = $item["level"];
151: }
152:
153: if (is_array($item["attributes"])) {
154: $mitem[$item["id"]]->setAttributes($item["attributes"]);
155: }
156:
157: if (array_key_exists("collapsed", $item)) {
158: $mitem[$item["id"]]->setCollapsed($item["collapsed"]);
159: }
160:
161:
162: if (array_key_exists("payload", $item)) {
163: $mitem[$item["id"]]->setPayloadObject($item["payload"]);
164: }
165:
166: if (is_object($mitem[$lastobj[$currentlevel - 1]])) {
167: $mitem[$lastobj[$currentlevel - 1]]->addItem($mitem[$item["id"]]);
168: } else {
169: $this->addItemToID($lastobj[$currentlevel - 1], $mitem[$item["id"]]);
170: }
171:
172: $lastobj[$currentlevel] = $item["id"];
173: }
174: }
175:
176: public function importStructuredArray($array) {
177: $i = array();
178:
179: $lastid = 1;
180: $level = 1;
181:
182: $this->_flattenArray($array, $i, $lastid, $level);
183:
184: $this->importTable($i);
185: }
186:
187: protected function _flattenArray($sourcearray, &$destarray, &$lastid, &$level) {
188: if ($lastid == false) {
189: $lastid = 1;
190: }
191:
192: if ($level == false) {
193: $level = 1;
194: }
195:
196: if (!is_array($sourcearray)) {
197: return false;
198: }
199:
200: foreach ($sourcearray as $id => $item) {
201: $lastid++;
202: $destarray[$lastid]["id"] = $item["class"] . "." . $id;
203:
204:
205: $meta = $item["object"]->getMetaObject();
206:
207: if (is_object($meta)) {
208: $destarray[$lastid]["name"] = $meta->getName();
209: }
210:
211: $destarray[$lastid]["level"] = $level;
212: $destarray[$lastid]["payload"] = $item["object"];
213:
214: if (count($item["items"]) > 0) {
215: $level++;
216: $this->_flattenArray($item["items"], $destarray, $lastid, $level);
217: $level--;
218: }
219: }
220: }
221:
222: 223: 224: 225: 226:
227: public function addItem(&$item) {
228:
229: if (($lastitem = end($this->_subitems)) !== false) {
230: $this->_subitems[key($this->_subitems)]->_next = $item->_id;
231: }
232:
233: $this->_subitems[count($this->_subitems)] = &$item;
234: $item->_parent = $this->_id;
235: $item->_previous = $lastitem->_id;
236: }
237:
238: 239: 240: 241: 242: 243:
244: public function addItemToID($id, &$item) {
245: if ($this->_id == $id) {
246:
247: if ($lastitem = end($this->_subitems) !== false) {
248: $this->_subitems[key($this->_subitems)]->_next = $item->_id;
249: }
250:
251: $this->_subitems[count($this->_subitems)] = &$item;
252: $item->_parent = $this->_id;
253: $item->_previous = $lastitem->_id;
254: return true;
255: } else {
256: foreach (array_keys($this->_subitems) as $key) {
257: $result = $this->_subitems[$key]->addItemToID($id, $item);
258: if ($result == true) {
259: return true;
260: }
261: }
262: }
263:
264: return false;
265: }
266:
267: 268: 269: 270: 271: 272:
273: public function moveItem($targetItem, $itemToMove) {
274: }
275:
276: 277: 278: 279: 280: 281:
282: public function deleteItem($id) {
283: foreach (array_keys($this->_subitems) as $key) {
284: if ($this->_subitems[$key]->_id == $id) {
285:
286: $nextitem = next($this->_subitems);
287: $nkey = key($this->_subitems);
288: prev($this->_subitems);
289:
290: $previtem = &prev($this->_subitems);
291: $pkey = key($this->_subitems);
292: next($this->_subitems);
293:
294: if ($nextitem !== false) {
295: if ($previtem !== false) {
296: $this->_subitems[$nkey]->_previous = $this->_subitems[$pkey]->_id;
297: }
298: }
299:
300: if ($previtem !== false) {
301: if ($nextitem !== false) {
302: $this->_subitems[$pkey]->_next = $this->_subitems[$nkey]->_id;
303: }
304: }
305:
306: $itemcopy = $this->_subitems[$key];
307: unset($this->_subitems[$key]);
308:
309: return ($itemcopy);
310: } else {
311: $this->_subitems[$key]->deleteItem($id);
312: }
313: }
314: }
315:
316: 317: 318: 319: 320: 321: 322: 323:
324: public function &getItemByID($id) {
325: if ($this->_id == $id) {
326: return $this;
327: } else {
328: foreach (array_keys($this->_subitems) as $key) {
329: $retObj = &$this->_subitems[$key]->getItemByID($id);
330: if ($retObj->_id == $id) {
331: return $retObj;
332: }
333: }
334: }
335:
336: return false;
337: }
338:
339: 340: 341: 342: 343: 344:
345: public function setAttribute($attributeName, $attributeValue) {
346: $this->_attributes[$attributeName] = $attributeValue;
347: }
348:
349: 350: 351: 352: 353: 354:
355: public function setAttributes($aAttributeArray) {
356: $this->_attributes = array_merge($aAttributeArray, $this->_attributes);
357: }
358:
359: 360: 361: 362: 363: 364:
365: public function getAttribute($attributeName) {
366: if (array_key_exists($attributeName, $this->_attributes)) {
367: return ($this->_attributes[$attributeName]);
368: } else {
369: return false;
370: }
371: }
372:
373: 374: 375: 376: 377:
378: public function deleteAttribute($attributeName) {
379: if (array_key_exists($attributeName, $this->_attributes)) {
380: unset($this->_attributes[$attributeName]);
381: return true;
382: } else {
383: return false;
384: }
385: }
386:
387: public function hasAttribute($attributeName, $bRecursive = false) {
388: if (array_key_exists($attributeName, $this->_attributes)) {
389: return true;
390: } else {
391: if ($bRecursive == true) {
392: if (count($this->_subitems) > 0) {
393: foreach ($this->_subitems as $oSubitem) {
394: $bFound = $oSubitem->hasAttribute($attributeName, true);
395: if ($bFound == true) {
396: return true;
397: }
398: }
399: }
400:
401: return false;
402: } else {
403: return false;
404: }
405: }
406: }
407:
408: 409: 410: 411:
412: public function setExpanded($id) {
413: if (is_array($id)) {
414: if (in_array($this->_id, $id, true)) {
415: $this->_collapsed = false;
416: }
417:
418: foreach (array_keys($this->_subitems) as $key) {
419: $this->_subitems[$key]->setExpanded($id);
420: }
421: } else {
422: if ($this->_id === $id) {
423: $this->_collapsed = false;
424: return true;
425: } else {
426: foreach (array_keys($this->_subitems) as $key) {
427: $this->_subitems[$key]->setExpanded($id);
428: }
429: }
430: }
431: }
432:
433: 434: 435: 436:
437: public function setCollapsed($id) {
438: if (is_array($id)) {
439: if (in_array($this->_id, $id, true)) {
440: $this->_collapsed = true;
441: }
442:
443: foreach (array_keys($this->_subitems) as $key) {
444: $this->_subitems[$key]->setCollapsed($id);
445: }
446: } else {
447: if ($this->_id === $id) {
448: $this->_collapsed = true;
449: return true;
450: } else {
451: foreach (array_keys($this->_subitems) as $key) {
452: $this->_subitems[$key]->setCollapsed($id);
453: }
454: }
455: }
456: }
457:
458: 459: 460: 461: 462:
463: protected function _expandBelowLevel($leveloffset) {
464: if ($leveloffset > 0) {
465: $leveloffset--;
466: } else {
467: $this->_collapsed = false;
468: }
469:
470: foreach (array_keys($this->_subitems) as $key) {
471: $this->_subitems[$key]->expandBelowLevel($leveloffset);
472: }
473: }
474:
475: 476: 477: 478: 479:
480: protected function _collapseBelowLevel($leveloffset) {
481: if ($leveloffset > 0) {
482: $leveloffset--;
483: } else {
484: $this->_collapsed = true;
485: }
486:
487: foreach (array_keys($this->_subitems) as $key) {
488: $this->_subitems[$key]->collapseBelowLevel($leveloffset);
489: }
490: }
491:
492: 493: 494: 495: 496:
497: protected function _expandBelowID($id, $found = false) {
498: if ($found === true) {
499: $this->_collapsed = false;
500: }
501:
502: if ($this->_id == $id) {
503: $found = true;
504: $this->_collapsed = false;
505: }
506:
507: foreach (array_keys($this->_subitems) as $key) {
508: $this->_subitems[$key]->expandBelowID($id, $found);
509: }
510: }
511:
512: 513: 514: 515: 516:
517: protected function _collapseBelowID($id, $found = false) {
518: if ($found === true) {
519: $this->_collapsed = true;
520: }
521:
522: if ($this->_id == $id) {
523: $found = true;
524: $this->_collapsed = true;
525: }
526:
527: foreach (array_keys($this->_subitems) as $key) {
528: $this->_subitems[$key]->collapseBelowID($id, $found);
529: }
530: }
531:
532: 533: 534: 535: 536: 537:
538: public function getCollapsedList(&$list) {
539: if (!is_array($list)) {
540: $list = array();
541: }
542:
543: if ($this->_collapsed == true) {
544: $list[] = $this->_id;
545: }
546:
547: foreach (array_keys($this->_subitems) as $key) {
548: $this->_subitems[$key]->getCollapsedList($list);
549: }
550: }
551:
552: 553: 554: 555: 556: 557:
558: public function getExpandedList(&$list) {
559: if (!is_array($list)) {
560: $list = array();
561: }
562:
563: if ($this->_collapsed == false && !in_array($this->_id, $list)) {
564: $list[] = $this->_id;
565: }
566:
567: foreach (array_keys($this->_subitems) as $key) {
568: $this->_subitems[$key]->getExpandedList($list);
569: }
570: }
571:
572: 573: 574: 575: 576:
577: public function setPayloadObject($payload) {
578: $this->payload = $payload;
579: }
580:
581: 582: 583: 584: 585:
586: public function unsetPayloadObject() {
587:
588: }
589:
590: 591: 592: 593: 594: 595: 596: 597:
598: public function traverse(&$objects, $level = 0) {
599: $objects[count($objects)] = &$this;
600: $this->_level = $level;
601:
602: if ($this->_collapsed == false) {
603: foreach (array_keys($this->_subitems) as $key) {
604: $this->_subitems[$key]->traverse($objects, $level + 1);
605: }
606: }
607: }
608:
609: 610: 611: 612: 613: 614: 615: 616:
617: public function flatTraverse($level = 0) {
618: $objects[] = &$this;
619: $this->_level = $level;
620:
621: if ($this->_collapsed == false) {
622: foreach (array_keys($this->_subitems) as $key) {
623: $objects = array_merge($objects, $this->_subitems[$key]->flatTraverse($level + 1));
624: }
625: }
626:
627: return $objects;
628: }
629:
630: 631: 632: 633: 634: 635: 636:
637: public function setName($name) {
638: $this->_name = $name;
639: }
640:
641: }
642: