1 | <?php |
---|
2 | /** |
---|
3 | * Description: generating elements of a form with PEAR::Calendar, using |
---|
4 | * selections as well as validating the submission |
---|
5 | */ |
---|
6 | function getmicrotime(){ |
---|
7 | list($usec, $sec) = explode(" ",microtime()); |
---|
8 | return ((float)$usec + (float)$sec); |
---|
9 | } |
---|
10 | $start = getmicrotime(); |
---|
11 | |
---|
12 | if ( !@include 'Calendar/Calendar.php' ) { |
---|
13 | define('CALENDAR_ROOT','../../'); |
---|
14 | } |
---|
15 | require_once CALENDAR_ROOT.'Year.php'; |
---|
16 | require_once CALENDAR_ROOT.'Month.php'; |
---|
17 | require_once CALENDAR_ROOT.'Day.php'; |
---|
18 | require_once CALENDAR_ROOT.'Hour.php'; |
---|
19 | require_once CALENDAR_ROOT.'Minute.php'; |
---|
20 | require_once CALENDAR_ROOT.'Second.php'; |
---|
21 | |
---|
22 | // Initialize if not set |
---|
23 | if (!isset($_POST['y'])) $_POST['y'] = date('Y'); |
---|
24 | if (!isset($_POST['m'])) $_POST['m'] = date('n'); |
---|
25 | if (!isset($_POST['d'])) $_POST['d'] = date('j'); |
---|
26 | if (!isset($_POST['h'])) $_POST['h'] = date('H'); |
---|
27 | if (!isset($_POST['i'])) $_POST['i'] = date('i'); |
---|
28 | if (!isset($_POST['s'])) $_POST['s'] = date('s'); |
---|
29 | ?> |
---|
30 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
---|
31 | <html> |
---|
32 | <head> |
---|
33 | <title> Select and Update </title> |
---|
34 | </head> |
---|
35 | <body> |
---|
36 | <h1>Select and Update</h1> |
---|
37 | <?php |
---|
38 | if ( isset($_POST['update']) ) { |
---|
39 | $Second = & new Calendar_Second($_POST['y'],$_POST['m'],$_POST['d'],$_POST['h'],$_POST['i'],$_POST['s']); |
---|
40 | if ( !$Second->isValid() ) { |
---|
41 | $V= & $Second->getValidator(); |
---|
42 | echo ('<p>Validation failed:</p>' ); |
---|
43 | while ( $error = $V->fetch() ) { |
---|
44 | echo ( $error->toString() .'<br>' ); |
---|
45 | } |
---|
46 | } else { |
---|
47 | echo ('<p>Validation success.</p>' ); |
---|
48 | echo ( '<p>New timestamp is: '.$Second->getTimeStamp().' which could be used to update a database, for example'); |
---|
49 | } |
---|
50 | } else { |
---|
51 | $Year = new Calendar_Year($_POST['y']); |
---|
52 | $Month = new Calendar_Month($_POST['y'],$_POST['m']); |
---|
53 | $Day = new Calendar_Day($_POST['y'],$_POST['m'],$_POST['d']); |
---|
54 | $Hour = new Calendar_Hour($_POST['y'],$_POST['m'],$_POST['d'],$_POST['h']); |
---|
55 | $Minute = new Calendar_Minute($_POST['y'],$_POST['m'],$_POST['d'],$_POST['h'],$_POST['i']); |
---|
56 | $Second = new Calendar_Second($_POST['y'],$_POST['m'],$_POST['d'],$_POST['h'],$_POST['i'],$_POST['s']); |
---|
57 | ?> |
---|
58 | <p><b>Set the alarm clock</p></p> |
---|
59 | <form action="<?php echo ( $_SERVER['PHP_SELF'] ); ?>" method="post"> |
---|
60 | Year: <input type="text" name="y" value="<?php echo ( $_POST['y'] ); ?>" size="4"> |
---|
61 | Month:<select name="m"> |
---|
62 | <?php |
---|
63 | $selection = array($Month); |
---|
64 | $Year->build($selection); |
---|
65 | while ( $Child = & $Year->fetch() ) { |
---|
66 | if ( $Child->isSelected() ) { |
---|
67 | echo ( "<option value=\"".$Child->thisMonth()."\" selected>".$Child->thisMonth()."\n" ); |
---|
68 | } else { |
---|
69 | echo ( "<option value=\"".$Child->thisMonth()."\">".$Child->thisMonth()."\n" ); |
---|
70 | } |
---|
71 | } |
---|
72 | ?> |
---|
73 | </select> |
---|
74 | Day:<select name="d"> |
---|
75 | <?php |
---|
76 | $selection = array($Day); |
---|
77 | $Month->build($selection); |
---|
78 | while ( $Child = & $Month->fetch() ) { |
---|
79 | if ( $Child->isSelected() ) { |
---|
80 | echo ( "<option value=\"".$Child->thisDay()."\" selected>".$Child->thisDay()."\n" ); |
---|
81 | } else { |
---|
82 | echo ( "<option value=\"".$Child->thisDay()."\">".$Child->thisDay()."\n" ); |
---|
83 | } |
---|
84 | } |
---|
85 | ?> |
---|
86 | </select> |
---|
87 | Hour:<select name="h"> |
---|
88 | <?php |
---|
89 | $selection = array($Hour); |
---|
90 | $Day->build($selection); |
---|
91 | while ( $Child = & $Day->fetch() ) { |
---|
92 | if ( $Child->isSelected() ) { |
---|
93 | echo ( "<option value=\"".$Child->thisHour()."\" selected>".$Child->thisHour()."\n" ); |
---|
94 | } else { |
---|
95 | echo ( "<option value=\"".$Child->thisHour()."\">".$Child->thisHour()."\n" ); |
---|
96 | } |
---|
97 | } |
---|
98 | ?> |
---|
99 | </select> |
---|
100 | Minute:<select name="i"> |
---|
101 | <?php |
---|
102 | $selection = array($Minute); |
---|
103 | $Hour->build($selection); |
---|
104 | while ( $Child = & $Hour->fetch() ) { |
---|
105 | if ( $Child->isSelected() ) { |
---|
106 | echo ( "<option value=\"".$Child->thisMinute()."\" selected>".$Child->thisMinute()."\n" ); |
---|
107 | } else { |
---|
108 | echo ( "<option value=\"".$Child->thisMinute()."\">".$Child->thisMinute()."\n" ); |
---|
109 | } |
---|
110 | } |
---|
111 | ?> |
---|
112 | </select> |
---|
113 | Second:<select name="s"> |
---|
114 | <?php |
---|
115 | $selection = array($Second); |
---|
116 | $Minute->build($selection); |
---|
117 | while ( $Child = & $Minute->fetch() ) { |
---|
118 | if ( $Child->isSelected() ) { |
---|
119 | echo ( "<option value=\"".$Child->thisSecond()."\" selected>".$Child->thisSecond()."\n" ); |
---|
120 | } else { |
---|
121 | echo ( "<option value=\"".$Child->thisSecond()."\">".$Child->thisSecond()."\n" ); |
---|
122 | } |
---|
123 | } |
---|
124 | ?> |
---|
125 | </select> |
---|
126 | <input type="submit" name="update" value="Set Alarm"><br> |
---|
127 | <?php |
---|
128 | } |
---|
129 | ?> |
---|
130 | <?php echo ( '<p><b>Took: '.(getmicrotime()-$start).' seconds</b></p>' ); ?> |
---|
131 | </body> |
---|
132 | </html> |
---|