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_sum_fields is a Horde_Form_Action that sets the target
16 * field to the sum of one or more other numeric fields.
17 *
18 * The params array should contain the names of the fields which will be
19 * summed.
20 *
21 * @author    Matt Kynaston <matt@kynx.org>
22 * @category  Horde
23 * @copyright 2002-2017 Horde LLC
24 * @license   http://www.horde.org/licenses/lgpl21 LGPL
25 * @package   Form
26 */
27class Horde_Form_Action_SumFields extends Horde_Form_Action {
28
29    var $_trigger = array('onload');
30
31    function getActionScript(&$form, $renderer, $varname)
32    {
33        $GLOBALS['injector']->getInstance('Horde_PageOutput')->addScriptFile('form_helpers.js', 'horde');
34
35        $form_name = $form->getName();
36        $fields = "'" . implode("','", $this->_params) . "'";
37        $js = array();
38        $js[] = sprintf('document.forms[\'%s\'].elements[\'%s\'].disabled = true;',
39                        $form_name,
40                        $varname);
41        foreach ($this->_params as $field) {
42            $js[] = sprintf("addEvent(document.forms['%1\$s'].elements['%2\$s'], \"onchange\", \"sumFields(document.forms['%1\$s'], '%3\$s', %4\$s);\");",
43                            $form_name,
44                            $field,
45                            $varname,
46                            $fields);
47        }
48
49        return implode("\n", $js);
50    }
51
52}
53