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

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

Updated example config file. Added admin2.inc.css and minor corrections into HTML. Module maker fixes.

File size: 16.7 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 * @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 multidimentional 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    // Retreive 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 multidimentioal 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    // Retreive 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 cluase. Something like "WHERE girls != 'buckteeth'"
303 */
304function printSelectForm($db_table, $key_column, $val_column, $preselected, $blank=false, $extra_clause='')
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($val, $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    while ($row = mysql_fetch_assoc($qid)) {
330        $selected = in_array($row[$val_column], $preselected) ? ' selected="selected"' : '';
331        ?><option value="<?php echo $row[$val_column]; ?>"<?php echo $selected; ?>><?php echo oTxt($row[$key_column]); ?></option><?php
332    }
333}
334
335/**
336 * Prints checkbox fields. Works only with enum or set
337 * data types in table columns.
338 *
339 * @param  string $db_table         database table to lookup
340 * @param  string $key_column       column containing the human-readable titles for the select menu
341 * @param  string $val_column       column containing the computer values for the select menu
342 * @param  string $preselected      the currently selected value of the menu. compared to the $val_column
343 * @param  int    $columns          number of table columns to print
344 * @param  int    $extra_clause     extra sql to send at end of select statement.
345 * @param  bool   $vert_columns     display checkboxes in vertical orientation first.
346 */
347function printDBCheckboxes($db_table, $key_column, $val_column, $preselected, $columns=1, $extra_clause='', $vert_columns=false)
348{
349    $db =& DB::getInstance();
350   
351    // Sometimes preselected comes as a comma list.
352    if (!is_array($preselected)) {
353        $preselected = explode(',', $preselected);
354    }
355
356    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
357    // Here we assume in all the values of an array are 'on' that we can find the data
358    // in the array keys.
359    $ps_value_count = array_count_values($preselected);
360    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
361        $preselected = array_keys($preselected);
362    }
363
364    $qid = $db->query("SELECT $key_column, $val_column FROM $db_table $extra_clause",false);
365    while ($row = mysql_fetch_assoc($qid)) {
366        $values[] = $row;
367    }
368
369    // Rearrange array so sort is in vertical columns. FIXME: doesn't work.
370//  if ($vert_columns) {
371//      $per_col = ceil(sizeof($values) / $columns);
372//      $columns = ceil(sizeof($values) / $per_col);
373//      $curr_row = 0;
374//      $curr_col = 0;
375//      $pos = 0;
376//         foreach ($values as $k=>$v) {
377//             $pos = $curr_row * $columns + $curr_col;
378//             if ($curr_row <= $per_col) {
379//                 $curr_row++;
380//             } else {
381//                 $curr_row = 0;
382//                 $curr_col++;
383//             }
384// //             echo '<li>' . $pos . '-' . $v[$key_column];
385//             $new_values[$pos] = $v;
386//         }
387//         $values = $new_values;
388//         ksort($values);
389//  }
390
391    if (empty($values)) {
392        return false;
393    }
394
395    // Initialize the HTML table generation vars.
396    $num_cells = sizeof($values) - 1;
397    $num_rows = floor($num_cells / $columns);
398    $cols_lastrow = $num_cells % $columns;
399    $row_cnt = 0;
400    $col_cnt = 0;
401    ?>
402    <table border="0" cellspacing="0" cellpadding="0">
403        <tr>
404    <?php
405    foreach ($values as $box) {
406        if ($col_cnt == $columns) {
407            // Begin a new row.
408            ?></tr><tr><?php
409            $col_cnt = 0;
410            $row_cnt++;
411        }
412
413        if ($col_cnt < $cols_lastrow) {
414            $lastrow_add = $col_cnt;
415        } else {
416            $lastrow_add = $cols_lastrow;
417        }
418
419        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
420        $col_cnt++;
421
422        // Look for preselected value.
423        if (in_array($box[$val_column], $preselected)) {
424            $checked = ' checked="checked"';
425        } else {
426            $checked = '';
427        }
428
429        // Print a cell with basic named checkboxes.
430        $html_name = $val_column . '[' . $box[$val_column] . ']';
431        $html_id = $val_column . '-' . $box[$val_column] . '';
432        ?>
433        <td><input type="checkbox" name="<?php echo $html_name ?>" id="<?php echo $html_id ?>"<?php echo $checked; ?> /></td>
434        <td><label for="<?php echo $html_id ?>" class="sc-normal"><?php echo oTxt($box[$key_column]); ?></label></td>
435        <?php
436    }
437
438    if ($col_cnt < $columns) {
439        // This last cell must expand to fill the last blank cells.
440        ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
441    }
442    ?>
443        </tr>
444    </table><?php
445}
446
447/**
448 * Prints submit buttons based on given array of submit button names and titles.
449 *
450 * @access  public
451 * @param   array   $submit_buttons     Array of buttons, the key being the button name, and value being the title of the button.
452 * @return  void
453 * @author  Quinn Comendant <quinn@strangecode.com>
454 * @version 1.0
455 * @since   15 Jun 2005 13:42:21
456 */
457function printSubmitButtons($submit_buttons)
458{
459    if (is_array($submit_buttons) && !empty($submit_buttons)) {
460        foreach ($submit_buttons as $i => $b) {
461            if (is_array($b)) {
462                ?><input type="submit" name="<?php echo oTxt($b['name']) ?>" value="<?php echo oTxt($b['value']); ?>" accesskey="<?php echo oTxt($b['accesskey']); ?>" /><?php
463            } else {
464                // For backwards compatability.
465                ?><input type="submit" name="<?php echo oTxt($i) ?>" value="<?php echo oTxt($b); ?>" /><?php
466            }
467        }
468    }
469}
470
471?>
Note: See TracBrowser for help on using the repository browser.