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

Revision 135, 9.3 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 radiobutton patForms element that builds and validates radio buttons, with the
4 * particularity that it does not generate a fully serialized element, but an array with
5 * serialized subelements.
6 *
7 * $Id$
8 *
9 * @access      protected
10 * @package     patForms
11 * @subpackage  patForms_Element
12 */
13
14/**
15 * Error: no default value is available for the element.
16 */
17 define( 'PATFORMS_ERROR_RADIO_NO_DEFAULT_VALUE_AVAILABLE', 7001 );
18
19/**
20 * Warning: the clicklabel attribute needs the id attribute to be set.
21 */
22 define( 'PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_ID', 7002 );
23
24/**
25 * Warning: the clicklabel attribute needs the label attribute to be set.
26 */
27 define( 'PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_LABEL', 7003 );
28
29/**
30 * simple radiobutton patForms element that builds and validates radio buttons, with the
31 * particularity that it does not generate a fully serialized element, but an array with
32 * serialized subelements.
33 *
34 * $Id$
35 *
36 * @access      protected
37 * @package     patForms
38 * @subpackage  patForms_Element
39 * @author      Sebastian Mordziol <argh@php-tools.net>
40 * @license     LGPL, see license.txt for details
41 */
42class patForms_Element_Radio extends patForms_Element
43{
44   /**
45    * Stores the name of the element - this is used mainly by the patForms
46    * error management and should be set in every element class.
47    * @access   public
48    */
49    var $elementName    =   'Radio';
50
51   /**
52    * the type of the element - set this to the type of element you are creating
53    * if you want to use the {@link patForms_Element::element2html()} method to
54    * create the final HTML tag for your element.
55    *
56    * @access   public
57    * @see      patForms_Element::element2html()
58    */
59    var $elementType    =   array(  "html"  =>  "input",
60                                );
61   
62   /**
63    * The radio element uses a renderer to serialize its content, so we set the flag
64    * to true here
65    *
66    * @access   private
67    * @var      boolean
68    */
69    var $usesRenderer   =   false;
70   
71   /**
72    * set here which attributes you want to include in the element if you want to use
73    * the {@link patForms_Element::convertDefinition2Attributes()} method to automatically
74    * convert the values from your element definition into element attributes.
75    *
76    * @access   protected
77    * @see      patForms_Element::convertDefinition2Attribute()
78    */
79    var $attributeDefinition    =   array( 
80           
81            "id"            =>  array(  "required"      =>  false,
82                                        "format"        =>  "string",
83                                        "outputFormats" =>  array( "html" ),
84                                    ),
85            "name"          =>  array(  "required"      =>  true,
86                                        "format"        =>  "string",
87                                        "outputFormats" =>  array( "html" ),
88                                    ),
89            "type"          =>  array(  "required"      =>  true,
90                                        "format"        =>  "string",
91                                        "outputFormats" =>  array( "html" ),
92                                    ),
93            "title"         =>  array(  "required"      =>  false,
94                                        "format"        =>  "string",
95                                        "outputFormats" =>  array( "html" ),
96                                        "modifiers"     =>  array( "insertSpecials" => array() ),
97                                    ),
98            "description"   =>  array(  "required"      =>  false,
99                                        "format"        =>  "string",
100                                        "outputFormats" =>  array(),
101                                        "modifiers"     =>  array( "insertSpecials" => array() ),
102                                    ),
103            "default"       =>  array(  "required"      =>  false,
104                                        "format"        =>  "string",
105                                        "outputFormats" =>  array(),
106                                    ),
107            "label"         =>  array(  "required"      =>  false,
108                                        "format"        =>  "string",
109                                        "outputFormats" =>  array(),
110                                    ),
111            "display"       =>  array(  "required"      =>  false,
112                                        "format"        =>  "string",
113                                        "default"       =>  "yes",
114                                        "outputFormats" =>  array(),
115                                    ),
116            "edit"          =>  array(  "required"      =>  false,
117                                        "format"        =>  "string",
118                                        "default"       =>  "yes",
119                                        "outputFormats" =>  array(),
120                                    ),
121            "required"      =>  array(  "required"      =>  false,
122                                        "format"        =>  "string",
123                                        "default"       =>  "yes",
124                                        "outputFormats" =>  array(),
125                                    ),
126            "value"         =>  array(  "required"      =>  false,
127                                        "format"        =>  "string",
128                                        "outputFormats" =>  array( 'html' ),
129                                    ),
130            "style"         =>  array(  "required"      =>  false,
131                                        "outputFormats" =>  array( "html" ),
132                                        "format"        =>  "string",
133                                    ),
134            "class"         =>  array(  "required"      =>  false,
135                                        "outputFormats" =>  array( "html" ),
136                                        "format"        =>  "string",
137                                    ),
138            "onchange"      =>  array(  "required"      =>  false,
139                                        "format"        =>  "string",
140                                        "outputFormats" =>  array( "html" ),
141                                        "modifiers"     =>  array( "insertSpecials" => array() ),
142                                    ),
143            "onclick"       =>  array(  "required"      =>  false,
144                                        "format"        =>  "string",
145                                        "outputFormats" =>  array( "html" ),
146                                        "modifiers"     =>  array( "insertSpecials" => array() ),
147                                    ),
148            "onfocus"       =>  array(  "required"      =>  false,
149                                        "format"        =>  "string",
150                                        "outputFormats" =>  array( "html" ),
151                                        "modifiers"     =>  array( "insertSpecials" => array() ),
152                                    ),
153            "onmouseover"   =>  array(  "required"      =>  false,
154                                        "format"        =>  "string",
155                                        "outputFormats" =>  array( "html" ),
156                                        "modifiers"     =>  array( "insertSpecials" => array() ),
157                                    ),
158            "onmouseout"    =>  array(  "required"      =>  false,
159                                        "format"        =>  "string",
160                                        "outputFormats" =>  array( "html" ),
161                                        "modifiers"     =>  array( "insertSpecials" => array() ),
162                                    ),
163            "onblur"        =>  array(  "required"      =>  false,
164                                        "format"        =>  "string",
165                                        "outputFormats" =>  array( "html" ),
166                                        "modifiers"     =>  array( "insertSpecials" => array() ),
167                                    ),
168            "position"      =>  array(  "required"      =>  false,
169                                        "format"        =>  "int",
170                                        "outputFormats" =>  array(),
171                                    ),
172            "values"        =>  array(  "required"      =>  false,
173                                        "format"        =>  "values",
174                                        "outputFormats" =>  array(),
175                                    ),
176            "disabled"      =>  array(  "required"      =>  false,
177                                        "format"        =>  "string",
178                                        "default"       =>  "no",
179                                        "outputFormats" =>  array( "html" ),
180                                    ),
181            "clicklabel"    =>  array(  "required"      =>  false,
182                                        "format"        =>  "string",
183                                        "outputFormats" =>  array(),
184                                    ),
185            "checked"       =>  array(  "required"      =>  false,
186                                        "format"        =>  "string",
187                                        "outputFormats" =>  array( 'html' ),
188                                    ),
189        );
190
191    /**
192     *  define error codes an messages for each form element
193     *
194     *  @access private
195     *  @var    array   $validatorErrorCodes
196     */
197    var $validatorErrorCodes  =   array(
198        "C" =>  array(
199            1   =>  "This field is has to be checked.",
200        ),
201        "de" => array(
202            1   =>  "Diese Option muss gewählt werden.",
203        ),
204        "fr" => array(
205            1   =>  "Cette option doit être sélectionnée.",
206        )
207    );
208   
209   /**
210    * element creation method for the 'HTML' format in the 'default' form mode.
211    *
212    * @access   public
213    * @param    mixed   value of the element
214    * @return   mixed   $element    The element, or false if failed.
215    */
216    function serializeHtmlDefault( $value )
217    {
218        $this->attributes["type"]   =   "radio";
219       
220        if( $this->attributes['display'] == 'no' )
221        {
222            return $this->createDisplaylessTag( $value );
223        }
224       
225        if( $value == $this->attributes["value"] )
226        {
227            $this->attributes['checked'] = 'checked';
228        }
229       
230        if( isset( $this->attributes["edit"] ) && $this->attributes["edit"] == "no" )
231        {
232            $this->attributes['disabled']   =   'yes';
233        }
234       
235        // clicklabel attribute
236        if( isset( $this->attributes['clicklabel'] ) && $this->attributes['clicklabel'] == 'yes' )
237        {
238            // call this to initialize all attributes
239            $this->getAttributesFor( $this->getFormat() );
240
241            if( !isset( $this->attributes['id'] ) )
242            {
243                patErrorManager::raiseWarning(
244                    PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_ID,
245                    'The "clicklabel" attribute needs the "id" attribute to be set.'
246                );
247            }
248            else if( !isset( $this->attributes['label'] ) )
249            {
250                patErrorManager::raiseWarning(
251                    PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_LABEL,
252                    'The "clicklabel" attribute needs the "label" attribute to be set.'
253                );
254            }
255            else
256            {
257                $this->setAttribute( 'label', $this->createTag( 'label', 'full', array( 'for' => $this->getAttribute( 'id' ), 'title' => $this->getAttribute( 'title' ) ), $this->getAttribute( 'label' ) ) );
258            }
259        }
260       
261        // create element
262        return $this->toHtml();
263    }
264   
265   /**
266    * element creation method for the 'HTML' format in the 'readonly' form mode.
267    *
268    * @access   public
269    * @param    mixed   value of the element
270    * @return   string  $value  The element's value
271    */
272    function serializeHtmlReadonly( $value )
273    {
274        $tag = $this->createDisplaylessTag( $value );
275       
276        if( $this->attributes['display'] == 'no' )
277        {
278            return $tag;
279        }
280
281        return $this->attributes['label'].$tag;
282    }
283   
284   /**
285    * Retrieves the default value to display in the element's readonly mode if the
286    * user has not selected any entry, according to the selected locale
287    *
288    * @access   public
289    * @return   string  $defaultValue   The default readonly value in the needed locale
290    */
291    function getReadonlyDefaultValue()
292    {   
293        $lang   =   $this->locale;
294   
295        if( !isset( $this->defaultReadonlyValue[$lang] ) )
296        {
297            patErrorManager::raiseWarning(
298                PATFORMS_ERROR_RADIO_NO_DEFAULT_VALUE_AVAILABLE,
299                'There is no default readonly value available for the locale "'.$lang.'", using default locale "C" instead.'
300            );
301            return $this->defaultReadonlyValue['C'];
302        }
303       
304        return $this->defaultReadonlyValue[$lang];
305    }
306   
307   /**
308    * validates the element.
309    *
310    * @access   public
311    * @param    mixed   value of the element
312    * @return   bool    $isValid    True if element could be validated, false otherwise.
313    */
314    function validateElement( $value )
315    {
316        if( $this->attributes['required'] == 'yes' && empty( $value ) )
317        {
318            $this->addValidationError( 1 );
319            return false;
320        }
321       
322        return true;
323    }
324}
325
326?>
Note: See TracBrowser for help on using the repository browser.