JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
metaform: added pulldown support, added leftcheck
[wfpl.git] / encode.php
1 <?php
2
3 #  Copyright (C) 2005 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # This file contains basic encodings. These are used by the encoder. You can
23 # specify any template tag to be encoded with this syntax: ~variable.encoding~
24 #
25 # this example: <p>~foo.html~</p>
26 # will encode foo (using enc_html()) before displaying it, so that characters
27 # such as < will display properly.
28
29
30 # encode for putting within double-quotes in SQL
31 function enc_sql($str) {
32         $str = str_replace("\\", "\\\\", $str);
33         $str = str_replace('"', "\\\"", $str);
34         return $str;
35 }
36
37 # Encode for output in html. does nothing with whitespace
38 #
39 # Example: <p>~foo.html~</p>
40 function enc_html($str) {
41         $str = str_replace('&', '&amp;', $str);
42         $str = str_replace('<', '&lt;', $str);
43         $str = str_replace('>', '&gt;', $str);
44         return $str;
45 }
46
47
48 # HTML attribute.
49 #
50 # Example: <input name="foo" value="~foo.attr~">
51 function enc_attr($str) {
52         $str = str_replace('&', '&amp;', $str);
53         $str = str_replace('"', '&quot;', $str);
54         return $str;
55 }
56
57 # URI agument value.
58 #
59 # Example:  <a href="http://example.com?foo=~foo.url_val.attr~">http://example.com?foo=~foo.url_val~</a>
60 function enc_url_val($str) {
61         return rawurlencode($str);
62 }
63
64 # This is a hack to work around html's stupid syntax for checkboxes.
65 #
66 # Place the template marker just before a " somewhere.
67 #
68 # Example: <input type="checkbox" name="foo~foo.checked~">
69 function enc_checked($str) {
70         if($str == 'Yes') {
71                 return '" checked="checked';
72         } else {
73                 return '';
74         }
75 }
76
77 # add a tab at the begining of each non-empty line
78 function enc_tab($str) {
79         $lines = explode("\n", $str);
80         $out = '';
81         foreach($lines as $line) {
82                 if($line) {
83                         $out .= "\t$line";
84                 }
85                 $out .= "\n";
86         }
87
88         # remove the extra newline added above
89         return substr($out, 0, -1);
90 }
91
92 function enc_upper($str) {
93         return strtoupper($str);
94 }
95
96
97 # display <option>s
98 function enc_states($str) {
99         $states_assoc = array("AL" => "Alabama", "AK" => "Alaska", "AZ" => "Arizona", "AR" => "Arkansas", "CA" => "California", "CO" => "Colorado", "CT" => "Connecticut", "DE" => "Delaware", "FL" => "Florida", "GA" => "Georgia", "HI" => "Hawaii", "ID" => "Idaho", "IL" => "Illinois", "IN" => "Indiana", "IA" => "Iowa", "KS" => "Kansas", "KY" => "Kentucky", "LA" => "Louisiana", "ME" => "Maine", "MD" => "Maryland", "MA" => "Massachusetts", "MI" => "Michigan", "MN" => "Minnesota", "MS" => "Mississippi", "MO" => "Missouri", "MT" => "Montana", "NE" => "Nebraska", "NV" => "Nevada", "NH" => "New Hampshire", "NJ" => "New Jersey", "NM" => "New Mexico", "NY" => "New York", "NC" => "North Carolina", "ND" => "North Dakota", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "PA" => "Pennsylvania", "RI" => "Rhode Island", "SC" => "South Carolina", "SD" => "South Dakota", "TN" => "Tennessee", "TX" => "Texas", "UT" => "Utah", "VT" => "Vermont", "VA" => "Virginia", "WA" => "Washington", "DC" => "Washington, DC", "WV" => "West Virginia", "WI" => "Wisconsin", "WY" => "Wyoming");
100         $ret = '';
101
102         return encode_options($str, $states_assoc, $use_keys = true);
103 }
104
105
106 define('PULLDOWN_ARRAY', 0); define('PULLDOWN_HASH', 1); define('PULLDOWN_2D', 2);
107
108 function pulldown_options_to_hash($options, $keys_from) {
109         # convert other types of input to value=>display hash
110         switch($keys_from) {
111                 case PULLDOWN_HASH:
112                         return $options;
113                 case PULLDOWN_ARRAY:
114                         $new_options = array();
115                         foreach($options as $opt) {
116                                 $new_options[$opt] = $opt;
117                         }
118                         return $new_options;
119                 break;
120                 case PULLDOWN_2D:
121                         $new_options = array();
122                         foreach($options as $opt) {
123                                 $new_options[$opt[0]] = $opt[1];
124                         }
125                         return $new_options;
126                 break;
127                 default:
128                         die('unknown value: "' . print_r($keys_from) . '" passed in $keys_from parameter');
129         }
130 }
131
132
133 # call this function before you run() the template so enc_options() knows what
134 # to do
135 #
136 # Parameters:
137 #
138 #   name: the name of the html control
139 #
140 #   options: an array of options to display in the pulldown/selectbox
141 #
142 #   keys_from: Set to one of:
143 #        PULLDOWN_ARRAY: (default) values of $options are displayd and posted
144 #        PULLDOWN_HASH: values of $options are display, keys are posted
145 #        PULLDOWN_2D: $options is a 2 dimensional array.
146 #                     $options[0][1] is displayed, $options[0][0] is posted.
147 #                     $options[1][1] is displayed, $options[1][0] is posted.
148 #
149 #   multiple: UNTESTED set to true for multiple-select boxes. 
150
151 function pulldown($name, $options, $keys_from = PULLDOWN_ARRAY, $multiple = false) {
152         $options = pulldown_options_to_hash($options, $keys_from);
153         $GLOBALS[$name . '_options'] = array();
154         $GLOBALS[$name . '_options']['options'] = $options;
155         $GLOBALS[$name . '_options']['multiple'] = $multiple;
156 }
157
158 # output a bunch of <option> tags
159 function enc_options($values, $name) {
160         if(!isset($GLOBALS[$name . '_options'])) {
161                 die('pulldown() must be called before this template can be run. See code/wfpl/encode.php');
162         }
163         if($GLOBALS[$name . '_options']['multiple']) { # FIXME test this
164                 $values = explode(', ', $values);
165         }
166         return encode_options($values, $GLOBALS[$name . '_options']['options'], PULLDOWN_HASH);
167 }
168
169 # use this function along with a special template to generate the html for pulldowns and multiple select boxes.
170 #
171 # Parameters:
172 #
173 #    selected: can be a string or (for multiple-selects) an array
174 #
175 #    options, keys_from: see documentation for pulldown() above
176 function encode_options($selected, $options, $keys_from) {
177         if(!is_array($selected)) {
178                 $selected = array($selected);
179         }
180
181         $options = pulldown_options_to_hash($options, $keys_from);
182
183         $out = '';
184         foreach($options as $value => $display) {
185                 $out .= '<option';
186
187                 if(in_array($value, $selected, $strict = true)) {
188                         $out .= ' selected="selected"';
189                 }
190
191                 if($value !== $display) {
192                         $out .= ' value="';
193                         $out .= enc_attr($value);
194                         $out .= '"';
195                 }
196                         
197                 $out .= '>';
198
199                 $out .= enc_html($display);
200
201                 $out .= "</option>\n";
202         }
203
204         return $out;
205 }
206
207 ?>