1: <?php
2: /**
3: * The Horde_Form_Action class provides an API for adding actions to
4: * Horde_Form variables.
5: *
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Chuck Hagenbuch <chuck@horde.org>
11: * @package Form
12: */
13: class Horde_Form_Action {
14:
15: var $_id;
16: var $_params;
17: var $_trigger = null;
18:
19: function Horde_Form_Action($params = null)
20: {
21: $this->_params = $params;
22: $this->_id = md5(mt_rand());
23: }
24:
25: function getTrigger()
26: {
27: return $this->_trigger;
28: }
29:
30: function id()
31: {
32: return $this->_id;
33: }
34:
35: function getActionScript($form, $renderer, $varname)
36: {
37: return '';
38: }
39:
40: function printJavaScript()
41: {
42: }
43:
44: function _printJavaScriptStart()
45: {
46: echo '<script type="text/javascript"><!--';
47: }
48:
49: function _printJavaScriptEnd()
50: {
51: echo '// --></script>';
52: }
53:
54: function getTarget()
55: {
56: return isset($this->_params['target']) ? $this->_params['target'] : null;
57: }
58:
59: function setValues(&$vars, $sourceVal, $index = null, $arrayVal = false)
60: {
61: }
62:
63: /**
64: * Attempts to return a concrete Horde_Form_Action instance
65: * based on $form.
66: *
67: * @param mixed $action The type of concrete Horde_Form_Action subclass
68: * to return. If $action is an array, then we will look
69: * in $action[0]/lib/Form/Action/ for the subclass
70: * implementation named $action[1].php.
71: * @param array $params A hash containing any additional configuration a
72: * form might need.
73: *
74: * @return Horde_Form_Action The concrete Horde_Form_Action reference, or
75: * false on an error.
76: */
77: function &factory($action, $params = null)
78: {
79: if (is_array($action)) {
80: $app = $action[0];
81: $action = $action[1];
82: }
83:
84: $action = basename($action);
85: $class = 'Horde_Form_Action_' . $action;
86: if (!class_exists($class)) {
87: if (!empty($app)) {
88: include_once $GLOBALS['registry']->get('fileroot', $app) . '/lib/Form/Action/' . $action . '.php';
89: }
90: }
91:
92: if (class_exists($class)) {
93: $instance = new $class($params);
94: } else {
95: $instance = PEAR::raiseError('Class definition of ' . $class . ' not found.');
96: }
97:
98: return $instance;
99: }
100:
101: /**
102: * Attempts to return a reference to a concrete
103: * Horde_Form_Action instance based on $action. It will only
104: * create a new instance if no Horde_Form_Action instance with
105: * the same parameters currently exists.
106: *
107: * This should be used if multiple types of form renderers (and,
108: * thus, multiple Horde_Form_Action instances) are required.
109: *
110: * This method must be invoked as: $var =
111: * &Horde_Form_Action::singleton()
112: *
113: * @param mixed $action The type of concrete Horde_Form_Action subclass to return.
114: * The code is dynamically included. If $action is an array,
115: * then we will look in $action[0]/lib/Form/Action/ for
116: * the subclass implementation named $action[1].php.
117: * @param array $params A hash containing any additional configuration a
118: * form might need.
119: *
120: * @return Horde_Form_Action The concrete Horde_Form_Action reference, or
121: * false on an error.
122: */
123: function &singleton($action, $params = null)
124: {
125: static $instances = array();
126:
127: $signature = serialize(array($action, $params));
128: if (!isset($instances[$signature])) {
129: $instances[$signature] = &Horde_Form_Action::factory($action, $params);
130: }
131:
132: return $instances[$signature];
133: }
134:
135: }
136: