[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file form_field_select.class.php
Documentation is available at
form_field_select.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Form_Field_Select
*
*
@package
tangra_lib
*
@subpackage
form
*/
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'form/fields/form_field.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_select_value_already_exists.class.php'
)
;
/**
* Represents select field
*
*
@package
tangra_lib
*
@subpackage
form
*/
class
Form_Field_Select
extends
Form_Field
{
/**
* Array that contains options
*
*
@var
array
*
@internal
*/
protected
$options
=
array
(
)
;
/**
* Empty value
*
*
@var
integer
*
@internal
*/
protected
$empty_value
=
NULL
;
/**
* Constructor
*
*
@param
string
$name
Name of the field
*
@param
integer
$value
Default value
*
@param
boolean
$required
Is field required. Default is false
*
@param
integer
$empty_value
Value of empty value
*/
function
__construct
(
$name
,
$value
=
NULL
,
$required
=
false
,
$empty_value
=
NULL
)
{
parent
::
__construct
(
$name
,
$value
,
$required
)
;
$this
->
set_empty_value
(
$empty_value
)
;
$this
->
add_potential_error
(
'invalid_value'
)
;
}
/**
* Sets empty value
*
*
@param
integer
$empty_value
*/
public
function
set_empty_value
(
$empty_value
)
{
$this
->
empty_value
=
$empty_value
;
}
/**
* Returns empty value
*
*
@return
unknown
*/
public
function
get_empty_value
(
)
{
return
$this
->
empty_value
;
}
/**
* Adds new option
*
*
@param
integer
$value
Value of the option
*
@throws
TE_Select_Value_Already_Exists
*/
public
function
add_option
(
$value
)
{
if
(
$this
->
is_unique_value
(
$value
))
{
if
(
$value
==
$this
->
get_value
(
))
{
$selected
=
true
;
}
else
{
$selected
=
false
;
}
$c
=
count
(
$this
->
options
)
;
$this
->
options
[
$c
]
[
'value'
]
=
$value
;
$this
->
options
[
$c
]
[
'selected'
]
=
$selected
;
$this
->
options
[
$c
]
[
'html_value'
]
=
$c
+
1
;
}
else
{
throw
new
TE_Select_Value_Already_Exists
(
$value
)
;
}
}
/**
* Sets all options at once using array
*
*
@param
array
$options_arr
*/
public
function
set_options
(
$options_arr
)
{
$this
->
options
=
array
(
)
;
if
(
!
is_array
(
$options_arr
))
{
throw
new
Tangra_Exception
(
'$options_arr parameter is not an array'
)
;
}
foreach
(
$options_arr
as
$key
=>
$value
)
{
$this
->
add_option
(
$value
)
;
}
}
/**
* Performs basic check for validity
*
*
@return
boolean
*
@internal
*/
public
function
basic_check
(
)
{
$has_errors
=
parent
::
basic_check
(
)
;
$ok
=
false
;
if
(
!
$has_errors
)
{
$current_value
=
null
;
foreach
(
$this
->
options
as
$option
)
{
if
(
$option
[
'html_value'
]
==
$this
->
get_html_value
(
))
{
$current_value
=
$option
[
'value'
]
;
$ok
=
true
;
break
;
}
}
if
(
$this
->
get_html_value
(
)
==
$current_value
)
{
$ok
=
true
;
}
if
(
!
$ok
)
{
$this
->
set_error
(
'invalid_value'
)
;
$has_errors
=
true
;
}
else
{
if
(
$this
->
get_empty_value
(
)
!==
NULL
)
{
if
(
$current_value
==
$this
->
get_empty_value
(
)
&&
$this
->
get_required
(
))
{
$this
->
set_value
(
$this
->
get_empty_value
(
))
;
$this
->
set_error
(
'mandatory_missing'
)
;
$has_errors
=
true
;
}
}
}
}
return
$has_errors
;
}
/**
* Sets current value
*
*
@param
integer
$value
*/
public
function
set_value
(
$value
)
{
$this
->
_set_value
(
$value
)
;
$this
->
set_option_selected
(
$value
)
;
}
/**
* Returns array with field properties
*
*
@return
array
*
@internal
*/
public
function
get_properties_array
(
)
{
$ret
=
parent
::
get_properties_array
(
)
;
$ret
[
'options'
]
=
$this
->
options
;
return
$ret
;
}
/**
* Returns all options as array
*
*
@return
array
*/
public
function
get_options
(
)
{
return
$this
->
options
;
}
/**
* Sets current value
*
*
@param
integer
$value
Value of the option
*
@internal
*/
protected
function
_set_value
(
$value
)
{
parent
::
set_value
(
$value
)
;
}
/**
* marks option as selected
*
*
@param
integer
$value
Value of the option
*
@internal
*/
private
function
set_option_selected
(
$value
)
{
foreach
(
$this
->
options
as
&
$option
)
{
if
(
$option
[
'value'
]
!=
$value
)
{
$option
[
'selected'
]
=
false
;
}
else
{
$option
[
'selected'
]
=
true
;
}
}
}
/**
* Checks if there is option with same value
*
*
@param
integer
$value
Value of the option
*
@return
boolean
*
@internal
*/
private
function
is_unique_value
(
$value
)
{
$ret
=
true
;
foreach
(
$this
->
options
as
$option
)
{
if
(
$value
==
$option
[
'value'
]
)
{
$ret
=
false
;
break
;
}
}
return
$ret
;
}
/**
* Transfers data from html_value to value
*
@internal
*/
protected
function
translate_value_html2app
(
)
{
foreach
(
$this
->
options
as
$option
)
{
if
(
$option
[
'html_value'
]
==
$this
->
get_html_value
(
))
{
$this
->
set_value
(
$option
[
'value'
]
)
;
break
;
}
}
}
/**
* Sets default value
*
* Makes option with that value selected
*
*
@param
mixed
$default_value
*/
public
function
set_default_value
(
$default_value
)
{
parent
::
set_default_value
(
$default_value
)
;
$this
->
set_option_selected
(
$default_value
)
;
}
}