Explorar o código

Merge pull request #127 from danielcsgomes/treenestedarray

refactored the nested array output
Gediminas Morkevicius %!s(int64=14) %!d(string=hai) anos
pai
achega
d45a280bae
Modificáronse 1 ficheiros con 93 adicións e 10 borrados
  1. 93 10
      lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php

+ 93 - 10
lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php

@@ -844,14 +844,16 @@ class NestedTreeRepository extends AbstractTreeRepository
     }
     }
 
 
     /**
     /**
-     * Get hierarchy array of children followed by given $node
+     * Retrieves the nested array or the html output
      *
      *
+     * @throws \Gedmo\Exception\InvalidArgumentException
      * @param object $node - from which node to start reordering the tree
      * @param object $node - from which node to start reordering the tree
      * @param boolean $direct - true to take only direct children
      * @param boolean $direct - true to take only direct children
-     * @param string $direction - sort direction : "ASC" or "DESC"
-     * @return array $trees
+     * @param bool $html
+     * @param array|null $options
+     * @return array|string
      */
      */
-    public function childrenArrayHierarchy($node = null, $direct = false, $direction = "ASC")
+    public function childrenHierarchy($node = null, $direct = false, $html = false, array $options = null)
     {
     {
         $meta = $this->getClassMetadata();
         $meta = $this->getClassMetadata();
         $config = $this->listener->getConfiguration($this->_em, $meta->name);
         $config = $this->listener->getConfiguration($this->_em, $meta->name);
@@ -867,8 +869,64 @@ class NestedTreeRepository extends AbstractTreeRepository
 
 
         // Gets the array of $node results.
         // Gets the array of $node results.
         // It must be order by 'root' field
         // It must be order by 'root' field
-        $nodes = self::childrenQuery($node, $direct, 'root' , $direction)->getArrayResult();
+        $nodes = self::childrenQuery($node, $direct, 'root' , 'ASC')->getArrayResult();
+
+        return $this->buildTree($nodes, $html, $options);
+    }
+
+    /**
+     * Builds the tree
+     *
+     * @param array $nodes
+     * @param bool $html
+     * @param array|null $options
+     * @return array|string
+     */
+    private function buildTree(array $nodes, $html = false, array $options = null)
+    {
+        //process the nested tree into a nested array
+        $nestedTree = $this->processTree($nodes);
+        
+        // If you don't want any html output it will return the nested array
+        if (!$html) {
+            return $nestedTree;
+        }
+
+        //Defines html decorators and opcional options
+        if (!empty($options['root'])) {
+            $root_open  = $options['root']['open'];
+            $root_close = $options['root']['close'];
+        } else {
+            $root_open  = "<ul> ";
+            $root_close = " </ul>";
+        }
+        if (!empty($options['child'])) {
+            $child_open  = $options['child']['open'];
+            $child_close = $options['child']['close'];
+        } else {
+            $child_open  = "<li> ";
+            $child_close = " </li>";
+        }
+
+        $html_decorator = array(
+            'root'  => array('open' => $root_open,  'close' => $root_close),
+            'child' => array('open' => $child_open, 'close' => $child_close),
+        );
+
+        $html_output = $this->processHtmlTree($nestedTree, $html_decorator, $html_output = null);
+
+        return $html_output;
+    }
 
 
+    /**
+     * Creates the nested array
+     *
+     * @static
+     * @param array $nodes
+     * @return array
+     */
+    private static function processTree($nodes)
+    {
         // Trees mapped
         // Trees mapped
         $trees = array();
         $trees = array();
         $l = 0;
         $l = 0;
@@ -880,7 +938,7 @@ class NestedTreeRepository extends AbstractTreeRepository
             foreach ($nodes as $child) {
             foreach ($nodes as $child) {
                 $item = $child;
                 $item = $child;
 
 
-                $item['__children'] = array();
+                $item['children'] = array();
 
 
                 // Number of stack items
                 // Number of stack items
                 $l = count($stack);
                 $l = count($stack);
@@ -899,12 +957,37 @@ class NestedTreeRepository extends AbstractTreeRepository
                     $stack[] = & $trees[$i];
                     $stack[] = & $trees[$i];
                 } else {
                 } else {
                     // Add child to parent
                     // Add child to parent
-                    $i = count($stack[$l - 1]['__children']);
-                    $stack[$l - 1]['__children'][$i] = $item;
-                    $stack[] = & $stack[$l - 1]['__children'][$i];
+                    $i = count($stack[$l - 1]['children']);
+                    $stack[$l - 1]['children'][$i] = $item;
+                    $stack[] = & $stack[$l - 1]['children'][$i];
                 }
                 }
             }
             }
         }
         }
         return $trees;
         return $trees;
     }
     }
-}
+
+    /**
+     * Creates the html output of the nested tree
+     *
+     * @param $parent_node
+     * @param $html_decorator
+     * @param $html_output
+     * @return string
+     */
+    private function processHtmlTree($parent_node, $html_decorator, $html_output)
+    {
+        if (is_array($parent_node)) {
+            $html_output .= $html_decorator['root']['open'];
+            foreach ($parent_node as $item) {
+                 $html_output .= $html_decorator['child']['open'] . $item['title'];
+                if (count($item['children']) > 0) {
+                    $html_output = $this->processHtmlTree($item['children'], $html_decorator, $html_output);
+                }
+                $html_output .= $html_decorator['child']['close'];
+            }
+            $html_output .= $html_decorator['root']['close'];
+        }
+            
+        return $html_output;
+    }
+}