Overview

Packages

  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Datatype
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
  • 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

  • ArticleForumRightBottom
  • cApiClickableAction
  • cApiClickableQuestionAction
  • cGuiBackendHelpbox
  • cGuiFoldingRow
  • cGuiList
  • cGuiMenu
  • cGuiNavigation
  • cGuiNotification
  • cGuiObjectPager
  • cGuiPage
  • cGuiScrollList
  • cGuiTableForm
  • cGuiTree
  • cPager
  • cTemplate
  • cTree
  • cTreeItem
  • NoteLink
  • NoteList
  • NoteListItem
  • NoteView
  • TODOBackendList
  • TODOLink
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the tree item class.
  4:  *
  5:  * @package    Core
  6:  * @subpackage GUI
  7:  * @version    SVN Revision $Rev:$
  8:  *
  9:  * @author     Bjoern Behrens
 10:  * @copyright  four for business AG <www.4fb.de>
 11:  * @license    http://www.contenido.org/license/LIZENZ.txt
 12:  * @link       http://www.4fb.de
 13:  * @link       http://www.contenido.org
 14:  */
 15: 
 16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 17: 
 18: /**
 19:  * Tree item class
 20:  *
 21:  * @package    Core
 22:  * @subpackage GUI
 23:  */
 24: class cTreeItem {
 25: 
 26:     /**
 27:      * Sub Items for this tree item
 28:      *
 29:      * @var array
 30:      */
 31:     var $_subitems = array();
 32: 
 33:     /**
 34:      * Determinates if this tree item is collapsed
 35:      *
 36:      * @var boolean
 37:      */
 38:     var $_collapsed;
 39: 
 40:     /**
 41:      * ID for this item
 42:      *
 43:      * @var string
 44:      */
 45:     var $_id;
 46: 
 47:     /**
 48:      * Name for this item
 49:      *
 50:      * @var string
 51:      */
 52:     var $_name;
 53: 
 54:     /**
 55:      * Contains the level of this item
 56:      *
 57:      * @var integer
 58:      */
 59:     var $_level;
 60: 
 61:     /**
 62:      * Contains custom entries
 63:      *
 64:      * @var array
 65:      */
 66:     var $_attributes = array();
 67: 
 68:     /**
 69:      * Contains the parent of this item
 70:      *
 71:      * @var array
 72:      */
 73:     var $_parent = false;
 74: 
 75:     /**
 76:      * Contains the next item
 77:      *
 78:      * @var array
 79:      */
 80:     var $_next = false;
 81: 
 82:     /**
 83:      * Contains the previous item
 84:      *
 85:      * @var array
 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:      * Id getter
 97:      *
 98:      * @return string
 99:      */
100:     public function getId() {
101:         return $this->_id;
102:     }
103: 
104:     /**
105:      * Name getter
106:      *
107:      * @return string
108:      */
109:     public function getName() {
110:         return $this->_name;
111:     }
112: 
113:     /**
114:      * Collapsed state getter
115:      *
116:      * @return bool
117:      */
118:     public function getCollapsed() {
119:         return $this->_collapsed;
120:     }
121: 
122:     /**
123:      * Imports a table from an array of arrays.
124:      * Array format:
125:      * array(
126:      * array("id" => "Item ID", "name" => "Item name", "level" => 1, "collapsed"
127:      * => true|false, "attributes" => array("attr_name" => "attr_value"))
128:      * );
129:      *
130:      * The entries "collapsed" and "attributes" are optional!
131:      *
132:      * @param array flat_array See above
133:      * @return void
134:      * @access public
135:      */
136:     public function importTable($flat_array) {
137:         $lastobj[0] = $this->_id;
138:         $currentlevel = 1;
139: 
140:         if (!is_array($flat_array)) {
141:             return false;
142:         }
143: 
144:         foreach ($flat_array as $item) {
145:             $mitem[$item["id"]] = new cTreeItem($item["id"], $item["name"]);
146: 
147:             if ($item["level"] > $currentlevel) {
148:                 $currentlevel++;
149:             }
150: 
151:             if ($item["level"] < $currentlevel) {
152:                 $currentlevel = $item["level"];
153:             }
154: 
155:             if (is_array($item["attributes"])) {
156:                 $mitem[$item["id"]]->setAttributes($item["attributes"]);
157:             }
158: 
159:             if (array_key_exists("collapsed", $item)) {
160:                 $mitem[$item["id"]]->setCollapsed($item["collapsed"]);
161:             }
162: 
163:             /* Set payload object */
164:             if (array_key_exists("payload", $item)) {
165:                 $mitem[$item["id"]]->setPayloadObject($item["payload"]);
166:             }
167: 
168:             if (is_object($mitem[$lastobj[$currentlevel - 1]])) {
169:                 $mitem[$lastobj[$currentlevel - 1]]->addItem($mitem[$item["id"]]);
170:             } else {
171:                 $this->addItemToID($lastobj[$currentlevel - 1], $mitem[$item["id"]]);
172:             }
173: 
174:             $lastobj[$currentlevel] = $item["id"];
175:         }
176:     }
177: 
178:     public function importStructuredArray($array) {
179:         $i = array();
180: 
181:         $lastid = 1;
182:         $level = 1;
183: 
184:         $this->_flattenArray($array, $i, $lastid, $level);
185: 
186:         $this->importTable($i);
187:     }
188: 
189:     protected function _flattenArray($sourcearray, &$destarray, &$lastid, &$level) {
190:         if ($lastid == false) {
191:             $lastid = 1;
192:         }
193: 
194:         if ($level == false) {
195:             $level = 1;
196:         }
197: 
198:         if (!is_array($sourcearray)) {
199:             return false;
200:         }
201: 
202:         foreach ($sourcearray as $id => $item) {
203:             $lastid++;
204:             $destarray[$lastid]["id"] = $item["class"] . "." . $id;
205: 
206:             // Name should be fetched via the meta object
207:             $meta = $item["object"]->getMetaObject();
208: 
209:             if (is_object($meta)) {
210:                 $destarray[$lastid]["name"] = $meta->getName();
211:             }
212: 
213:             $destarray[$lastid]["level"] = $level;
214:             $destarray[$lastid]["payload"] = $item["object"];
215: 
216:             if (count($item["items"]) > 0) {
217:                 $level++;
218:                 $this->_flattenArray($item["items"], $destarray, $lastid, $level);
219:                 $level--;
220:             }
221:         }
222:     }
223: 
224:     /**
225:      * adds an item as a subitem to the current item
226:      *
227:      * @param cTreeItem item item object to add
228:      * @return void
229:      * @access public
230:      */
231:     public function addItem(&$item) {
232:         // Update last item
233:         if (($lastitem = end($this->_subitems)) !== false) {
234:             $this->_subitems[key($this->_subitems)]->_next = $item->_id;
235:         }
236: 
237:         $this->_subitems[count($this->_subitems)] = &$item;
238:         $item->_parent = $this->_id;
239:         $item->_previous = $lastitem->_id;
240:     }
241: 
242:     /**
243:      * adds an item to a specific ID
244:      *
245:      * @param string id ID to add the item to
246:      * @param cTreeItem item Item to add
247:      * @return void
248:      * @access public
249:      */
250:     public function addItemToID($id, &$item) {
251:         if ($this->_id == $id) {
252:             // Update last item
253:             if ($lastitem = end($this->_subitems) !== false) {
254:                 $this->_subitems[key($this->_subitems)]->_next = $item->_id;
255:             }
256: 
257:             $this->_subitems[count($this->_subitems)] = &$item;
258:             $item->_parent = $this->_id;
259:             $item->_previous = $lastitem->_id;
260:             return true;
261:         } else {
262:             foreach (array_keys($this->_subitems) as $key) {
263:                 $result = $this->_subitems[$key]->addItemToID($id, $item);
264:                 if ($result == true) {
265:                     return true;
266:                 }
267:             }
268:         }
269: 
270:         return false;
271:     }
272: 
273:     /**
274:      * moves an item to another object
275:      *
276:      * @param cTreeItem targetItem Item to move the subitem to
277:      * @param mixed itemToMove cTreeItem-Object or id of object to move
278:      * @return void
279:      * @access public
280:      */
281:     public function moveItem($targetItem, $itemToMove) {
282:     }
283: 
284:     /**
285:      * deletes a subitem
286:      *
287:      * @param mixed item object or ID to delete
288:      * @return deleted object
289:      * @access public
290:      */
291:     public function deleteItem($id) {
292:         foreach (array_keys($this->_subitems) as $key) {
293:             if ($this->_subitems[$key]->_id == $id) {
294:                 // Fetch next item, reset to current item
295:                 $nextitem = next($this->_subitems);
296:                 $nkey = key($this->_subitems);
297:                 prev($this->_subitems);
298: 
299:                 $previtem = &prev($this->_subitems);
300:                 $pkey = key($this->_subitems);
301:                 next($this->_subitems);
302: 
303:                 if ($nextitem !== false) {
304:                     if ($previtem !== false) {
305:                         $this->_subitems[$nkey]->_previous = $this->_subitems[$pkey]->_id;
306:                     }
307:                 }
308: 
309:                 if ($previtem !== false) {
310:                     if ($nextitem !== false) {
311:                         $this->_subitems[$pkey]->_next = $this->_subitems[$nkey]->_id;
312:                     }
313:                 }
314: 
315:                 $itemcopy = $this->_subitems[$key];
316:                 unset($this->_subitems[$key]);
317: 
318:                 return ($itemcopy);
319:             } else {
320:                 $this->_subitems[$key]->deleteItem($id);
321:             }
322:         }
323:     }
324: 
325:     /**
326:      * Retrieves a specific item by its ID.
327:      * Note that this
328:      * function traverses all subitems to find the correct item.
329:      *
330:      * @param string id ID to retrieve
331:      * @return cTreeItem
332:      * @access public
333:      */
334:     public function &getItemByID($id) {
335:         if ($this->_id == $id) {
336:             return $this;
337:         } else {
338:             foreach (array_keys($this->_subitems) as $key) {
339:                 $retObj = &$this->_subitems[$key]->getItemByID($id);
340:                 if ($retObj->_id == $id) {
341:                     return $retObj;
342:                 }
343:             }
344:         }
345: 
346:         return false;
347:     }
348: 
349:     /**
350:      * sets a custom attribute for this TreeItem
351:      *
352:      * @param string attributeName
353:      * @param array attributeValue The value(s) of the attribute
354:      * @return void
355:      * @access public
356:      */
357:     public function setAttribute($attributeName, $attributeValue) {
358:         $this->_attributes[$attributeName] = $attributeValue;
359:     }
360: 
361:     /**
362:      * sets a bunch of attributes
363:      *
364:      * @param string attributeName
365:      * @param array attributeValue The value(s) of the attribute
366:      * @return void
367:      * @access public
368:      */
369:     public function setAttributes($aAttributeArray) {
370:         $this->_attributes = array_merge($aAttributeArray, $this->_attributes);
371:     }
372: 
373:     /**
374:      * returns an attribute
375:      *
376:      * @param string attributeName
377:      * @return mixed
378:      * @access public
379:      */
380:     public function getAttribute($attributeName) {
381:         if (array_key_exists($attributeName, $this->_attributes)) {
382:             return ($this->_attributes[$attributeName]);
383:         } else {
384:             return false;
385:         }
386:     }
387: 
388:     /**
389:      * deletes an attribute
390:      *
391:      * @param string attributeName
392:      * @return void
393:      * @access public
394:      */
395:     public function deleteAttribute($attributeName) {
396:         if (array_key_exists($attributeName, $this->_attributes)) {
397:             unset($this->_attributes[$attributeName]);
398:             return true;
399:         } else {
400:             return false;
401:         }
402:     }
403: 
404:     public function hasAttribute($attributeName, $bRecursive = false) {
405:         if (array_key_exists($attributeName, $this->_attributes)) {
406:             return true;
407:         } else {
408:             if ($bRecursive == true) {
409:                 if (count($this->_subitems) > 0) {
410:                     foreach ($this->_subitems as $oSubitem) {
411:                         $bFound = $oSubitem->hasAttribute($attributeName, true);
412:                         if ($bFound == true) {
413:                             return true;
414:                         }
415:                     }
416:                 }
417: 
418:                 return false;
419:             } else {
420:                 return false;
421:             }
422:         }
423:     }
424: 
425:     /**
426:      *
427:      * @param mixed expand ID of item to expand or array of item ID's to expand
428:      * @return void
429:      * @access public
430:      */
431:     public function setExpanded($id) {
432:         if (is_array($id)) {
433:             if (in_array($this->_id, $id, true)) {
434:                 $this->_collapsed = false;
435:             }
436: 
437:             foreach (array_keys($this->_subitems) as $key) {
438:                 $this->_subitems[$key]->setExpanded($id);
439:             }
440:         } else {
441:             if ($this->_id === $id) {
442:                 $this->_collapsed = false;
443:                 return true;
444:             } else {
445:                 foreach (array_keys($this->_subitems) as $key) {
446:                     $this->_subitems[$key]->setExpanded($id);
447:                 }
448:             }
449:         }
450:     }
451: 
452:     /**
453:      *
454:      * @param mixed collapse ID to collapse or an array with items to collapse
455:      * @return void
456:      * @access public
457:      */
458:     public function setCollapsed($id) {
459:         if (is_array($id)) {
460:             if (in_array($this->_id, $id, true)) {
461:                 $this->_collapsed = true;
462:             }
463: 
464:             foreach (array_keys($this->_subitems) as $key) {
465:                 $this->_subitems[$key]->setCollapsed($id);
466:             }
467:         } else {
468:             if ($this->_id === $id) {
469:                 $this->_collapsed = true;
470:                 return true;
471:             } else {
472:                 foreach (array_keys($this->_subitems) as $key) {
473:                     $this->_subitems[$key]->setCollapsed($id);
474:                 }
475:             }
476:         }
477:     }
478: 
479:     /**
480:      *
481:      * @param int leveloffset Level offset. Ignores all expand operations below
482:      *        the offset.
483:      * @return void
484:      * @access public
485:      */
486:     protected function _expandBelowLevel($leveloffset) {
487:         if ($leveloffset > 0) {
488:             $leveloffset--;
489:         } else {
490:             $this->_collapsed = false;
491:         }
492: 
493:         foreach (array_keys($this->_subitems) as $key) {
494:             $this->_subitems[$key]->expandBelowLevel($leveloffset);
495:         }
496:     }
497: 
498:     /**
499:      *
500:      * @param int leveloffset Level offset. Ignores all expand operations below
501:      *        the offset.
502:      * @return void
503:      * @access public
504:      */
505:     protected function _collapseBelowLevel($leveloffset) {
506:         if ($leveloffset > 0) {
507:             $leveloffset--;
508:         } else {
509:             $this->_collapsed = true;
510:         }
511: 
512:         foreach (array_keys($this->_subitems) as $key) {
513:             $this->_subitems[$key]->collapseBelowLevel($leveloffset);
514:         }
515:     }
516: 
517:     /**
518:      *
519:      * @param int leveloffset Level offset. Ignores all expand operations below
520:      *        the offset.
521:      * @return void
522:      * @access public
523:      */
524:     protected function _expandBelowID($id, $found = false) {
525:         if ($found === true) {
526:             $this->_collapsed = false;
527:         }
528: 
529:         if ($this->_id == $id) {
530:             $found = true;
531:             $this->_collapsed = false;
532:         }
533: 
534:         foreach (array_keys($this->_subitems) as $key) {
535:             $this->_subitems[$key]->expandBelowID($id, $found);
536:         }
537:     }
538: 
539:     /**
540:      *
541:      * @param int leveloffset Level offset. Ignores all expand operations below
542:      *        the offset.
543:      * @return void
544:      * @access public
545:      */
546:     protected function _collapseBelowID($id, $found = false) {
547:         if ($found === true) {
548:             $this->_collapsed = true;
549:         }
550: 
551:         if ($this->_id == $id) {
552:             $found = true;
553:             $this->_collapsed = true;
554:         }
555: 
556:         foreach (array_keys($this->_subitems) as $key) {
557:             $this->_subitems[$key]->collapseBelowID($id, $found);
558:         }
559:     }
560: 
561:     /**
562:      * getCollapsedList
563:      * Returns all items (as ID array) which are collapsed.
564:      *
565:      * @param array $list Contains the list with all collapsed items
566:      */
567:     public function getCollapsedList(&$list) {
568:         if (!is_array($list)) {
569:             $list = array();
570:         }
571: 
572:         if ($this->_collapsed == true) {
573:             $list[] = $this->_id;
574:         }
575: 
576:         foreach (array_keys($this->_subitems) as $key) {
577:             $this->_subitems[$key]->getCollapsedList($list);
578:         }
579:     }
580: 
581:     /**
582:      * getExpandedList
583:      * Returns all items (as ID array) which are expanded.
584:      *
585:      * @param array $list Contains the list with all expanded items
586:      */
587:     public function getExpandedList(&$list) {
588:         if (!is_array($list)) {
589:             $list = array();
590:         }
591: 
592:         if ($this->_collapsed == false && !in_array($this->_id, $list)) {
593:             $list[] = $this->_id;
594:         }
595: 
596:         foreach (array_keys($this->_subitems) as $key) {
597:             $this->_subitems[$key]->getExpandedList($list);
598:         }
599:     }
600: 
601:     /**
602:      * sets a payload object for later reference
603:      *
604:      * @param object payload The object to payload
605:      * @return void
606:      * @access public
607:      */
608:     public function setPayloadObject($payload) {
609:         $this->payload = $payload;
610:     }
611: 
612:     /**
613:      * unsets a payload object
614:      *
615:      * @return object
616:      * @access public
617:      */
618:     public function unsetPayloadObject() {
619:     }
620: 
621:     /**
622:      * traverse
623:      * traverses the tree starting from this item, and returning
624:      * all objects as $objects in a nested array.
625:      *
626:      * @param object $objects all found objects
627:      * @param integer $level Level to start on
628:      */
629:     public function traverse(&$objects, $level = 0) {
630:         $objects[count($objects)] = &$this;
631:         $this->_level = $level;
632: 
633:         if ($this->_collapsed == false) {
634:             foreach (array_keys($this->_subitems) as $key) {
635:                 $this->_subitems[$key]->traverse($objects, $level + 1);
636:             }
637:         }
638:     }
639: 
640:     /**
641:      * flatTraverse
642:      * traverses the tree starting from this item, and returning
643:      * all objects as $objects in a flat array.
644:      *
645:      * @param object $objects all found objects
646:      * @param integer $level Level to start on
647:      */
648:     public function flatTraverse($level = 0) {
649:         $objects[] = &$this;
650:         $this->_level = $level;
651: 
652:         if ($this->_collapsed == false) {
653:             foreach (array_keys($this->_subitems) as $key) {
654:                 $objects = array_merge($objects, $this->_subitems[$key]->flatTraverse($level + 1));
655:             }
656:         }
657: 
658:         return $objects;
659:     }
660: 
661:     /**
662:      * setName
663:      * sets the Name for this item.
664:      *
665:      * @param string $name New name for this item
666:      * @return none
667:      */
668:     public function setName($name) {
669:         $this->_name = $name;
670:     }
671: 
672: }
673: 
CMS CONTENIDO 4.9.2 API documentation generated by ApiGen 2.8.0