source: trunk/lib/TemplateGlue.inc.php @ 253

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

Q - fixed a php notice event.

File size: 16.4 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 * @param   mixed   $file   File path relative to DOCUMENT_ROOT (i.e. '/_templates/index.ihtml').
13 * @return  string  <a> with file basename as text.
14 */
15function bbftp($file)
16{
17    return sprintf('<a href="bbftp://%s@%s:%s%s" title="%s" style="text-decoration: none; border-bottom: blue 1px dotted;">%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 */
27function oImg($src, $alt='', $extra='')
28{
29    $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src;
30
31    if (true === ($gis = @getimagesize($filepath)) && !preg_match('/width|height/', $extra)) {
32        $image_size = $gis[3];
33    } else {
34        $image_size = '';
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 */
52function 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 */
66function getSetEnumFieldValues($db_table, $db_col)
67{
68    $app =& App::getInstance();
69    $db =& DB::getInstance();
70   
71    $qid = $db->query("SHOW COLUMNS FROM " . $db->escapeString($db_table) . " LIKE '" . $db->escapeString($db_col) . "'",false);
72
73    $row = mysql_fetch_row($qid);
74    if (preg_match('/^enum|^set/i', $row[1]) && preg_match_all("/'([^']*)'/", $row[1], $enum)) {
75        return $enum[1];
76    } else {
77        $app->logMsg(sprintf('No set or enum fields found.', null), LOG_ERR, __FILE__, __LINE__);
78        return false;
79    }
80}
81
82/**
83 * Prints option fields for a select form. Works only with enum or set
84 * data types in table columns.
85 *
86 * @param  string $db_table   database table to lookup
87 * @param  string $db_col     database column to lookup
88 */
89function printSetSelectForm($db_table, $db_col, $preselected, $blank=false)
90{
91    $values = getSetEnumFieldValues($db_table, $db_col);
92    if ($values === false) {
93        ?><option value="">&nbsp;</option>
94        <?php
95        return false;
96    }
97    if (true === $blank) {
98        $selected = ($preselected == '') ? ' selected' : '';
99        ?><option value=""<?php echo $selected; ?>>&nbsp;</option>
100        <?php
101    }
102    // When the 'blank' value needs a specific key->val pair.
103    if (is_array($blank)) {
104        foreach ($blank as $key=>$val) {
105            $selected = ($preselected == $val) ? ' selected="selected"' : '';
106            ?><option value="<?php echo $key; ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option>
107            <?php
108        }
109    }
110    foreach ($values as $v) {
111        $selected = ($v == $preselected) ? ' selected' : '';
112        ?><option value="<?php echo $v; ?>"<?php echo $selected; ?>><?php echo oTxt($v); ?></option>
113        <?php
114    }
115}
116
117/**
118 * Prints radio buttons from a set/enum column.
119 *
120 * @param  string $db_table   database table to lookup
121 * @param  string $db_col     database column to lookup
122 */
123function printEnumRadios($name, $db_table, $db_col, $preselected, $blank=false)
124{
125    $values = getSetEnumFieldValues($db_table, $db_col);
126    if ($values === false) {
127        return false;
128    }
129
130    foreach ($values as $v) {
131        $selected = ($v == $preselected) ? ' selected' : '';
132        ?><input type="radio" name="<?php echo $name ?>" id="<?php echo $v; ?>" value="<?php echo $v; ?>"<?php echo $selected; ?> /><label for="<?php echo $v; ?>"><?php echo oTxt($v); ?></label>
133        <?php
134    }
135}
136
137/**
138 * Prints checkbox fields. Works only with enum or set
139 * data types in table columns.
140 *
141 * @param  string $db_table      database table to lookup
142 * @param  string $db_col        database column to lookup
143 * @param  array  $preselected   array of preselected values (matching the values in $db_col)
144 * @param  int    $columns       number of table columns to print
145 * @param  int    $flag          set to 'allone' for name of input fields to all be the same of a multidimentional array.
146 */
147function printSetCheckboxes($db_table, $db_col, $preselected, $columns=1, $flag=null)
148{
149    ?>
150    <table>
151        <tr>
152    <?php
153    // Sometimes preselected comes as a comma list.
154    if (!is_array($preselected)) {
155        $preselected = explode(',', $preselected);
156    }
157
158    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
159    // Here we assume in all the values of an array are 'on' that we can find the data
160    // in the array keys.
161    $ps_value_count = array_count_values($preselected);
162    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
163        $preselected = array_keys($preselected);
164    }
165
166    // Retreive values of a Set or ENUM database column.
167    $values = getSetEnumFieldValues($db_table, $db_col);
168
169    // Initialize the HTML table generation vars.
170    $num_cells = sizeof($values) - 1;
171    $num_rows = floor($num_cells / $columns);
172    $cols_lastrow = $num_cells % $columns;
173    $row_cnt = 0;
174    $col_cnt = 0;
175    foreach ($values as $v) {
176        if ($col_cnt == $columns) {
177            // Begin a new row.
178            ?></tr><tr><?php
179            $col_cnt = 0;
180            $row_cnt++;
181        }
182        if ($col_cnt < $cols_lastrow) {
183            $lastrow_add = $col_cnt;
184        } else {
185            $lastrow_add = $cols_lastrow;
186        }
187        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
188        $col_cnt++;
189
190        // Look for preselected value.
191        if (in_array($v, $preselected)) {
192            $checked = ' checked="checked"';
193        } else {
194            $checked = '';
195        }
196        if ('allone' == $flag) {
197            // Print a cell with multidimentioal array checkboxes.
198            $html_name = 'dbcol[' . $db_col . '][' . $v . ']';
199        } else {
200            // Print a cell with basic named checkboxes.
201            $html_name = $db_col . '[' . $v . ']';
202        }
203        ?><td><input type="checkbox" name="<?php echo $html_name ?>" id="<?php echo $html_name ?>"<?php echo $checked; ?> /><label for="<?php echo $html_name ?>"><?php echo oTxt($v); ?></label></td>
204        <?php
205    }
206    if ($col_cnt < $columns) {
207        // This last cell must expand to fill the last blank cells.
208        ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
209    }
210    ?>
211        </tr>
212    </table><?php
213}
214
215/**
216 * Prints radio select fields. Works only with enum or set
217 * data types in table columns.
218 *
219 * @param  string $db_table      database table to lookup
220 * @param  string $db_col        database column to lookup
221 * @param  array  $preselected   array of preselected values (matching the values in $db_col)
222 * @param  int    $columns       number of table columns to print
223 * @param  int    $flag          set to 'allone' for name of input fields to all be the same of a multidimentional array.
224 */
225function printSetRadios($db_table, $db_col, $preselected, $columns=1, $flag=null)
226{
227    ?>
228    <table>
229        <tr>
230    <?php
231    // Sometimes preselected comes as a comma list.
232    if (!is_array($preselected)) {
233        $preselected = explode(',', $preselected);
234    }
235
236    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
237    // Here we assume in all the values of an array are 'on' that we can find the data
238    // in the array keys.
239    $ps_value_count = array_count_values($preselected);
240    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
241        $preselected = array_keys($preselected);
242    }
243
244    // Retreive values of a Set or ENUM database column.
245    $values = getSetEnumFieldValues($db_table, $db_col);
246
247    // Initialize the HTML table generation vars.
248    $num_cells = sizeof($values) - 1;
249    $num_rows = floor($num_cells / $columns);
250    $cols_lastrow = $num_cells % $columns;
251    $row_cnt = 0;
252    $col_cnt = 0;
253    foreach ($values as $v) {
254        if ($col_cnt == $columns) {
255            // Begin a new row.
256            ?></tr><tr><?php
257            $col_cnt = 0;
258            $row_cnt++;
259        }
260        if ($col_cnt < $cols_lastrow) {
261            $lastrow_add = $col_cnt;
262        } else {
263            $lastrow_add = $cols_lastrow;
264        }
265        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
266        $col_cnt++;
267
268        // Look for preselected value.
269        if (in_array($v, $preselected)) {
270            $checked = ' checked="checked"';
271        } else {
272            $checked = '';
273        }
274        // Print a cell with basic named checkboxes.
275        ?><td><input type="radio" name="<?php echo $db_col; ?>" id="<?php echo $db_col . '-' . $v; ?>" value="<?php echo $v ?>"<?php echo $checked; ?> /><label class="sc-label-secondary" for="<?php echo $db_col . '-' . $v ?>"><?php echo oTxt($v); ?></label></td>
276        <?php
277    }
278    if ($col_cnt < $columns) {
279        // This last cell must expand to fill the last blank cells.
280        ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
281    }
282    ?>
283        </tr>
284    </table><?php
285}
286
287/**
288 * Prints a pulldown menu containing the specified values and keys of a table.
289 *
290 * @param  string $db_table         database table to lookup
291 * @param  string $key_column       column containing the human-readable titles for the select menu
292 * @param  string $val_column       column containing the computer values for the select menu
293 * @param  string $preselected      the currently selected value of the menu. compared to the $val_column
294 * @param  bool   $blank            leave one blank at the top?
295 * @param  string $extra_clause     SQL exclude cluase. Something like "WHERE girls != 'buckteeth'"
296 */
297function printSelectForm($db_table, $key_column, $val_column, $preselected, $blank=false, $extra_clause='')
298{
299    $db =& DB::getInstance();
300   
301    // Sometimes preselected comes as a comma list.
302    if (!is_array($preselected)) {
303        $preselected = array($preselected);
304    }
305
306    // Print a blank first option.
307    if (true === $blank) {
308        $selected = in_array('', $preselected) ? ' selected="selected"' : '';
309        ?><option value=""<?php echo $selected; ?>>&nbsp;</option>
310        <?php
311    }
312
313    // When the 'blank' value needs a specific key->val pair.
314    if (is_array($blank)) {
315        foreach ($blank as $key=>$val) {
316            $selected = in_array($val, $preselected) ? ' selected="selected"' : '';
317            ?><option value="<?php echo $key; ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option>
318            <?php
319        }
320    }
321    $qid = $db->query("SELECT $key_column, $val_column FROM $db_table $extra_clause",false);
322    while ($row = mysql_fetch_assoc($qid)) {
323        $selected = in_array($row[$val_column], $preselected) ? ' selected="selected"' : '';
324        ?><option value="<?php echo $row[$val_column]; ?>"<?php echo $selected; ?>><?php echo oTxt($row[$key_column]); ?></option><?php
325    }
326}
327
328/**
329 * Prints checkbox fields. Works only with enum or set
330 * data types in table columns.
331 *
332 * @param  string $db_table         database table to lookup
333 * @param  string $key_column       column containing the human-readable titles for the select menu
334 * @param  string $val_column       column containing the computer values for the select menu
335 * @param  string $preselected      the currently selected value of the menu. compared to the $val_column
336 * @param  int    $columns          number of table columns to print
337 * @param  int    $extra_clause     extra sql to send at end of select statement.
338 * @param  bool   $vert_columns     display checkboxes in vertical orientation first.
339 */
340function printDBCheckboxes($db_table, $key_column, $val_column, $preselected, $columns=1, $extra_clause='', $vert_columns=false)
341{
342    $db =& DB::getInstance();
343   
344    // Sometimes preselected comes as a comma list.
345    if (!is_array($preselected)) {
346        $preselected = explode(',', $preselected);
347    }
348
349    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
350    // Here we assume in all the values of an array are 'on' that we can find the data
351    // in the array keys.
352    $ps_value_count = array_count_values($preselected);
353    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
354        $preselected = array_keys($preselected);
355    }
356
357    $qid = $db->query("SELECT $key_column, $val_column FROM $db_table $extra_clause",false);
358    while ($row = mysql_fetch_assoc($qid)) {
359        $values[] = $row;
360    }
361
362    // Rearrange array so sort is in vertical columns. FIXME: doesn't work.
363//  if ($vert_columns) {
364//      $per_col = ceil(sizeof($values) / $columns);
365//      $columns = ceil(sizeof($values) / $per_col);
366//      $curr_row = 0;
367//      $curr_col = 0;
368//      $pos = 0;
369//         foreach ($values as $k=>$v) {
370//             $pos = $curr_row * $columns + $curr_col;
371//             if ($curr_row <= $per_col) {
372//                 $curr_row++;
373//             } else {
374//                 $curr_row = 0;
375//                 $curr_col++;
376//             }
377// //             echo '<li>' . $pos . '-' . $v[$key_column];
378//             $new_values[$pos] = $v;
379//         }
380//         $values = $new_values;
381//         ksort($values);
382//  }
383
384    if (empty($values)) {
385        return false;
386    }
387
388    // Initialize the HTML table generation vars.
389    $num_cells = sizeof($values) - 1;
390    $num_rows = floor($num_cells / $columns);
391    $cols_lastrow = $num_cells % $columns;
392    $row_cnt = 0;
393    $col_cnt = 0;
394    ?>
395    <table border="0" cellspacing="0" cellpadding="0">
396        <tr>
397    <?php
398    foreach ($values as $box) {
399        if ($col_cnt == $columns) {
400            // Begin a new row.
401            ?></tr><tr><?php
402            $col_cnt = 0;
403            $row_cnt++;
404        }
405
406        if ($col_cnt < $cols_lastrow) {
407            $lastrow_add = $col_cnt;
408        } else {
409            $lastrow_add = $cols_lastrow;
410        }
411
412        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
413        $col_cnt++;
414
415        // Look for preselected value.
416        if (in_array($box[$val_column], $preselected)) {
417            $checked = ' checked="checked"';
418        } else {
419            $checked = '';
420        }
421
422        // Print a cell with basic named checkboxes.
423        $html_name = $val_column . '[' . $box[$val_column] . ']';
424        ?>
425        <td><input type="checkbox" name="<?php echo $html_name ?>" id="<?php echo $html_name ?>"<?php echo $checked; ?> /></td>
426        <td><label for="<?php echo $html_name ?>" class="normal"><?php echo oTxt($box[$key_column]); ?></label></td>
427        <?php /// FIXME: class=normal?
428    }
429
430    if ($col_cnt < $columns) {
431        // This last cell must expand to fill the last blank cells.
432        ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
433    }
434    ?>
435        </tr>
436    </table><?php
437}
438
439/**
440 * Prints submit buttons based on given array of submit button names and titles.
441 *
442 * @access  public
443 * @param   array   $submit_buttons     Array of buttons, the key being the button name, and value being the title of the button.
444 * @return  void
445 * @author  Quinn Comendant <quinn@strangecode.com>
446 * @version 1.0
447 * @since   15 Jun 2005 13:42:21
448 */
449function printSubmitButtons($submit_buttons)
450{
451    if (is_array($submit_buttons) && !empty($submit_buttons)) {
452        foreach ($submit_buttons as $i => $b) {
453            if (is_array($b)) {
454                ?><input type="submit" name="<?php echo oTxt($b['name']) ?>" value="<?php echo oTxt($b['value']); ?>" accesskey="<?php echo oTxt($b['accesskey']); ?>" /><?php
455            } else {
456                // For backwards compatability.
457                ?><input type="submit" name="<?php echo oTxt($i) ?>" value="<?php echo oTxt($b); ?>" /><?php
458            }
459        }
460    }
461}
462
463?>
Note: See TracBrowser for help on using the repository browser.