1: <?php
2: /**
3: * A Horde_Injector:: based Horde_Tree:: factory.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Core
9: * @author Michael Slusarz <slusarz@horde.org>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Core
12: */
13:
14: /**
15: * A Horde_Injector:: based Horde_Tree:: factory.
16: *
17: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (LGPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21: *
22: * @category Horde
23: * @package Core
24: * @author Michael Slusarz <slusarz@horde.org>
25: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
26: * @link http://pear.horde.org/index.php?package=Core
27: */
28: class Horde_Core_Factory_Tree extends Horde_Core_Factory_Base
29: {
30: /**
31: * Singleton instances.
32: *
33: * @var array
34: */
35: private $_instances = array();
36:
37: /**
38: * Return the Horde_Tree:: instance.
39: *
40: * @param string $name The name of this tree instance.
41: * @param mixed $renderer The type of tree renderer.
42: * @param array $params Any additional parameters the constructor
43: * needs. Defined by this class:
44: * <pre>
45: * 'nosession' - (boolean) Don't store tree state in the session.
46: * DEFAULT: false
47: * </pre>
48: *
49: * @return Horde_Tree_Base The singleton instance.
50: * @throws Horde_Tree_Exception
51: */
52: public function create($name, $renderer, array $params = array())
53: {
54: $lc_renderer = Horde_String::lower($renderer);
55: $id = $name . '|' . $lc_renderer;
56:
57: if (!isset($this->_instances[$id])) {
58: switch ($lc_renderer) {
59: case 'html':
60: $renderer = 'Horde_Core_Tree_Html';
61: break;
62:
63: case 'javascript':
64: $renderer = 'Horde_Core_Tree_Javascript';
65: break;
66:
67: case 'simplehtml':
68: $renderer = 'Horde_Core_Tree_Simplehtml';
69: break;
70: }
71:
72: if (empty($params['nosession'])) {
73: $params['session'] = array(
74: 'get' => array(__CLASS__, 'getSession'),
75: 'set' => array(__CLASS__, 'setSession')
76: );
77: }
78:
79: $this->_instances[$id] = Horde_Tree::factory($name, $renderer, $params);
80: }
81:
82: return $this->_instances[$id];
83: }
84:
85: /**
86: */
87: static public function getSession($instance, $id, $mask = 0)
88: {
89: return $GLOBALS['session']->get('horde', 'tree-' . $instance . '/' . $id, $mask);
90: }
91:
92: /**
93: */
94: static public function setSession($instance, $id, $val)
95: {
96: if ($val) {
97: $GLOBALS['session']->set('horde', 'tree-' . $instance . '/' . $id, $val);
98: } else {
99: $GLOBALS['session']->remove('horde', 'tree-' . $instance . '/' . $id);
100: }
101: }
102:
103: }
104: