[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file form_field_select_multiple.class.php
Documentation is available at
form_field_select_multiple.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Form_Field_Date
*
*
@package
tangra_lib
*
@subpackage
form
*/
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'form/fields/select/form_field_select.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_select_multiple_option_not_exists.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_select_multiple_value_not_an_array.class.php'
)
;
/**
* Represents select field with multiple choice
*
*
@package
tangra_lib
*
@subpackage
form
*/
class
Form_Field_Select_Multiple
extends
Form_Field_Select
{
/**
* Enter description here...
*
*
@param
string
$name
Name of the field
*
@param
integer
$value
Array containing option values to be selected
*
@param
boolean
$required
Is field required. Default is false
*/
function
__construct
(
$name
,
$value
=
NULL
,
$required
=
false
)
{
parent
::
__construct
(
$name
,
$value
,
$required
)
;
}
/**
* Sets which options are selected
*
*
@param
array
$value
Array that contains values of options to be selected
*
@throws
TE_Select_Multiple_Value_Not_An_Array
*/
public
function
set_value
(
$value
)
{
if
(
$value
!=
NULL
)
{
if
(
is_array
(
$value
))
{
parent
::
set_value
(
array
(
))
;
$tmp_value
=
array
(
)
;
foreach
(
$value
as
$v
)
{
$this
->
select_option
(
$v
)
;
array_push
(
$tmp_value
,
$v
)
;
}
parent
::
_set_value
(
$tmp_value
)
;
}
else
{
throw
new
TE_Select_Multiple_Value_Not_An_Array
(
)
;
}
}
else
{
$this
->
deselect_all_options
(
)
;
}
}
/**
* Marks option as selected
*
*
@param
integer
$value
Value of the option
*
@throws
TE_Select_Multiple_Option_Not_Exists
*/
public
function
select_option
(
$value
)
{
$ok
=
false
;
foreach
(
$this
->
options
as
&
$option
)
{
if
(
$option
[
'value'
]
==
$value
)
{
$option
[
'selected'
]
=
true
;
$ok
=
true
;
break
;
}
}
if
(
!
$ok
)
{
throw
new
TE_Select_Multiple_Option_Not_Exists
(
'Value: '
.
$value
)
;
}
}
/**
* Marks options as not selected
*
*
@param
unknown_type
$value
Value of the option
*
@throws
TE_Select_Multiple_Option_Not_Exists
*/
public
function
deselect_option
(
$value
)
{
$ok
=
false
;
foreach
(
$this
->
options
as
&
$option
)
{
if
(
$option
[
'value'
]
==
$value
)
{
$option
[
'selected'
]
=
false
;
$ok
=
true
;
break
;
}
}
if
(
!
$ok
)
{
throw
new
TE_Select_Multiple_Option_Not_Exists
(
'Value: '
.
$value
)
;
}
}
/**
* Captures submit
*
*
@param
unknown_type
$form_name
*
@param
unknown_type
$submit_array
*
@internal
*/
public
function
capture_submit
(
$form_name
,
&
$submit_array
)
{
if
(
array_key_exists
(
$form_name
.
'_'
.
$this
->
get_name
(
)
,
$submit_array
))
{
$this
->
set_html_value
(
$submit_array
[
$form_name
.
'_'
.
$this
->
get_name
(
)
]
)
;
}
else
{
$this
->
set_html_value
(
array
(
))
;
}
}
/**
* Performs basic check for validity
*
*
@return
boolean
*
@internal
*/
public
function
basic_check
(
)
{
$has_errors
=
false
;
if
(
$this
->
get_required
(
)
&&
$this
->
get_html_value
(
)
==
NULL
)
{
$has_errors
=
true
;
$this
->
set_error
(
'mandatory_missing'
)
;
}
else
{
$this
->
clear_errors
(
)
;
}
return
$has_errors
;
}
/**
* Transfers data from html_value to value
*
@internal
*/
public
function
translate_value_html2app
(
)
{
$tmp_value
=
array
(
)
;
$this
->
deselect_all_options
(
)
;
if
(
count
(
$this
->
get_html_value
(
)))
{
foreach
(
$this
->
get_html_value
(
)
as
$html_value
)
{
array_push
(
$tmp_value
,
$this
->
options
[
$this
->
find_option_by_html_value
(
$html_value
)
]
[
'value'
]
)
;
// $this->select_option($this->options[$this->find_option_by_html_value($html_value)]['value']);
}
}
else
{
$tmp_value
=
array
(
)
;
}
$this
->
set_value
(
$tmp_value
)
;
}
/**
* Deselects all options
*
@internal
*
*/
private
function
deselect_all_options
(
)
{
foreach
(
$this
->
options
as
$option
)
{
$this
->
deselect_option
(
$option
[
'value'
]
)
;
}
}
/**
* Returns option value for option with html_value = <var>$html_value</var>
*
*
@param
integer
$html_value
*
@return
integer
*/
private
function
find_option_by_html_value
(
$html_value
)
{
$ret
=
false
;
foreach
(
$this
->
options
as
$key
=>
$option
)
{
if
(
$option
[
'html_value'
]
==
$html_value
)
{
$ret
=
$key
;
break
;
}
}
if
(
$ret
===
false
)
{
throw
new
TE_Select_Multiple_Option_Not_Exists
(
'html_value: '
.
$html_value
)
;
}
return
$ret
;
}
}