root/trunk/patForms/Rule/ConditionalEnum.php

Revision 325, 4.2 kB (checked in by schst, 3 years ago)

Fixed bug #193: Javascript of form rules is not generated

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms Rule ConditionalEnum
4  *
5  * $Id$
6  *
7  * @package        patForms
8  * @subpackage    Rules
9  */
10
11 /**
12  * patForms Rule ConditionalEnum
13  *
14  * This rule is used to change the values of an Enum
15  * element depending on the value of any other field
16  * in your form.
17  *
18  * It has to be applied after validating the form.
19  *
20  * @package        patForms
21  * @subpackage    Rules
22  * @author        Stephan Schmidt <schst@php-tools.net>
23  * @license        LGPL, see license.txt for details
24  * @link        http://www.php-tools.net
25  */
26 class patForms_Rule_ConditionalEnum extends patForms_Rule
27 {
28    /**
29     * script that will be displayed only once
30     *
31     * @access    private
32     * @var        array
33     */
34     var $globalScript    =    array(
35                                     'html'    =>    'Html/Rule/ConditionalEnum.js'
36                                 );
37
38    /**
39     * javascript that will be displayed once per instance
40     *
41     * @access    private
42     * @var        array
43     */
44     var $instanceScript    =    array(
45                                         'html'    =>    "var pfr_[RULE::ID] = new pFRC_ConditionalEnum( '[RULE::SOURCE]', '[RULE::TARGET]' );\n"
46                                     );
47
48    /**
49     * properties that have to be replaced in the instance script.
50     *
51     * @access    private
52     * @var        array
53     */
54     var $scriptPlaceholders    =    array(
55                                     'RULE::SOURCE'    =>    '_source',
56                                     'RULE::TARGET'    =>    '_target'
57                                 );
58
59    /**
60     * define error codes and messages for the rule
61     *
62     * @access    private
63     * @var        array    $validatorErrorCodes
64     * @todo     translate error messages
65     */
66     var    $validatorErrorCodes  =   array(
67         "C"    =>    array(
68             1    =>    "The selection in [TARGET_LABEL] does not match your selection in [SOURCE_LABEL].",
69         ),
70         "de" =>    array(
71             1    =>    "Ihre Auswahl in [TARGET_LABEL] passt nicht zur Auswahl in [SOURCE_LABEL].",
72         ),
73         "fr" =>    array(
74             1    =>    "La sélection dans [TARGET_LABEL] ne correspond pas à votre sélection dans [SOURCE_LABEL].",
75         )
76     );
77
78    /**
79     * source field
80     *
81     * @access    private
82     * @var        string
83     */
84     var $_source;
85
86    /**
87     * target field
88     *
89     * @access    private
90     * @var        string
91     */
92     var $_target;
93
94    /**
95     * conditions
96     *
97     * @access    private
98     * @var        array
99     */
100     var $_conditions    =    array();
101
102    /**
103     * prepare the rule
104     *
105     * @access    public
106     * @param    object patForms
107     */
108     function prepareRule(&$container)
109     {
110         patForms_Rule::prepareRule( $container );
111         
112         $source = &$container->getElementByName( $this->_source );
113         $onChange = $source->getAttribute( 'onchange' );
114         
115         $newHandler    = sprintf('pfr_%s.adjustTarget();', $this->_id);
116         $source->setAttribute('onchange', $newHandler . $onChange);       
117         return true;
118     }
119
120    /**
121     * set the name of the source field
122     *
123     * @access    public
124     * @param    string    source field for the condition
125     */
126     function setSourceField($field)
127     {
128         $this->_source = $field;
129     }
130
131    /**
132     * set the name of the target field
133     *
134     * @access    public
135     * @param    string    target field for the condition
136     */
137     function setTargetField($field)
138     {
139         $this->_target = $field;
140     }
141
142    /**
143     * add a condition
144     *
145     * @access    public
146     * @param    string    condition value
147     * @param    mixed    options for the enum
148     */
149     function addCondition($value, $options)
150     {
151         $this->_conditions[$value] = $options;
152     }
153
154    /**
155     * method called by patForms
156     *
157     * @access    public
158     * @param    object patForms    form object
159     */
160     function applyRule(&$form, $type = PATFORMS_RULE_AFTER_VALIDATION)
161     {
162         $source    = &$form->getElementByName($this->_source);
163         $target    = &$form->getElementByName($this->_target);
164
165         $sourceVal = $source->getValue();
166         
167         if (!isset($this->_conditions[$sourceVal])) {
168             return true;
169         }
170
171         $values = $this->_conditions[$sourceVal];
172         if (in_array( $target->getValue(), $values )) {
173             return true;
174         }
175
176         $vars = array(
177                     'SOURCE_LABEL' => $source->getAttribute('label'),
178                     'TARGET_LABEL' => $target->getAttribute('label'),
179                     );
180
181         $this->addValidationError(1, $vars);
182         return false;
183     }
184
185    /**
186     * get the instance javascript of the rule
187     *
188     * @access    public
189     * @return    string
190     */
191     function getInstanceJavascript()
192     {
193         $script = patForms_Rule::getInstanceJavascript();
194
195         if ($script === false) {
196             return false;
197         }
198         
199         foreach ($this->_conditions as $value => $options) {
200             $list = array();
201             foreach ($options as $option) {
202                 array_push($list, "'$option'");
203             }
204             $script .= sprintf("pfr_%s.addCondition( '%s', new Array( %s ) );\n", $this->_id, $value, implode(',', $list));
205         }           
206         return $script;
207     }
208 }
209 ?>
Note: See TracBrowser for help on using the browser.