JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
API CHANGE remove "format" arg from pulldown()
[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 # normally, checkboxes values from get/post to 0 or 1, and stored in the database this way. enc_yesno() can be used in your templates to display this as "Yes" or "No".
131 # Example template:  Subscribe to mailing list?: ~subscribe yesno~
132 function enc_yesno($str) {
133         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
134                 return 'Yes';
135         } else {
136                 return 'No';
137         }
138 }
139
140
141 # add a tab at the begining of each non-empty line
142 function enc_tab($str) {
143         $lines = explode("\n", $str);
144         $out = '';
145         foreach($lines as $line) {
146                 if($line) {
147                         $out .= "\t$line";
148                 }
149                 $out .= "\n";
150         }
151
152         # remove the extra newline added above
153         return substr($out, 0, -1);
154 }
155
156 function enc_upper($str) {
157         return strtoupper($str);
158 }
159
160 # pass date in the form 2008-05-23
161 # ercodes date as 05/23/2008
162 function enc_mmddyyyy($yyyy_mm_dd) {
163         if($yyyy_mm_dd == '') {
164                 return '';
165         }
166         if(strlen($yyyy_mm_dd) != 10) {
167                 return date('m/d/Y');
168         }
169         return substr($yyyy_mm_dd, 5, 2) . '/' . substr($yyyy_mm_dd, 8, 2) . '/' . substr($yyyy_mm_dd, 0, 4);
170 }
171
172 # depricated. call enc_mmddyyyy() instead
173 function enc_mdy($str) {
174         return enc_mmddyyyy($str);
175 }
176
177 function enc_mmddyyyyhhmm($seconds) {
178         return date('m/d/Y g:ia', (int)$seconds);
179 }
180
181 # takes decimal
182 # returns hh:mm
183 function enc_hhmm($str) {
184         if(strlen($str) == 0) {
185                 return '';
186         }
187         $hours = floor($str);
188         $minutes = round(($str - $hours) * 60);
189         $str = sprintf("%d:%02d", $hours, $minutes);
190         return $str;
191 }
192
193 # takes decimal
194 # returns hh:mm followed by "am" or "pm" with no space
195 function enc_12hr($str) {
196         if(strlen($str) == 0) {
197                 return '';
198         }
199         $hours = floor($str);
200         $minutes = round(($str - $hours) * 60);
201         $suffix = 'am';
202         if($hours >= 12.0) {
203                 $suffix = 'pm';
204                 if($hours > 12.0) {
205                         $hours -= 12.0;
206                 }
207         }
208         $str = sprintf("%d:%02d", $hours, $minutes);
209         $str .= $suffix;
210         return $str;
211 }
212
213
214
215 # These are depricated! All but PULLDOWN_HASH still work, but you should update your code.
216 define('PULLDOWN_AUTO', 0); define('PULLDOWN_ARRAY', 1); define('PULLDOWN_HASH', 2); define('PULLDOWN_2D', 3);
217
218
219 # call this function before you run() the template so enc_options() knows what
220 # to do
221 #
222 # Parameters:
223 #
224 #   name: the name of the html control
225 #
226 #   options: an array of options to display in the pulldown/selectbox. Each
227 #   element can be either a string, or an array with two elements (first the
228 #   value to post, and second the value to display in the pulldown)
229 #
230 #   multiple: UNTESTED set to true for multiple-select boxes. 
231
232 function pulldown($name, $in_options, $multiple = false) {
233         if($multiple === PULLDOWN_HASH) {
234                 die('Webmaster error: PULLDOWN_HASH is depricated. Pass array(a,b) not a=>b');
235         }
236         if($multiple !== true) {
237                 # Probably due to API change (removing 3rd argument) but don't bother
238                 # emitting a warning, because the above warning handles the only
239                 # important case.
240                 $multiple = false;
241         }
242         $options = array();
243         foreach($in_options as $option) {
244                 if(is_array($option)) {
245                         $options[] = $option;
246                 } else {
247                         $options[] = array($option, $option);
248                 }
249         }
250         $GLOBALS[$name . '_options'] = array(
251                 'options' => $options,
252                 'multiple' => $multiple);
253 }
254
255 # output a bunch of <option> tags
256 function enc_options($values, $name) {
257         if(!isset($GLOBALS[$name . '_options'])) {
258                 die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
259         }
260         if($GLOBALS[$name . '_options']['multiple']) { # FIXME test this
261                 $values = explode(', ', $values);
262         }
263         return encode_options($values, $GLOBALS[$name . '_options']['options'], PULLDOWN_2D);
264 }
265
266 function enc_radio_n($str, $name, $n) {
267         if(!isset($GLOBALS[$name . '_options'])) {
268                 die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
269         }
270
271         if(!isset($GLOBALS[$name . '_options']['options'][$n])) {
272                 die("Template error: pulldown('$name') does not have element # $n");
273         }
274
275         $value = enc_attr($GLOBALS[$name . '_options']['options'][$n][0]);
276
277         if($str === $value) {
278                 $value .= '" checked="checked';
279         }
280
281         return $value;
282 }
283 function enc_radio_0($str, $name) { return enc_radio_n($str, $name, 0); }
284 function enc_radio_1($str, $name) { return enc_radio_n($str, $name, 1); }
285 function enc_radio_2($str, $name) { return enc_radio_n($str, $name, 2); }
286 function enc_radio_3($str, $name) { return enc_radio_n($str, $name, 3); }
287 function enc_radio_4($str, $name) { return enc_radio_n($str, $name, 4); }
288 function enc_radio_5($str, $name) { return enc_radio_n($str, $name, 5); }
289 function enc_radio_6($str, $name) { return enc_radio_n($str, $name, 6); }
290 function enc_radio_7($str, $name) { return enc_radio_n($str, $name, 7); }
291
292
293 function enc_radio_caption_n($str, $name, $n) {
294         if(!isset($GLOBALS[$name . '_options'])) {
295                 die("pulldown('$name') must be called before this template can be run. See code/wfpl/encode.php");
296         }
297
298         if(!isset($GLOBALS[$name . '_options']['options'][$n])) {
299                 die("Template error: pulldown('$name') does not have element #$n");
300         }
301
302         return $GLOBALS[$name . '_options']['options'][$n][1];
303 }
304 function enc_radio_caption_0($str, $name) { return enc_radio_caption_n($str, $name, 0); }
305 function enc_radio_caption_1($str, $name) { return enc_radio_caption_n($str, $name, 1); }
306 function enc_radio_caption_2($str, $name) { return enc_radio_caption_n($str, $name, 2); }
307 function enc_radio_caption_3($str, $name) { return enc_radio_caption_n($str, $name, 3); }
308 function enc_radio_caption_4($str, $name) { return enc_radio_caption_n($str, $name, 4); }
309 function enc_radio_caption_5($str, $name) { return enc_radio_caption_n($str, $name, 5); }
310 function enc_radio_caption_6($str, $name) { return enc_radio_caption_n($str, $name, 6); }
311 function enc_radio_caption_7($str, $name) { return enc_radio_caption_n($str, $name, 7); }
312
313
314 # use this function along with a special template to generate the html for pulldowns and multiple select boxes.
315 #
316 # Parameters:
317 #
318 #    selected: can be a string or (for multiple-selects) an array
319 #
320 #    options, keys_from: see documentation for pulldown() above
321 function encode_options($selected, $options, $keys_from) {
322         if(!is_array($selected)) {
323                 $selected = array($selected);
324         }
325
326         if($keys_from != PULLDOWN_2D) {
327                 $options = pulldown_options_to_2d($options, $keys_from);
328         }
329
330         $out = '';
331         foreach($options as $valdisp) {
332                 list($value, $display) = $valdisp;
333                 $out .= '<option';
334
335                 if(in_array($value, $selected)) {
336                         $out .= ' selected="selected"';
337                 }
338
339                 if($value !== $display || strpos($value, ' ') !== false) {
340                         $out .= ' value="';
341                         $out .= enc_attr($value);
342                         $out .= '"';
343                 }
344                         
345                 $out .= '>';
346
347                 $out .= enc_htmlnbsp($display);
348
349                 $out .= "</option>\n";
350         }
351
352         return $out;
353 }
354
355 $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");
356
357 # display <option>s
358 function enc_states($str) {
359         $ret = '';
360
361         return encode_options($str, $GLOBALS['wfpl_states_assoc'], PULLDOWN_HASH);
362 }
363
364 $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");
365
366 # display <option>s
367 function enc_provinces($str) {
368         $ret = '';
369
370         return encode_options($str, $GLOBALS['wfpl_provinces_assoc'], PULLDOWN_HASH);
371 }
372
373 # returns "odd", then "even", then "odd" etc.
374 function enc_evenodd($values, $name) {
375         if(!isset($GLOBALS['wfpl_even_odds'])) {
376                 $GLOBALS['wfpl_even_odds'] = array();
377         }
378
379         if($GLOBALS['wfpl_even_odds'][$name]) {
380                 $GLOBALS['wfpl_even_odds'][$name] = false;
381                 return 'even';
382         } else {
383                 $GLOBALS['wfpl_even_odds'][$name] = true;
384                 return 'odd';
385         }
386 }
387
388 function enc_image_src($str) {
389         list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
390         return $src;
391 }
392
393 function enc_image_width($str) {
394         list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
395         return $width;
396 }
397
398 function enc_image_height($str) {
399         list($src, $width, $height, $a, $b, $c) = explode(' ', $str);
400         return $height;
401 }
402
403 function enc_thumb_src($str) {
404         list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
405         return $src;
406 }
407
408 function enc_thumb_width($str) {
409         list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
410         return $width;
411 }
412 function enc_thumb_height($str) {
413         list($a, $b, $c, $src, $width, $height) = explode(' ', $str);
414         return $height;
415 }
416
417 # example template: Length: ~length html~ day~length s~
418 function enc_s($str) {
419         if($str == '1') {
420                 return '';
421         }
422
423         return 's';
424 }