1: <?php
2: /**
3: * Horde_Form_Action_conditional_enable is a Horde_Form_Action that
4: * enables or disables an element based on the value of another element
5: *
6: * Format of the $params passed to the constructor:
7: * <pre>
8: * $params = array(
9: * 'target' => '[name of element this is conditional on]',
10: * 'enabled' => 'true' | 'false',
11: * 'values' => array([target values to check])
12: * );
13: * </pre>
14: *
15: * So $params = array('foo', 'true', array(1, 2)) will enable the field this
16: * action is attached to if the value of 'foo' is 1 or 2, and disable it
17: * otherwise.
18: *
19: * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
20: *
21: * See the enclosed file COPYING for license information (LGPL). If you
22: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
23: *
24: * @author Matt Kynaston <matt@kynx.org>
25: * @package Form
26: */
27: class Horde_Form_Action_conditional_enable extends Horde_Form_Action {
28:
29: var $_trigger = array('onload');
30:
31: function getActionScript(&$form, $renderer, $varname)
32: {
33: Horde::addScriptFile('form_helpers.js', 'horde');
34:
35: $form_name = $form->getName();
36: $target = $this->_params['target'];
37: $enabled = $this->_params['enabled'];
38: if (!is_string($enabled)) {
39: $enabled = ($enabled) ? 'true' : 'false';
40: }
41: $vals = $this->_params['values'];
42: $vals = (is_array($vals)) ? $vals : array($vals);
43: $args = "'$varname', $enabled, '" . implode("','", $vals) . "'";
44:
45: return "if (addEvent(document.getElementById('$form_name').$target, 'onchange', \"checkEnabled(this, $args);\")) { "
46: . " checkEnabled(document.getElementById('$form_name').$varname, $args); };";
47: }
48:
49: }
50: