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

Revision 17, 9.7 KB checked in by argh, 9 years ago (diff)

Implemented the clicklabel attribute functionality

  • 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 required, please complete it.",
200            2   =>  "The value given for the element does not match any of the possible values.",
201        ),
202        "de" => array(
203            1   =>  "Pflichtfeld. Bitte vervollständigen Sie Ihre Angabe.",
204            2   =>  "Der angegebene Wert stimmt mit keinem der möglichen Werte überein.",
205        ),
206        "fr" => array(
207            1   =>  "Ce champ est obligatoire.",
208            2   =>  "La valeur de ce champ ne correspond à aucune des valeurs admises.",
209        )
210    );
211   
212    var $defaultReadonlyValue  =   array(
213        "C" =>  "No selection",
214        "de" => "Keine Angabe",
215        "fr" => "Pas de sélection.",
216    );
217
218   /**
219    * element creation method for the 'HTML' format in the 'default' form mode.
220    *
221    * @access   public
222    * @param    mixed   value of the element
223    * @return   mixed   $element    The element, or false if failed.
224    */
225    function serializeHtmlDefault( $value )
226    {
227        if( $value == $this->attributes["value"] )
228        {
229            $this->attributes['checked'] = 'checked';
230        }
231       
232        $this->attributes["type"]   =   "radio";
233       
234        if( $this->submitted && !$this->valid )
235        {
236            // do what?
237        }
238       
239        if( isset( $this->attributes["edit"] ) && $this->attributes["edit"] == "no" )
240        {
241            $this->attributes['disabled']   =   'yes';
242        }
243       
244        // clicklabel attribute
245        if( isset( $this->attributes['clicklabel'] ) && $this->attributes['clicklabel'] == 'yes' )
246        {
247            // call this to initialize all attributes
248            $this->getAttributesFor( $this->getFormat() );
249
250            if( !isset( $this->attributes['id'] ) )
251            {
252                patErrorManager::raiseWarning(
253                    PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_ID,
254                    'The "clicklabel" attribute needs the "id" attribute to be set.'
255                );
256            }
257            else if( !isset( $this->attributes['label'] ) )
258            {
259                patErrorManager::raiseWarning(
260                    PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_LABEL,
261                    'The "clicklabel" attribute needs the "label" attribute to be set.'
262                );
263            }
264            else
265            {
266                $this->setAttribute( 'label', $this->createTag( 'label', 'full', array( 'for' => $this->getAttribute( 'id' ), 'title' => $this->getAttribute( 'title' ) ), $this->getAttribute( 'label' ) ) );
267            }
268        }
269       
270        // create element
271        $element = $this->toHtml();
272        if( $element === false )
273        {
274            return patErrorManager::raiseError( 
275                PATFORMS_ERROR_NO_HTML_CONTENT, 
276                'Could not get html contents.'
277            );
278        }
279
280        // and return to sender...
281        return $element;
282    }
283   
284   /**
285    * element creation method for the 'HTML' format in the 'readonly' form mode.
286    *
287    * @access   public
288    * @param    mixed   value of the element
289    * @return   string  $value  The element's value
290    */
291    function serializeHtmlReadonly( $value )
292    {
293        $display    =   $value;
294       
295        // call this to initialize all attributes
296        $this->getAttributesFor( $this->getFormat() );
297   
298        return $display.$this->createHiddenTag( $value );
299    }
300   
301   /**
302    * Retrieves the default value to display in the element's readonly mode if the
303    * user has not selected any entry, according to the selected locale
304    *
305    * @access   public
306    * @return   string  $defaultValue   The default readonly value in the needed locale
307    */
308    function getReadonlyDefaultValue()
309    {   
310        $lang   =   $this->locale;
311   
312        if( !isset( $this->defaultReadonlyValue[$lang] ) )
313        {
314            patErrorManager::raiseWarning(
315                PATFORMS_ERROR_RADIO_NO_DEFAULT_VALUE_AVAILABLE,
316                'There is no default readonly value available for the locale "'.$lang.'", using default locale "C" instead.'
317            );
318            return $this->defaultReadonlyValue['C'];
319        }
320       
321        return $this->defaultReadonlyValue[$lang];
322    }
323   
324   /**
325    * validates the element.
326    *
327    * @access   public
328    * @param    mixed   value of the element
329    * @return   bool    $isValid    True if element could be validated, false otherwise.
330    */
331    function validateElement( $value )
332    {
333        return true;
334    }
335}
336
337?>
Note: See TracBrowser for help on using the repository browser.