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