/** 
  * 
  * Custom getChildrenAsUL - specific for Pages/Tabsets/Tabs/Fields
  * TODO this is very slow - improve it!
  * TODO could load branches via AJAX instead
  */
 public static function getChildrenAsUL($fields, $level = 0, $ulExtraAttributes = null, $parentPage, &$itemCount = 0)
 {
     $output = "";
     $hasNextLevel = false;
     //Set to true to remove any node from being displayed. Its children still will be.
     $removeNode = false;
     //Remove Root, as its not really needed and confuses this tree
     if (is_a($fields, "FieldSet") && is_a($fields->First(), "TabSet")) {
         $firstField = $fields->First();
         $firstField = method_exists($firstField, "Name") ? $firstField->Name() : "";
         if ($firstField == "Root") {
             $removeNode = true;
         }
     }
     if (!$removeNode) {
         $output = "<ul {$ulExtraAttributes}>\n";
     }
     $ulExtraAttributes = null;
     foreach ($fields as $field) {
         $css = '';
         $display = '';
         $recurse = false;
         $name = '';
         $type = '';
         //Handle Page classes and children (getCMSFields)
         if (is_a($field, "Page")) {
             $css .= "tree-page ";
             $recurse = true;
             $name = $field->class;
             $display = $field->class;
             $parentPage = $field->class;
             $children = $field->getCMSFields(null);
         } else {
             //Handle TabSet classes and children (Tabs)
             if (is_a($field, "TabSet")) {
                 $css .= "tree-tabset ";
                 $recurse = true;
                 $display = method_exists($field, "Name") ? $field->Name() : $field->class;
                 $name = $display;
                 $children = $field->Tabs();
             } else {
                 //Handle Tab classes and children (Fields)
                 if (is_a($field, "Tab")) {
                     $css .= "tree-tab ";
                     $recurse = true;
                     $display = method_exists($field, "Name") ? $field->Name() : $field->class;
                     $name = $display;
                     $children = $field->Fields();
                 } else {
                     //Handle all FormField subclasses - excluding LiteralField
                     //If the class doesn't have a Title, display the class instead
                     //If the class has a Name, display that in brackets afterwards (maybe, comm for now)
                     if (is_subclass_of($field, "FormField") and !is_a($field, "LiteralField")) {
                         $title = method_exists($field, "Title") ? $field->Title() : $field->class;
                         $name = method_exists($field, "Name") ? $field->Name() : $field->class;
                         if (!$title) {
                             $title = $field->class;
                         }
                         $css .= "tree-field ";
                         $display = $title . "(" . $field->class . ")";
                     } else {
                         //Handle LiteralField classes - the content is HTML, so convert to raw first
                         if (is_a($field, "LiteralField")) {
                             $css .= "tree-literal ";
                             $name = method_exists($field, "Name") ? $field->Name() : $field->class;
                             $display = Convert::xml2raw($field->getContent());
                         } else {
                             //If the item isn't any of the above classes, we don't know what it is...
                             $css .= "tree-unknown ";
                             $name = method_exists($field, "Name") ? $field->Name() : $field->class;
                             $display = $field->class . " is an unknown type...";
                         }
                     }
                 }
             }
         }
         //Find out if this field has a SimplifyPermission entry for the given group
         if (SimplifyPermission::checkField($parentPage, $name, $field->class, self::$group)) {
             $css .= 'selected ';
         }
         //Build the page|field|type|group key
         $code = $parentPage . "|" . $name . "|" . $field->class . "|" . self::$group->ID;
         //Build the node
         if (!$removeNode) {
             $output .= "<li class='{$css}'><a href='#' rel='{$code}'>{$display}</a>\n";
         }
         //Do the recursive call
         if ($recurse) {
             $output .= self::getChildrenAsUL($children, $level + 1, $ulExtraAttributes, $parentPage);
         }
         if (!$removeNode) {
             $output .= "</li>\n";
         }
         $itemCount++;
     }
     if (!$removeNode) {
         $output .= "</ul>\n";
     }
     return $output;
 }