[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file form_field_date_triad.class.php
Documentation is available at
form_field_date_triad.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Form_Field_Date_Triad
*
*
@package
tangra_lib
*
@subpackage
form
*/
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'form/fields/form_field.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'nls/date.inc.php'
)
;
/**
* Represents 3 HTML text fields.
*
*
@package
tangra_lib
*
@subpackage
form
*/
class
Form_Field_Date_Triad
extends
Form_Field
{
/**
* Day holder
*
*
@var
integer
*
@internal
*/
private
$day
;
/**
* Month holder
*
*
@var
integer
*
@internal
*/
private
$month
;
/**
* Year holder
*
*
@var
integer
*
@internal
*/
private
$year
;
/**
* Constructor
*
*
@param
string
$name
Name of the field
*
@param
boolean
$required
Is field required?
*
@param
string
$value
Default value. Must be valid date in YYYY-MM-DD format or 0000-00-00
*/
function
__construct
(
$name
,
$required
=
false
,
$value
=
'0000-00-00'
)
{
parent
::
__construct
(
$name
,
$value
,
$required
)
;
$this
->
add_potential_error
(
'invalid_date'
)
;
}
/**
* Sets field value
*
*
@param
string
$value.
Must be valid date in YYYY-MM-DD format or 0000-00-00
*/
public
function
set_value
(
$value
)
{
if
(
$value
==
'0000-00-00'
)
{
$this
->
_set_value
(
$value
)
;
}
else
{
if
(
is_valid_date
(
$value
))
{
$this
->
_set_value
(
$value
)
;
}
else
{
throw
new
Tangra_Exception
(
'Invalid date.'
)
;
}
}
}
/**
* Captures submit
*
*
@param
form_name
$form_name
*
@param
array
$submit_array
*
@internal
*/
public
function
capture_submit
(
$form_name
,
&
$submit_array
)
{
if
(
array_key_exists
(
$form_name
.
'_'
.
$this
->
get_name
(
)
.
'_day'
,
$submit_array
))
{
$this
->
day
=
$submit_array
[
$form_name
.
'_'
.
$this
->
get_name
(
)
.
'_day'
]
;
$this
->
day
=
str_pad
(
$this
->
day
,
2
,
'0'
,
STR_PAD_LEFT
)
;
}
else
{
$this
->
day
=
NULL
;
}
if
(
array_key_exists
(
$form_name
.
'_'
.
$this
->
get_name
(
)
.
'_month'
,
$submit_array
))
{
$this
->
month
=
$submit_array
[
$form_name
.
'_'
.
$this
->
get_name
(
)
.
'_month'
]
;
$this
->
month
=
str_pad
(
$this
->
month
,
2
,
'0'
,
STR_PAD_LEFT
)
;
}
else
{
$this
->
month
=
NULL
;
}
if
(
array_key_exists
(
$form_name
.
'_'
.
$this
->
get_name
(
)
.
'_year'
,
$submit_array
))
{
$this
->
year
=
$submit_array
[
$form_name
.
'_'
.
$this
->
get_name
(
)
.
'_year'
]
;
$this
->
year
=
str_pad
(
$this
->
year
,
4
,
'0'
,
STR_PAD_LEFT
)
;
}
else
{
$this
->
year
=
NULL
;
}
if
(
$this
->
day
||
$this
->
month
||
$this
->
year
)
{
$this
->
set_html_value
(
$this
->
year
.
'-'
.
$this
->
month
.
'-'
.
$this
->
day
)
;
}
else
{
$this
->
set_html_value
(
NULL
)
;
}
}
/**
* Performs basic check for validity
*
*
@return
boolean
*
@internal
*/
public
function
basic_check
(
)
{
$has_errors
=
false
;
if
(
!
(
$this
->
get_required
(
)
&&
$this
->
get_html_value
(
)
==
'0000-00-00'
))
{
if
(
$this
->
get_html_value
(
)
!=
'0000-00-00'
)
{
if
(
!
is_valid_date
(
$this
->
get_html_value
(
)))
{
$this
->
set_error
(
'invalid_date'
)
;
$has_errors
=
true
;
}
}
}
else
{
$this
->
set_error
(
'mandatory_missing'
)
;
$has_errors
=
true
;
}
return
$has_errors
;
}
/**
* Returns day
*
*
@return
integer
*/
public
function
get_day
(
)
{
return
$this
->
day
;
}
/**
* Returns month
*
*
@return
integer
*/
public
function
get_month
(
)
{
return
$this
->
month
;
}
/**
* Returns year
*
*
@return
integer
*/
public
function
get_year
(
)
{
return
$this
->
year
;
}
/**
* Sets field value
*
*
@param
string
$value
*
@internal
*/
private
function
_set_value
(
$value
)
{
$this
->
year
=
substr
(
$value
,
0
,
4
)
;
$this
->
month
=
substr
(
$value
,
5
,
2
)
;
$this
->
day
=
substr
(
$value
,
8
,
2
)
;
}
}