Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cGuiBackendHelpbox
  • cGuiFileOverview
  • cGuiFoldingRow
  • cGuiList
  • cGuiMenu
  • cGuiNavigation
  • cGuiNotification
  • cGuiObjectPager
  • cGuiPage
  • cGuiScrollList
  • cGuiSourceEditor
  • cGuiTableForm
  • cGuiTree
  • cPager
  • cTemplate
  • cTree
  • cTreeItem
  • NoteLink
  • NoteList
  • NoteListItem
  • NoteView
  • TODOLink
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * This file contains the tree item class.
  5:  *
  6:  * @package    Core
  7:  * @subpackage GUI
  8:  * @version    SVN Revision $Rev:$
  9:  *
 10:  * @author     Bjoern Behrens
 11:  * @copyright  four for business AG <www.4fb.de>
 12:  * @license    http://www.contenido.org/license/LIZENZ.txt
 13:  * @link       http://www.4fb.de
 14:  * @link       http://www.contenido.org
 15:  */
 16: 
 17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 18: 
 19: /**
 20:  * Tree item class
 21:  *
 22:  * @package    Core
 23:  * @subpackage GUI
 24:  */
 25: class cTreeItem {
 26: 
 27:     /**
 28:      * Sub Items for this tree item
 29:      *
 30:      * @var array
 31:      */
 32:     var $_subitems = array();
 33: 
 34:     /**
 35:      * Determinates if this tree item is collapsed
 36:      *
 37:      * @var bool
 38:      */
 39:     var $_collapsed;
 40: 
 41:     /**
 42:      * ID for this item
 43:      *
 44:      * @var string
 45:      */
 46:     var $_id;
 47: 
 48:     /**
 49:      * Name for this item
 50:      *
 51:      * @var string
 52:      */
 53:     var $_name;
 54: 
 55:     /**
 56:      * Contains the level of this item
 57:      *
 58:      * @var int
 59:      */
 60:     var $_level;
 61: 
 62:     /**
 63:      * Contains custom entries
 64:      *
 65:      * @var array
 66:      */
 67:     var $_attributes = array();
 68: 
 69:     /**
 70:      * Contains the parent of this item
 71:      *
 72:      * @var array
 73:      */
 74:     var $_parent = false;
 75: 
 76:     /**
 77:      * Contains the next item
 78:      *
 79:      * @var array
 80:      */
 81:     var $_next = false;
 82: 
 83:     /**
 84:      * Contains the previous item
 85:      *
 86:      * @var array
 87:      */
 88:     var $_previous = false;
 89: 
 90:     /**
 91:      *
 92:      * @param string $id [optional]
 93:      * @param string $name [optional]
 94:      * @param bool $collapsed [optional]
 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:      * Id getter
104:      *
105:      * @return string
106:      */
107:     public function getId() {
108:         return $this->_id;
109:     }
110: 
111:     /**
112:      * Name getter
113:      *
114:      * @return string
115:      */
116:     public function getName() {
117:         return $this->_name;
118:     }
119: 
120:     /**
121:      * Collapsed state getter
122:      *
123:      * @return bool
124:      */
125:     public function getCollapsed() {
126:         return $this->_collapsed;
127:     }
128: 
129:     /**
130:      * Imports a table from an array of arrays.
131:      *
132:      * The entries "collapsed" and "attributes" are optional!
133:      *
134:      * @param array $flat_array
135:      *         array(
136:      *             array(
137:      *                 "id" => "Item ID",
138:      *                 "name" => "Item name",
139:      *                 "level" => 1,
140:      *                 "collapsed" => true|false,
141:      *                 "attributes" => array(
142:      *                     "attr_name" => "attr_value"
143:      *                 )
144:      *             )
145:      *         )
146:      * @return bool
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:             /* Set payload object */
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:      * @param array $array
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:      * @param array $sourcearray
208:      * @param array $destarray
209:      * @param int $lastid
210:      * @param int $level
211:      * @return bool
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:             // Name should be fetched via the meta object
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:      * adds an item as a subitem to the current item
250:      *
251:      * @param cTreeItem $item
252:      *         item object to add
253:      */
254:     public function addItem(&$item) {
255:         // Update last item
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:      * adds an item to a specific ID
267:      *
268:      * @param string $id
269:      *         ID to add the item to
270:      * @param cTreeItem $item
271:      *         Item to add
272:      * @return bool
273:      */
274:     public function addItemToID($id, &$item) {
275:         if ($this->_id == $id) {
276:             // Update last item
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:      * moves an item to another object
299:      *
300:      * @param cTreeItem $targetItem
301:      *         Item to move the subitem to
302:      * @param mixed $itemToMove
303:      *         cTreeItem-Object or id of object to move
304:      */
305:     public function moveItem($targetItem, $itemToMove) {
306:     }
307: 
308:     /**
309:      * deletes a subitem
310:      *
311:      * @param mixed $id
312:      *         item object or ID to delete
313:      * @return object
314:      *         deleted object
315:      */
316:     public function deleteItem($id) {
317:         foreach (array_keys($this->_subitems) as $key) {
318:             if ($this->_subitems[$key]->_id == $id) {
319:                 // Fetch next item, reset to current item
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:      * Retrieves a specific item by its ID.
352:      * Note that this
353:      * function traverses all subitems to find the correct item.
354:      *
355:      * @param string $id
356:      *         ID to retrieve
357:      * @return cTreeItem
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:      * sets a custom attribute for this TreeItem
376:      *
377:      * @param string $attributeName
378:      * @param array $attributeValue
379:      *         The value(s) of the attribute
380:      */
381:     public function setAttribute($attributeName, $attributeValue) {
382:         $this->_attributes[$attributeName] = $attributeValue;
383:     }
384: 
385:     /**
386:      * sets a bunch of attributes
387:      *
388:      * @param array $aAttributeArray
389:      */
390:     public function setAttributes($aAttributeArray) {
391:         $this->_attributes = array_merge($aAttributeArray, $this->_attributes);
392:     }
393: 
394:     /**
395:      * returns an attribute
396:      *
397:      * @param string attributeName
398:      * @return mixed
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:      * deletes an attribute
410:      *
411:      * @param string attributeName
412:      * @return bool
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:      * @param string $attributeName
426:      * @param bool $bRecursive [optional]
427:      * @return bool
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:      * @param mixed $id
452:      *         expand ID of item to expand or array of item ID's to expand
453:      * @return bool
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:      * @param mixed $id
478:      *         collapse ID to collapse or an array with items to collapse
479:      * @return void|bool
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:      * @param int $leveloffset
505:      *         leveloffset Level offset. Ignores all expand operations below the offset.
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:      * @param int leveloffset
522:      *         Level offset. Ignores all expand operations below the offset.
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:      * @param string $id
539:      * @param bool $found [optional]
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:      * @param string $id
559:      * @param bool $found [optional]
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:      * Returns all items (as ID array) which are collapsed.
578:      *
579:      * @param array $list
580:      *         Contains the list with all collapsed items
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:      * getExpandedList
598:      * Returns all items (as ID array) which are expanded.
599:      *
600:      * @param array $list
601:      *         Contains the list with all expanded items
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:      * sets a payload object for later reference
619:      *
620:      * @param object $payload
621:      *         The object to payload
622:      */
623:     public function setPayloadObject($payload) {
624:         $this->payload = $payload;
625:     }
626: 
627:     /**
628:      * unsets a payload object
629:      *
630:      * @return object
631:      */
632:     public function unsetPayloadObject() {
633:         // @todo implement!
634:     }
635: 
636:     /**
637:      * traverse
638:      * traverses the tree starting from this item, and returning
639:      * all objects as $objects in a nested array.
640:      *
641:      * @param object $objects
642:      *         all found objects
643:      * @param int $level [optional]
644:      *         Level to start on
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:      * Traverses the tree starting from this item, and returning
659:      * all objects as $objects in a flat array.
660:      *
661:      * @param int $level [optional]
662:      *         Level to start on
663:      * @return array
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:      * Sets the name for this item.
680:      *
681:      * @param string $name
682:      *         New name for this item
683:      * @return none
684:      */
685:     public function setName($name) {
686:         $this->_name = $name;
687:     }
688: 
689: }
690: 
CMS CONTENIDO 4.9.8 API documentation generated by ApiGen 2.8.0