Make textareas dynamically resizable.
[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     $id = "textarea_$name";
34     echo "<textarea id=\"$id\" name=\"$name\" cols=30>";
35     if (isset($value)) echo $value;
36     else echo $_POST[$name];
37     echo "</textarea>";
38     echo "<script>\n  $(function() {\n";
39     echo "    $(\"#$id\").autosize();\n";
40     echo "  });</script>";
41   }
42
43   function option($select, $value, $text, $selected = null) {
44     echo "  <option value=\"$value\"";
45     if (! isset($selected)) $selected = $_POST[$select];
46     if ($value == $selected) echo " selected";
47     echo ">$text\n";
48   }
49
50   function datepicker($name, $value = null, $past = true, $past_picker = null, $future = true, $future_picker = null) {
51     $id = "datepicker_$name";
52     echo "<script>\n  $(function() {\n";
53     echo "    $(\"#$id\").datepicker({\n";
54     /*
55       If this is the "to" picker in a date range, restrict the end date of the
56       "from" picker to be the date selected by this picker.
57     */
58     if (isset($past_picker)) {
59       echo "      onClose: function(selectedDate) {\n";
60       echo "        $(\"#datepicker_$past_picker\").datepicker(\"option\", \"maxDate\", selectedDate);\n";
61       echo "      },\n";
62     }
63     /*
64       If this is the "from" picker in a date range, restrict the start date of
65       the "to" picker to be the date selected by this picker.
66     */
67     if (isset($future_picker)) {
68       echo "      onClose: function(selectedDate) {\n";
69       echo "        $(\"#datepicker_$future_picker\").datepicker(\"option\", \"minDate\", selectedDate);\n";
70       echo "      },\n";
71     }
72     echo "      changeMonth: true,\n";
73     echo "      changeYear: true,\n";
74     echo "      dateFormat: 'yy-mm-dd',\n";
75     /* Are we allowed to show dates earlier than today? */
76     if (! $past) echo "      minDate: 0,\n";
77     /* Are we allowed to show dates later than today? */
78     if (! $future) echo "      maxDate: 0,\n";
79     echo "      showButtonPanel: true\n";
80     echo "    });\n  });</script>";
81     echo "<input name=\"$name\" id=\"$id\"";
82     echo " maxlength=10 size=10";
83     if (isset($value)) echo " value=\"$value\"";
84     else echo " value=\"" . $_POST[$name] . "\"";
85     echo "><em class=\"small\">(Y-m-d)</em>";
86   }
87
88 ?>