~ubuntu-branches/ubuntu/saucy/horde3/saucy

« back to all changes in this revision

Viewing changes to lib/Horde/Form/Action/conditional_setvalue.php

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-05-04 23:08:08 UTC
  • Revision ID: james.westby@ubuntu.com-20050504230808-p4hf3hk28o3v7wir
Tags: upstream-3.0.4
ImportĀ upstreamĀ versionĀ 3.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Horde_Form_Action_conditional_setvalue is a Horde_Form_Action that
 
4
 * sets the value of one Horde_Form variable based on the value of the
 
5
 * variable the action is attached to.
 
6
 *
 
7
 * $Horde: framework/Form/Form/Action/conditional_setvalue.php,v 1.19.10.1 2005/01/03 12:19:00 jan Exp $
 
8
 *
 
9
 * Copyright 2002-2005 Chuck Hagenbuch <chuck@horde.org>
 
10
 *
 
11
 * See the enclosed file COPYING for license information (LGPL). If you
 
12
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 
13
 *
 
14
 * @author  Chuck Hagenbuch <chuck@horde.org>
 
15
 * @version $Revision: 1.19.10.1 $
 
16
 * @package Horde_Form
 
17
 */
 
18
class Horde_Form_Action_conditional_setvalue extends Horde_Form_Action {
 
19
 
 
20
    /**
 
21
     * Which JS events should trigger this action?
 
22
     * @var array $_trigger
 
23
     */
 
24
    var $_trigger = array('onchange', 'onload');
 
25
 
 
26
    function getActionScript($form, $renderer, $varname)
 
27
    {
 
28
        return 'map(\'' . $renderer->_genID($varname, false) . "', '" . $renderer->_genID($this->getTarget(), false) . '\');';
 
29
    }
 
30
 
 
31
    function setValues(&$vars, $sourceVal, $arrayVal = false)
 
32
    {
 
33
        $map = $this->_params['map'];
 
34
        $target = $this->getTarget();
 
35
 
 
36
        if ($arrayVal) {
 
37
            $i = 0;
 
38
            if (is_array($sourceVal)) {
 
39
                foreach ($sourceVal as $val) {
 
40
                    if (!empty($map[$val])) {
 
41
                        $vars->set($target, $map[$val], $i);
 
42
                    }
 
43
                    $i++;
 
44
                }
 
45
            }
 
46
        } else {
 
47
            if (!empty($map[$sourceVal])) {
 
48
                $vars->set($target, $map[$sourceVal]);
 
49
            }
 
50
        }
 
51
    }
 
52
 
 
53
    function printJavaScript()
 
54
    {
 
55
        $this->_printJavaScriptStart();
 
56
        $map = $this->_params['map'];
 
57
?>
 
58
 
 
59
var _map = new Array(<?php
 
60
$i = 0;
 
61
foreach ($map as $val) {
 
62
    if ($i > 0) {
 
63
        echo ', ';
 
64
    }
 
65
    echo '"' . $val . '"';
 
66
    $i++;
 
67
}?>);
 
68
 
 
69
function map(sourceId, targetId)
 
70
{
 
71
    var newval;
 
72
    var source = document.getElementById(sourceId);
 
73
    var element = document.getElementById(targetId);
 
74
    if (element) {
 
75
        if (_map[source.selectedIndex]) {
 
76
            newval = _map[source.selectedIndex];
 
77
            replace = true;
 
78
        } else {
 
79
            newval = '';
 
80
            replace = false;
 
81
            for (i = 0; i < _map.length; i++) {
 
82
                if (element.value == _map[i]) {
 
83
                    replace = true;
 
84
                    break;
 
85
                }
 
86
            }
 
87
        }
 
88
 
 
89
        if (replace) {
 
90
            element.value = newval;
 
91
        }
 
92
    }
 
93
}<?php
 
94
        $this->_printJavaScriptEnd();
 
95
    }
 
96
 
 
97
}