root/trunk/patForms/Element/Switch.php

Revision 321, 10.1 kB (checked in by argh, 3 years ago)

Fixed bugs #171 (Date element accepts invalid dates), #187 (french translations for storage rules) and #182 (switch element does not check for correct value)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * simple Switch patForms element that builds and validates checkboxes.
4  *
5  * @access        protected
6  * @package        patForms
7  * @subpackage    Element
8  * @author        Sebastian Mordziol <argh@php-tools.net>
9  */
10
11 /**
12  * Warning: the clicklabel attribute needs the id attribute to be set.
13  */
14  define( 'PATFORMS_ELEMENT_SWITCH_WARNING_CLICKLABEL_NEEDS_ID', 'patForms:Element:Switch:01' );
15
16 /**
17  * Warning: the clicklabel attribute needs the label attribute to be set.
18  */
19  define( 'PATFORMS_ELEMENT_SWITCH_WARNING_CLICKLABEL_NEEDS_LABEL', 'patForms:Element:Switch:02' );
20
21 /**
22  * Warning: there is no default value for the given locale
23  */
24  define( 'PATFORMS_ELEMENT_SWITCH_WARNING_NO_DEFAULT_VALUE_AVAILABLE', 'patForms:Element:Switch:03' );
25
26 /**
27  * simple Switch patForms element that builds and validates checkboxes.
28  *
29  * @access        protected
30  * @package        patForms
31  * @subpackage    Element
32  * @author        Sebastian Mordziol <argh@php-tools.net>
33  * @license        LGPL, see license.txt for details
34  */
35 class patForms_Element_Switch extends patForms_Element
36 {
37    /**
38     * Stores the name of the element - this is used mainly by the patForms
39     * error management and should be set in every element class.
40     * @access    public
41     */
42     var $elementName    =    'Switch';
43
44    /**
45     * the type of the element - set this to the type of element you are creating
46     * if you want to use the {@link patForms_Element::element2html()} method to
47     * create the final HTML tag for your element.
48     *
49     * @access    public
50     * @see        patForms_Element::element2html()
51     */
52     var $elementType    =    array(    "html"    =>    "input",
53                                 );
54
55    /**
56     * set here which attributes you want to include in the element if you want to use
57     * the {@link patForms_Element::convertDefinition2Attributes()} method to automatically
58     * convert the values from your element definition into element attributes.
59     *
60     * @access    protected
61     * @see        patForms_Element::convertDefinition2Attribute()
62     */
63     var    $attributeDefinition    =    array(
64
65             "id"            =>    array(    "required"        =>    false,
66                                         "format"        =>    "string",
67                                         "outputFormats"    =>    array( "html" ),
68                                     ),
69             "name"            =>    array(    "required"        =>    true,
70                                         "format"        =>    "string",
71                                         "outputFormats"    =>    array( "html" ),
72                                         "modifiers"        =>    array( "insertSpecials" => array() ),
73                                     ),
74             "value"            =>    array(    "required"        =>    true,
75                                         "format"        =>    "string",
76                                         "outputFormats"    =>    array( "html" ),
77                                         "default"        =>    "yes"
78                                     ),
79             "title"            =>    array(    "required"        =>    false,
80                                         "format"        =>    "string",
81                                         "outputFormats"    =>    array( "html" ),
82                                         "modifiers"        =>    array( "insertSpecials" => array() ),
83                                     ),
84             "type"            =>    array(    "required"        =>    false,
85                                         "format"        =>    "string",
86                                         'default'        =>    'checkbox',
87                                         "outputFormats"    =>    array( "html" ),
88                                     ),
89             "description"    =>    array(    "required"        =>    false,
90                                         "format"        =>    "string",
91                                         "outputFormats"    =>    array(),
92                                         "modifiers"        =>    array( "insertSpecials" => array() ),
93                                     ),
94             "default"        =>    array(    "required"        =>    false,
95                                         "format"        =>    "string",
96                                         "outputFormats"    =>    array(),
97                                     ),
98             "label"            =>    array(    "required"        =>    false,
99                                         "format"        =>    "string",
100                                         "outputFormats"    =>    array(),
101                                     ),
102             "edit"            =>    array(    "required"        =>    false,
103                                         "format"        =>    "string",
104                                         "default"        =>    "yes",
105                                         "outputFormats"    =>    array(),
106                                     ),
107             "display"        =>    array(    "required"        =>    false,
108                                         "format"        =>    "string",
109                                         "default"        =>    "yes",
110                                         "outputFormats"    =>    array(),
111                                     ),
112             "required"        =>    array(    "required"        =>    false,
113                                         "format"        =>    "string",
114                                         "default"        =>    "yes",
115                                         "outputFormats"    =>    array(),
116                                     ),
117             "style"            =>    array(    "required"        =>    false,
118                                         "outputFormats"    =>    array( "html" ),
119                                         "format"        =>    "string",
120                                     ),
121             "class"            =>    array(    "required"        =>    false,
122                                         "outputFormats"    =>    array( "html" ),
123                                         "format"        =>    "string",
124                                     ),
125             "onchange"        =>    array(    "required"        =>    false,
126                                         "format"        =>    "string",
127                                         "outputFormats"    =>    array( "html" ),
128                                         "modifiers"        =>    array( "insertSpecials" => array() ),
129                                     ),
130             "onclick"        =>    array(    "required"        =>    false,
131                                         "format"        =>    "string",
132                                         "outputFormats"    =>    array( "html" ),
133                                         "modifiers"        =>    array( "insertSpecials" => array() ),
134                                     ),
135             "onfocus"        =>    array(    "required"        =>    false,
136                                         "format"        =>    "string",
137                                         "outputFormats"    =>    array( "html" ),
138                                         "modifiers"        =>    array( "insertSpecials" => array() ),
139                                     ),
140             "onmouseover"    =>    array(    "required"        =>    false,
141                                         "format"        =>    "string",
142                                         "outputFormats"    =>    array( "html" ),
143                                         "modifiers"        =>    array( "insertSpecials" => array() ),
144                                     ),
145             "onmouseout"    =>    array(    "required"        =>    false,
146                                         "format"        =>    "string",
147                                         "outputFormats"    =>    array( "html" ),
148                                         "modifiers"        =>    array( "insertSpecials" => array() ),
149                                     ),
150             "onblur"        =>    array(    "required"        =>    false,
151                                         "format"        =>    "string",
152                                         "outputFormats"    =>    array( "html" ),
153                                         "modifiers"        =>    array( "insertSpecials" => array() ),
154                                     ),
155             "accesskey"        =>    array(    "required"        =>    false,
156                                         "format"        =>    "string",
157                                         "outputFormats"    =>    array( "html" ),
158                                     ),
159             "position"        =>    array(    "required"        =>    false,
160                                         "format"        =>    "int",
161                                         "outputFormats"    =>    array(),
162                                     ),
163             "tabindex"        =>    array(    "required"        =>    false,
164                                         "format"        =>    "int",
165                                         "outputFormats"    =>    array( "html" ),
166                                     ),
167             "disabled"        =>    array(    "required"        =>    false,
168                                         "format"        =>    "string",
169                                         "default"        =>    "no",
170                                         "outputFormats"    =>    array( "html" ),
171                                     ),
172             "checked"        =>    array(    "required"        =>    false,
173                                         "format"        =>    "string",
174                                         "outputFormats"    =>    array( "html" ),
175                                     ),
176             "clicklabel"    =>    array(    "required"        =>    false,
177                                         "format"        =>    "string",
178                                         "outputFormats"    =>    array(),
179                                     ),
180         );
181
182     /**
183      *    define error codes an messages for each form element
184      *
185      *  @access private
186      *  @var    array    $validatorErrorCodes
187      */
188     var    $validatorErrorCodes  =   array(
189         "C"    =>    array(
190             1    =>    "This field is required, please check it.",
191         ),
192         "de" =>    array(
193             1    =>    "Pflichtfeld. Bitte wählen Sie dieses Feld an.",
194         ),
195         "fr" =>    array(
196             1    =>    "Vous devez sélectionner ce champ.",
197         )
198     );
199
200    /**
201     * defines readonly display values for the switch element for the
202     * available locales.
203     *
204     * @access    private
205     * @var        array
206     */
207     var $readonlyValues = array(
208         'C' => array(
209             'checked'    =>    'Yes',
210             'unchecked'    =>    'No',
211         ),
212         'de' => array(
213             'checked'    =>    'Ja',
214             'unchecked'    =>    'Nein',
215         ),
216         'fr' => array(
217             'checked'    =>    'Oui',
218             'unchecked'    =>    'Non',
219         ),
220     );
221
222    /**
223     * element creation method for the 'HTML' format in the 'default' form mode.
224     *
225     * @access    public
226     * @param    mixed    value of the element
227     * @return    mixed    $element    The element, or false if failed.
228     */
229     function serializeHtmlDefault( $value )
230     {
231         if( $this->attributes['display'] == 'no' )
232         {
233             return $this->createDisplaylessTag( $value );
234         }
235
236         if( $this->attributes["edit"] == "no" )
237         {
238             $this->attributes['disabled']    =    'yes';
239         }
240
241         if( isset( $this->attributes["value"] ) && $this->attributes["value"] == $value )
242         {
243             $this->attributes["checked"]    =    "checked";
244         }
245
246         $namespace = $this->getNamespace();
247         if( isset( $this->attributes['id'] ) && $namespace ) {
248             if (strpos($this->attributes['id'], $namespace . '_') !== 0) {
249                 $this->attributes['id'] = $namespace . '_' . $this->attributes['id'];
250             }
251         }
252
253         $this->attributes["type"] = "checkbox";
254
255         // clicklabel attribute
256         if( isset( $this->attributes['clicklabel'] ) && $this->attributes['clicklabel'] == 'yes' )
257         {
258             // call this to initialize all attributes
259             $this->getAttributesFor( $this->getFormat() );
260
261             if( !isset( $this->attributes['id'] ) )
262             {
263                 patErrorManager::raiseWarning(
264                     PATFORMS_ELEMENT_SWITCH_WARNING_CLICKLABEL_NEEDS_ID,
265                     'The "clicklabel" attribute needs the "id" attribute to be set.'
266                 );
267             }
268             else if( !isset( $this->attributes['label'] ) )
269             {
270                 patErrorManager::raiseWarning(
271                     PATFORMS_ELEMENT_SWITCH_WARNING_CLICKLABEL_NEEDS_LABEL,
272                     'The "clicklabel" attribute needs the "label" attribute to be set.'
273                 );
274             }
275             else
276             {
277                 $this->setAttribute( 'label', $this->createTag( 'label', 'full', array( 'for' => $this->getAttribute( 'id' ), 'title' => $this->getAttribute( 'title' ) ), $this->getAttribute( 'label' ) ) );
278             }
279         }
280
281          return $this->toHtml();
282     }
283
284    /**
285     * element creation method for the 'HTML' format in the 'readonly' form mode.
286     * Returns 'Yes' or 'No' as string, along with the hidden tag to keep the value.
287     *
288     * @access    public
289     * @param    mixed    value of the element
290     * @return    string    $value    The element's value
291     */
292     function serializeHtmlReadonly( $value )
293     {
294         $tag = $this->createDisplaylessTag( $value );
295
296         if( $this->attributes['display'] == 'no' )
297         {
298             return $tag;
299         }
300
301         $state = 'unchecked';
302         if( isset( $this->attributes["value"] ) && $this->attributes["value"] == $value )
303         {
304             $state = 'checked';
305         }
306
307         return $this->getReadonlyValue( $state ).$tag;
308     }
309
310    /**
311     * Retrieves the value to display in the element's readonly mode if the
312     * user has not selected any entry, according to the selected locale
313     *
314     * @access    public
315     * @return    string    $defaultValue    The default readonly value in the needed locale
316     */
317     function getReadonlyValue( $state )
318     {
319         $lang    =    $this->locale;
320
321         if( !isset( $this->readonlyValues[$lang] ) )
322         {
323             patErrorManager::raiseWarning(
324                 PATFORMS_ELEMENT_SWITCH_WARNING_NO_DEFAULT_VALUE_AVAILABLE,
325                 'There is no default readonly value available for the locale "'.$lang.'", using default locale "C" instead.'
326             );
327             return $this->readonlyValues['C'][$state];
328         }
329
330         return $this->readonlyValues[$lang][$state];
331     }
332
333    /**
334     * validates the element.
335     *
336     * @access    public
337     * @param    mixed    value of the element
338     * @return    bool    $isValid    True if element could be validated, false otherwise.
339     */
340     function validateElement( $value )
341     {
342         if( $this->getAttribute( 'required' ) == 'yes' && $value != $this->getAttribute( 'value' ) )
343         {
344             $this->addValidationError( 1 );
345             return false;
346         }
347         
348         return true;
349     }
350 }
351
352 ?>
353
Note: See TracBrowser for help on using the browser.