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:  * @author     Bjoern Behrens
  9:  * @copyright  four for business AG <www.4fb.de>
 10:  * @license    http://www.contenido.org/license/LIZENZ.txt
 11:  * @link       http://www.4fb.de
 12:  * @link       http://www.contenido.org
 13:  */
 14: 
 15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 16: 
 17: /**
 18:  * Tree item class.
 19:  *
 20:  * @package    Core
 21:  * @subpackage GUI
 22:  */
 23: class cTreeItem {
 24: 
 25:     /**
 26:      * Sub Items of this tree item
 27:      *
 28:      * @var array
 29:      */
 30:     protected $_subitems = array();
 31: 
 32:     /**
 33:      * Determinates if this tree item is collapsed
 34:      *
 35:      * @var bool
 36:      */
 37:     protected $_collapsed;
 38: 
 39:     /**
 40:      * ID of this tree item
 41:      *
 42:      * @var string
 43:      */
 44:     protected $_id;
 45: 
 46:     /**
 47:      * Name of this tree item
 48:      *
 49:      * @var string
 50:      */
 51:     protected $_name;
 52: 
 53:     /**
 54:      * level of this tree item
 55:      *
 56:      * @var int
 57:      */
 58:     protected $_level;
 59: 
 60:     /**
 61:      * Contains custom entries
 62:      *
 63:      * @var array
 64:      */
 65:     protected $_attributes = array();
 66: 
 67:     /**
 68:      * parent of this tree item
 69:      *
 70:      * @var array
 71:      */
 72:     protected $_parent = false;
 73: 
 74:     /**
 75:      * next sibling of this tree item
 76:      *
 77:      * @var array
 78:      */
 79:     protected $_next = false;
 80: 
 81:     /**
 82:      * previous sibling of this tree item
 83:      *
 84:      * @var array
 85:      */
 86:     protected $_previous = false;
 87: 
 88:     /**
 89:      * Constructor to create an instance of this class.
 90:      *
 91:      * @param string $id [optional]
 92:      * @param string $name [optional]
 93:      * @param bool $collapsed [optional]
 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:      * Id getter.
103:      *
104:      * @return string
105:      */
106:     public function getId() {
107:         return $this->_id;
108:     }
109: 
110:     /**
111:      * Name getter.
112:      *
113:      * @return string
114:      */
115:     public function getName() {
116:         return $this->_name;
117:     }
118: 
119:     /**
120:      * Collapsed state getter.
121:      *
122:      * @return bool
123:      */
124:     public function getCollapsed() {
125:         return $this->_collapsed;
126:     }
127: 
128:     /**
129:      * Imports a table from an array of arrays.
130:      *
131:      * The entries "collapsed" and "attributes" are optional!
132:      *
133:      * @param array $flat_array
134:      *         array(
135:      *             array(
136:      *                 "id" => "Item ID",
137:      *                 "name" => "Item name",
138:      *                 "level" => 1,
139:      *                 "collapsed" => true|false,
140:      *                 "attributes" => array(
141:      *                     "attr_name" => "attr_value"
142:      *                 )
143:      *             )
144:      *         )
145:      * @return bool
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:             /* Set payload object */
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:      * @param array $array
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:      * @param array $sourcearray
207:      * @param array $destarray
208:      * @param int $lastid
209:      * @param int $level
210:      * @return bool
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:             // Name should be fetched via the meta object
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:      * Adds an item as a subitem to the current item.
249:      *
250:      * @param cTreeItem $item
251:      *         item object to add
252:      */
253:     public function addItem(&$item) {
254:         // Update last item
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:      * Adds an item to a specific ID.
266:      *
267:      * @param string $id
268:      *         ID to add the item to
269:      * @param cTreeItem $item
270:      *         Item to add
271:      * @return bool
272:      */
273:     public function addItemToID($id, &$item) {
274:         if ($this->_id == $id) {
275:             // Update last item
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:      * Moves an item to another object.
298:      *
299:      * @param cTreeItem $targetItem
300:      *         Item to move the subitem to
301:      * @param mixed $itemToMove
302:      *         cTreeItem-Object or id of object to move
303:      */
304:     public function moveItem($targetItem, $itemToMove) {
305:     }
306: 
307:     /**
308:      * Deletes a subitem.
309:      *
310:      * @param mixed $id
311:      *         item object or ID to delete
312:      * @return object
313:      *         deleted object
314:      */
315:     public function deleteItem($id) {
316:         foreach (array_keys($this->_subitems) as $key) {
317:             if ($this->_subitems[$key]->_id == $id) {
318:                 // Fetch next item, reset to current item
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:      * Retrieves a specific item by its ID.
351:      *
352:      * Note that this function traverses all subitems to find the
353:      * 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:      *
452:      * @param mixed $id
453:      *         expand ID of item to expand or array of item ID's to expand
454:      * @return bool
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:      * @param mixed $id
480:      *         collapse ID to collapse or an array with items to collapse
481:      * @return void|bool
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:      * @param int $leveloffset
507:      *         leveloffset Level offset. Ignores all expand operations below the offset.
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:      * @param int $leveloffset
524:      *         Level offset. Ignores all expand operations below the offset.
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:      * @param string $id
541:      * @param bool $found [optional]
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:      * @param string $id
561:      * @param bool $found [optional]
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:      * Returns all items (as ID array) which are collapsed.
580:      *
581:      * @param array $list
582:      *         Contains the list with all collapsed items
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:      * Returns all items (as ID array) which are expanded.
600:      *
601:      * @param array $list
602:      *         Contains the list with all expanded items
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:      * Sets a payload object for later reference.
620:      *
621:      * @param object $payload
622:      *         The object to payload
623:      */
624:     public function setPayloadObject($payload) {
625:         $this->payload = $payload;
626:     }
627: 
628:     /**
629:      * Unsets a payload object.
630:      *
631:      * @return object
632:      */
633:     public function unsetPayloadObject() {
634:     }
635: 
636:     /**
637:      * Traverses the tree starting from this item, and returning all
638:      * objects as $objects in a nested array.
639:      *
640:      * @param array $objects
641:      *         all found objects
642:      * @param int $level [optional]
643:      *         Level to start on
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:      * Traverses the tree starting from this item, and returning
658:      * all objects as $objects in a flat array.
659:      *
660:      * @param int $level [optional]
661:      *         Level to start on
662:      * @return array
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:      * Sets the name for this item.
679:      *
680:      * @param string $name
681:      *         New name for this item
682:      */
683:     public function setName($name) {
684:         $this->_name = $name;
685:     }
686: 
687: }
688: 
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0