[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file form_view.class.php
Documentation is available at
form_view.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Form_View
*
*
@package
tangra_lib
*
@subpackage
form
*/
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'interfaces/i_static_viewable.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'web_site/tple_exports.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'form/fields/form_field_view.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_key_already_exists.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_key_not_exists.class.php'
)
;
/**
* Form_View is used for static HTML representation of Form objects
*
*
@package
tangra_lib
*
@subpackage
form
*/
class
Form_View
extends
Tangra_Class
implements
I_Static_Viewable
{
/**
* Form for which is current view
*
*
@var
Form
*
@internal
*/
private
$form
;
/**
* Views of form fields
*
*
@var
array
*
@internal
*
*/
private
$fields_views
=
array
(
)
;
/**
* Charset that will be used to prepare data of the fields (escaping with htmleentities)
* Default value is UTF-8
*
@var
string
*/
protected
$charset
=
'UTF-8'
;
/**
* Constructor
*
*
@param
Form
$form
*/
function
__construct
(
Form
&
$form
)
{
$this
->
form
=
$form
;
}
/**
* Returns TPLE exports of the form
*
* Returns object of class TPLE_Exports that contains all "assigns" required for
* correct HTML presentation of the form
*
*
@return
TPLE_Exports
*/
public
function
get_tple_exports
(
)
{
$exports
=
new
TPLE_Exports
(
)
;
$fields
=
$this
->
form
->
get_fields
(
)
;
foreach
(
$fields
as
$f
)
{
$exports_tmp
=
$this
->
get_field_view_exports
(
$f
->
get_name
(
))
;
$exports
->
merge
(
$exports_tmp
)
;
}
$form_name
=
$this
->
form
->
get_name
(
)
;
return
$exports
;
}
/**
* Adds field's view object
*
* Each form field have to have own field view object.
*
*
@param
strinfg$field_name
Name of the field
*
@param
Form_Field_View
$field_view
- view object
*
@throws
TE_Key_Already_Exists
*/
public
function
add_field_view
(
$field_name
,
Form_Field_View
$field_view
)
{
if
(
!
array_key_exists
(
$field_name
,
$this
->
fields_views
))
{
$this
->
fields_views
[
$field_name
]
=
$field_view
;
}
else
{
throw
new
TE_Key_Already_Exists
(
$field_name
)
;
}
}
/**
* Returns exports of field view
*
*
@param
string
$field_name
Name of the field
*
@return
TPLE_Exports
*
@internal
*/
private
function
get_field_view_exports
(
$field_name
)
{
if
(
array_key_exists
(
$field_name
,
$this
->
fields_views
))
{
$exports
=
$this
->
fields_views
[
$field_name
]
->
get_tple_exports
(
)
;
}
else
{
throw
new
TE_Key_Not_Exists
(
$field_name
)
;
}
return
$exports
;
}
/**
* Returns Form_Field_View object of field specified with $field_view
*
*
@param
string
$field_name
Name of the field
*
@return
Form_Field_View
*/
public
function
&
get_field_view
(
$field_name
)
{
if
(
array_key_exists
(
$field_name
,
$this
->
fields_views
))
{
return
$this
->
fields_views
[
$field_name
]
;
}
else
{
throw
new
TE_Key_Not_Exists
(
$field_name
)
;
}
}
/**
* Sets charset that field views will use for escaping field data (i.e. htmlentities)
*
*
@param
string
$charset
*/
public
function
set_charset
(
$charset
)
{
$this
->
charset
=
$charset
;
//TODO - check against list of usable charsets
}
/**
* Returns charset
*
*
@return
string
*/
public
function
get_charset
(
)
{
return
$this
->
charset
;
}
/**
* Sets charset of the form view and cycles all field views to pass them the charset (calling their set_charset method)
* Default value for parameter $charset is UTF-8
*
@param
string
$charset
*/
public
function
set_charset_cascade
(
$charset
=
'UTF-8'
)
{
$this
->
set_charset
(
$charset
)
;
foreach
(
$this
->
fields_views
as
&
$fv
)
{
$fv
->
set_charset
(
$charset
)
;
}
}
}