JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed encoding of spaces in pulldown value
[wfpl.git] / encode.php
1 <?php
2
3 #  Copyright (C) 2005 Jason Woofenden
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #  
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #  
15 #  You should have received a copy of the GNU General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 # This file contains basic encodings. These are used by the encoder. You can
20 # specify any template tag to be encoded with this syntax: ~variable.encoding~
21 #
22 # this example: <p>~foo.html~</p>
23 # will encode foo (using enc_html()) before displaying it, so that characters
24 # such as < will display properly.
25
26 function enc_cap($str) {
27         $str = ucfirst($str);
28         return $str;
29 }
30
31 function enc_jsdq($str) {
32         $str = enc_sql($str);
33         $str = str_replace("\n", "\\n", $str);
34         return str_replace("\r", "\\r", $str);
35 }
36
37 # encode for putting within double-quotes in SQL
38 function enc_sql($str) {
39         $str = str_replace("\\", "\\\\", $str);
40         $str = str_replace('"', "\\\"", $str);
41         return $str;
42 }
43
44 # Encode for output in html. does nothing with whitespace
45 #
46 # Example: <p>~foo.html~</p>
47 function enc_html($str) {
48         $str = str_replace('&', '&amp;', $str);
49         $str = str_replace('<', '&lt;', $str);
50         $str = str_replace('>', '&gt;', $str);
51         return $str;
52 }
53
54 # Encode for output in html. Convert newlines to <br />
55 #
56 # Example: <p>~foo.htmlbr~</p>
57 function enc_htmlbr($str) {
58         $str = enc_html($str);
59         $str = str_replace("\n", "<br />\n", $str);
60         return $str;
61 }
62
63 # Encode for output in html. Preserves newlines and indentation by converting
64 # newlines to <br /> and spaces at the begining of lines to &nbsp;&nbsp;
65 #
66 # Example: <p>~foo.htmlbrtab~</p>
67 function enc_htmlbrtab($str) {
68         $str = enc_htmlbr($str);
69         $space_to_nbsp = create_function('$matches', 'return str_repeat(\'&nbsp;\', strlen($matches[0]) * 2);');
70         $str = preg_replace_callback("|^ *|m", $space_to_nbsp, $str);
71         return $str;
72 }
73
74 # Encode for output in html. Spaces converted to &nbsp; and \n to <br />
75 #
76 # Example: <option value="12">~foo.htmlbrnbsp~</option>
77 function enc_htmlbrnbsp($str) {
78         $str = enc_htmlbr($str);
79         $str = str_replace(' ', '&nbsp;', $str);
80         return $str;
81 }
82
83 # Encode for output in html. Spaces converted to &nbsp;
84 #
85 # Example: <option value="12">~foo.htmlnbsp~</option>
86 function enc_htmlnbsp($str) {
87         $str = enc_html($str);
88         $str = str_replace(' ', '&nbsp;', $str);
89         return $str;
90 }
91
92
93 # HTML attribute.
94 #
95 # Example: <input name="foo" value="~foo.attr~">
96 function enc_attr($str) {
97         $str = str_replace('&', '&amp;', $str);
98         $str = str_replace('"', '&quot;', $str);
99         return $str;
100 }
101
102 # URI agument value.
103 #
104 # Example:  <a href="http://example.com?foo=~foo.url_val.attr~">http://example.com?foo=~foo.url_val~</a>
105 function enc_url_val($str) {
106         return rawurlencode($str);
107 }
108
109 # FIXME
110 function enc_url_path($str) {
111         $str = rawurlencode($str);
112         $str = str_replace('%2F', '/', $str);
113         return $str;
114 }
115
116
117 # This is a hack to work around html's stupid syntax for checkboxes.
118 #
119 # Place the template marker just before a " somewhere.
120 #
121 # Example: <input type="checkbox" name="foo~foo.checked~">
122 function enc_checked($str) {
123         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
124                 return '" checked="checked';
125         } else {
126                 return '';
127         }
128 }
129
130 # checkboxe values are stored in the db and handled in php as 0 or 1. When you
131 # want it displayed as "Yes" or "No" use this:
132 # Example: (displaying values from a form submission)  Over 60?: ~over_60.yesno~
133 function enc_yesno($str) {
134         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
135                 return 'Yes';
136         } else {
137                 return 'No';
138         }
139 }
140
141
142 # add a tab at the begining of each non-empty line
143 function enc_tab($str) {
144         $lines = explode("\n", $str);
145         $out = '';
146         foreach($lines as $line) {
147                 if($line) {
148                         $out .= "\t$line";
149                 }
150                 $out .= "\n";
151         }
152
153         # remove the extra newline added above
154         return substr($out, 0, -1);
155 }
156
157 function enc_upper($str) {
158         return strtoupper($str);
159 }
160
161 # pass date in the form 2008-05-23
162 # ercodes date as 05/23/2008
163 function enc_mmddyyyy($yyyy_mm_dd) {
164         if($yyyy_mm_dd == '') {
165                 return '';
166         }
167         if(strlen($yyyy_mm_dd) != 10) {
168                 return date('m/d/Y');
169         }
170         return substr($yyyy_mm_dd, 5, 2) . '/' . substr($yyyy_mm_dd, 8, 2) . '/' . substr($yyyy_mm_dd, 0, 4);
171 }
172
173 # depricated. call enc_mmddyyyy() instead
174 function enc_mdy($str) {
175         return enc_mmddyyyy($str);
176 }
177
178 function enc_mmddyyyyhhmm($seconds) {
179         return date('m/d/Y g:ia', (int)$seconds);
180 }
181
182 # takes decimal
183 # returns hh:mm
184 function enc_hhmm($str) {
185         if(strlen($str) == 0) {
186                 return '';
187         }
188         $hours = floor($str);
189         $minutes = round(($str - $hours) * 60);
190         $str = sprintf("%d:%02d", $hours, $minutes);
191         return $str;
192 }
193
194 # takes decimal
195 # returns hh:mm followed by "am" or "pm" with no space
196 function enc_12hr($str) {
197         if(strlen($str) == 0) {
198                 return '';
199         }
200         $hours = floor($str);
201         $minutes = round(($str - $hours) * 60);
202         $suffix = 'am';
203         if($hours >= 12.0) {
204                 $suffix = 'pm';
205                 if($hours > 12.0) {
206                         $hours -= 12.0;
207                 }
208         }
209         $str = sprintf("%d:%02d", $hours, $minutes);
210         $str .= $suffix;
211         return $str;
212 }
213
214
215
216
217 define('PULLDOWN_ARRAY', 0); define('PULLDOWN_HASH', 1); define('PULLDOWN_2D', 2);
218
219 function pulldown_options_to_2d($options, $keys_from) {
220         # convert other types of input to value=>display hash
221         switch($keys_from) {
222                 case PULLDOWN_HASH:
223                         $new_options = array();
224                         foreach($options as $value => $display) {
225                                 $new_options[] = array($value, $display);
226                         }
227                         return $new_options;
228                 case PULLDOWN_ARRAY:
229                         $new_options = array();
230                         foreach($options as $opt) {
231                                 $new_options[] = array($opt, $opt);
232                         }
233                         return $new_options;
234                 break;
235                 case PULLDOWN_2D:
236                         return $options;
237                 break;
238                 default:
239                         die('pulldown_options_to_2d(): unknown value: "' . print_r($keys_from) . '" passed in $keys_from parameter');
240         }
241 }
242
243
244 # call this function before you run() the template so enc_options() knows what
245 # to do
246 #
247 # Parameters:
248 #
249 #   name: the name of the html control
250 #
251 #   options: an array of options to display in the pulldown/selectbox
252 #
253 #   keys_from: Set to one of:
254 #        PULLDOWN_ARRAY: (default) values of $options are displayd and posted
255 #        PULLDOWN_HASH: values of $options are display, keys are posted
256 #        PULLDOWN_2D: $options is a 2 dimensional array.
257 #                     $options[0][1] is displayed, $options[0][0] is posted.
258 #                     $options[1][1] is displayed, $options[1][0] is posted.
259 #
260 #   multiple: UNTESTED set to true for multiple-select boxes. 
261
262 function pulldown($name, $options, $keys_from = PULLDOWN_ARRAY, $multiple = false) {
263         $options = pulldown_options_to_2d($options, $keys_from);
264         $GLOBALS[$name . '_options'] = array();
265         $GLOBALS[$name . '_options']['options'] = $options;
266         $GLOBALS[$name . '_options']['multiple'] = $multiple;
267 }
268
269 # output a bunch of <option> tags
270 function enc_options($values, $name) {
271         if(!isset($GLOBALS[$name . '_options'])) {
272                 die('pulldown() must be called before this template can be run. See code/wfpl/encode.php');
273         }
274         if($GLOBALS[$name . '_options']['multiple']) { # FIXME test this
275                 $values = explode(', ', $values);
276         }
277         return encode_options($values, $GLOBALS[$name . '_options']['options'], PULLDOWN_2D);
278 }
279
280 # use this function along with a special template to generate the html for pulldowns and multiple select boxes.
281 #
282 # Parameters:
283 #
284 #    selected: can be a string or (for multiple-selects) an array
285 #
286 #    options, keys_from: see documentation for pulldown() above
287 function encode_options($selected, $options, $keys_from) {
288         if(!is_array($selected)) {
289                 $selected = array($selected);
290         }
291
292         if($keys_from != PULLDOWN_2D) {
293                 $options = pulldown_options_to_2d($options, $keys_from);
294         }
295
296         $out = '';
297         foreach($options as $valdisp) {
298                 list($value, $display) = $valdisp;
299                 $out .= '<option';
300
301                 if(in_array($value, $selected)) {
302                         $out .= ' selected="selected"';
303                 }
304
305                 if($value !== $display || strpos($value, ' ') !== false) {
306                         $out .= ' value="';
307                         $out .= enc_attr($value);
308                         $out .= '"';
309                 }
310                         
311                 $out .= '>';
312
313                 $out .= enc_htmlnbsp($display);
314
315                 $out .= "</option>\n";
316         }
317
318         return $out;
319 }
320
321 $GLOBALS['wfpl_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");
322
323 # display <option>s
324 function enc_states($str) {
325         $ret = '';
326
327         return encode_options($str, $GLOBALS['wfpl_states_assoc'], PULLDOWN_HASH);
328 }
329
330 $GLOBALS['wfpl_provinces_assoc'] = array("AB" => "Alberta", "BC" => "British Columbia", "MB" => "Manitoba", "NF" => "Newfoundland", "NB" => "New Brunswick", "NS" => "Nova Scotia", "NT" => "Northwest Territories", "NU" => "Nunavut", "ON" => "Ontario", "PE" => "Prince Edward Island", "QC" => "Quebec", "SK" => "Saskatchewan", "YT" => "Yukon Territory");
331
332 # display <option>s
333 function enc_provinces($str) {
334         $ret = '';
335
336         return encode_options($str, $GLOBALS['wfpl_provinces_assoc'], PULLDOWN_HASH);
337 }
338
339 # returns "odd", then "even", then "odd" etc.
340 function enc_evenodd($values, $name) {
341         if(!isset($GLOBALS['wfpl_even_odds'])) {
342                 $GLOBALS['wfpl_even_odds'] = array();
343         }
344
345         if($GLOBALS['wfpl_even_odds'][$name]) {
346                 $GLOBALS['wfpl_even_odds'][$name] = false;
347                 return 'even';
348         } else {
349                 $GLOBALS['wfpl_even_odds'][$name] = true;
350                 return 'odd';
351         }
352 }
353
354 function enc_image_src($str) {
355         list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
356         return $src;
357 }
358
359 function enc_image_width($str) {
360         list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
361         return $width;
362 }
363
364 function enc_image_height($str) {
365         list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
366         return $height;
367 }
368
369 function enc_thumb_src($str) {
370         list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
371         return $src;
372 }
373
374 function enc_thumb_width($str) {
375         list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
376         return $width;
377 }
378 function enc_thumb_height($str) {
379         list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
380         return $height;
381 }
382
383 # example template: Length: ~length.html~ day~length.s~
384 function enc_s($str) {
385         if($str == '1') {
386                 return '';
387         }
388
389         return 's';
390 }