[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file static_html_form_generator.class.php
Documentation is available at
static_html_form_generator.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Static_HTML_Form_Generator
*
*
@package
tangra_lib
*
@subpackage
form
*/
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'form/fields/hidden/form_field_hidden.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'
)
;
/**
* Generates HTML for "static" forms
*
* Static forms are forms that once created remain the same in temrs of count of fields and their type.
* In contrary, dynamic form may can be manipulated at runtime and receive new fields (with usual add_field() method), or remove fields from it.
*
*
@package
tangra_lib
*
@subpackage
form
*/
class
Static_HTML_Form_Generator
extends
Tangra_Class
{
/**
* Form object
*
*
@var
Form
*
@internal
*/
private
$form
;
/**
* Path to template dir
*
*
@var
string
*/
private
$tpl_path
;
/**
* Form language (in term of native language)
*
*
@var
string
*/
private
$language
;
/**
* Map for fields templates
*
*
@var
array
*/
private
$fields_tpl_map
=
array
(
)
;
/**
* Map for fields' errors templates
*
*
@var
array
*/
private
$errs_tpl_map
=
array
(
)
;
/**
* Constructor
*
*
@param
Form
$form
*
@param
string
$tpl_path
*
@param
string
$language
*/
function
__construct
(
Form
$form
,
$tpl_path
,
$language
)
{
$this
->
tpl_path
=
tangra_normalize_path
(
$tpl_path
)
;
$this
->
form
=
$form
;
$this
->
language
=
$language
;
}
/**
* Returns template path
*
*
@return
unknown
*/
public
function
get_tpl_path
(
)
{
return
$this
->
tpl_path
;
}
/**
* Returns Form object
*
*
@return
Form
*/
public
function
get_form
(
)
{
return
$this
->
form
;
}
/**
* Returns form language
*
*
@return
string
*/
public
function
get_language
(
)
{
return
$this
->
language
;
}
/**
* Returns generated HTML code
*
*
@return
string
*/
public
function
get_static_html
(
)
{
$tple
=
new
Tple
(
)
;
$fields
=
$this
->
form
->
get_fields
(
)
;
$fields
=
$this
->
sort_fields
(
$fields
)
;
$c
=
0
;
$f_arr
=
array
(
)
;
foreach
(
$fields
as
$f
)
{
$tmp_arr
=
$f
->
get_properties_array
(
)
;
$tmp_arr
[
'tpl_file'
]
=
$this
->
get_fields_tpl_map_entry_value
(
$f
->
get_name
(
))
;
$tmp_arr
[
'err_tpl_file_arr'
]
=
$this
->
errs_tpl_map
[
$f
->
get_name
(
)
]
;
$f_arr
[
$c
]
=
$tmp_arr
;
$c
++
;
}
$tple
->
set_delimiters
(
'{@'
,
'@}'
)
;
// print_r_pre($f_arr);
$tple
->
assign
(
'fields'
,
$f_arr
)
;
$tple
->
assign
(
'form_name'
,
$this
->
form
->
get_name
(
))
;
$tple
->
assign
(
'submit'
,
$this
->
form
->
get_submit_name
(
))
;
$method
=
$this
->
form
->
get_method
(
)
==
Tangra_Form_Submit_Method
::
GET
?
'get'
:
'post'
;
$tple
->
assign
(
'method'
,
$method
)
;
$tple
->
assign
(
'enctype'
,
$this
->
form
->
get_enctype
(
))
;
$tple
->
assign
(
'action'
,
$this
->
form
->
get_action
(
))
;
$tple
->
assign
(
'_language'
,
$this
->
get_language
(
))
;
$path
=
$this
->
get_tpl_path
(
)
;
$tple
->
assign
(
'path'
,
$path
)
;
return
$tple
->
fetch
(
$path
.
'form.tpl'
)
;
}
/**
* Creates default template maps for fields and fields potential errors
*
*/
public
function
create_default_tpl_maps
(
)
{
$fields
=
$this
->
form
->
get_fields
(
)
;
foreach
(
$fields
as
$f
)
{
$field_tpl_path
=
$this
->
get_tpl_path
(
)
.
'/fields/'
.
strtolower
(
$f
->
get_type
(
))
.
'.tpl'
;
$this
->
add_tpl_map_entry
(
$f
->
get_name
(
)
,
$field_tpl_path
)
;
$this
->
errs_tpl_map
[
$f
->
get_name
(
)
]
=
$this
->
create_field_errors_tpl_map
(
$f
)
;
}
}
/**
* Changes existying map entry for template
*
*
@param
unknown_type
$field_name
*
@param
unknown_type
$tpl_path
*/
public
function
change_tpl_map_entry
(
$field_name
,
$tpl_path
)
{
if
(
array_key_exists
(
$field_name
,
$this
->
fields_tpl_map
))
{
$this
->
fields_tpl_map
[
$field_name
]
=
$tpl_path
;
}
else
{
throw
new
TE_Key_Not_Exists
(
$field_name
)
;
}
}
/**
* Changes map entry for field error template
*
*
@param
string
$field_name
Name of the field
*
@param
string
$p_error_name
Name of the error
*
@param
string
$tpl_path
Path to template file
*/
public
function
change_field_p_error_map_entry
(
$field_name
,
$p_error_name
,
$tpl_path
)
{
if
(
array_key_exists
(
$field_name
,
$this
->
errs_tpl_map
))
{
if
(
array_key_exists
(
$p_error_name
,
$this
->
errs_tpl_map
[
$field_name
]
))
{
$this
->
errs_tpl_map
[
$field_name
]
[
$p_error_name
]
=
$tpl_path
;
}
else
{
throw
new
TE_Key_Not_Exists
(
'Field name: '
.
$field_name
.
'. Potential error name: '
.
$p_error_name
)
;
}
}
else
{
throw
new
TE_Key_Not_Exists
(
'Field name: '
.
$field_name
)
;
}
}
/**
* Enter description here...
*
*
@param
unknown_type
$field_name
*
@return
unknown
*/
public
function
get_fields_tpl_map_entry_value
(
$field_name
)
{
$ret
=
false
;
if
(
array_key_exists
(
$field_name
,
$this
->
fields_tpl_map
))
{
$ret
=
$this
->
fields_tpl_map
[
$field_name
]
;
}
else
{
throw
new
TE_Key_Not_Exists
(
$field_name
)
;
}
return
$ret
;
}
/**
* Adds new map entry for templates
*
*
@param
unknown_type
$field_name
*
@param
unknown_type
$tpl_path
*
@internal
*/
protected
function
add_tpl_map_entry
(
$field_name
,
$tpl_path
)
{
if
(
!
array_key_exists
(
$field_name
,
$this
->
fields_tpl_map
))
{
$this
->
fields_tpl_map
[
$field_name
]
=
$tpl_path
;
}
else
{
throw
new
TE_Key_Already_Exists
(
$field_name
)
;
}
}
/**
* Sorts fields array
*
* Purpose is to move all hidden fields at the end of the result array. That way hidden
* fields does not bother the sequence of odd/even rows when HTML is generated (e.g. if
* diferent styles are used for odd and even rows (and if template uses one row per field))
*
*
@param
array
$fields
*
@return
array
*
@internal
*/
protected
function
sort_fields
(
&
$fields
)
{
$normal_fields
=
array
(
)
;
$hidden_fields
=
array
(
)
;
foreach
(
$fields
as
$key
=>
$value
)
{
if
(
$value
instanceof
Form_Field_Hidden
)
{
$hidden_fields
[
$key
]
=
$value
;
}
else
{
$normal_fields
[
$key
]
=
$value
;
}
}
$ret
=
array_merge
(
$normal_fields
,
$hidden_fields
)
;
return
$ret
;
}
/**
* Creates default map for fields' potential errors
*
*
@return
unknown
*
@internal
*/
private
function
create_field_errors_tpl_map
(
$f
)
{
$ret
=
array
(
)
;
$f_p_arr
=
$f
->
get_properties_array
(
)
;
$f_err_arr
=
$f_p_arr
[
'p_errors'
]
;
foreach
(
$f_err_arr
as
$err
)
{
$ret
[
$err
]
=
$this
->
get_tpl_path
(
)
.
'/fields/stock_errors/'
.
$err
.
'.tpl'
;
}
return
$ret
;
}
}