ae08607fa9f2554eade66724954f35ab0e07e885
[readifood.git] / lib / forms.php
1 <?php
2
3   function form($classes = null, $action = null) {
4     if (is_null($classes)) $classes = array();
5     else if (! is_array($classes)) $classes = explode('/\s+/', $classes);
6     if (! isset($action)) $action = $_SERVER['REQUEST_URI'];
7     echo "<form ";
8     if (count($classes)) printf("class=\"%s\" ", implode(" ", $classes));
9     echo "method=\"POST\" action=\"$action\">\n";
10   }
11
12   function end_form() {
13     echo "</form>\n";
14   }
15
16   function input($name, $value = null, $type = null) {
17     echo "<input name=\"$name\"";
18     if (isset($type)) echo " type=\"$type\"";
19     if (isset($value)) echo " value=\"$value\"";
20     else echo " value=\"" . $_POST[$name] . "\"";
21     echo ">";
22   }
23
24   function hidden($name, $value = null) {
25     return input($name, $value, "hidden");
26   }
27
28   function submit($name, $value = null) {
29     return input($name, $value, "submit");
30   }
31
32   function textarea($name, $value = null) {
33     echo "<textarea name=\"$name\">";
34     if (isset($value)) echo $value;
35     else echo $_POST[$name];
36     echo "</textarea>";
37   }
38
39   function option($select, $value, $text, $selected = null) {
40     echo "  <option value=\"$value\"";
41     if (! isset($selected)) $selected = $_POST[$select];
42     if ($value == $selected) echo " selected";
43     echo ">$text\n";
44   }
45
46   function datepicker($name, $value = null, $past = true, $past_picker = null, $future = true, $future_picker = null) {
47     $id = "datepicker_$name";
48     echo "<script>\n  $(function() {\n";
49     echo "    $(\"#$id\").datepicker({\n";
50     /*
51       If this is the "to" picker in a date range, restrict the end date of the
52       "from" picker to be the date selected by this picker.
53     */
54     if (isset($past_picker)) {
55       echo "      onClose: function(selectedDate) {\n";
56       echo "        $(\"#datepicker_$past_picker\").datepicker(\"option\", \"maxDate\", selectedDate);\n";
57       echo "      },\n";
58     }
59     /*
60       If this is the "from" picker in a date range, restrict the start date of
61       the "to" picker to be the date selected by this picker.
62     */
63     if (isset($future_picker)) {
64       echo "      onClose: function(selectedDate) {\n";
65       echo "        $(\"#datepicker_$future_picker\").datepicker(\"option\", \"minDate\", selectedDate);\n";
66       echo "      },\n";
67     }
68     echo "      changeMonth: true,\n";
69     echo "      changeYear: true,\n";
70     echo "      dateFormat: 'yy-mm-dd',\n";
71     /* Are we allowed to show dates earlier than today? */
72     if (! $past) echo "      minDate: 0,\n";
73     /* Are we allowed to show dates later than today? */
74     if (! $future) echo "      maxDate: 0,\n";
75     echo "      showButtonPanel: true\n";
76     echo "    });\n  });</script>";
77     echo "<input name=\"$name\" id=\"$id\"";
78     echo " maxlength=10 size=10";
79     if (isset($value)) echo " value=\"$value\"";
80     else echo " value=\"" . $_POST[$name] . "\"";
81     echo "><em class=\"small\">(Y-m-d)</em>";
82   }
83
84 ?>