| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
define( 'PATFORMS_ERROR_RADIO_NO_DEFAULT_VALUE_AVAILABLE', 7001 ); |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
define( 'PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_ID', 7002 ); |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
define( 'PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_LABEL', 7003 ); |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
class 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 |
$namespace = $this->getNamespace(); |
|---|
| 236 |
if( isset( $this->attributes['id'] ) && $namespace ) { |
|---|
| 237 |
if (strpos($this->attributes['id'], $namespace . '_') !== 0) { |
|---|
| 238 |
$this->attributes['id'] = $namespace . '_' . $this->attributes['id']; |
|---|
| 239 |
} |
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
if( isset( $this->attributes['clicklabel'] ) && $this->attributes['clicklabel'] == 'yes' ) |
|---|
| 244 |
{ |
|---|
| 245 |
|
|---|
| 246 |
$this->getAttributesFor( $this->getFormat() ); |
|---|
| 247 |
|
|---|
| 248 |
if( !isset( $this->attributes['id'] ) ) |
|---|
| 249 |
{ |
|---|
| 250 |
patErrorManager::raiseWarning( |
|---|
| 251 |
PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_ID, |
|---|
| 252 |
'The "clicklabel" attribute needs the "id" attribute to be set.' |
|---|
| 253 |
); |
|---|
| 254 |
} |
|---|
| 255 |
else if( !isset( $this->attributes['label'] ) ) |
|---|
| 256 |
{ |
|---|
| 257 |
patErrorManager::raiseWarning( |
|---|
| 258 |
PATFORMS_WARNING_RADIO_CLICKLABEL_NEEDS_LABEL, |
|---|
| 259 |
'The "clicklabel" attribute needs the "label" attribute to be set.' |
|---|
| 260 |
); |
|---|
| 261 |
} |
|---|
| 262 |
else |
|---|
| 263 |
{ |
|---|
| 264 |
$this->setAttribute( 'label', $this->createTag( 'label', 'full', array( 'for' => $this->getAttribute( 'id' ), 'title' => $this->getAttribute( 'title' ) ), $this->getAttribute( 'label' ) ) ); |
|---|
| 265 |
} |
|---|
| 266 |
} |
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
return $this->toHtml(); |
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
* element creation method for the 'HTML' format in the 'readonly' form mode. |
|---|
| 274 |
* |
|---|
| 275 |
* @access public |
|---|
| 276 |
* @param mixed value of the element |
|---|
| 277 |
* @return string $value The element's value |
|---|
| 278 |
*/ |
|---|
| 279 |
function serializeHtmlReadonly( $value ) |
|---|
| 280 |
{ |
|---|
| 281 |
$tag = $this->createDisplaylessTag( $value ); |
|---|
| 282 |
|
|---|
| 283 |
if( $this->attributes['display'] == 'no' ) |
|---|
| 284 |
{ |
|---|
| 285 |
return $tag; |
|---|
| 286 |
} |
|---|
| 287 |
|
|---|
| 288 |
return $this->attributes['label'].$tag; |
|---|
| 289 |
} |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
* Retrieves the default value to display in the element's readonly mode if the |
|---|
| 293 |
* user has not selected any entry, according to the selected locale |
|---|
| 294 |
* |
|---|
| 295 |
* @access public |
|---|
| 296 |
* @return string $defaultValue The default readonly value in the needed locale |
|---|
| 297 |
*/ |
|---|
| 298 |
function getReadonlyDefaultValue() |
|---|
| 299 |
{ |
|---|
| 300 |
$lang = $this->locale; |
|---|
| 301 |
|
|---|
| 302 |
if( !isset( $this->defaultReadonlyValue[$lang] ) ) |
|---|
| 303 |
{ |
|---|
| 304 |
patErrorManager::raiseWarning( |
|---|
| 305 |
PATFORMS_ERROR_RADIO_NO_DEFAULT_VALUE_AVAILABLE, |
|---|
| 306 |
'There is no default readonly value available for the locale "'.$lang.'", using default locale "C" instead.' |
|---|
| 307 |
); |
|---|
| 308 |
return $this->defaultReadonlyValue['C']; |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
return $this->defaultReadonlyValue[$lang]; |
|---|
| 312 |
} |
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
* validates the element. |
|---|
| 316 |
* |
|---|
| 317 |
* @access public |
|---|
| 318 |
* @param mixed value of the element |
|---|
| 319 |
* @return bool $isValid True if element could be validated, false otherwise. |
|---|
| 320 |
*/ |
|---|
| 321 |
function validateElement( $value ) |
|---|
| 322 |
{ |
|---|
| 323 |
if ($this->attributes['required'] == 'yes' && strlen($value) === 0) { |
|---|
| 324 |
$this->addValidationError( 1 ); |
|---|
| 325 |
return false; |
|---|
| 326 |
} |
|---|
| 327 |
return true; |
|---|
| 328 |
} |
|---|
| 329 |
} |
|---|
| 330 |
?> |
|---|