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

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

More random updates. Improved self-instantiation pattern in SessionCache? to match that of App. More little tweaks.

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