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

Last change on this file since 338 was 337, checked in by dan, 16 years ago

printEnumRadios wasn't working so I fixed it. Radio buttons use checked="checked" not selected.

File size: 17.1 KB
RevLine 
[1]1<?php
2/**
3 * TemplateGlue.inc.php
4 * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information
5 */
6
7/**
[42]8 * Print URL to download file with BBEdit/Interarchy. "USER" must be a pre-set
[1]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{
[35]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));
[1]18}
19
20/**
[42]21 * Returns an image tag for image specified in $src.
[1]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;
[42]30
[35]31    if (true === ($gis = @getimagesize($filepath)) && !preg_match('/width|height/', $extra)) {
32        $image_size = $gis[3];
33    } else {
[1]34        $image_size = '';
35    }
[42]36
[1]37    return sprintf('<img src="%s" %s alt="%s" %s />',
38        $src,
39        $image_size,
40        oTxt($alt),
41        $extra
42    );
43}
44
45/**
[42]46 * Prints an image tag for image specified in $src.
[1]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.
[334]59 * Use this to generate a pull-down menu of options or to validate the existence
[42]60 * of options. (Quinn 10 Feb 2001)
[1]61 *
62 * @param  string $db_table   database table to lookup
63 * @param  string $db_col     database column to lookup
[295]64 * @param  bool   $sort          Sort the output.
[1]65 * @return array    Array of the set/enum values on success, false on failure.
66 */
[295]67function getSetEnumFieldValues($db_table, $db_col, $sort=false)
[1]68{
[136]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);
[42]73
[1]74    $row = mysql_fetch_row($qid);
75    if (preg_match('/^enum|^set/i', $row[1]) && preg_match_all("/'([^']*)'/", $row[1], $enum)) {
[295]76        if ($sort) {
77            natsort($enum[1]);           
78        }
[1]79        return $enum[1];
80    } else {
[136]81        $app->logMsg(sprintf('No set or enum fields found.', null), LOG_ERR, __FILE__, __LINE__);
[1]82        return false;
83    }
84}
85
86/**
87 * Prints option fields for a select form. Works only with enum or set
[42]88 * data types in table columns.
[1]89 *
90 * @param  string $db_table   database table to lookup
91 * @param  string $db_col     database column to lookup
[295]92 * @param  bool   $sort          Sort the output.
[1]93 */
[295]94function printSetSelectForm($db_table, $db_col, $preselected, $blank=false, $sort=false)
[1]95{
[295]96    $values = getSetEnumFieldValues($db_table, $db_col, $sort);
[1]97    if ($values === false) {
[21]98        ?><option value="">&nbsp;</option>
[1]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    }
[32]115    foreach ($values as $v) {
116        $selected = ($v == $preselected) ? ' selected' : '';
117        ?><option value="<?php echo $v; ?>"<?php echo $selected; ?>><?php echo oTxt($v); ?></option>
[1]118        <?php
119    }
120}
121
122/**
[32]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
[295]127 * @param  bool   $sort          Sort the output.
[32]128 */
[295]129function printEnumRadios($name, $db_table, $db_col, $preselected, $blank=false, $sort=false)
[32]130{
[295]131    $values = getSetEnumFieldValues($db_table, $db_col, $sort);
[32]132    if ($values === false) {
133        return false;
134    }
135
136    foreach ($values as $v) {
[337]137        $selected = ($v == $preselected) ? ' checked="checked"' : '';
[32]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/**
[1]144 * Prints checkbox fields. Works only with enum or set
[42]145 * data types in table columns.
[1]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
[334]151 * @param  int    $flag          set to 'allone' for name of input fields to all be the same of a multidimensional array.
[295]152 * @param  bool   $sort          Sort the output.
[1]153 */
[295]154function printSetCheckboxes($db_table, $db_col, $preselected, $columns=1, $flag=null, $sort)
[42]155{
[1]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    }
[42]164
[1]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);
[64]169    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
[1]170        $preselected = array_keys($preselected);
171    }
[42]172
[334]173    // Retrieve values of a Set or ENUM database column.
[295]174    $values = getSetEnumFieldValues($db_table, $db_col, $sort);
[42]175
[1]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;
[32]182    foreach ($values as $v) {
[1]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++;
[42]196
[1]197        // Look for preselected value.
[32]198        if (in_array($v, $preselected)) {
[1]199            $checked = ' checked="checked"';
200        } else {
201            $checked = '';
202        }
203        if ('allone' == $flag) {
[334]204            // Print a cell with multidimensional array checkboxes.
[32]205            $html_name = 'dbcol[' . $db_col . '][' . $v . ']';
[28]206        } else {
[1]207            // Print a cell with basic named checkboxes.
[32]208            $html_name = $db_col . '[' . $v . ']';
[1]209        }
[32]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>
[28]211        <?php
[1]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
[42]224 * data types in table columns.
[1]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
[295]230 * @param  bool   $sort          Sort the output.
[1]231 */
[295]232function printSetRadios($db_table, $db_col, $preselected, $columns=1, $sort=false)
[42]233{
[1]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    }
[42]242
[1]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);
[53]247    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
[1]248        $preselected = array_keys($preselected);
249    }
[42]250
[334]251    // Retrieve values of a Set or ENUM database column.
[295]252    $values = getSetEnumFieldValues($db_table, $db_col, $sort);
[42]253
[1]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;
[32]260    foreach ($values as $v) {
[1]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++;
[42]274
[1]275        // Look for preselected value.
[32]276        if (in_array($v, $preselected)) {
[1]277            $checked = ' checked="checked"';
278        } else {
279            $checked = '';
280        }
281        // Print a cell with basic named checkboxes.
[53]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>
[1]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/**
[42]295 * Prints a pulldown menu containing the specified values and keys of a table.
[1]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?
[334]302 * @param  string $extra_clause     SQL exclude clause. Something like "WHERE girls != 'buckteeth'"
[1]303 */
[324]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')
[1]305{
[136]306    $db =& DB::getInstance();
307   
[1]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    }
[42]319
[1]320    // When the 'blank' value needs a specific key->val pair.
321    if (is_array($blank)) {
322        foreach ($blank as $key=>$val) {
[323]323            $selected = in_array($key, $preselected) ? ' selected="selected"' : '';
[1]324            ?><option value="<?php echo $key; ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option>
325            <?php
326        }
327    }
[324]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);
[1]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
[42]338 * data types in table columns.
[1]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{
[136]350    $db =& DB::getInstance();
351   
[1]352    // Sometimes preselected comes as a comma list.
353    if (!is_array($preselected)) {
354        $preselected = explode(',', $preselected);
355    }
[42]356
[1]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);
[253]361    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
[1]362        $preselected = array_keys($preselected);
363    }
[42]364
[136]365    $qid = $db->query("SELECT $key_column, $val_column FROM $db_table $extra_clause",false);
[1]366    while ($row = mysql_fetch_assoc($qid)) {
367        $values[] = $row;
368    }
[42]369
[1]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    }
[42]395
[1]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    ?>
[28]403    <table border="0" cellspacing="0" cellpadding="0">
[1]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        }
[42]413
[1]414        if ($col_cnt < $cols_lastrow) {
415            $lastrow_add = $col_cnt;
416        } else {
417            $lastrow_add = $cols_lastrow;
418        }
[42]419
[1]420        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
421        $col_cnt++;
[42]422
[1]423        // Look for preselected value.
424        if (in_array($box[$val_column], $preselected)) {
425            $checked = ' checked="checked"';
426        } else {
427            $checked = '';
428        }
[42]429
[1]430        // Print a cell with basic named checkboxes.
[28]431        $html_name = $val_column . '[' . $box[$val_column] . ']';
[254]432        $html_id = $val_column . '-' . $box[$val_column] . '';
[28]433        ?>
[254]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
[1]437    }
[42]438
[1]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)) {
[197]461        foreach ($submit_buttons as $i => $b) {
462            if (is_array($b)) {
[336]463                $defaults = array();
464                $defaults['type'] = isset($b['type']) ? $b['type'] : 'submit';
465                $defaults['id'] = isset($b['id']) ? $b['id'] : sprintf('sc-%s-button', $b['name']);
466                $b = array_merge($defaults, $b);
467                echo '<input';
468                foreach ($b as $key => $value) {
469                    printf(' %s="%s"', $key, oTxt($value));
470                }
471                echo ' />';
[197]472            } else {
[334]473                // For backwards compatibility.
[197]474                ?><input type="submit" name="<?php echo oTxt($i) ?>" value="<?php echo oTxt($b); ?>" /><?php
475            }
[1]476        }
477    }
478}
479
[337]480?>
Note: See TracBrowser for help on using the repository browser.