[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file form_field_radio_group.class.php
Documentation is available at
form_field_radio_group.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Form_Field_Radio_Group
*
*
@package
tangra_lib
*
@subpackage
form
*/
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'form/fields/form_field.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_radio_value_already_exists.class.php'
)
;
/**
* Represents group of radio buttons
*
*
@package
tangra_lib
*
@subpackage
form
*/
class
Form_Field_Radio_Group
extends
Form_Field
{
/**
* Array that contains radio buttons
*
*
@var
array
*
@internal
*/
private
$radios
=
array
(
)
;
/**
* Constructor
*
*
@param
string
$name
Name of the field
*
@param
integer
$value
Default value.
*
@param
boolean
$required
Is field required. Default is false
*/
function
__construct
(
$name
,
$value
=
NULL
,
$required
=
false
)
{
parent
::
__construct
(
$name
,
$value
,
$required
)
;
$this
->
add_potential_error
(
'invalid_value'
)
;
}
/**
* Adds radio button
*
*
@param
integer
$value
Value of the radio button
*
@throws
TE_Radio_Value_Already_Exists
*/
public
function
add_radio
(
$value
)
{
if
(
$this
->
is_unique_value
(
$value
))
{
if
(
$value
==
$this
->
get_value
(
))
{
$checked
=
true
;
}
else
{
$checked
=
false
;
}
$c
=
count
(
$this
->
radios
)
;
$this
->
radios
[
$c
]
[
'value'
]
=
$value
;
$this
->
radios
[
$c
]
[
'checked'
]
=
$checked
;
$this
->
radios
[
$c
]
[
'html_value'
]
=
$c
+
1
;
}
else
{
throw
new
TE_Radio_Value_Already_Exists
(
$value
)
;
}
}
/**
* Sets all radios at once using array
*
*
@param
array
$radios_arr
*/
public
function
set_radios
(
$radios_arr
)
{
$this
->
radios
=
array
(
)
;
foreach
(
$radios_arr
as
$key
=>
$value
)
{
$this
->
add_radio
(
$value
)
;
}
}
/**
* Performs basic check for validity
*
*
@return
boolean
*
@internal
*/
public
function
basic_check
(
)
{
$has_errors
=
parent
::
basic_check
(
)
;
$ok
=
false
;
if
(
!
$has_errors
)
{
if
(
$this
->
get_required
(
))
{
$ok
=
$this
->
is_value_valid
(
$this
->
get_html_value
(
))
;
if
(
!
$ok
)
{
$this
->
set_error
(
'invalid_value'
)
;
$has_errors
=
true
;
}
}
else
{
if
(
$this
->
get_html_value
(
)
!=
NULL
)
{
$ok
=
$this
->
is_value_valid
(
$this
->
get_html_value
(
))
;
if
(
!
$ok
)
{
$this
->
set_error
(
'invalid_value'
)
;
$has_errors
=
true
;
}
}
}
}
return
$has_errors
;
}
/**
* Checks if there is radio button with that value
*
*
@param
integer
$value
*
@return
unknown
*/
private
function
is_value_valid
(
$value
)
{
$ret
=
false
;
foreach
(
$this
->
radios
as
$radio
)
{
if
(
$radio
[
'html_value'
]
==
$value
)
{
$ret
=
true
;
break
;
}
}
return
$ret
;
}
/**
* Sets value
*
*
@param
integer
$value
*/
public
function
set_value
(
$value
)
{
parent
::
set_value
(
$value
)
;
$this
->
set_radio_checked
(
$value
)
;
}
/**
* Returns array with field properties
*
*
@return
array
*
@internal
*/
public
function
get_properties_array
(
)
{
$ret
=
parent
::
get_properties_array
(
)
;
$ret
[
'radios'
]
=
$this
->
radios
;
$ret
[
'missing'
]
=
true
;
return
$ret
;
}
/**
* Returns all radios as array
*
*
@return
array
*/
public
function
get_radios
(
)
{
return
$this
->
radios
;
}
/**
* Checks if there is a radio with same value
*
*
@param
integer
$value
*
@return
boolean
*
@internal
*/
private
function
is_unique_value
(
$value
)
{
$ret
=
true
;
foreach
(
$this
->
radios
as
$radio
)
{
if
(
$value
==
$radio
[
'value'
]
)
{
$ret
=
false
;
break
;
}
}
return
$ret
;
}
/**
* Marks radio as checked
*
*
@param
integer
$value
Value of radio that will be marked as checked
*/
private
function
set_radio_checked
(
$value
)
{
foreach
(
$this
->
radios
as
&
$radio
)
{
if
(
$radio
[
'value'
]
!=
$value
)
{
$radio
[
'checked'
]
=
false
;
}
else
{
$radio
[
'checked'
]
=
true
;
}
}
}
/**
* Transfers data from html_value to value
*
@internal
*/
protected
function
translate_value_html2app
(
)
{
foreach
(
$this
->
radios
as
$radio
)
{
if
(
$radio
[
'html_value'
]
==
$this
->
get_html_value
(
))
{
$this
->
set_value
(
$radio
[
'value'
]
)
;
break
;
}
}
}
}