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