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
RevLine 
[1]1<?php
2/**
[362]3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
[396]5 * Copyright 2001-2012 Strangecode, LLC
[468]6 *
[362]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.
[468]13 *
[362]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.
[468]18 *
[362]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/**
[1]24 * TemplateGlue.inc.php
25 */
26
27/**
[42]28 * Returns an image tag for image specified in $src.
[1]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;
[42]37
[35]38    if (true === ($gis = @getimagesize($filepath)) && !preg_match('/width|height/', $extra)) {
39        $image_size = $gis[3];
40    } else {
[1]41        $image_size = '';
42    }
[42]43
[1]44    return sprintf('<img src="%s" %s alt="%s" %s />',
45        $src,
46        $image_size,
47        oTxt($alt),
48        $extra
49    );
50}
51
52/**
[42]53 * Prints an image tag for image specified in $src.
[1]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.
[334]66 * Use this to generate a pull-down menu of options or to validate the existence
[42]67 * of options. (Quinn 10 Feb 2001)
[1]68 *
69 * @param  string $db_table   database table to lookup
70 * @param  string $db_col     database column to lookup
[295]71 * @param  bool   $sort          Sort the output.
[1]72 * @return array    Array of the set/enum values on success, false on failure.
73 */
[295]74function getSetEnumFieldValues($db_table, $db_col, $sort=false)
[1]75{
[479]76    $app =& App::getInstance();
77    $db =& DB::getInstance();
[468]78
[136]79    $qid = $db->query("SHOW COLUMNS FROM " . $db->escapeString($db_table) . " LIKE '" . $db->escapeString($db_col) . "'",false);
[42]80
[1]81    $row = mysql_fetch_row($qid);
[759]82    if (isset($row[1]) && preg_match('/^enum|^set/i', $row[1]) && preg_match_all("/'([^']*)'/", $row[1], $enum)) {
[295]83        if ($sort) {
[468]84            natsort($enum[1]);
[295]85        }
[1]86        return $enum[1];
87    } else {
[772]88        $app->logMsg(sprintf('No set or enum fields found for %s.%s', $db_table, $db_col), LOG_ERR, __FILE__, __LINE__);
[1]89        return false;
90    }
91}
92
93/**
94 * Prints option fields for a select form. Works only with enum or set
[42]95 * data types in table columns.
[1]96 *
[407]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.
[1]102 */
[407]103function printSetSelectForm($db_table, $db_col, $preselected, $first=false, $sort=false)
[1]104{
[295]105    $values = getSetEnumFieldValues($db_table, $db_col, $sort);
[1]106    if ($values === false) {
[21]107        ?><option value="">&nbsp;</option>
[1]108        <?php
109        return false;
110    }
[407]111    if (true === $first) {
[1]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.
[407]117    if (is_array($first)) {
118        foreach ($first as $key=>$val) {
[1]119            $selected = ($preselected == $val) ? ' selected="selected"' : '';
[348]120            ?><option value="<?php echo oTxt($key); ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option>
[1]121            <?php
122        }
123    }
[32]124    foreach ($values as $v) {
125        $selected = ($v == $preselected) ? ' selected' : '';
[348]126        ?><option value="<?php echo oTxt($v); ?>"<?php echo $selected; ?>><?php echo oTxt($v); ?></option>
[1]127        <?php
128    }
129}
130
131/**
[32]132 * Prints radio buttons from a set/enum column.
133 *
[407]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.
[32]139 */
[407]140function printEnumRadios($name, $db_table, $db_col, $preselected, $first=false, $sort=false)
[32]141{
[295]142    $values = getSetEnumFieldValues($db_table, $db_col, $sort);
[32]143    if ($values === false) {
144        return false;
145    }
146
147    foreach ($values as $v) {
[337]148        $selected = ($v == $preselected) ? ' checked="checked"' : '';
[349]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>
[32]150        <?php
151    }
152}
153
154/**
[1]155 * Prints checkbox fields. Works only with enum or set
[42]156 * data types in table columns.
[1]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
[334]162 * @param  int    $flag          set to 'allone' for name of input fields to all be the same of a multidimensional array.
[295]163 * @param  bool   $sort          Sort the output.
[1]164 */
[799]165function printSetCheckboxes($db_table, $db_col, $preselected, $columns=1, $flag=null, $sort=false)
[42]166{
[1]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    }
[42]175
[1]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);
[64]180    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
[1]181        $preselected = array_keys($preselected);
182    }
[42]183
[334]184    // Retrieve values of a Set or ENUM database column.
[295]185    $values = getSetEnumFieldValues($db_table, $db_col, $sort);
[42]186
[1]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;
[32]193    foreach ($values as $v) {
[1]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++;
[42]207
[1]208        // Look for preselected value.
[32]209        if (in_array($v, $preselected)) {
[1]210            $checked = ' checked="checked"';
211        } else {
212            $checked = '';
213        }
214        if ('allone' == $flag) {
[334]215            // Print a cell with multidimensional array checkboxes.
[32]216            $html_name = 'dbcol[' . $db_col . '][' . $v . ']';
[28]217        } else {
[1]218            // Print a cell with basic named checkboxes.
[32]219            $html_name = $db_col . '[' . $v . ']';
[1]220        }
[349]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>
[28]222        <?php
[1]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
[42]235 * data types in table columns.
[1]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
[295]241 * @param  bool   $sort          Sort the output.
[1]242 */
[295]243function printSetRadios($db_table, $db_col, $preselected, $columns=1, $sort=false)
[42]244{
[1]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    }
[42]253
[1]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);
[53]258    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
[1]259        $preselected = array_keys($preselected);
260    }
[42]261
[334]262    // Retrieve values of a Set or ENUM database column.
[295]263    $values = getSetEnumFieldValues($db_table, $db_col, $sort);
[42]264
[1]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;
[32]271    foreach ($values as $v) {
[1]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++;
[42]285
[1]286        // Look for preselected value.
[32]287        if (in_array($v, $preselected)) {
[1]288            $checked = ' checked="checked"';
289        } else {
290            $checked = '';
291        }
292        // Print a cell with basic named checkboxes.
[349]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>
[1]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/**
[42]306 * Prints a pulldown menu containing the specified values and keys of a table.
[1]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
[407]312 * @param  bool   $first            Optional first item; set true for a blank item, array for item with name and value.
[334]313 * @param  string $extra_clause     SQL exclude clause. Something like "WHERE girls != 'buckteeth'"
[1]314 */
[407]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')
[1]316{
[479]317    $db =& DB::getInstance();
[468]318
[1]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.
[407]325    if (true === $first) {
[1]326        $selected = in_array('', $preselected) ? ' selected="selected"' : '';
327        ?><option value=""<?php echo $selected; ?>>&nbsp;</option>
328        <?php
329    }
[42]330
[1]331    // When the 'blank' value needs a specific key->val pair.
[407]332    if (is_array($first)) {
333        foreach ($first as $key=>$val) {
[323]334            $selected = in_array($key, $preselected) ? ' selected="selected"' : '';
[1]335            ?><option value="<?php echo $key; ?>"<?php echo $selected; ?>><?php echo oTxt($val); ?></option>
336            <?php
337        }
338    }
[324]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);
[1]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
[42]349 * data types in table columns.
[1]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{
[479]361    $db =& DB::getInstance();
[468]362
[1]363    // Sometimes preselected comes as a comma list.
364    if (!is_array($preselected)) {
365        $preselected = explode(',', $preselected);
366    }
[42]367
[1]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);
[253]372    if (isset($ps_value_count['on']) && $ps_value_count['on'] == sizeof($preselected)) {
[1]373        $preselected = array_keys($preselected);
374    }
[42]375
[136]376    $qid = $db->query("SELECT $key_column, $val_column FROM $db_table $extra_clause",false);
[1]377    while ($row = mysql_fetch_assoc($qid)) {
378        $values[] = $row;
379    }
[42]380
[1]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    }
[42]406
[1]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    ?>
[28]414    <table border="0" cellspacing="0" cellpadding="0">
[1]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        }
[42]424
[1]425        if ($col_cnt < $cols_lastrow) {
426            $lastrow_add = $col_cnt;
427        } else {
428            $lastrow_add = $cols_lastrow;
429        }
[42]430
[1]431        $curr = $num_rows * $col_cnt + $lastrow_add + $row_cnt;
432        $col_cnt++;
[42]433
[1]434        // Look for preselected value.
435        if (in_array($box[$val_column], $preselected)) {
436            $checked = ' checked="checked"';
437        } else {
438            $checked = '';
439        }
[42]440
[1]441        // Print a cell with basic named checkboxes.
[28]442        $html_name = $val_column . '[' . $box[$val_column] . ']';
[254]443        $html_id = $val_column . '-' . $box[$val_column] . '';
[28]444        ?>
[349]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>
[254]447        <?php
[1]448    }
[42]449
[1]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)) {
[197]472        foreach ($submit_buttons as $i => $b) {
473            if (is_array($b)) {
[336]474                $defaults = array();
475                $defaults['type'] = isset($b['type']) ? $b['type'] : 'submit';
[615]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 ' />';
[336]490                }
[197]491            } else {
[334]492                // For backwards compatibility.
[197]493                ?><input type="submit" name="<?php echo oTxt($i) ?>" value="<?php echo oTxt($b); ?>" /><?php
494            }
[1]495        }
496    }
497}
Note: See TracBrowser for help on using the repository browser.