source: trunk/lib/TemplateGlue.inc.php

Last change on this file was 799, checked in by anonymous, 9 months ago

Fix a few PHP Deprecated issues

File size: 18.3 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2012 Strangecode, LLC
6 *
7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/**
24 * TemplateGlue.inc.php
25 */
26
27/**
28 * Returns an image tag for image specified in $src.
29 *
30 * @param  string $src     File name of the image, including path and file extension.
31 * @param  string $alt     Alt tag text.
32 * @param  string $extra   Additional attributes.
33 */
34function oImg($src, $alt='', $extra='')
35{
36    $filepath = preg_match('!://!', $src) ? $src : getenv('DOCUMENT_ROOT') . $src;
37
38    if (true === ($gis = @getimagesize($filepath)) && !preg_match('/width|height/', $extra)) {
39        $image_size = $gis[3];
40    } else {
41        $image_size = '';
42    }
43
44    return sprintf('<img src="%s" %s alt="%s" %s />',
45        $src,
46        $image_size,
47        oTxt($alt),
48        $extra
49    );
50}
51
52/**
53 * Prints an image tag for image specified in $src.
54 *
55 * @param  string $src     File name of the image, including path and file extension.
56 * @param  string $alt     Alt tag text.
57 * @param  string $extra   Additional attributes.
58 */
59function printImg($src, $alt='', $extra='')
60{
61    echo oImg($src, $alt, $extra);
62}
63
64/**
65 * Finds the values of an enumeration or set column of a MySQL database, returning them in an array.
66 * Use this to generate a pull-down menu of options or to validate the existence
67 * of options. (Quinn 10 Feb 2001)
68 *
69 * @param  string $db_table   database table to lookup
70 * @param  string $db_col     database column to lookup
71 * @param  bool   $sort          Sort the output.
72 * @return array    Array of the set/enum values on success, false on failure.
73 */
74function getSetEnumFieldValues($db_table, $db_col, $sort=false)
75{
76    $app =& App::getInstance();
77    $db =& DB::getInstance();
78
79    $qid = $db->query("SHOW COLUMNS FROM " . $db->escapeString($db_table) . " LIKE '" . $db->escapeString($db_col) . "'",false);
80
81    $row = mysql_fetch_row($qid);
82    if (isset($row[1]) && preg_match('/^enum|^set/i', $row[1]) && preg_match_all("/'([^']*)'/", $row[1], $enum)) {
83        if ($sort) {
84            natsort($enum[1]);
85        }
86        return $enum[1];
87    } else {
88        $app->logMsg(sprintf('No set or enum fields found for %s.%s', $db_table, $db_col), LOG_ERR, __FILE__, __LINE__);
89        return false;
90    }
91}
92
93/**
94 * Prints option fields for a select form. Works only with enum or set
95 * data types in table columns.
96 *
97 * @param  string $db_table     Database table to lookup
98 * @param  string $db_col       Database column to lookup
99 * @param  string $preselected  The currently selected value of the menu. compared to the $val_column
100 * @param  bool   $first        Optional first item; set true for a blank item, array for item with name and value.
101 * @param  bool   $sort         Sort the output.
102 */
103function printSetSelectForm($db_table, $db_col, $preselected, $first=false, $sort=false)
104{
105    $values = getSetEnumFieldValues($db_table, $db_col, $sort);
106    if ($values === false) {
107        ?><option value="">&nbsp;</option>
108        <?php
109        return false;
110    }
111    if (true === $first) {
112        $selected = ($preselected == '') ? ' selected' : '';
113        ?><option value=""<?php echo $selected; ?>>&nbsp;</option>
114        <?php
115    }
116    // When the 'blank' value needs a specific key->val pair.
117    if (is_array($first)) {
118        foreach ($first as $key=>$val) {
119            $selected = ($preselected == $val) ? ' selected="selected"' : '';
120            ?><option value="<?php echo oTxt($key); ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option>
121            <?php
122        }
123    }
124    foreach ($values as $v) {
125        $selected = ($v == $preselected) ? ' selected' : '';
126        ?><option value="<?php echo oTxt($v); ?>"<?php echo $selected; ?>><?php echo oTxt($v); ?></option>
127        <?php
128    }
129}
130
131/**
132 * Prints radio buttons from a set/enum column.
133 *
134 * @param  string $db_table     Database table to lookup
135 * @param  string $db_col       Database column to lookup
136 * @param  string $preselected  The currently selected value of the menu. compared to the $val_column
137 * @param  bool   $first        Optional first item; set true for a blank item, array for item with name and value.
138 * @param  bool   $sort         Sort the output.
139 */
140function printEnumRadios($name, $db_table, $db_col, $preselected, $first=false, $sort=false)
141{
142    $values = getSetEnumFieldValues($db_table, $db_col, $sort);
143    if ($values === false) {
144        return false;
145    }
146
147    foreach ($values as $v) {
148        $selected = ($v == $preselected) ? ' checked="checked"' : '';
149        ?><label class="sc-label-secondary"><input type="radio" name="<?php echo oTxt($name) ?>" id="<?php echo oTxt($v); ?>" value="<?php echo oTxt($v); ?>"<?php echo $selected; ?> /> <?php echo oTxt($v); ?></label>
150        <?php
151    }
152}
153
154/**
155 * Prints checkbox fields. Works only with enum or set
156 * data types in table columns.
157 *
158 * @param  string $db_table      database table to lookup
159 * @param  string $db_col        database column to lookup
160 * @param  array  $preselected   array of preselected values (matching the values in $db_col)
161 * @param  int    $columns       number of table columns to print
162 * @param  int    $flag          set to 'allone' for name of input fields to all be the same of a multidimensional array.
163 * @param  bool   $sort          Sort the output.
164 */
165function printSetCheckboxes($db_table, $db_col, $preselected, $columns=1, $flag=null, $sort=false)
166{
167    ?>
168    <table>
169        <tr>
170    <?php
171    // Sometimes preselected comes as a comma list.
172    if (!is_array($preselected)) {
173        $preselected = explode(',', $preselected);
174    }
175
176    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
177    // Here we assume in all the values of an array are 'on' that we can find the data
178    // in the array keys.
179    $ps_value_count = array_count_values($preselected);
180    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
181        $preselected = array_keys($preselected);
182    }
183
184    // Retrieve values of a Set or ENUM database column.
185    $values = getSetEnumFieldValues($db_table, $db_col, $sort);
186
187    // Initialize the HTML table generation vars.
188    $num_cells = sizeof($values) - 1;
189    $num_rows = floor($num_cells / $columns);
190    $cols_lastrow = $num_cells % $columns;
191    $row_cnt = 0;
192    $col_cnt = 0;
193    foreach ($values as $v) {
194        if ($col_cnt == $columns) {
195            // Begin a new row.
196            ?></tr><tr><?php
197            $col_cnt = 0;
198            $row_cnt++;
199        }
200        if ($col_cnt < $cols_lastrow) {
201            $lastrow_add = $col_cnt;
202        } else {
203            $lastrow_add = $cols_lastrow;
204        }
205        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
206        $col_cnt++;
207
208        // Look for preselected value.
209        if (in_array($v, $preselected)) {
210            $checked = ' checked="checked"';
211        } else {
212            $checked = '';
213        }
214        if ('allone' == $flag) {
215            // Print a cell with multidimensional array checkboxes.
216            $html_name = 'dbcol[' . $db_col . '][' . $v . ']';
217        } else {
218            // Print a cell with basic named checkboxes.
219            $html_name = $db_col . '[' . $v . ']';
220        }
221        ?><td><label class="sc-label-secondary"><input type="checkbox" name="<?php echo oTxt($html_name); ?>" id="<?php echo oTxt($html_name); ?>"<?php echo $checked; ?> /> <?php echo oTxt($v); ?></label></td>
222        <?php
223    }
224    if ($col_cnt < $columns) {
225        // This last cell must expand to fill the last blank cells.
226        ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
227    }
228    ?>
229        </tr>
230    </table><?php
231}
232
233/**
234 * Prints radio select fields. Works only with enum or set
235 * data types in table columns.
236 *
237 * @param  string $db_table      database table to lookup
238 * @param  string $db_col        database column to lookup
239 * @param  array  $preselected   array of preselected values (matching the values in $db_col)
240 * @param  int    $columns       number of table columns to print
241 * @param  bool   $sort          Sort the output.
242 */
243function printSetRadios($db_table, $db_col, $preselected, $columns=1, $sort=false)
244{
245    ?>
246    <table>
247        <tr>
248    <?php
249    // Sometimes preselected comes as a comma list.
250    if (!is_array($preselected)) {
251        $preselected = explode(',', $preselected);
252    }
253
254    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
255    // Here we assume in all the values of an array are 'on' that we can find the data
256    // in the array keys.
257    $ps_value_count = array_count_values($preselected);
258    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
259        $preselected = array_keys($preselected);
260    }
261
262    // Retrieve values of a Set or ENUM database column.
263    $values = getSetEnumFieldValues($db_table, $db_col, $sort);
264
265    // Initialize the HTML table generation vars.
266    $num_cells = sizeof($values) - 1;
267    $num_rows = floor($num_cells / $columns);
268    $cols_lastrow = $num_cells % $columns;
269    $row_cnt = 0;
270    $col_cnt = 0;
271    foreach ($values as $v) {
272        if ($col_cnt == $columns) {
273            // Begin a new row.
274            ?></tr><tr><?php
275            $col_cnt = 0;
276            $row_cnt++;
277        }
278        if ($col_cnt < $cols_lastrow) {
279            $lastrow_add = $col_cnt;
280        } else {
281            $lastrow_add = $cols_lastrow;
282        }
283        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
284        $col_cnt++;
285
286        // Look for preselected value.
287        if (in_array($v, $preselected)) {
288            $checked = ' checked="checked"';
289        } else {
290            $checked = '';
291        }
292        // Print a cell with basic named checkboxes.
293        ?><td><label class="sc-label-secondary"><input type="radio" name="<?php echo oTxt($db_col); ?>" id="<?php echo oTxt($db_col . '-' . $v); ?>" value="<?php echo oTxt($v); ?>"<?php echo $checked; ?> /> <?php echo oTxt($v); ?></label></td>
294        <?php
295    }
296    if ($col_cnt < $columns) {
297        // This last cell must expand to fill the last blank cells.
298        ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
299    }
300    ?>
301        </tr>
302    </table><?php
303}
304
305/**
306 * Prints a pulldown menu containing the specified values and keys of a table.
307 *
308 * @param  string $db_table         database table to lookup
309 * @param  string $key_column       column containing the human-readable titles for the select menu
310 * @param  string $val_column       column containing the computer values for the select menu
311 * @param  string $preselected      the currently selected value of the menu. compared to the $val_column
312 * @param  bool   $first            Optional first item; set true for a blank item, array for item with name and value.
313 * @param  string $extra_clause     SQL exclude clause. Something like "WHERE girls != 'buckteeth'"
314 */
315function printSelectForm($db_table, $key_column, $val_column, $preselected, $first=false, $extra_clause='', $sql_format='SELECT %1$s, %2$s FROM %3$s %4$s')
316{
317    $db =& DB::getInstance();
318
319    // Sometimes preselected comes as a comma list.
320    if (!is_array($preselected)) {
321        $preselected = array($preselected);
322    }
323
324    // Print a blank first option.
325    if (true === $first) {
326        $selected = in_array('', $preselected) ? ' selected="selected"' : '';
327        ?><option value=""<?php echo $selected; ?>>&nbsp;</option>
328        <?php
329    }
330
331    // When the 'blank' value needs a specific key->val pair.
332    if (is_array($first)) {
333        foreach ($first as $key=>$val) {
334            $selected = in_array($key, $preselected) ? ' selected="selected"' : '';
335            ?><option value="<?php echo $key; ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option>
336            <?php
337        }
338    }
339    // $qid = $db->query("SELECT $key_column, $val_column FROM $db_table $extra_clause",false);
340    $qid = $db->query(sprintf($sql_format, $key_column, $val_column, $db_table, $extra_clause),false);
341    while ($row = mysql_fetch_assoc($qid)) {
342        $selected = in_array($row[$val_column], $preselected) ? ' selected="selected"' : '';
343        ?><option value="<?php echo $row[$val_column]; ?>"<?php echo $selected; ?>><?php echo oTxt($row[$key_column]); ?></option><?php
344    }
345}
346
347/**
348 * Prints checkbox fields. Works only with enum or set
349 * data types in table columns.
350 *
351 * @param  string $db_table         database table to lookup
352 * @param  string $key_column       column containing the human-readable titles for the select menu
353 * @param  string $val_column       column containing the computer values for the select menu
354 * @param  string $preselected      the currently selected value of the menu. compared to the $val_column
355 * @param  int    $columns          number of table columns to print
356 * @param  int    $extra_clause     extra sql to send at end of select statement.
357 * @param  bool   $vert_columns     display checkboxes in vertical orientation first.
358 */
359function printDBCheckboxes($db_table, $key_column, $val_column, $preselected, $columns=1, $extra_clause='', $vert_columns=false)
360{
361    $db =& DB::getInstance();
362
363    // Sometimes preselected comes as a comma list.
364    if (!is_array($preselected)) {
365        $preselected = explode(',', $preselected);
366    }
367
368    // Checkbox POST data has the primary data in the keys, and 'on' as the values.
369    // Here we assume in all the values of an array are 'on' that we can find the data
370    // in the array keys.
371    $ps_value_count = array_count_values($preselected);
372    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
373        $preselected = array_keys($preselected);
374    }
375
376    $qid = $db->query("SELECT $key_column, $val_column FROM $db_table $extra_clause",false);
377    while ($row = mysql_fetch_assoc($qid)) {
378        $values[] = $row;
379    }
380
381    // Rearrange array so sort is in vertical columns. FIXME: doesn't work.
382//  if ($vert_columns) {
383//      $per_col = ceil(sizeof($values) / $columns);
384//      $columns = ceil(sizeof($values) / $per_col);
385//      $curr_row = 0;
386//      $curr_col = 0;
387//      $pos = 0;
388//         foreach ($values as $k=>$v) {
389//             $pos = $curr_row * $columns + $curr_col;
390//             if ($curr_row <= $per_col) {
391//                 $curr_row++;
392//             } else {
393//                 $curr_row = 0;
394//                 $curr_col++;
395//             }
396// //             echo '<li>' . $pos . '-' . $v[$key_column];
397//             $new_values[$pos] = $v;
398//         }
399//         $values = $new_values;
400//         ksort($values);
401//  }
402
403    if (empty($values)) {
404        return false;
405    }
406
407    // Initialize the HTML table generation vars.
408    $num_cells = sizeof($values) - 1;
409    $num_rows = floor($num_cells / $columns);
410    $cols_lastrow = $num_cells % $columns;
411    $row_cnt = 0;
412    $col_cnt = 0;
413    ?>
414    <table border="0" cellspacing="0" cellpadding="0">
415        <tr>
416    <?php
417    foreach ($values as $box) {
418        if ($col_cnt == $columns) {
419            // Begin a new row.
420            ?></tr><tr><?php
421            $col_cnt = 0;
422            $row_cnt++;
423        }
424
425        if ($col_cnt < $cols_lastrow) {
426            $lastrow_add = $col_cnt;
427        } else {
428            $lastrow_add = $cols_lastrow;
429        }
430
431        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
432        $col_cnt++;
433
434        // Look for preselected value.
435        if (in_array($box[$val_column], $preselected)) {
436            $checked = ' checked="checked"';
437        } else {
438            $checked = '';
439        }
440
441        // Print a cell with basic named checkboxes.
442        $html_name = $val_column . '[' . $box[$val_column] . ']';
443        $html_id = $val_column . '-' . $box[$val_column] . '';
444        ?>
445        <td><input type="checkbox" name="<?php echo oTxt($html_name); ?>" id="<?php echo oTxt($html_id); ?>"<?php echo $checked; ?> /></td>
446        <td><label for="<?php echo oTxt($html_id); ?>" class="sc-normal"><?php echo oTxt($box[$key_column]); ?></label></td>
447        <?php
448    }
449
450    if ($col_cnt < $columns) {
451        // This last cell must expand to fill the last blank cells.
452        ?><td colspan="<?php echo $columns - $col_cnt; ?>">&nbsp;</td><?php
453    }
454    ?>
455        </tr>
456    </table><?php
457}
458
459/**
460 * Prints submit buttons based on given array of submit button names and titles.
461 *
462 * @access  public
463 * @param   array   $submit_buttons     Array of buttons, the key being the button name, and value being the title of the button.
464 * @return  void
465 * @author  Quinn Comendant <quinn@strangecode.com>
466 * @version 1.0
467 * @since   15 Jun 2005 13:42:21
468 */
469function printSubmitButtons($submit_buttons)
470{
471    if (is_array($submit_buttons) && !empty($submit_buttons)) {
472        foreach ($submit_buttons as $i => $b) {
473            if (is_array($b)) {
474                $defaults = array();
475                $defaults['type'] = isset($b['type']) ? $b['type'] : 'submit';
476                if (isset($b['href'])) {
477                    echo '<a';
478                    foreach (array_diff_key($b, array('value' => null)) as $key => $value) {
479                        printf(' %s="%s"', $key, oTxt($value));
480                    }
481                    echo '>' . oTxt($b['value']) . '</a>';
482                } else {
483                    $defaults['id'] = isset($b['id']) ? $b['id'] : sprintf('sc-%s-button', $b['name']);
484                    $b = array_merge($defaults, $b);
485                    echo '<input';
486                    foreach ($b as $key => $value) {
487                        printf(' %s="%s"', $key, oTxt($value));
488                    }
489                    echo ' />';
490                }
491            } else {
492                // For backwards compatibility.
493                ?><input type="submit" name="<?php echo oTxt($i) ?>" value="<?php echo oTxt($b); ?>" /><?php
494            }
495        }
496    }
497}
Note: See TracBrowser for help on using the repository browser.