예제 #1
0
 /**
  * Import method for creating HTML_TreeMenu objects/structures
  * out of existing tree objects/structures. Currently supported
  * are Wolfram Kriesings' PEAR Tree class, and Richard Heyes' (me!)
  * Tree class (available here: http://www.phpguru.org/). This
  * method is intended to be used statically, eg:
  * $treeMenu = &HTML_TreeMenu::createFromStructure($myTreeStructureObj);
  *
  * @param  array  $params   An array of parameters that determine
  *                          how the import happens. This can consist of:
  *                            structure   => The tree structure
  *                            type        => The type of the structure, currently
  *                                           can be either 'heyes' or 'kriesing'
  *                            nodeOptions => Default options for each node
  *                            
  * @return object           The resulting HTML_TreeMenu object
  */
 function createFromStructure($params)
 {
     if (!isset($params['nodeOptions'])) {
         $params['nodeOptions'] = array();
     }
     switch (@$params['type']) {
         /**
          * Wolfram Kriesings' PEAR Tree class
          */
         case 'kriesing':
             $className = strtolower(get_class($params['structure']->dataSourceClass));
             $isXMLStruct = strpos($className, '_xml') !== false ? true : false;
             // Get the entire tree, the $nodes are sorted like in the tree view
             // from top to bottom, so we can easily put them in the nodes
             $nodes = $params['structure']->getNode();
             // Make a new menu and fill it with the values from the tree
             $treeMenu = new treemenu();
             $curNode[0] =& $treeMenu;
             // we need the current node as the reference to the
             foreach ($nodes as $aNode) {
                 $events = array();
                 $data = array();
                 // In an XML, all the attributes are saved in an array, but since they might be
                 // used as the parameters, we simply extract them here if we handle an XML-structure
                 if ($isXMLStruct && sizeof($aNode['attributes'])) {
                     foreach ($aNode['attributes'] as $key => $val) {
                         if (!$aNode[$key]) {
                             // dont overwrite existing values
                             $aNode[$key] = $val;
                         }
                     }
                 }
                 // Process all the data that are saved in $aNode and put them in the data and/or events array
                 foreach ($aNode as $key => $val) {
                     if (!is_array($val)) {
                         // Dont get the recursive data in here! they are always arrays
                         if (substr($key, 0, 2) == 'on') {
                             // get the events
                             $events[$key] = $val;
                         }
                         // I put it in data too, so in case an options starts with 'on' its also passed to the node ... not too cool i know
                         $data[$key] = $val;
                     }
                 }
                 // Normally the text is in 'name' in the Tree class, so we check both but 'text' is used if found
                 $data['text'] = $aNode['text'] ? $aNode['text'] : $aNode['name'];
                 // Add the item to the proper node
                 $thisNode =& $curNode[$aNode['level']]->addItem(new treenode($data, $events));
                 $curNode[$aNode['level'] + 1] =& $thisNode;
             }
             break;
             /**
              * Richard Heyes' (me!) second (array based) Tree class
              */
         /**
          * Richard Heyes' (me!) second (array based) Tree class
          */
         case 'heyes_array':
             // Need to create a HTML_TreeMenu object ?
             if (!isset($params['treeMenu'])) {
                 $treeMenu =& new treemenu();
                 $parentID = 0;
             } else {
                 $treeMenu =& $params['treeMenu'];
                 $parentID = $params['parentID'];
             }
             // Loop thru the trees nodes
             foreach ($params['structure']->getChildren($parentID) as $nodeID) {
                 $data = $params['structure']->getData($nodeID);
                 $parentNode =& $treeMenu->addItem(new treenode(array_merge($params['nodeOptions'], $data)));
                 // Recurse ?
                 if ($params['structure']->hasChildren($nodeID)) {
                     $recurseParams['type'] = 'heyes_array';
                     $recurseParams['parentID'] = $nodeID;
                     $recurseParams['nodeOptions'] = $params['nodeOptions'];
                     $recurseParams['structure'] =& $params['structure'];
                     $recurseParams['treeMenu'] =& $parentNode;
                     treemenu::createFromStructure($recurseParams);
                 }
             }
             break;
             /**
              * Richard Heyes' (me!) original OO based Tree class
              */
         /**
          * Richard Heyes' (me!) original OO based Tree class
          */
         case 'heyes':
         default:
             // Need to create a HTML_TreeMenu object ?
             if (!isset($params['treeMenu'])) {
                 $treeMenu =& new treemenu();
             } else {
                 $treeMenu =& $params['treeMenu'];
             }
             // Loop thru the trees nodes
             foreach ($params['structure']->nodes->nodes as $node) {
                 $tag = $node->getTag();
                 $parentNode =& $treeMenu->addItem(new treenode(array_merge($params['nodeOptions'], $tag)));
                 // Recurse ?
                 if (!empty($node->nodes->nodes)) {
                     $recurseParams['structure'] = $node;
                     $recurseParams['nodeOptions'] = $params['nodeOptions'];
                     $recurseParams['treeMenu'] =& $parentNode;
                     treemenu::createFromStructure($recurseParams);
                 }
             }
             break;
     }
     return $treeMenu;
 }