source: tags/1.0.0/lib/TemplateGlue.inc.php @ 1

Last change on this file since 1 was 1, checked in by scdev, 19 years ago

Initial import.

File size: 13.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 *
13 * @param   mixed   $file   File path relative to DOCUMENT_ROOT (i.e. '/_templates/index.ihtml').
14 *
15 * @return  string  <a> with file basename as text.
16 */
17function bbftp($file)
18{
19    return sprintf('<a href="bbftp://%s@%s:%s%s" title="%s">%s</a>', $_SERVER['USER'], $_SERVER['HTTP_HOST'], SITE_BASE, $file, $file, basename($file));
20}
21
22/**
23 * Prints an image tag for image specified in $src.
24 *
25 * @param  string $src     File name of the image, including dir and file extension.
26 * @param  string $alt     Alt tag text.
27 * @param  string $extra   Additional attributes.
28 */
29function oImg($src, $alt='', $extra='')
30{
31    $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src;
32   
33    if (false === ($gis = @getimagesize($filepath)) || preg_match('/width|height/', $extra)) {
34        $image_size = '';
35    } else {
36        $image_size = $gis[3];
37    }
38   
39    return sprintf('<img src="%s" %s alt="%s" %s />',
40        $src,
41        $image_size,
42        oTxt($alt),
43        $extra
44    );
45}
46function printImg($src, $alt='', $extra='')
47{
48    echo oImg($src, $alt, $extra);
49}
50
51/**
52 * Finds the values of an enumeration or set column of a MySQL database, returning them in an array.
53 * Use this to generate a pull-down menu of options or to validate the existance
54 * of options. (Quinn 10 Feb 2001)
55 *
56 * @param  string $db_table   database table to lookup
57 * @param  string $db_col     database column to lookup
58 *
59 * @return array    Array of the set/enum values on success, false on failure.
60 */
61function getSetEnumFieldValues($db_table, $db_col)
62{
63    $qid = dbQuery("SHOW COLUMNS FROM $db_table LIKE '$db_col'",false);
64       
65    $row = mysql_fetch_row($qid);
66    if (preg_match('/^enum|^set/i', $row[1]) && preg_match_all("/'([^']*)'/", $row[1], $enum)) {
67        return $enum[1];
68    } else {
69        return false;
70    }
71}
72
73/**
74 * Prints option fields for a select form. Works only with enum or set
75 * data types in table columns.
76 *
77 * @param  string $db_table   database table to lookup
78 * @param  string $db_col     database column to lookup
79 */
80function printSetSelectForm($db_table, $db_col, $preselected, $blank=false)
81{
82    $values = getSetEnumFieldValues($db_table, $db_col);
83    if ($values === false) {
84        ?><option value=""><?php echo _("n/a"); ?></option>
85        <?php
86        return false;
87    }
88    if (true === $blank) {
89        $selected = ($preselected == '') ? ' selected' : '';
90        ?><option value=""<?php echo $selected; ?>>&nbsp;</option>
91        <?php
92    }
93    // When the 'blank' value needs a specific key->val pair.
94    if (is_array($blank)) {
95        foreach ($blank as $key=>$val) {
96            $selected = ($preselected == $val) ? ' selected="selected"' : '';
97            ?><option value="<?php echo $key; ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option>
98            <?php
99        }
100    }
101    foreach ($values as $item) {
102        $selected = ($item == $preselected) ? ' selected' : '';
103        ?><option value="<?php echo $item; ?>"<?php echo $selected; ?>><?php echo oTxt($item); ?></option>
104        <?php
105    }
106}
107
108/**
109 * Prints checkbox fields. Works only with enum or set
110 * data types in table columns.
111 *
112 * @param  string $db_table      database table to lookup
113 * @param  string $db_col        database column to lookup
114 * @param  array  $preselected   array of preselected values (matching the values in $db_col)
115 * @param  int    $columns       number of table columns to print
116 * @param  int    $flag          set to 'allone' for name of input fields to all be the same of a multidimentional array.
117 */
118function printSetCheckboxes($db_table, $db_col, $preselected, $columns=1, $flag=null)
119{   
120    ?>
121    <table>
122        <tr>
123    <?php
124    // Sometimes preselected comes as a comma list.
125    if (!is_array($preselected)) {
126        $preselected = explode(',', $preselected);
127    }
128   
129    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
130    // Here we assume in all the values of an array are 'on' that we can find the data
131    // in the array keys.
132    $ps_value_count = array_count_values($preselected);
133    if ($ps_value_count['on'] == sizeof($preselected)) {
134        $preselected = array_keys($preselected);
135    }
136   
137    // Retreive values of a Set or ENUM database column.
138    $values = getSetEnumFieldValues($db_table, $db_col);
139   
140    // Initialize the HTML table generation vars.
141    $num_cells = sizeof($values) - 1;
142    $num_rows = floor($num_cells / $columns);
143    $cols_lastrow = $num_cells % $columns;
144    $row_cnt = 0;
145    $col_cnt = 0;
146    foreach ($values as $item) {
147        if ($col_cnt == $columns) {
148            // Begin a new row.
149            ?></tr><tr><?php
150            $col_cnt = 0;
151            $row_cnt++;
152        }
153        if ($col_cnt < $cols_lastrow) {
154            $lastrow_add = $col_cnt;
155        } else {
156            $lastrow_add = $cols_lastrow;
157        }
158        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
159        $col_cnt++;
160       
161        // Look for preselected value.
162        if (in_array($item, $preselected)) {
163            $checked = ' checked="checked"';
164        } else {
165            $checked = '';
166        }
167        if ('allone' == $flag) {
168            // Print a cell with multidimentioal array checkboxes.
169            ?><td><input type="checkbox" name="dbcol[<?php echo $db_col; ?>][<?php echo $item; ?>]"<?php echo $checked; ?> />&nbsp;<?php echo oTxt($item); ?>&nbsp;</td>
170            <?php
171         } else {
172            // Print a cell with basic named checkboxes.
173            ?><td><input type="checkbox" name="<?php echo $db_col; ?>[<?php echo $item; ?>]"<?php echo $checked; ?> />&nbsp;<?php echo oTxt($item); ?>&nbsp;</td>
174            <?php
175        }
176    }
177    if ($col_cnt < $columns) {
178        // This last cell must expand to fill the last blank cells.
179        ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
180    }
181    ?>
182        </tr>
183    </table><?php
184}
185
186/**
187 * Prints radio select fields. Works only with enum or set
188 * data types in table columns.
189 *
190 * @param  string $db_table      database table to lookup
191 * @param  string $db_col        database column to lookup
192 * @param  array  $preselected   array of preselected values (matching the values in $db_col)
193 * @param  int    $columns       number of table columns to print
194 * @param  int    $flag          set to 'allone' for name of input fields to all be the same of a multidimentional array.
195 */
196function printSetRadios($db_table, $db_col, $preselected, $columns=1, $flag=null)
197{   
198    ?>
199    <table>
200        <tr>
201    <?php
202    // Sometimes preselected comes as a comma list.
203    if (!is_array($preselected)) {
204        $preselected = explode(',', $preselected);
205    }
206   
207    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
208    // Here we assume in all the values of an array are 'on' that we can find the data
209    // in the array keys.
210    $ps_value_count = array_count_values($preselected);
211    if ($ps_value_count['on'] == sizeof($preselected)) {
212        $preselected = array_keys($preselected);
213    }
214   
215    // Retreive values of a Set or ENUM database column.
216    $values = getSetEnumFieldValues($db_table, $db_col);
217   
218    // Initialize the HTML table generation vars.
219    $num_cells = sizeof($values) - 1;
220    $num_rows = floor($num_cells / $columns);
221    $cols_lastrow = $num_cells % $columns;
222    $row_cnt = 0;
223    $col_cnt = 0;
224    foreach ($values as $item) {
225        if ($col_cnt == $columns) {
226            // Begin a new row.
227            ?></tr><tr><?php
228            $col_cnt = 0;
229            $row_cnt++;
230        }
231        if ($col_cnt < $cols_lastrow) {
232            $lastrow_add = $col_cnt;
233        } else {
234            $lastrow_add = $cols_lastrow;
235        }
236        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
237        $col_cnt++;
238       
239        // Look for preselected value.
240        if (in_array($item, $preselected)) {
241            $checked = ' checked="checked"';
242        } else {
243            $checked = '';
244        }
245        // Print a cell with basic named checkboxes.
246        ?><td><input type="radio" name="<?php echo $db_col; ?>" value="<?php echo $item ?>"<?php echo $checked; ?> />&nbsp;<?php echo oTxt($item); ?>&nbsp;</td>
247        <?php
248    }
249    if ($col_cnt < $columns) {
250        // This last cell must expand to fill the last blank cells.
251        ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
252    }
253    ?>
254        </tr>
255    </table><?php
256}
257
258/**
259 * Prints a pulldown menu containing the specified values and keys of a table.
260 *
261 * @param  string $db_table         database table to lookup
262 * @param  string $key_column       column containing the human-readable titles for the select menu
263 * @param  string $val_column       column containing the computer values for the select menu
264 * @param  string $preselected      the currently selected value of the menu. compared to the $val_column
265 * @param  bool   $blank            leave one blank at the top?
266 * @param  string $extra_clause     SQL exclude cluase. Something like "WHERE girls != 'buckteeth'"
267 */
268function printSelectForm($db_table, $key_column, $val_column, $preselected, $blank=false, $extra_clause='')
269{
270    // Sometimes preselected comes as a comma list.
271    if (!is_array($preselected)) {
272        $preselected = array($preselected);
273    }
274
275    // Print a blank first option.
276    if (true === $blank) {
277        $selected = in_array('', $preselected) ? ' selected="selected"' : '';
278        ?><option value=""<?php echo $selected; ?>>&nbsp;</option>
279        <?php
280    }
281   
282    // When the 'blank' value needs a specific key->val pair.
283    if (is_array($blank)) {
284        foreach ($blank as $key=>$val) {
285            $selected = in_array($val, $preselected) ? ' selected="selected"' : '';
286            ?><option value="<?php echo $key; ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option>
287            <?php
288        }
289    }
290    $qid = dbQuery("SELECT $key_column, $val_column FROM $db_table $extra_clause",false);
291    while ($row = mysql_fetch_assoc($qid)) {
292        $selected = in_array($row[$val_column], $preselected) ? ' selected="selected"' : '';
293        ?><option value="<?php echo $row[$val_column]; ?>"<?php echo $selected; ?>><?php echo oTxt($row[$key_column]); ?></option><?php
294    }
295}
296
297/**
298 * Prints checkbox fields. Works only with enum or set
299 * data types in table columns.
300 *
301 * @param  string $db_table         database table to lookup
302 * @param  string $key_column       column containing the human-readable titles for the select menu
303 * @param  string $val_column       column containing the computer values for the select menu
304 * @param  string $preselected      the currently selected value of the menu. compared to the $val_column
305 * @param  int    $columns          number of table columns to print
306 * @param  int    $extra_clause     extra sql to send at end of select statement.
307 * @param  int    $vert_columns     display checkboxes in vertical orientation first.
308 */
309function printDBCheckboxes($db_table, $key_column, $val_column, $preselected, $columns=1, $extra_clause='', $vert_columns=true)
310{
311    // Sometimes preselected comes as a comma list.
312    if (!is_array($preselected)) {
313        $preselected = explode(',', $preselected);
314    }
315   
316    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
317    // Here we assume in all the values of an array are 'on' that we can find the data
318    // in the array keys.
319    $ps_value_count = array_count_values($preselected);
320    if ($ps_value_count['on'] == sizeof($preselected)) {
321        $preselected = array_keys($preselected);
322    }
323   
324    $qid = dbQuery("SELECT $key_column, $val_column FROM $db_table $extra_clause",false);
325    while ($row = mysql_fetch_assoc($qid)) {
326        $values[] = $row;
327    }
328   
329    // Rearrange array so sort is in vertical columns.
330//  if ($vert_columns) {
331//      $per_col = ceil(sizeof($values) / $columns);
332//      $columns = ceil(sizeof($values) / $per_col);
333//      $curr_row = 0;
334//      $curr_col = 0;
335//      $pos = 0;
336//         foreach ($values as $k=>$v) {
337//             $pos = $curr_row * $columns + $curr_col;
338//             if ($curr_row <= $per_col) {
339//                 $curr_row++;
340//             } else {
341//                 $curr_row = 0;
342//                 $curr_col++;
343//             }
344// //             echo '<li>' . $pos . '-' . $v[$key_column];
345//             $new_values[$pos] = $v;
346//         }
347//         $values = $new_values;
348//         ksort($values);
349//  }
350
351    if (empty($values)) {
352        return false;
353    }
354   
355    // Initialize the HTML table generation vars.
356    $num_cells = sizeof($values) - 1;
357    $num_rows = floor($num_cells / $columns);
358    $cols_lastrow = $num_cells % $columns;
359    $row_cnt = 0;
360    $col_cnt = 0;
361    ?>
362    <table border="0" cellspacing="0" cellpadding="0" class="formcheckboxes">
363        <tr>
364    <?php
365    foreach ($values as $box) {
366        if ($col_cnt == $columns) {
367            // Begin a new row.
368            ?></tr><tr><?php
369            $col_cnt = 0;
370            $row_cnt++;
371        }
372       
373        if ($col_cnt < $cols_lastrow) {
374            $lastrow_add = $col_cnt;
375        } else {
376            $lastrow_add = $cols_lastrow;
377        }
378       
379        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
380        $col_cnt++;
381       
382        // Look for preselected value.
383        if (in_array($box[$val_column], $preselected)) {
384            $checked = ' checked="checked"';
385        } else {
386            $checked = '';
387        }
388       
389        // Print a cell with basic named checkboxes.
390        ?><td><input type="checkbox" name="<?php echo $val_column; ?>[<?php echo $box[$val_column]; ?>]"<?php echo $checked; ?> /></td><td><?php echo oTxt($box[$key_column]); ?></td>
391        <?php
392    }
393   
394    if ($col_cnt < $columns) {
395        // This last cell must expand to fill the last blank cells.
396        ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
397    }
398    ?>
399        </tr>
400    </table><?php
401}
402
403?>
Note: See TracBrowser for help on using the repository browser.