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