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

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

Fixed lots of misplings. I'm so embarrassed! ;P

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