source: branches/1.1dev/lib/TemplateGlue.inc.php @ 283

Last change on this file since 283 was 283, checked in by quinn, 17 years ago

Added <label> tags to TemplateGlue? forms

File size: 14.5 KB
Line 
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 */
17function 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 */
29function 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}
46function 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 */
61function 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        return $enum[1];
68    } else {
69        return false;
70    }
71}
72
73/**
74 * Prints option fields for a select form. Works only with enum or set
75 * data types in table columns.
76 *
77 * @param  string $db_table   database table to lookup
78 * @param  string $db_col     database column to lookup
79 */
80function printSetSelectForm($db_table, $db_col, $preselected, $blank=false)
81{
82    $values = getSetEnumFieldValues($db_table, $db_col);
83    if ($values === false) {
84        ?><option value=""><?php echo _("n/a"); ?></option>
85        <?php
86        return false;
87    }
88    if (true === $blank) {
89        $selected = ($preselected == '') ? ' selected' : '';
90        ?><option value=""<?php echo $selected; ?>>&nbsp;</option>
91        <?php
92    }
93    // When the 'blank' value needs a specific key->val pair.
94    if (is_array($blank)) {
95        foreach ($blank as $key=>$val) {
96            $selected = ($preselected == $val) ? ' selected="selected"' : '';
97            ?><option value="<?php echo $key; ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option>
98            <?php
99        }
100    }
101    foreach ($values as $item) {
102        $selected = ($item == $preselected) ? ' selected' : '';
103        ?><option value="<?php echo $item; ?>"<?php echo $selected; ?>><?php echo oTxt($item); ?></option>
104        <?php
105    }
106}
107
108/**
109 * Prints checkbox fields. Works only with enum or set
110 * data types in table columns.
111 *
112 * @param  string $db_table      database table to lookup
113 * @param  string $db_col        database column to lookup
114 * @param  array  $preselected   array of preselected values (matching the values in $db_col)
115 * @param  int    $columns       number of table columns to print
116 * @param  int    $flag          set to 'allone' for name of input fields to all be the same of a multidimentional array.
117 */
118function printSetCheckboxes($db_table, $db_col, $preselected, $columns=1, $flag=null)
119{   
120    // Sometimes preselected comes as a comma list.
121    if (!is_array($preselected)) {
122        $preselected = explode(',', $preselected);
123    }
124   
125    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
126    // Here we assume in all the values of an array are 'on' that we can find the data
127    // in the array keys.
128    $ps_value_count = array_count_values($preselected);
129    if ($ps_value_count['on'] == sizeof($preselected)) {
130        $preselected = array_keys($preselected);
131    }
132   
133    // Retreive values of a Set or ENUM database column.
134    $values = getSetEnumFieldValues($db_table, $db_col);
135   
136    if (!empty($values)) {
137        ?>
138        <table>
139            <tr>
140        <?php
141        // Initialize the HTML table generation vars.
142        $num_cells = sizeof($values) - 1;
143        $num_rows = floor($num_cells / $columns);
144        $cols_lastrow = $num_cells % $columns;
145        $row_cnt = 0;
146        $col_cnt = 0;
147        foreach ($values as $item) {
148            if ($col_cnt == $columns) {
149                // Begin a new row.
150                ?></tr><tr><?php
151                $col_cnt = 0;
152                $row_cnt++;
153            }
154            if ($col_cnt < $cols_lastrow) {
155                $lastrow_add = $col_cnt;
156            } else {
157                $lastrow_add = $cols_lastrow;
158            }
159            $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
160            $col_cnt++;
161       
162            // Look for preselected value.
163            if (in_array($item, $preselected)) {
164                $checked = ' checked="checked"';
165            } else {
166                $checked = '';
167            }
168            if ('allone' == $flag) {
169                // Print a cell with multidimentioal array checkboxes.
170                ?><td><label><input type="checkbox" name="dbcol[<?php echo $db_col; ?>][<?php echo $item; ?>]"<?php echo $checked; ?> />&nbsp;<?php echo oTxt($item); ?></label>&nbsp;</td>
171                <?php
172             } else {
173                // Print a cell with basic named checkboxes.
174                ?><td><label><input type="checkbox" name="<?php echo $db_col; ?>[<?php echo $item; ?>]"<?php echo $checked; ?> />&nbsp;<?php echo oTxt($item); ?></label>&nbsp;</td>
175                <?php
176            }
177        }
178        if ($col_cnt < $columns) {
179            // This last cell must expand to fill the last blank cells.
180            ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
181        }
182        ?>
183            </tr>
184        </table><?php
185    }
186}
187
188/**
189 * Prints radio select fields. Works only with enum or set
190 * data types in table columns.
191 *
192 * @param  string $db_table      database table to lookup
193 * @param  string $db_col        database column to lookup
194 * @param  array  $preselected   array of preselected values (matching the values in $db_col)
195 * @param  int    $columns       number of table columns to print
196 * @param  int    $flag          set to 'allone' for name of input fields to all be the same of a multidimentional array.
197 */
198function printSetRadios($db_table, $db_col, $preselected, $columns=1, $flag=null)
199{   
200    // Sometimes preselected comes as a comma list.
201    if (!is_array($preselected)) {
202        $preselected = explode(',', $preselected);
203    }
204   
205    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
206    // Here we assume in all the values of an array are 'on' that we can find the data
207    // in the array keys.
208    $ps_value_count = array_count_values($preselected);
209    if ($ps_value_count['on'] == sizeof($preselected)) {
210        $preselected = array_keys($preselected);
211    }
212   
213    // Retreive values of a Set or ENUM database column.
214    $values = getSetEnumFieldValues($db_table, $db_col);
215   
216    if (!empty($values)) {
217        ?>
218        <table>
219            <tr>
220        <?php
221        // Initialize the HTML table generation vars.
222        $num_cells = sizeof($values) - 1;
223        $num_rows = floor($num_cells / $columns);
224        $cols_lastrow = $num_cells % $columns;
225        $row_cnt = 0;
226        $col_cnt = 0;
227        foreach ($values as $item) {
228            if ($col_cnt == $columns) {
229                // Begin a new row.
230                ?></tr><tr><?php
231                $col_cnt = 0;
232                $row_cnt++;
233            }
234            if ($col_cnt < $cols_lastrow) {
235                $lastrow_add = $col_cnt;
236            } else {
237                $lastrow_add = $cols_lastrow;
238            }
239            $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
240            $col_cnt++;
241       
242            // Look for preselected value.
243            if (in_array($item, $preselected)) {
244                $checked = ' checked="checked"';
245            } else {
246                $checked = '';
247            }
248            // Print a cell with basic named checkboxes.
249            ?><td><label><input type="radio" name="<?php echo $db_col; ?>" value="<?php echo $item ?>"<?php echo $checked; ?> />&nbsp;<?php echo oTxt($item); ?></label>&nbsp;</td>
250            <?php
251        }
252        if ($col_cnt < $columns) {
253            // This last cell must expand to fill the last blank cells.
254            ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
255        }
256        ?>
257            </tr>
258        </table><?php
259    }
260}
261
262/**
263 * Prints a pulldown menu containing the specified values and keys of a table.
264 *
265 * @param  string $db_table         database table to lookup
266 * @param  string $key_column       column containing the human-readable titles for the select menu
267 * @param  string $val_column       column containing the computer values for the select menu
268 * @param  string $preselected      the currently selected value of the menu. compared to the $val_column
269 * @param  bool   $blank            leave one blank at the top?
270 * @param  string $extra_clause     SQL exclude cluase. Something like "WHERE girls != 'buckteeth'"
271 */
272function printSelectForm($db_table, $key_column, $val_column, $preselected, $blank=false, $extra_clause='')
273{
274    // Sometimes preselected comes as a comma list.
275    if (!is_array($preselected)) {
276        $preselected = array($preselected);
277    }
278
279    // Print a blank first option.
280    if (true === $blank) {
281        $selected = in_array('', $preselected) ? ' selected="selected"' : '';
282        ?><option value=""<?php echo $selected; ?>>&nbsp;</option>
283        <?php
284    }
285   
286    // When the 'blank' value needs a specific key->val pair.
287    if (is_array($blank)) {
288        foreach ($blank as $key=>$val) {
289            $selected = in_array($val, $preselected) ? ' selected="selected"' : '';
290            ?><option value="<?php echo $key; ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option>
291            <?php
292        }
293    }
294    $qid = dbQuery("SELECT $key_column, $val_column FROM $db_table $extra_clause",false);
295    while ($row = mysql_fetch_assoc($qid)) {
296        $selected = in_array($row[$val_column], $preselected) ? ' selected="selected"' : '';
297        ?><option value="<?php echo $row[$val_column]; ?>"<?php echo $selected; ?>><?php echo oTxt($row[$key_column]); ?></option><?php
298    }
299}
300
301/**
302 * Prints checkbox fields. Works only with enum or set
303 * data types in table columns.
304 *
305 * @param  string $db_table         database table to lookup
306 * @param  string $key_column       column containing the human-readable titles for the select menu
307 * @param  string $val_column       column containing the computer values for the select menu
308 * @param  string $preselected      the currently selected value of the menu. compared to the $val_column
309 * @param  int    $columns          number of table columns to print
310 * @param  int    $extra_clause     extra sql to send at end of select statement.
311 * @param  int    $vert_columns     display checkboxes in vertical orientation first.
312 */
313function printDBCheckboxes($db_table, $key_column, $val_column, $preselected, $columns=1, $extra_clause='', $vert_columns=true)
314{
315    // Sometimes preselected comes as a comma list.
316    if (!is_array($preselected)) {
317        $preselected = explode(',', $preselected);
318    }
319   
320    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
321    // Here we assume in all the values of an array are 'on' that we can find the data
322    // in the array keys.
323    $ps_value_count = array_count_values($preselected);
324    if ($ps_value_count['on'] == sizeof($preselected)) {
325        $preselected = array_keys($preselected);
326    }
327   
328    $qid = dbQuery("SELECT $key_column, $val_column FROM $db_table $extra_clause",false);
329    while ($row = mysql_fetch_assoc($qid)) {
330        $values[] = $row;
331    }
332   
333    // Rearrange array so sort is in vertical columns.
334//  if ($vert_columns) {
335//      $per_col = ceil(sizeof($values) / $columns);
336//      $columns = ceil(sizeof($values) / $per_col);
337//      $curr_row = 0;
338//      $curr_col = 0;
339//      $pos = 0;
340//         foreach ($values as $k=>$v) {
341//             $pos = $curr_row * $columns + $curr_col;
342//             if ($curr_row <= $per_col) {
343//                 $curr_row++;
344//             } else {
345//                 $curr_row = 0;
346//                 $curr_col++;
347//             }
348// //             echo '<li>' . $pos . '-' . $v[$key_column];
349//             $new_values[$pos] = $v;
350//         }
351//         $values = $new_values;
352//         ksort($values);
353//  }
354
355    if (empty($values)) {
356        return false;
357    }
358   
359    // Initialize the HTML table generation vars.
360    $num_cells = sizeof($values) - 1;
361    $num_rows = floor($num_cells / $columns);
362    $cols_lastrow = $num_cells % $columns;
363    $row_cnt = 0;
364    $col_cnt = 0;
365    ?>
366    <table border="0" cellspacing="0" cellpadding="0" class="formcheckboxes">
367        <tr>
368    <?php
369    foreach ($values as $box) {
370        if ($col_cnt == $columns) {
371            // Begin a new row.
372            ?></tr><tr><?php
373            $col_cnt = 0;
374            $row_cnt++;
375        }
376       
377        if ($col_cnt < $cols_lastrow) {
378            $lastrow_add = $col_cnt;
379        } else {
380            $lastrow_add = $cols_lastrow;
381        }
382       
383        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
384        $col_cnt++;
385       
386        // Look for preselected value.
387        if (in_array($box[$val_column], $preselected)) {
388            $checked = ' checked="checked"';
389        } else {
390            $checked = '';
391        }
392       
393        // Print a cell with basic named checkboxes.
394        ?><td><input type="checkbox" id="<?php echo $val_column . '-' . $box[$val_column]; ?>" name="<?php echo $val_column; ?>[<?php echo $box[$val_column]; ?>]"<?php echo $checked; ?> /></td><td><label for="<?php echo $val_column . '-' . $box[$val_column]; ?>"><?php echo oTxt($box[$key_column]); ?></label></td>
395        <?php
396    }
397   
398    if ($col_cnt < $columns) {
399        // This last cell must expand to fill the last blank cells.
400        ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
401    }
402    ?>
403        </tr>
404    </table><?php
405}
406
407?>
Note: See TracBrowser for help on using the repository browser.