Cleanup tree code
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 25 Aug 2010 07:17:04 +0000 (01:17 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 25 Aug 2010 16:30:58 +0000 (10:30 -0600)
framework/Tree/lib/Horde/Tree/Html.php
framework/Tree/lib/Horde/Tree/Select.php

index 78243a8..24ef09c 100644 (file)
@@ -41,7 +41,7 @@ class Horde_Tree_Html extends Horde_Tree
      *
      * @var integer
      */
-    protected $_alt_count = 0;
+    protected $_altCount = 0;
 
     /**
      * Allowed parameters for nodes.
@@ -188,8 +188,8 @@ class Horde_Tree_Html extends Horde_Tree
         /* If using alternating row shading, work out correct
          * shade. */
         if ($this->getOption('alternate')) {
-            $className .= ' item' . $this->_alt_count;
-            $this->_alt_count = 1 - $this->_alt_count;
+            $className .= ' item' . $this->_altCount;
+            $this->_altCount = 1 - $this->_altCount;
         }
 
         $html = '<div class="' . $className . '">';
@@ -217,16 +217,16 @@ class Horde_Tree_Html extends Horde_Tree
      */
     protected function _buildTree($node_id)
     {
+        $node = $this->_nodes[$node_id];
         $output = $this->_buildLine($node_id);
 
-        if (isset($this->_nodes[$node_id]['children']) &&
-            $this->_nodes[$node_id]['expanded']) {
-            $num_subnodes = count($this->_nodes[$node_id]['children']);
-            for ($c = 0; $c < $num_subnodes; $c++) {
-                $child_node_id = $this->_nodes[$node_id]['children'][$c];
-                $this->_node_pos[$child_node_id] = array();
-                $this->_node_pos[$child_node_id]['pos'] = $c + 1;
-                $this->_node_pos[$child_node_id]['count'] = $num_subnodes;
+        if (isset($node['children']) && $node['expanded']) {
+            foreach ($node['children'] as $key => $val) {
+                $child_node_id = $node['children'][$key];
+                $this->_node_pos[$child_node_id] = array(
+                    'count' => count($node['children']),
+                    'pos' => $key + 1
+                );
                 $output .= $this->_buildTree($child_node_id);
             }
         }
@@ -243,15 +243,18 @@ class Horde_Tree_Html extends Horde_Tree
      */
     protected function _buildLine($node_id)
     {
+        $node = $this->_nodes[$node_id];
+
         $className = 'treeRow';
-        if (!empty($this->_nodes[$node_id]['class'])) {
-            $className .= ' ' . $this->_nodes[$node_id]['class'];
+        if (!empty($node['class'])) {
+            $className .= ' ' . $node['class'];
         }
+
         /* If using alternating row shading, work out correct
          * shade. */
         if ($this->getOption('alternate')) {
-            $className .= ' item' . $this->_alt_count;
-            $this->_alt_count = 1 - $this->_alt_count;
+            $className .= ' item' . $this->_altCount;
+            $this->_altCount = 1 - $this->_altCount;
         }
 
         $line = '<div class="' . $className . '">';
@@ -260,12 +263,12 @@ class Horde_Tree_Html extends Horde_Tree
          * for any given cell of content. */
         $column = 0;
 
-        if (isset($this->_nodes[$node_id]['extra'][self::EXTRA_LEFT])) {
-            $extra = $this->_nodes[$node_id]['extra'][self::EXTRA_LEFT];
+        if (isset($node['extra'][self::EXTRA_LEFT])) {
+            $extra = $node['extra'][self::EXTRA_LEFT];
             $cMax = count($extra);
             while ($column < $cMax) {
                 $line .= $this->_addColumn($column) . $extra[$column] . '</span>';
-                $column++;
+                ++$column;
             }
         }
 
@@ -275,7 +278,7 @@ class Horde_Tree_Html extends Horde_Tree
             $line .= '<table cellspacing="0"><tr><td>';
         }
 
-        for ($i = intval($this->_static); $i < $this->_nodes[$node_id]['indent']; ++$i) {
+        for ($i = intval($this->_static); $i < $node['indent']; ++$i) {
             $line .= $this->_generateImage(($this->_dropline[$i] && $this->getOption('lines')) ? $this->_images['line'] : $this->_images['blank']);
         }
         $line .= $this->_setNodeToggle($node_id) . $this->_setNodeIcon($node_id);
@@ -290,12 +293,11 @@ class Horde_Tree_Html extends Horde_Tree
 
         $line .= '</span>';
 
-        if (isset($this->_nodes[$node_id]['extra'][self::EXTRA_RIGHT])) {
-            $extra = $this->_nodes[$node_id]['extra'][self::EXTRA_RIGHT];
+        if (isset($node['extra'][self::EXTRA_RIGHT])) {
+            $extra = $node['extra'][self::EXTRA_RIGHT];
             $cMax = count($extra);
             for ($c = 0, $cMax = count($extra); $c < $cMax; ++$c) {
-                $line .= $this->_addColumn($column) . $extra[$c] . '</span>';
-                $column++;
+                $line .= $this->_addColumn($column++) . $extra[$c] . '</span>';
             }
         }
 
@@ -356,9 +358,10 @@ class Horde_Tree_Html extends Horde_Tree
     protected function _setNodeToggle($node_id)
     {
         $link_start = '';
+        $node = $this->_nodes[$node_id];
 
         /* Top level node. */
-        if ($this->_nodes[$node_id]['indent'] == 0) {
+        if ($node['indent'] == 0) {
             $this->_dropline[0] = false;
 
             if ($this->_static) {
@@ -390,10 +393,10 @@ class Horde_Tree_Html extends Horde_Tree
                 }
             }
 
-            if (isset($this->_nodes[$node_id]['children'])) {
+            if (isset($node['children'])) {
                 if (!$this->getOption('lines')) {
                     $img = $this->_images['blank'];
-                } elseif ($this->_nodes[$node_id]['expanded']) {
+                } elseif ($node['expanded']) {
                     $img = $node_type
                         ? (($node_type == 2) ? $this->_images['minus'] : $this->_images['minus_bottom'])
                         : $this->_images['minus_only'];
@@ -427,7 +430,7 @@ class Horde_Tree_Html extends Horde_Tree
                     $img = $this->_images['blank'];
                 }
             }
-        } elseif (isset($this->_nodes[$node_id]['children'])) {
+        } elseif (isset($node['children'])) {
             /* Node with children. */
             if ($this->_node_pos[$node_id]['pos'] < $this->_node_pos[$node_id]['count']) {
                 /* Not last node. */
@@ -435,24 +438,24 @@ class Horde_Tree_Html extends Horde_Tree
                     $img = $this->_images['blank'];
                 } elseif ($this->_static) {
                     $img = $this->_images['join'];
-                } elseif ($this->_nodes[$node_id]['expanded']) {
+                } elseif ($node['expanded']) {
                     $img = $this->_images['minus'];
                 } else {
                     $img = $this->_images['plus'];
                 }
-                $this->_dropline[$this->_nodes[$node_id]['indent']] = true;
+                $this->_dropline[$node['indent']] = true;
             } else {
                 /* Last node. */
                 if (!$this->getOption('lines')) {
                     $img = $this->_images['blank'];
                 } elseif ($this->_static) {
                     $img = $this->_images['join_bottom'];
-                } elseif ($this->_nodes[$node_id]['expanded']) {
+                } elseif ($node['expanded']) {
                     $img = $this->_images['minus_bottom'];
                 } else {
                     $img = $this->_images['plus_bottom'];
                 }
-                $this->_dropline[$this->_nodes[$node_id]['indent']] = false;
+                $this->_dropline[$node['indent']] = false;
             }
 
             if (!$this->_static) {
@@ -466,14 +469,14 @@ class Horde_Tree_Html extends Horde_Tree
                     ? $this->_images['join']
                     : $this->_images['blank'];
 
-                $this->_dropline[$this->_nodes[$node_id]['indent']] = true;
+                $this->_dropline[$node['indent']] = true;
             } else {
                 /* Last node. */
                 $img = $this->getOption('lines')
                     ? $this->_images['join_bottom']
                     : $this->_images['blank'];
 
-                $this->_dropline[$this->_nodes[$node_id]['indent']] = false;
+                $this->_dropline[$node['indent']] = false;
             }
         }
 
@@ -528,32 +531,28 @@ class Horde_Tree_Html extends Horde_Tree
      */
     protected function _setNodeIcon($node_id)
     {
-        if (isset($this->_nodes[$node_id]['icon'])) {
-            if (empty($this->_nodes[$node_id]['icon'])) {
+        $node = $this->_nodes[$node_id];
+
+        if (isset($node['icon'])) {
+            if (empty($node['icon'])) {
                 return '';
             }
 
             /* Node has a user defined icon. */
-            if (isset($this->_nodes[$node_id]['iconopen']) &&
-                $this->_nodes[$node_id]['expanded']) {
-                $img = $this->_nodes[$node_id]['iconopen'];
-            } else {
-                $img = $this->_nodes[$node_id]['icon'];
-            }
+            $img = (isset($node['iconopen']) && $node['expanded'])
+                ? $node['iconopen']
+                : $node['icon'];
+        } elseif (isset($node['children'])) {
+            /* Standard icon set: node with children. */
+            $img = $node['expanded']
+                ? $this->_images['folderopen']
+                : $this->_images['folder'];
         } else {
-            /* Use standard icon set. */
-            if (isset($this->_nodes[$node_id]['children'])) {
-                /* Node with children. */
-                $img = ($this->_nodes[$node_id]['expanded'])
-                    ? $this->_images['folderopen']
-                    : $this->_images['folder'];
-            } else {
-                /* Leaf node (no children). */
-                $img = $this->_images['leaf'];
-            }
+            /* Standard icon set: leaf node (no children). */
+            $img = $this->_images['leaf'];
         }
 
-        return $this->_generateImage($img, 'treeIcon', isset($this->_nodes[$node_id]['iconalt']) ? htmlspecialchars($this->_nodes[$node_id]['iconalt']) : null);
+        return $this->_generateImage($img, 'treeIcon', isset($node['iconalt']) ? htmlspecialchars($node['iconalt']) : null);
     }
 
 }
index 086ffa2..cb946f7 100644 (file)
@@ -71,20 +71,17 @@ class Horde_Tree_Select extends Horde_Tree
      */
     protected function _buildTree($node_id)
     {
-        $selected = $this->_nodes[$node_id]['selected']
-            ? ' selected="selected"'
-            : '';
+        $node = $this->_nodes[$node_id];
 
-        $output = '<option value="' . htmlspecialchars($node_id) . '"' . $selected . '>' .
-            str_repeat('&nbsp;&nbsp;', intval($this->_nodes[$node_id]['indent'])) . htmlspecialchars($this->_nodes[$node_id]['label']) .
+        $output = '<option value="' . htmlspecialchars($node_id) . '"' .
+            (empty($node['selected']) ? '' : ' selected="selected"') .
+            '>' .
+            str_repeat('&nbsp;&nbsp;', intval($node['indent'])) . htmlspecialchars($node['label']) .
             '</option>';
 
-        if (isset($this->_nodes[$node_id]['children']) &&
-            $this->_nodes[$node_id]['expanded']) {
-            $num_subnodes = count($this->_nodes[$node_id]['children']);
-            for ($c = 0; $c < $num_subnodes; ++$c) {
-                $child_node_id = $this->_nodes[$node_id]['children'][$c];
-                $output .= $this->_buildTree($child_node_id);
+        if (isset($node['children']) && $node['expanded']) {
+            foreach ($node['children'] as $val) {
+                $output .= $this->_buildTree($val);
             }
         }