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

Last change on this file since 98 was 64, checked in by scdev, 18 years ago

${1}

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