JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
changed metaform's checkbox to use 0/1 in php and mysql
[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 function enc_jsdq($str) {
31         $str = enc_sql($str);
32         $str = str_replace("\n", "\\n", $str);
33         return str_replace("\r", "\\r", $str);
34 }
35
36 # encode for putting within double-quotes in SQL
37 function enc_sql($str) {
38         $str = str_replace("\\", "\\\\", $str);
39         $str = str_replace('"', "\\\"", $str);
40         return $str;
41 }
42
43 # Encode for output in html. does nothing with whitespace
44 #
45 # Example: <p>~foo.html~</p>
46 function enc_html($str) {
47         $str = str_replace('&', '&amp;', $str);
48         $str = str_replace('<', '&lt;', $str);
49         $str = str_replace('>', '&gt;', $str);
50         return $str;
51 }
52
53 # Encode for output in html. Convert newlines to <br />
54 #
55 # Example: <p>~foo.html~</p>
56 function enc_htmlbr($str) {
57         $str = enc_html($str);
58         $str = str_replace("\n", "<br />\n", $str);
59         return $str;
60 }
61
62 # Encode for output in html. Spaces converted to &nbsp;
63 #
64 # Example: <option value="12">~foo.htmlnbsp~</option>
65 function enc_htmlnbsp($str) {
66         $str = enc_html($str);
67         $str = str_replace(' ', '&nbsp;', $str);
68         return $str;
69 }
70
71
72 # HTML attribute.
73 #
74 # Example: <input name="foo" value="~foo.attr~">
75 function enc_attr($str) {
76         $str = str_replace('&', '&amp;', $str);
77         $str = str_replace('"', '&quot;', $str);
78         return $str;
79 }
80
81 # URI agument value.
82 #
83 # Example:  <a href="http://example.com?foo=~foo.url_val.attr~">http://example.com?foo=~foo.url_val~</a>
84 function enc_url_val($str) {
85         return rawurlencode($str);
86 }
87
88 # FIXME
89 function enc_url_path($str) {
90         $str = rawurlencode($str);
91         $str = str_replace('%2F', '/', $str);
92         return $str;
93 }
94
95
96 # This is a hack to work around html's stupid syntax for checkboxes.
97 #
98 # Place the template marker just before a " somewhere.
99 #
100 # Example: <input type="checkbox" name="foo~foo.checked~">
101 function enc_checked($str) {
102         if($str == 'Yes') {
103                 return '" checked="checked';
104         } else {
105                 return '';
106         }
107 }
108
109 # checkboxe values are stored in the db and handled in php as 0 or 1. When you
110 # want it displayed as "Yes" or "No" use this:
111 # Example: (displaying values from a form submission)  Over 60?: ~over_60.yesno~
112 function enc_yesno($str) {
113         if($str && $str !== 'No' && $str !== 'False' && $str !== 'false') {
114                 return 'Yes';
115         } else {
116                 return 'No';
117         }
118 }
119
120
121 # add a tab at the begining of each non-empty line
122 function enc_tab($str) {
123         $lines = explode("\n", $str);
124         $out = '';
125         foreach($lines as $line) {
126                 if($line) {
127                         $out .= "\t$line";
128                 }
129                 $out .= "\n";
130         }
131
132         # remove the extra newline added above
133         return substr($out, 0, -1);
134 }
135
136 function enc_upper($str) {
137         return strtoupper($str);
138 }
139
140 # pass date in the form 2008-05-23
141 # ercodes date as 05/23/2008
142 function enc_mmddyyyy($yyyy_mm_dd) {
143         if(strlen($yyyy_mm_dd) != 10) {
144                 return date('m/d/Y');
145         }
146         return substr($yyyy_mm_dd, 5, 2) . '/' . substr($yyyy_mm_dd, 8, 2) . '/' . substr($yyyy_mm_dd, 0, 4);
147 }
148
149 function enc_mmddyyyyhhmm($seconds) {
150         return date('m/d/Y g:ia', (int)$seconds);
151 }
152
153
154
155
156 define('PULLDOWN_ARRAY', 0); define('PULLDOWN_HASH', 1); define('PULLDOWN_2D', 2);
157
158 function pulldown_options_to_2d($options, $keys_from) {
159         # convert other types of input to value=>display hash
160         switch($keys_from) {
161                 case PULLDOWN_HASH:
162                         $new_options = array();
163                         foreach($options as $value => $display) {
164                                 $new_options[] = array($value, $display);
165                         }
166                         return $new_options;
167                 case PULLDOWN_ARRAY:
168                         $new_options = array();
169                         foreach($options as $opt) {
170                                 $new_options[] = array($opt, $opt);
171                         }
172                         return $new_options;
173                 break;
174                 case PULLDOWN_2D:
175                         return $options;
176                 break;
177                 default:
178                         die('pulldown_options_to_2d(): unknown value: "' . print_r($keys_from) . '" passed in $keys_from parameter');
179         }
180 }
181
182
183 # call this function before you run() the template so enc_options() knows what
184 # to do
185 #
186 # Parameters:
187 #
188 #   name: the name of the html control
189 #
190 #   options: an array of options to display in the pulldown/selectbox
191 #
192 #   keys_from: Set to one of:
193 #        PULLDOWN_ARRAY: (default) values of $options are displayd and posted
194 #        PULLDOWN_HASH: values of $options are display, keys are posted
195 #        PULLDOWN_2D: $options is a 2 dimensional array.
196 #                     $options[0][1] is displayed, $options[0][0] is posted.
197 #                     $options[1][1] is displayed, $options[1][0] is posted.
198 #
199 #   multiple: UNTESTED set to true for multiple-select boxes. 
200
201 function pulldown($name, $options, $keys_from = PULLDOWN_ARRAY, $multiple = false) {
202         $options = pulldown_options_to_2d($options, $keys_from);
203         $GLOBALS[$name . '_options'] = array();
204         $GLOBALS[$name . '_options']['options'] = $options;
205         $GLOBALS[$name . '_options']['multiple'] = $multiple;
206 }
207
208 # output a bunch of <option> tags
209 function enc_options($values, $name) {
210         if(!isset($GLOBALS[$name . '_options'])) {
211                 die('pulldown() must be called before this template can be run. See code/wfpl/encode.php');
212         }
213         if($GLOBALS[$name . '_options']['multiple']) { # FIXME test this
214                 $values = explode(', ', $values);
215         }
216         return encode_options($values, $GLOBALS[$name . '_options']['options'], PULLDOWN_2D);
217 }
218
219 # use this function along with a special template to generate the html for pulldowns and multiple select boxes.
220 #
221 # Parameters:
222 #
223 #    selected: can be a string or (for multiple-selects) an array
224 #
225 #    options, keys_from: see documentation for pulldown() above
226 function encode_options($selected, $options, $keys_from) {
227         if(!is_array($selected)) {
228                 $selected = array($selected);
229         }
230
231         if($keys_from != PULLDOWN_2D) {
232                 $options = pulldown_options_to_2d($options, $keys_from);
233         }
234
235         $out = '';
236         foreach($options as $valdisp) {
237                 list($value, $display) = $valdisp;
238                 $out .= '<option';
239
240                 if(in_array($value, $selected)) {
241                         $out .= ' selected="selected"';
242                 }
243
244                 if($value !== $display) {
245                         $out .= ' value="';
246                         $out .= enc_attr($value);
247                         $out .= '"';
248                 }
249                         
250                 $out .= '>';
251
252                 $out .= enc_htmlnbsp($display);
253
254                 $out .= "</option>\n";
255         }
256
257         return $out;
258 }
259
260 $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");
261
262 # display <option>s
263 function enc_states($str) {
264         $ret = '';
265
266         return encode_options($str, $GLOBALS['wfpl_states_assoc'], PULLDOWN_HASH);
267 }
268
269 $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");
270
271 # display <option>s
272 function enc_provinces($str) {
273         $ret = '';
274
275         return encode_options($str, $GLOBALS['wfpl_provinces_assoc'], PULLDOWN_HASH);
276 }
277
278 # returns "odd", then "even", then "odd" etc.
279 function enc_evenodd($values, $name) {
280         if(!isset($GLOBALS['wfpl_even_odds'])) {
281                 $GLOBALS['wfpl_even_odds'] = array();
282         }
283
284         if($GLOBALS['wfpl_even_odds'][$name]) {
285                 $GLOBALS['wfpl_even_odds'][$name] = false;
286                 return 'even';
287         } else {
288                 $GLOBALS['wfpl_even_odds'][$name] = true;
289                 return 'odd';
290         }
291 }
292
293 ?>