Removed _SESSION references in Horde_Tree
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 14 Oct 2010 03:48:13 +0000 (21:48 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 15 Oct 2010 16:13:55 +0000 (10:13 -0600)
framework/Core/lib/Horde/Core/Factory/Tree.php
framework/Core/lib/Horde/Core/Tree/Javascript.php
framework/Tree/lib/Horde/Tree/Base.php

index a289f0c..aaeebaf 100644 (file)
@@ -87,7 +87,10 @@ class Horde_Core_Factory_Tree
             }
 
             if (empty($params['nosession'])) {
-                $params['session'] = 'horde_tree';
+                $params['session'] = array(
+                    'get' => array($this, 'getSession'),
+                    'set' => array($this, 'setSession')
+                );
             }
 
             $this->_instances[$id] = Horde_Tree::factory($name, $renderer, $params);
@@ -96,4 +99,18 @@ class Horde_Core_Factory_Tree
         return $this->_instances[$id];
     }
 
+    /**
+     */
+    public function getSession($instance, $id)
+    {
+        return $GLOBALS['session']['horde:tree-' . $instance . '/' . $id];
+    }
+
+    /**
+     */
+    public function setSession($instance, $id, $val)
+    {
+        $GLOBALS['session']['horde:tree-' . $instance . '/' . $id] = $val;
+    }
+
 }
index ee6d017..42a8b80 100644 (file)
@@ -36,13 +36,10 @@ class Horde_Core_Tree_Javascript extends Horde_Core_Tree_Html
         if (($session = $this->getOption('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[$session][$this->_instance]['expanded'] = array_combine(
-                $nodes,
-                array_fill(0, count($nodes), true)
-            );
+            foreach (explode(',', substr($_COOKIE[$this->_instance . '_expanded'], 3)) as $val) {
+                /* Save nodes to the session. */
+                call_user_func($session['set'], $this->_instance, $val, true);
+            }
         }
     }
 
index b3132fd..040ce3d 100644 (file)
@@ -95,8 +95,10 @@ abstract class Horde_Tree_Base implements Countable
      * @param string $name   The name of this tree instance.
      * @param array $params  Additional parameters.
      * <pre>
-     * session - (string) The name of the session array key to store data.
-     *           If this is an empty string, session storage will be disabled.
+     * session - (array) Callbacks used to store session data. Must define
+     *           two keys: 'get' and 'set'. Function definitions:
+     *           (string) = get([string - Instance], [string - ID]);
+     *           set([string - Instance], [string - ID], [boolean - value]);
      *           DEFAULT: No session storage
      * </pre>
      */
@@ -104,11 +106,6 @@ abstract class Horde_Tree_Base implements Countable
     {
         $this->_instance = $name;
         $this->setOption($params);
-
-        if (($sess = $this->getOption('session')) &&
-            !isset($_SESSION[$sess][$this->_instance])) {
-            $_SESSION[$sess][$this->_instance] = array();
-        }
     }
 
     /**
@@ -198,20 +195,20 @@ abstract class Horde_Tree_Base implements Countable
     {
         $nodeid = $this->_nodeId($id);
 
-        if (($session = $this->getOption('session'))) {
-            $sess = &$_SESSION[$session][$this->_instance];
+        if ($session = $this->getOption('session')) {
             $toggle_id = Horde_Util::getFormData(Horde_Tree::TOGGLE . $this->_instance);
 
             if ($nodeid == $toggle_id) {
                 /* We have a URL toggle request for this node. */
-                $expanded = $sess['expanded'][$nodeid] = isset($sess['expanded'][$id])
+                $expanded = (call_user_func($session['get'], $this->_instance, $id) !== null)
                     /* Use session state if it is set. */
-                    ? (!$sess['expanded'][$nodeid])
+                    ? !call_user_func($session['get'], $this->_instance, $nodeid)
                     /* Otherwise use what was passed through the function. */
-                    : (!$expanded);
-            } elseif (isset($sess['expanded'][$nodeid])) {
+                    : !$expanded;
+                call_user_func($session['set'], $this->_instance, $nodeid, $expanded);
+            } elseif (($exp_get = call_user_func($session['get'], $this->_instance, $nodeid)) !== null) {
                 /* If we have a saved session state use it. */
-                $expanded = $sess['expanded'][$nodeid];
+                $expanded = $exp_get;
             }
         }