source: trunk/patForms/Element/Switch.php @ 135

Revision 135, 10.0 KB checked in by argh, 9 years ago (diff)

Fixed the display attribute; Fixed some error handling issues; optimized the code.

  • 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  patForms_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  patForms_Element
32 * @author      Sebastian Mordziol <argh@php-tools.net>
33 * @license     LGPL, see license.txt for details
34 */
35class 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                                    ),
78            "title"         =>  array(  "required"      =>  false,
79                                        "format"        =>  "string",
80                                        "outputFormats" =>  array( "html" ),
81                                        "modifiers"     =>  array( "insertSpecials" => array() ),
82                                    ),
83            "type"          =>  array(  "required"      =>  false,
84                                        "format"        =>  "string",
85                                        'default'       =>  'checkbox',
86                                        "outputFormats" =>  array( "html" ),
87                                    ),
88            "description"   =>  array(  "required"      =>  false,
89                                        "format"        =>  "string",
90                                        "outputFormats" =>  array(),
91                                        "modifiers"     =>  array( "insertSpecials" => array() ),
92                                    ),
93            "default"       =>  array(  "required"      =>  false,
94                                        "format"        =>  "string",
95                                        "outputFormats" =>  array(),
96                                    ),
97            "label"         =>  array(  "required"      =>  false,
98                                        "format"        =>  "string",
99                                        "outputFormats" =>  array(),
100                                    ),
101            "edit"          =>  array(  "required"      =>  false,
102                                        "format"        =>  "string",
103                                        "default"       =>  "yes",
104                                        "outputFormats" =>  array(),
105                                    ),
106            "display"       =>  array(  "required"      =>  false,
107                                        "format"        =>  "string",
108                                        "default"       =>  "yes",
109                                        "outputFormats" =>  array(),
110                                    ),
111            "required"      =>  array(  "required"      =>  false,
112                                        "format"        =>  "string",
113                                        "default"       =>  "yes",
114                                        "outputFormats" =>  array(),
115                                    ),
116            "style"         =>  array(  "required"      =>  false,
117                                        "outputFormats" =>  array( "html" ),
118                                        "format"        =>  "string",
119                                    ),
120            "class"         =>  array(  "required"      =>  false,
121                                        "outputFormats" =>  array( "html" ),
122                                        "format"        =>  "string",
123                                    ),
124            "onchange"      =>  array(  "required"      =>  false,
125                                        "format"        =>  "string",
126                                        "outputFormats" =>  array( "html" ),
127                                        "modifiers"     =>  array( "insertSpecials" => array() ),
128                                    ),
129            "onclick"       =>  array(  "required"      =>  false,
130                                        "format"        =>  "string",
131                                        "outputFormats" =>  array( "html" ),
132                                        "modifiers"     =>  array( "insertSpecials" => array() ),
133                                    ),
134            "onfocus"       =>  array(  "required"      =>  false,
135                                        "format"        =>  "string",
136                                        "outputFormats" =>  array( "html" ),
137                                        "modifiers"     =>  array( "insertSpecials" => array() ),
138                                    ),
139            "onmouseover"   =>  array(  "required"      =>  false,
140                                        "format"        =>  "string",
141                                        "outputFormats" =>  array( "html" ),
142                                        "modifiers"     =>  array( "insertSpecials" => array() ),
143                                    ),
144            "onmouseout"    =>  array(  "required"      =>  false,
145                                        "format"        =>  "string",
146                                        "outputFormats" =>  array( "html" ),
147                                        "modifiers"     =>  array( "insertSpecials" => array() ),
148                                    ),
149            "onblur"        =>  array(  "required"      =>  false,
150                                        "format"        =>  "string",
151                                        "outputFormats" =>  array( "html" ),
152                                        "modifiers"     =>  array( "insertSpecials" => array() ),
153                                    ),
154            "accesskey"     =>  array(  "required"      =>  false,
155                                        "format"        =>  "string",
156                                        "outputFormats" =>  array( "html" ),
157                                    ),
158            "position"      =>  array(  "required"      =>  false,
159                                        "format"        =>  "int",
160                                        "outputFormats" =>  array(),
161                                    ),
162            "tabindex"      =>  array(  "required"      =>  false,
163                                        "format"        =>  "int",
164                                        "outputFormats" =>  array( "html" ),
165                                    ),
166            "disabled"      =>  array(  "required"      =>  false,
167                                        "format"        =>  "string",
168                                        "default"       =>  "no",
169                                        "outputFormats" =>  array( "html" ),
170                                    ),
171            "checked"       =>  array(  "required"      =>  false,
172                                        "format"        =>  "string",
173                                        "outputFormats" =>  array( "html" ),
174                                    ),
175            "clicklabel"    =>  array(  "required"      =>  false,
176                                        "format"        =>  "string",
177                                        "outputFormats" =>  array(),
178                                    ),
179        );
180       
181    /**
182     *  define error codes an messages for each form element
183     *
184     *  @access private
185     *  @var    array   $validatorErrorCodes
186     */
187    var $validatorErrorCodes  =   array(
188        "C" =>  array(
189            1   =>  "This field is required, please check it.",
190        ),
191        "de" => array(
192            1   =>  "Pflichtfeld. Bitte wählen Sie dieses Feld an.",
193        ),
194        "fr" => array(
195            1   =>  "Vous devez sélectionner ce champ.",
196        )
197    );
198
199   /**
200    * defines readonly display values for the switch element for the
201    * available locales.
202    *
203    * @access   private
204    * @var      array
205    */
206    var $readonlyValues = array(
207        'C' => array(
208            'checked'   =>  'Yes',
209            'unchecked' =>  'No',
210        ),
211        'de' => array(
212            'checked'   =>  'Ja',
213            'unchecked' =>  'Nein',
214        ),
215        'fr' => array(
216            'checked'   =>  'Oui',
217            'unchecked' =>  'Non',
218        ),
219    );
220   
221   /**
222    * element creation method for the 'HTML' format in the 'default' form mode.
223    *
224    * @access   public
225    * @param    mixed   value of the element
226    * @return   mixed   $element    The element, or false if failed.
227    */
228    function serializeHtmlDefault( $value )
229    {
230        if( $this->attributes['display'] == 'no' )
231        {
232            return $this->createDisplaylessTag( $value );
233        }
234       
235        if( $this->attributes["edit"] == "no" )
236        {
237            $this->attributes['disabled']   =   'yes';
238        }
239       
240        if( isset( $this->attributes["value"] ) && $this->attributes["value"] == $value )
241        {
242            $this->attributes["checked"]    =   "checked";
243        }
244       
245        $this->attributes["type"] = "checkbox";
246       
247        // clicklabel attribute
248        if( isset( $this->attributes['clicklabel'] ) && $this->attributes['clicklabel'] == 'yes' )
249        {
250            // call this to initialize all attributes
251            $this->getAttributesFor( $this->getFormat() );
252
253            if( !isset( $this->attributes['id'] ) )
254            {
255                patErrorManager::raiseWarning(
256                    PATFORMS_ELEMENT_SWITCH_WARNING_CLICKLABEL_NEEDS_ID,
257                    'The "clicklabel" attribute needs the "id" attribute to be set.'
258                );
259            }
260            else if( !isset( $this->attributes['label'] ) )
261            {
262                patErrorManager::raiseWarning(
263                    PATFORMS_ELEMENT_SWITCH_WARNING_CLICKLABEL_NEEDS_LABEL,
264                    'The "clicklabel" attribute needs the "label" attribute to be set.'
265                );
266            }
267            else
268            {
269                $this->setAttribute( 'label', $this->createTag( 'label', 'full', array( 'for' => $this->getAttribute( 'id' ), 'title' => $this->getAttribute( 'title' ) ), $this->getAttribute( 'label' ) ) );
270            }
271        }
272       
273         return $this->toHtml();
274    }
275   
276   /**
277    * element creation method for the 'HTML' format in the 'readonly' form mode.
278    * Returns 'Yes' or 'No' as string, along with the hidden tag to keep the value.
279    *
280    * @access   public
281    * @param    mixed   value of the element
282    * @return   string  $value  The element's value
283    */
284    function serializeHtmlReadonly( $value )
285    {
286        $tag = $this->createDisplaylessTag( $value );
287       
288        if( $this->attributes['display'] == 'no' )
289        {
290            return $tag;
291        }
292       
293        $state = 'unchecked';
294        if( isset( $this->attributes["value"] ) && $this->attributes["value"] == $value )
295        {
296            $state = 'checked';
297        }
298
299        return $this->getReadonlyValue( $state ).$tag;
300    }
301   
302   /**
303    * Retrieves the value to display in the element's readonly mode if the
304    * user has not selected any entry, according to the selected locale
305    *
306    * @access   public
307    * @return   string  $defaultValue   The default readonly value in the needed locale
308    */
309    function getReadonlyValue( $state )
310    {
311        $lang   =   $this->locale;
312   
313        if( !isset( $this->readonlyValues[$lang] ) )
314        {
315            patErrorManager::raiseWarning(
316                PATFORMS_ELEMENT_SWITCH_WARNING_NO_DEFAULT_VALUE_AVAILABLE,
317                'There is no default readonly value available for the locale "'.$lang.'", using default locale "C" instead.'
318            );
319            return $this->readonlyValues['C'][$state];
320        }
321       
322        return $this->readonlyValues[$lang][$state];
323    }
324   
325   /**
326    * validates the element.
327    *
328    * @access   public
329    * @param    mixed   value of the element
330    * @return   bool    $isValid    True if element could be validated, false otherwise.
331    * @todo     check whether the correct value has been supplied (never trust user input)
332    */
333    function validateElement( $value )
334    {
335        // required & empty
336        if( isset( $this->attributes["required"] ) && $this->attributes["required"] == "yes" && strlen( $value ) == 0 )
337        {
338            $this->addValidationError( 1 );
339            return false;
340        }
341        return true;
342    }
343}
344
345?>
Note: See TracBrowser for help on using the repository browser.