From: Michael M Slusarz Date: Wed, 14 Jul 2010 19:00:43 +0000 (-0600) Subject: Re-add session code to Horde_Tree. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=65b6788f024084c4c2a8a335b08dd800c8042468;p=horde.git Re-add session code to Horde_Tree. Sessions can be used outside of Horde - we aren't using any Horde specific session functionality. --- diff --git a/framework/Core/lib/Horde/Core/Factory/Tree.php b/framework/Core/lib/Horde/Core/Factory/Tree.php index 06a358bb6..d01697e08 100644 --- a/framework/Core/lib/Horde/Core/Factory/Tree.php +++ b/framework/Core/lib/Horde/Core/Factory/Tree.php @@ -78,6 +78,8 @@ class Horde_Core_Factory_Tree break; } + $params['session'] = 'horde_tree'; + $this->_instances[$id] = Horde_Tree::factory($name, $renderer, $params); } diff --git a/framework/Core/lib/Horde/Core/Tree/Html.php b/framework/Core/lib/Horde/Core/Tree/Html.php index 003c4cd00..b0ab4aee8 100644 --- a/framework/Core/lib/Horde/Core/Tree/Html.php +++ b/framework/Core/lib/Horde/Core/Tree/Html.php @@ -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); - } - } diff --git a/framework/Core/lib/Horde/Core/Tree/Javascript.php b/framework/Core/lib/Horde/Core/Tree/Javascript.php index 7bb43f791..7dfbfba59 100644 --- a/framework/Core/lib/Horde/Core/Tree/Javascript.php +++ b/framework/Core/lib/Horde/Core/Tree/Javascript.php @@ -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) ); diff --git a/framework/Tree/lib/Horde/Tree.php b/framework/Tree/lib/Horde/Tree.php index 6801ba65b..10c089f59 100644 --- a/framework/Tree/lib/Horde/Tree.php +++ b/framework/Tree/lib/Horde/Tree.php @@ -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 * */ 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;