Smutty

MVC Framework

View Code: function.datefields

Browse: All · Classes · Plugins

1
<?php
2
3
/**
4
* returns the html for a set of date field selects
5
*
6
* @param array $params assoc array of params
7
* @param Smarty $smarty smarty object
8
* @return html
9
*
10
*/
11
12
function smutty_function_datefields( $params, $smarty ) {
13
14
$smarty->depend( 'function', 'select' );
15
16
$html = '';
17
$value = $params['value'];
18
19
if ( $label = $params['label'] ) {
20
$smarty->depend( 'function', 'label' );
21
$html .= smutty_function_label(array(
22
'for' => $params['name'],
23
'text' => $label,
24
'class' => $params['labelClass'],
25
'id' => $params['labelId']
26
), $smarty );
27
}
28
29
// work out date values if we have
30
// something to work with.
31
$year = null;
32
$month = null;
33
$day = null;
34
if ( $value ) {
35
$ts = date( 'U', strtotime($value) );
36
$year = date( 'Y', $ts );
37
$month = date( 'm', $ts );
38
$day = date( 'd', $ts );
39
}
40
41
// year
42
$html .= smutty_function_select(array(
43
id => ( $params['id'] ? $params['id'] . '_year' : '' ),
44
name => $params['name'] . '_year',
45
from => smutty_function_datefields_getObjectRange(2000,2010),
46
selected => $year,
47
'class' => $params['class']
48
), $smarty );
49
50
// month
51
$html .= smutty_function_select(array(
52
id => $params['id'] ? $params['id'] . '_month' : '',
53
name => $params['name'] . '_month',
54
from => smutty_function_datefields_getObjectRange(1,12),
55
selected => $month,
56
'class' => $params['class']
57
), $smarty );
58
59
// day
60
$html .= smutty_function_select(array(
61
id => ( $params['id'] ? $params['id'] . '_day' : '' ),
62
name => $params['name'] . '_day',
63
from => smutty_function_datefields_getObjectRange(1,31),
64
selected => $day,
65
'class' => $params['class']
66
), $smarty );
67
68
return $html;
69
70
}
71
72
/**
73
* returns an array of objects with they're id and name
74
* attributes set to the specified range of numbers
75
*
76
* @param int $min minimum
77
* @param int $max maximum
78
* @return array
79
*
80
*/
81
82
function smutty_function_datefields_getObjectRange( $min, $max ) {
83
84
$values = array();
85
86
for ( $i=$min; $i<=$max; $i++ ) {
87
$value = new stdclass();
88
$value->id = $i;
89
$value->name = $i;
90
$values[] = $value;
91
}
92
93
return $values;
94
95
}
96
97
function smarty_function_datefields( $params, $smarty ) {
98
echo smutty_function_datefields( $params, $smarty );
99
}
100
101
?>

The code shown here is the code that is currently running this site. If you want to view the latest SVN version of the code then go to the Subversion repository.