1 | <?php |
---|
2 | /** |
---|
3 | * TemplateGlue.inc.php |
---|
4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
5 | */ |
---|
6 | |
---|
7 | /** |
---|
8 | * Print URL to download file with BBEdit/Interarchy. "USER" must be a pre-set |
---|
9 | * environment variable. Files must reside within and be relative to env "DOCUMENT_ROOT". |
---|
10 | * |
---|
11 | * @access public |
---|
12 | * |
---|
13 | * @param mixed $file File path relative to DOCUMENT_ROOT (i.e. '/_templates/index.ihtml'). |
---|
14 | * |
---|
15 | * @return string <a> with file basename as text. |
---|
16 | */ |
---|
17 | function bbftp($file) |
---|
18 | { |
---|
19 | return sprintf('<a href="bbftp://%s@%s:%s%s" title="%s">%s</a>', $_SERVER['USER'], $_SERVER['HTTP_HOST'], SITE_BASE, $file, $file, basename($file)); |
---|
20 | } |
---|
21 | |
---|
22 | /** |
---|
23 | * Prints an image tag for image specified in $src. |
---|
24 | * |
---|
25 | * @param string $src File name of the image, including dir and file extension. |
---|
26 | * @param string $alt Alt tag text. |
---|
27 | * @param string $extra Additional attributes. |
---|
28 | */ |
---|
29 | function oImg($src, $alt='', $extra='') |
---|
30 | { |
---|
31 | $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src; |
---|
32 | |
---|
33 | if (false === ($gis = @getimagesize($filepath)) || preg_match('/width|height/', $extra)) { |
---|
34 | $image_size = ''; |
---|
35 | } else { |
---|
36 | $image_size = $gis[3]; |
---|
37 | } |
---|
38 | |
---|
39 | return sprintf('<img src="%s" %s alt="%s" %s />', |
---|
40 | $src, |
---|
41 | $image_size, |
---|
42 | oTxt($alt), |
---|
43 | $extra |
---|
44 | ); |
---|
45 | } |
---|
46 | function printImg($src, $alt='', $extra='') |
---|
47 | { |
---|
48 | echo oImg($src, $alt, $extra); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * Finds the values of an enumeration or set column of a MySQL database, returning them in an array. |
---|
53 | * Use this to generate a pull-down menu of options or to validate the existance |
---|
54 | * of options. (Quinn 10 Feb 2001) |
---|
55 | * |
---|
56 | * @param string $db_table database table to lookup |
---|
57 | * @param string $db_col database column to lookup |
---|
58 | * |
---|
59 | * @return array Array of the set/enum values on success, false on failure. |
---|
60 | */ |
---|
61 | function getSetEnumFieldValues($db_table, $db_col) |
---|
62 | { |
---|
63 | $qid = dbQuery("SHOW COLUMNS FROM $db_table LIKE '$db_col'",false); |
---|
64 | |
---|
65 | $row = mysql_fetch_row($qid); |
---|
66 | if (preg_match('/^enum|^set/i', $row[1]) && preg_match_all("/'([^']*)'/", $row[1], $enum)) { |
---|
67 | natsort($enum[1]); |
---|
68 | return $enum[1]; |
---|
69 | } else { |
---|
70 | return false; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Prints option fields for a select form. Works only with enum or set |
---|
76 | * data types in table columns. |
---|
77 | * |
---|
78 | * @param string $db_table database table to lookup |
---|
79 | * @param string $db_col database column to lookup |
---|
80 | */ |
---|
81 | function printSetSelectForm($db_table, $db_col, $preselected, $blank=false) |
---|
82 | { |
---|
83 | $values = getSetEnumFieldValues($db_table, $db_col); |
---|
84 | if ($values === false) { |
---|
85 | ?><option value=""><?php echo _("n/a"); ?></option> |
---|
86 | <?php |
---|
87 | return false; |
---|
88 | } |
---|
89 | if (true === $blank) { |
---|
90 | $selected = ($preselected == '') ? ' selected' : ''; |
---|
91 | ?><option value=""<?php echo $selected; ?>> </option> |
---|
92 | <?php |
---|
93 | } |
---|
94 | // When the 'blank' value needs a specific key->val pair. |
---|
95 | if (is_array($blank)) { |
---|
96 | foreach ($blank as $key=>$val) { |
---|
97 | $selected = ($preselected == $val) ? ' selected="selected"' : ''; |
---|
98 | ?><option value="<?php echo oTxt($key); ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option> |
---|
99 | <?php |
---|
100 | } |
---|
101 | } |
---|
102 | foreach ($values as $item) { |
---|
103 | $selected = ($item == $preselected) ? ' selected' : ''; |
---|
104 | ?><option value="<?php echo oTxt($item); ?>"<?php echo $selected; ?>><?php echo oTxt($item); ?></option> |
---|
105 | <?php |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Prints checkbox fields. Works only with enum or set |
---|
111 | * data types in table columns. |
---|
112 | * |
---|
113 | * @param string $db_table database table to lookup |
---|
114 | * @param string $db_col database column to lookup |
---|
115 | * @param array $preselected array of preselected values (matching the values in $db_col) |
---|
116 | * @param int $columns number of table columns to print |
---|
117 | * @param int $flag set to 'allone' for name of input fields to all be the same of a multidimentional array. |
---|
118 | */ |
---|
119 | function printSetCheckboxes($db_table, $db_col, $preselected, $columns=1, $flag=null) |
---|
120 | { |
---|
121 | // Sometimes preselected comes as a comma list. |
---|
122 | if (!is_array($preselected)) { |
---|
123 | $preselected = explode(',', $preselected); |
---|
124 | } |
---|
125 | |
---|
126 | // Checkbox POST data has the primary data in the keys, and 'on' as the values. |
---|
127 | // Here we assume in all the values of an array are 'on' that we can find the data |
---|
128 | // in the array keys. |
---|
129 | $ps_value_count = array_count_values($preselected); |
---|
130 | if ($ps_value_count['on'] == sizeof($preselected)) { |
---|
131 | $preselected = array_keys($preselected); |
---|
132 | } |
---|
133 | |
---|
134 | // Retreive values of a Set or ENUM database column. |
---|
135 | $values = getSetEnumFieldValues($db_table, $db_col); |
---|
136 | |
---|
137 | if (!empty($values)) { |
---|
138 | ?> |
---|
139 | <table> |
---|
140 | <tr> |
---|
141 | <?php |
---|
142 | // Initialize the HTML table generation vars. |
---|
143 | $num_cells = sizeof($values) - 1; |
---|
144 | $num_rows = floor($num_cells / $columns); |
---|
145 | $cols_lastrow = $num_cells % $columns; |
---|
146 | $row_cnt = 0; |
---|
147 | $col_cnt = 0; |
---|
148 | foreach ($values as $item) { |
---|
149 | if ($col_cnt == $columns) { |
---|
150 | // Begin a new row. |
---|
151 | ?></tr><tr><?php |
---|
152 | $col_cnt = 0; |
---|
153 | $row_cnt++; |
---|
154 | } |
---|
155 | if ($col_cnt < $cols_lastrow) { |
---|
156 | $lastrow_add = $col_cnt; |
---|
157 | } else { |
---|
158 | $lastrow_add = $cols_lastrow; |
---|
159 | } |
---|
160 | $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt; |
---|
161 | $col_cnt++; |
---|
162 | |
---|
163 | // Look for preselected value. |
---|
164 | if (in_array($item, $preselected)) { |
---|
165 | $checked = ' checked="checked"'; |
---|
166 | } else { |
---|
167 | $checked = ''; |
---|
168 | } |
---|
169 | if ('allone' == $flag) { |
---|
170 | // Print a cell with multidimentioal array checkboxes. |
---|
171 | ?><td><label class="normal"><input type="checkbox" name="dbcol[<?php echo oTxt($db_col); ?>][<?php echo oTxt($item); ?>]"<?php echo $checked; ?> /> <?php echo oTxt($item); ?></label> </td> |
---|
172 | <?php |
---|
173 | } else { |
---|
174 | // Print a cell with basic named checkboxes. |
---|
175 | ?><td><label class="normal"><input type="checkbox" name="<?php echo oTxt($db_col); ?>[<?php echo oTxt($item); ?>]"<?php echo $checked; ?> /> <?php echo oTxt($item); ?></label> </td> |
---|
176 | <?php |
---|
177 | } |
---|
178 | } |
---|
179 | if ($col_cnt < $columns) { |
---|
180 | // This last cell must expand to fill the last blank cells. |
---|
181 | ?><td colspan="<?php echo $columns - $col_cnt; ?>"> </td><?php |
---|
182 | } |
---|
183 | ?> |
---|
184 | </tr> |
---|
185 | </table><?php |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * Prints radio select fields. Works only with enum or set |
---|
191 | * data types in table columns. |
---|
192 | * |
---|
193 | * @param string $db_table database table to lookup |
---|
194 | * @param string $db_col database column to lookup |
---|
195 | * @param array $preselected array of preselected values (matching the values in $db_col) |
---|
196 | * @param int $columns number of table columns to print |
---|
197 | * @param int $flag set to 'allone' for name of input fields to all be the same of a multidimentional array. |
---|
198 | */ |
---|
199 | function printSetRadios($db_table, $db_col, $preselected, $columns=1, $flag=null) |
---|
200 | { |
---|
201 | // Sometimes preselected comes as a comma list. |
---|
202 | if (!is_array($preselected)) { |
---|
203 | $preselected = explode(',', $preselected); |
---|
204 | } |
---|
205 | |
---|
206 | // Checkbox POST data has the primary data in the keys, and 'on' as the values. |
---|
207 | // Here we assume in all the values of an array are 'on' that we can find the data |
---|
208 | // in the array keys. |
---|
209 | $ps_value_count = array_count_values($preselected); |
---|
210 | if ($ps_value_count['on'] == sizeof($preselected)) { |
---|
211 | $preselected = array_keys($preselected); |
---|
212 | } |
---|
213 | |
---|
214 | // Retreive values of a Set or ENUM database column. |
---|
215 | $values = getSetEnumFieldValues($db_table, $db_col); |
---|
216 | |
---|
217 | if (!empty($values)) { |
---|
218 | ?> |
---|
219 | <table> |
---|
220 | <tr> |
---|
221 | <?php |
---|
222 | // Initialize the HTML table generation vars. |
---|
223 | $num_cells = sizeof($values) - 1; |
---|
224 | $num_rows = floor($num_cells / $columns); |
---|
225 | $cols_lastrow = $num_cells % $columns; |
---|
226 | $row_cnt = 0; |
---|
227 | $col_cnt = 0; |
---|
228 | foreach ($values as $item) { |
---|
229 | if ($col_cnt == $columns) { |
---|
230 | // Begin a new row. |
---|
231 | ?></tr><tr><?php |
---|
232 | $col_cnt = 0; |
---|
233 | $row_cnt++; |
---|
234 | } |
---|
235 | if ($col_cnt < $cols_lastrow) { |
---|
236 | $lastrow_add = $col_cnt; |
---|
237 | } else { |
---|
238 | $lastrow_add = $cols_lastrow; |
---|
239 | } |
---|
240 | $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt; |
---|
241 | $col_cnt++; |
---|
242 | |
---|
243 | // Look for preselected value. |
---|
244 | if (in_array($item, $preselected)) { |
---|
245 | $checked = ' checked="checked"'; |
---|
246 | } else { |
---|
247 | $checked = ''; |
---|
248 | } |
---|
249 | // Print a cell with basic named checkboxes. |
---|
250 | ?><td><label class="normal"><input type="radio" name="<?php echo oTxt($db_col); ?>" value="<?php echo oTxt($item) ?>"<?php echo $checked; ?> /> <?php echo oTxt($item); ?></label> </td> |
---|
251 | <?php |
---|
252 | } |
---|
253 | if ($col_cnt < $columns) { |
---|
254 | // This last cell must expand to fill the last blank cells. |
---|
255 | ?><td colspan="<?php echo $columns - $col_cnt; ?>"> </td><?php |
---|
256 | } |
---|
257 | ?> |
---|
258 | </tr> |
---|
259 | </table><?php |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * Prints a pulldown menu containing the specified values and keys of a table. |
---|
265 | * |
---|
266 | * @param string $db_table database table to lookup |
---|
267 | * @param string $key_column column containing the human-readable titles for the select menu |
---|
268 | * @param string $val_column column containing the computer values for the select menu |
---|
269 | * @param string $preselected the currently selected value of the menu. compared to the $val_column |
---|
270 | * @param bool $blank leave one blank at the top? |
---|
271 | * @param string $extra_clause SQL exclude cluase. Something like "WHERE girls != 'buckteeth'" |
---|
272 | */ |
---|
273 | function printSelectForm($db_table, $key_column, $val_column, $preselected, $blank=false, $extra_clause='') |
---|
274 | { |
---|
275 | // Sometimes preselected comes as a comma list. |
---|
276 | if (!is_array($preselected)) { |
---|
277 | $preselected = array($preselected); |
---|
278 | } |
---|
279 | |
---|
280 | // Print a blank first option. |
---|
281 | if (true === $blank) { |
---|
282 | $selected = in_array('', $preselected) ? ' selected="selected"' : ''; |
---|
283 | ?><option value=""<?php echo $selected; ?>> </option> |
---|
284 | <?php |
---|
285 | } |
---|
286 | |
---|
287 | // When the 'blank' value needs a specific key->val pair. |
---|
288 | if (is_array($blank)) { |
---|
289 | foreach ($blank as $key=>$val) { |
---|
290 | $selected = in_array($val, $preselected) ? ' selected="selected"' : ''; |
---|
291 | ?><option value="<?php echo oTxt($key); ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option> |
---|
292 | <?php |
---|
293 | } |
---|
294 | } |
---|
295 | $key_column_name = preg_replace('/^.*?\.?([^\.]+)$/', '$1', $key_column); |
---|
296 | $val_column_name = preg_replace('/^.*?\.?([^\.]+)$/', '$1', $val_column); |
---|
297 | $qid = dbQuery("SELECT $key_column AS `$key_column_name`, $val_column AS `$val_column_name` FROM $db_table $extra_clause",false); |
---|
298 | while ($row = mysql_fetch_assoc($qid)) { |
---|
299 | $selected = in_array($row[$val_column_name], $preselected) ? ' selected="selected"' : ''; |
---|
300 | ?><option value="<?php echo oTxt($row[$val_column_name]); ?>"<?php echo $selected; ?>><?php echo oTxt($row[$key_column_name]); ?></option><?php |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | /** |
---|
305 | * Prints checkbox fields. Works only with enum or set |
---|
306 | * data types in table columns. |
---|
307 | * |
---|
308 | * @param string $db_table database table to lookup |
---|
309 | * @param string $key_column column containing the human-readable titles for the select menu |
---|
310 | * @param string $val_column column containing the computer values for the select menu |
---|
311 | * @param string $preselected the currently selected value of the menu. compared to the $val_column |
---|
312 | * @param int $columns number of table columns to print |
---|
313 | * @param int $extra_clause extra sql to send at end of select statement. |
---|
314 | * @param int $vert_columns display checkboxes in vertical orientation first. |
---|
315 | * @param int $name id="" and name="" attributes for checkboxes. %1$s is replaced with the $val_column, and %2$s with the $box[$val_column]. |
---|
316 | */ |
---|
317 | function printDBCheckboxes($db_table, $key_column, $val_column, $preselected, $columns=1, $extra_clause='', $vert_columns=true, $name='%1$s-%2$s') |
---|
318 | { |
---|
319 | // Sometimes preselected comes as a comma list. |
---|
320 | if (!is_array($preselected)) { |
---|
321 | $preselected = explode(',', $preselected); |
---|
322 | } |
---|
323 | |
---|
324 | // Checkbox POST data has the primary data in the keys, and 'on' as the values. |
---|
325 | // Here we assume in all the values of an array are 'on' that we can find the data |
---|
326 | // in the array keys. |
---|
327 | $ps_value_count = array_count_values($preselected); |
---|
328 | if ($ps_value_count['on'] == sizeof($preselected)) { |
---|
329 | $preselected = array_keys($preselected); |
---|
330 | } |
---|
331 | |
---|
332 | $qid = dbQuery("SELECT $key_column, $val_column FROM $db_table $extra_clause",false); |
---|
333 | while ($row = mysql_fetch_assoc($qid)) { |
---|
334 | $values[] = $row; |
---|
335 | } |
---|
336 | |
---|
337 | // Rearrange array so sort is in vertical columns. |
---|
338 | // if ($vert_columns) { |
---|
339 | // $per_col = ceil(sizeof($values) / $columns); |
---|
340 | // $columns = ceil(sizeof($values) / $per_col); |
---|
341 | // $curr_row = 0; |
---|
342 | // $curr_col = 0; |
---|
343 | // $pos = 0; |
---|
344 | // foreach ($values as $k=>$v) { |
---|
345 | // $pos = $curr_row * $columns + $curr_col; |
---|
346 | // if ($curr_row <= $per_col) { |
---|
347 | // $curr_row++; |
---|
348 | // } else { |
---|
349 | // $curr_row = 0; |
---|
350 | // $curr_col++; |
---|
351 | // } |
---|
352 | // // echo '<li>' . $pos . '-' . $v[$key_column]; |
---|
353 | // $new_values[$pos] = $v; |
---|
354 | // } |
---|
355 | // $values = $new_values; |
---|
356 | // ksort($values); |
---|
357 | // } |
---|
358 | |
---|
359 | if (empty($values)) { |
---|
360 | return false; |
---|
361 | } |
---|
362 | |
---|
363 | // Initialize the HTML table generation vars. |
---|
364 | $num_cells = sizeof($values) - 1; |
---|
365 | $num_rows = floor($num_cells / $columns); |
---|
366 | $cols_lastrow = $num_cells % $columns; |
---|
367 | $row_cnt = 0; |
---|
368 | $col_cnt = 0; |
---|
369 | ?> |
---|
370 | <table border="0" cellspacing="0" cellpadding="0" class="formcheckboxes"> |
---|
371 | <tr> |
---|
372 | <?php |
---|
373 | foreach ($values as $box) { |
---|
374 | if ($col_cnt == $columns) { |
---|
375 | // Begin a new row. |
---|
376 | ?></tr><tr><?php |
---|
377 | $col_cnt = 0; |
---|
378 | $row_cnt++; |
---|
379 | } |
---|
380 | |
---|
381 | if ($col_cnt < $cols_lastrow) { |
---|
382 | $lastrow_add = $col_cnt; |
---|
383 | } else { |
---|
384 | $lastrow_add = $cols_lastrow; |
---|
385 | } |
---|
386 | |
---|
387 | $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt; |
---|
388 | $col_cnt++; |
---|
389 | |
---|
390 | // Look for preselected value. |
---|
391 | if (in_array($box[$val_column], $preselected)) { |
---|
392 | $checked = ' checked="checked"'; |
---|
393 | } else { |
---|
394 | $checked = ''; |
---|
395 | } |
---|
396 | |
---|
397 | // Print a cell with named checkboxes. |
---|
398 | $checkbox_name = oTxt(sprintf($name, $val_column, $box[$val_column])); |
---|
399 | ?><td><input type="checkbox" id="<?php echo $checkbox_name; ?>" name="<?php echo $checkbox_name; ?>"<?php echo $checked; ?> /></td><td><label for="<?php echo $checkbox_name; ?>"><?php echo oTxt($box[$key_column]); ?></label></td> |
---|
400 | <?php |
---|
401 | } |
---|
402 | |
---|
403 | if ($col_cnt < $columns) { |
---|
404 | // This last cell must expand to fill the last blank cells. |
---|
405 | ?><td colspan="<?php echo $columns - $col_cnt; ?>"> </td><?php |
---|
406 | } |
---|
407 | ?> |
---|
408 | </tr> |
---|
409 | </table><?php |
---|
410 | } |
---|
411 | |
---|
412 | ?> |
---|