| 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 | define( 'PATFORMS_ERROR_RADIO_NO_DEFAULT_VALUE_AVAILABLE', 7001 ); |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * simple radiobutton patForms element that builds and validates radio buttons, with the |
|---|
| 18 | * particularity that it does not generate a fully serialized element, but an array with |
|---|
| 19 | * serialized subelements. |
|---|
| 20 | * |
|---|
| 21 | * $Id$ |
|---|
| 22 | * |
|---|
| 23 | * @access protected |
|---|
| 24 | * @package patForms |
|---|
| 25 | * @subpackage patForms_Element |
|---|
| 26 | * @author Sebastian Mordziol <argh@php-tools.net> |
|---|
| 27 | * @license LGPL, see license.txt for details |
|---|
| 28 | */ |
|---|
| 29 | class patForms_Element_Radio extends patForms_Element |
|---|
| 30 | { |
|---|
| 31 | /** |
|---|
| 32 | * Stores the name of the element - this is used mainly by the patForms |
|---|
| 33 | * error management and should be set in every element class. |
|---|
| 34 | * @access public |
|---|
| 35 | */ |
|---|
| 36 | var $elementName = 'Radio'; |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * the type of the element - set this to the type of element you are creating |
|---|
| 40 | * if you want to use the {@link patForms_Element::element2html()} method to |
|---|
| 41 | * create the final HTML tag for your element. |
|---|
| 42 | * |
|---|
| 43 | * @access public |
|---|
| 44 | * @see patForms_Element::element2html() |
|---|
| 45 | */ |
|---|
| 46 | var $elementType = array( "html" => "input", |
|---|
| 47 | ); |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * The radio element uses a renderer to serialize its content, so we set the flag |
|---|
| 51 | * to true here |
|---|
| 52 | * |
|---|
| 53 | * @access private |
|---|
| 54 | * @var boolean |
|---|
| 55 | */ |
|---|
| 56 | var $usesRenderer = false; |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * set here which attributes you want to include in the element if you want to use |
|---|
| 60 | * the {@link patForms_Element::convertDefinition2Attributes()} method to automatically |
|---|
| 61 | * convert the values from your element definition into element attributes. |
|---|
| 62 | * |
|---|
| 63 | * @access protected |
|---|
| 64 | * @see patForms_Element::convertDefinition2Attribute() |
|---|
| 65 | */ |
|---|
| 66 | var $attributeDefinition = array( |
|---|
| 67 | |
|---|
| 68 | "id" => array( "required" => false, |
|---|
| 69 | "format" => "string", |
|---|
| 70 | "outputFormats" => array( "html" ), |
|---|
| 71 | ), |
|---|
| 72 | "name" => array( "required" => true, |
|---|
| 73 | "format" => "string", |
|---|
| 74 | "outputFormats" => array( "html" ), |
|---|
| 75 | ), |
|---|
| 76 | "type" => array( "required" => true, |
|---|
| 77 | "format" => "string", |
|---|
| 78 | "outputFormats" => array( "html" ), |
|---|
| 79 | ), |
|---|
| 80 | "title" => array( "required" => false, |
|---|
| 81 | "format" => "string", |
|---|
| 82 | "outputFormats" => array( "html" ), |
|---|
| 83 | "modifiers" => array( "insertSpecials" => array() ), |
|---|
| 84 | ), |
|---|
| 85 | "description" => array( "required" => false, |
|---|
| 86 | "format" => "string", |
|---|
| 87 | "outputFormats" => array(), |
|---|
| 88 | "modifiers" => array( "insertSpecials" => array() ), |
|---|
| 89 | ), |
|---|
| 90 | "default" => array( "required" => false, |
|---|
| 91 | "format" => "string", |
|---|
| 92 | "outputFormats" => array(), |
|---|
| 93 | ), |
|---|
| 94 | "label" => array( "required" => false, |
|---|
| 95 | "format" => "string", |
|---|
| 96 | "outputFormats" => array(), |
|---|
| 97 | ), |
|---|
| 98 | "display" => array( "required" => false, |
|---|
| 99 | "format" => "string", |
|---|
| 100 | "default" => "yes", |
|---|
| 101 | "outputFormats" => array(), |
|---|
| 102 | ), |
|---|
| 103 | "edit" => array( "required" => false, |
|---|
| 104 | "format" => "string", |
|---|
| 105 | "default" => "yes", |
|---|
| 106 | "outputFormats" => array(), |
|---|
| 107 | ), |
|---|
| 108 | "required" => array( "required" => false, |
|---|
| 109 | "format" => "string", |
|---|
| 110 | "default" => "yes", |
|---|
| 111 | "outputFormats" => array(), |
|---|
| 112 | ), |
|---|
| 113 | "value" => array( "required" => false, |
|---|
| 114 | "format" => "string", |
|---|
| 115 | "outputFormats" => array( 'html' ), |
|---|
| 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 | "position" => array( "required" => false, |
|---|
| 156 | "format" => "int", |
|---|
| 157 | "outputFormats" => array(), |
|---|
| 158 | ), |
|---|
| 159 | "values" => array( "required" => false, |
|---|
| 160 | "format" => "values", |
|---|
| 161 | "outputFormats" => array(), |
|---|
| 162 | ), |
|---|
| 163 | "disabled" => array( "required" => false, |
|---|
| 164 | "format" => "string", |
|---|
| 165 | "default" => "no", |
|---|
| 166 | "outputFormats" => array( "html" ), |
|---|
| 167 | ), |
|---|
| 168 | "clicklabel" => array( "required" => false, |
|---|
| 169 | "format" => "string", |
|---|
| 170 | "outputFormats" => array(), |
|---|
| 171 | ), |
|---|
| 172 | "checked" => array( "required" => false, |
|---|
| 173 | "format" => "string", |
|---|
| 174 | "outputFormats" => array( 'html' ), |
|---|
| 175 | ), |
|---|
| 176 | ); |
|---|
| 177 | |
|---|
| 178 | /** |
|---|
| 179 | * define error codes an messages for each form element |
|---|
| 180 | * |
|---|
| 181 | * @access private |
|---|
| 182 | * @var array $validatorErrorCodes |
|---|
| 183 | */ |
|---|
| 184 | var $validatorErrorCodes = array( |
|---|
| 185 | "C" => array( |
|---|
| 186 | 1 => "This field is required, please complete it.", |
|---|
| 187 | 2 => "The value given for the element does not match any of the possible values.", |
|---|
| 188 | ), |
|---|
| 189 | "de" => array( |
|---|
| 190 | 1 => "Pflichtfeld. Bitte vervollständigen Sie Ihre Angabe.", |
|---|
| 191 | 2 => "Der angegebene Wert stimmt mit keinem der möglichen Werte überein.", |
|---|
| 192 | ), |
|---|
| 193 | "fr" => array( |
|---|
| 194 | 1 => "Ce champ est obligatoire.", |
|---|
| 195 | 2 => "La valeur de ce champ ne correspond à aucune des valeurs admises.", |
|---|
| 196 | ) |
|---|
| 197 | ); |
|---|
| 198 | |
|---|
| 199 | var $defaultReadonlyValue = array( |
|---|
| 200 | "C" => "No selection", |
|---|
| 201 | "de" => "Keine Angabe", |
|---|
| 202 | "fr" => "Pas de sélection.", |
|---|
| 203 | ); |
|---|
| 204 | |
|---|
| 205 | /** |
|---|
| 206 | * element creation method for the 'HTML' format in the 'default' form mode. |
|---|
| 207 | * |
|---|
| 208 | * @access public |
|---|
| 209 | * @param mixed value of the element |
|---|
| 210 | * @return mixed $element The element, or false if failed. |
|---|
| 211 | */ |
|---|
| 212 | function serializeHtmlDefault( $value ) |
|---|
| 213 | { |
|---|
| 214 | if( $value == $this->attributes["value"] ) |
|---|
| 215 | { |
|---|
| 216 | $this->attributes['checked'] = 'checked'; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | $this->attributes["type"] = "radio"; |
|---|
| 220 | |
|---|
| 221 | if( $this->submitted && !$this->valid ) |
|---|
| 222 | { |
|---|
| 223 | // do what? |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | if( isset( $this->attributes["edit"] ) && $this->attributes["edit"] == "no" ) |
|---|
| 227 | { |
|---|
| 228 | $this->attributes['disabled'] = 'yes'; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | // create element |
|---|
| 232 | $element = $this->toHtml(); |
|---|
| 233 | if( $element === false ) |
|---|
| 234 | { |
|---|
| 235 | return patErrorManager::raiseError( |
|---|
| 236 | PATFORMS_ERROR_NO_HTML_CONTENT, |
|---|
| 237 | "Could not get html contents." |
|---|
| 238 | ); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | // and return to sender... |
|---|
| 242 | return $element; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | /** |
|---|
| 246 | * element creation method for the 'HTML' format in the 'readonly' form mode. |
|---|
| 247 | * |
|---|
| 248 | * @access public |
|---|
| 249 | * @param mixed value of the element |
|---|
| 250 | * @return string $value The element's value |
|---|
| 251 | */ |
|---|
| 252 | function serializeHtmlReadonly( $value ) |
|---|
| 253 | { |
|---|
| 254 | $display = $value; |
|---|
| 255 | |
|---|
| 256 | $this->getAttributesFor( $this->getFormat() ); |
|---|
| 257 | |
|---|
| 258 | return $display.$this->createHiddenTag( $value ); |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | /** |
|---|
| 262 | * Retrieves the default value to display in the element's readonly mode if the |
|---|
| 263 | * user has not selected any entry, according to the selected locale |
|---|
| 264 | * |
|---|
| 265 | * @access public |
|---|
| 266 | * @return string $defaultValue The default readonly value in the needed locale |
|---|
| 267 | */ |
|---|
| 268 | function getReadonlyDefaultValue() |
|---|
| 269 | { |
|---|
| 270 | $lang = $this->locale; |
|---|
| 271 | |
|---|
| 272 | if( !isset( $this->defaultReadonlyValue[$lang] ) ) |
|---|
| 273 | { |
|---|
| 274 | patErrorManager::raiseWarning( |
|---|
| 275 | PATFORMS_ERROR_RADIO_NO_DEFAULT_VALUE_AVAILABLE, |
|---|
| 276 | 'There is no default readonly value available for the locale "'.$lang.'", using default locale "C" instead.' |
|---|
| 277 | ); |
|---|
| 278 | return $this->defaultReadonlyValue['C']; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | return $this->defaultReadonlyValue[$lang]; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | /** |
|---|
| 285 | * validates the element. |
|---|
| 286 | * |
|---|
| 287 | * @access public |
|---|
| 288 | * @param mixed value of the element |
|---|
| 289 | * @return bool $isValid True if element could be validated, false otherwise. |
|---|
| 290 | */ |
|---|
| 291 | function validateElement( $value ) |
|---|
| 292 | { |
|---|
| 293 | return true; |
|---|
| 294 | } |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | ?> |
|---|