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

Last change on this file since 310 was 308, checked in by quinn, 16 years ago

General bug fixes. Backported email checking regex from codebase 2.1.2. Some css mods.

File size: 14.8 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        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 */
81function 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; ?>>&nbsp;</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 $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 $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 */
119function 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 $db_col; ?>][<?php echo $item; ?>]"<?php echo $checked; ?> />&nbsp;<?php echo oTxt($item); ?></label>&nbsp;</td>
172                <?php
173             } else {
174                // Print a cell with basic named checkboxes.
175                ?><td><label class="normal"><input type="checkbox" name="<?php echo $db_col; ?>[<?php echo $item; ?>]"<?php echo $checked; ?> />&nbsp;<?php echo oTxt($item); ?></label>&nbsp;</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; ?>">&nbsp;</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 */
199function 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 $db_col; ?>" value="<?php echo $item ?>"<?php echo $checked; ?> />&nbsp;<?php echo oTxt($item); ?></label>&nbsp;</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; ?>">&nbsp;</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 */
273function 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; ?>>&nbsp;</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    $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 $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 */
316function printDBCheckboxes($db_table, $key_column, $val_column, $preselected, $columns=1, $extra_clause='', $vert_columns=true)
317{
318    // Sometimes preselected comes as a comma list.
319    if (!is_array($preselected)) {
320        $preselected = explode(',', $preselected);
321    }
322   
323    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
324    // Here we assume in all the values of an array are 'on' that we can find the data
325    // in the array keys.
326    $ps_value_count = array_count_values($preselected);
327    if ($ps_value_count['on'] == sizeof($preselected)) {
328        $preselected = array_keys($preselected);
329    }
330   
331    $qid = dbQuery("SELECT $key_column, $val_column FROM $db_table $extra_clause",false);
332    while ($row = mysql_fetch_assoc($qid)) {
333        $values[] = $row;
334    }
335   
336    // Rearrange array so sort is in vertical columns.
337//  if ($vert_columns) {
338//      $per_col = ceil(sizeof($values) / $columns);
339//      $columns = ceil(sizeof($values) / $per_col);
340//      $curr_row = 0;
341//      $curr_col = 0;
342//      $pos = 0;
343//         foreach ($values as $k=>$v) {
344//             $pos = $curr_row * $columns + $curr_col;
345//             if ($curr_row <= $per_col) {
346//                 $curr_row++;
347//             } else {
348//                 $curr_row = 0;
349//                 $curr_col++;
350//             }
351// //             echo '<li>' . $pos . '-' . $v[$key_column];
352//             $new_values[$pos] = $v;
353//         }
354//         $values = $new_values;
355//         ksort($values);
356//  }
357
358    if (empty($values)) {
359        return false;
360    }
361   
362    // Initialize the HTML table generation vars.
363    $num_cells = sizeof($values) - 1;
364    $num_rows = floor($num_cells / $columns);
365    $cols_lastrow = $num_cells % $columns;
366    $row_cnt = 0;
367    $col_cnt = 0;
368    ?>
369    <table border="0" cellspacing="0" cellpadding="0" class="formcheckboxes">
370        <tr>
371    <?php
372    foreach ($values as $box) {
373        if ($col_cnt == $columns) {
374            // Begin a new row.
375            ?></tr><tr><?php
376            $col_cnt = 0;
377            $row_cnt++;
378        }
379       
380        if ($col_cnt < $cols_lastrow) {
381            $lastrow_add = $col_cnt;
382        } else {
383            $lastrow_add = $cols_lastrow;
384        }
385       
386        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
387        $col_cnt++;
388       
389        // Look for preselected value.
390        if (in_array($box[$val_column], $preselected)) {
391            $checked = ' checked="checked"';
392        } else {
393            $checked = '';
394        }
395       
396        // Print a cell with basic named checkboxes.
397        ?><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>
398        <?php
399    }
400   
401    if ($col_cnt < $columns) {
402        // This last cell must expand to fill the last blank cells.
403        ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
404    }
405    ?>
406        </tr>
407    </table><?php
408}
409
410?>
Note: See TracBrowser for help on using the repository browser.