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

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

Fixed getRemoteAddr() to return only one value of a comma-delimited list.

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