Re-add session code to Horde_Tree.
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 14 Jul 2010 19:00:43 +0000 (13:00 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 14 Jul 2010 19:16:56 +0000 (13:16 -0600)
Sessions can be used outside of Horde - we aren't using any Horde
specific session functionality.

framework/Core/lib/Horde/Core/Factory/Tree.php
framework/Core/lib/Horde/Core/Tree/Html.php
framework/Core/lib/Horde/Core/Tree/Javascript.php
framework/Tree/lib/Horde/Tree.php

index 06a358b..d01697e 100644 (file)
@@ -78,6 +78,8 @@ class Horde_Core_Factory_Tree
                 break;
             }
 
+            $params['session'] = 'horde_tree';
+
             $this->_instances[$id] = Horde_Tree::factory($name, $renderer, $params);
         }
 
index 003c4cd..b0ab4ae 100644 (file)
@@ -47,10 +47,6 @@ class Horde_Core_Tree_Html extends Horde_Tree_Html
     {
         parent::__construct($name, $params);
 
-        if (!isset($_SESSION['horde_tree'][$this->_instance])) {
-            $_SESSION['horde_tree'][$this->_instance] = array();
-        }
-
         if (!empty($GLOBALS['nls']['rtl'][$GLOBALS['language']])) {
             $no_rev = array('blank', 'folder', 'folder_open');
             foreach (array_diff(array_keys($this->_images), $no_rev) as $key) {
@@ -75,42 +71,4 @@ class Horde_Core_Tree_Html extends Horde_Tree_Html
         return Horde::link(Horde::selfUrl()->add(self::TOGGLE . $this->_instance, $node_id));
     }
 
-    /**
-     * Adds a node to the node tree array.
-     *
-     * @param string $id          The unique node id.
-     * @param string $parent      The parent's unique node id.
-     * @param string $label       The text label for the node.
-     * @param string $indent      Deprecated, this is calculated automatically
-     *                            based on the parent node.
-     * @param boolean $expanded   Is this level expanded or not.
-     * @param array $params       Any other parameters to set (@see
-     *                            addNodeParams() for full details).
-     * @param array $extra_right  Any other columns to display to the right of
-     *                            the tree.
-     * @param array $extra_left   Any other columns to display to the left of
-     *                            the tree.
-     */
-    public function addNode($id, $parent, $label, $indent = null,
-                            $expanded = true, $params = array(),
-                            $extra_right = array(), $extra_left = array())
-    {
-        $sess = $_SESSION['horde_tree'][$this->_instance];
-        $toggle_id = Horde_Util::getFormData(self::TOGGLE . $this->_instance);
-
-        if ($id == $toggle_id) {
-            /* We have a URL toggle request for this node. */
-            $expanded = $_SESSION['horde_tree'][$this->_instance]['expanded'][$id] = isset($sess['expanded'][$id])
-                /* Use session state if it is set. */
-                ? (!$sess['expanded'][$id])
-                /* Otherwise use what was passed through the function. */
-                : (!$expanded);
-        } elseif (isset($sess['expanded'][$id])) {
-            /* If we have a saved session state use it. */
-            $expanded = $sess['expanded'][$id];
-        }
-
-        parent::addNode($id, $parent, $label, $indent, $expanded, $params, $extra_right, $extra_left);
-    }
-
 }
index 7bb43f7..7dfbfba 100644 (file)
@@ -30,12 +30,13 @@ class Horde_Core_Tree_Javascript extends Horde_Core_Tree_Html
         Horde::addScriptFile('hordetree.js', 'horde');
 
         /* Check for a javascript session state. */
-        if (isset($_COOKIE[$this->_instance . '_expanded'])) {
+        if (!empty($this->_options['session']) &&
+            isset($_COOKIE[$this->_instance . '_expanded'])) {
             /* Remove "exp" prefix from cookie value. */
             $nodes = explode(',', substr($_COOKIE[$this->_instance . '_expanded'], 3));
 
             /* Save nodes to the session. */
-            $_SESSION['horde_tree'][$this->_instance]['expanded'] = array_combine(
+            $_SESSION[$this->_options['session']][$this->_instance]['expanded'] = array_combine(
                 $nodes,
                 array_fill(0, count($nodes), true)
             );
index 6801ba6..10c089f 100644 (file)
@@ -138,17 +138,6 @@ class Horde_Tree
     }
 
     /**
-     * Provide a simpler renderer to fallback to.
-     *
-     * @return string  The next best renderer.
-     * @throws Horde_Tree_Exception
-     */
-    public function fallback()
-    {
-        throw new Horde_Tree_Exception('No fallback renderer found.');
-    }
-
-    /**
      * Constructor.
      *
      * @param string $name   The name of this tree instance.
@@ -160,12 +149,31 @@ class Horde_Tree
      *               use the widths.
      * lines - (boolean) Show tree lines?
      * multiline - (boolean) Do the node labels contain linebreaks?
+     * session - (string) The name of the session array key to store data.
+     *           If this is an empty string, session storage will be disabled.
+     *           DEFAULT: No session storage
      * </pre>
      */
     public function __construct($name, array $params = array())
     {
         $this->_instance = $name;
         $this->setOption($params);
+
+        if (!empty($this->_options['session']) &&
+            !isset($_SESSION[$this->_options['session']][$this->_instance])) {
+            $_SESSION[$this->_options['session']][$this->_instance] = array();
+        }
+    }
+
+    /**
+     * Provide a simpler renderer to fallback to.
+     *
+     * @return string  The next best renderer.
+     * @throws Horde_Tree_Exception
+     */
+    public function fallback()
+    {
+        throw new Horde_Tree_Exception('No fallback renderer found.');
     }
 
     /**
@@ -262,6 +270,23 @@ class Horde_Tree
                             $expanded = true, $params = array(),
                             $extra_right = array(), $extra_left = array())
     {
+        if (!empty($this->_options['session'])) {
+            $sess = &$_SESSION[$this->_options['session']][$this->_instance];
+            $toggle_id = Horde_Util::getFormData(self::TOGGLE . $this->_instance);
+
+            if ($id == $toggle_id) {
+                /* We have a URL toggle request for this node. */
+                $expanded = $sess['expanded'][$id] = isset($sess['expanded'][$id])
+                    /* Use session state if it is set. */
+                    ? (!$sess['expanded'][$id])
+                    /* Otherwise use what was passed through the function. */
+                    : (!$expanded);
+            } elseif (isset($sess['expanded'][$id])) {
+                /* If we have a saved session state use it. */
+                $expanded = $sess['expanded'][$id];
+            }
+        }
+
         $this->_nodes[$id]['label'] = $label;
         $this->_nodes[$id]['expanded'] = $expanded;