[497] | 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/> |
---|
| 5 | * Copyright © 2014 Strangecode, LLC |
---|
| 6 | * |
---|
| 7 | * This program is free software: you can redistribute it and/or modify |
---|
| 8 | * it under the terms of the GNU General Public License as published by |
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 10 | * (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | /* |
---|
| 22 | * HTML() |
---|
| 23 | * |
---|
| 24 | * Tools for building HTML from PHP data. |
---|
| 25 | * |
---|
| 26 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 27 | * @version 1.0 |
---|
| 28 | * @since 11 Sep 2014 21:08:06 |
---|
| 29 | * |
---|
| 30 | * Example of use: |
---|
| 31 | --------------------------------------------------------------------- |
---|
| 32 | echo HTML::buttons(array( |
---|
| 33 | array('name' => 'submit', 'value' => _("Save changes"), 'class' => 'small button', 'accesskey' => 's'), |
---|
| 34 | array('name' => 'reset', 'value' => _("Reset"), 'class' => 'small button secondary', 'accesskey' => 'r'), |
---|
| 35 | array('name' => 'cancel', 'value' => _("Cancel"), 'class' => 'small button secondary', 'accesskey' => 'c'), |
---|
| 36 | )); |
---|
| 37 | --------------------------------------------------------------------- |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | class HTML { |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * Prints submit buttons based on given array of submit button names and titles. If the array includes an 'href' key, the |
---|
| 44 | * button is created using a <a> otherwise an <input type="âŠ" /> is used. |
---|
| 45 | * |
---|
| 46 | * @access public |
---|
| 47 | * @param array $buttons Array of buttons, the key being the button name, and value being the title of the button. |
---|
| 48 | * @return void |
---|
| 49 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 50 | * @version 1.0 |
---|
| 51 | * @since 12 Sep 2014 00:17:38 |
---|
| 52 | */ |
---|
| 53 | static public function printButtons($buttons=array(), $class='button-group') |
---|
| 54 | { |
---|
| 55 | if (!isset($buttons[0]) || !is_array($buttons[0])) { |
---|
| 56 | $app =& App::getInstance(); |
---|
| 57 | $app->logMsg(sprintf('Incorrect parameters passed to HTML::buttons(): %s', getDump($buttons)), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 58 | return false; |
---|
| 59 | } |
---|
| 60 | if (empty($buttons)) { |
---|
| 61 | return ''; |
---|
| 62 | } |
---|
| 63 | ?><ul class="<?php echo oTxt($class) ?>"><?php |
---|
| 64 | foreach ($buttons as $i => $b) { |
---|
| 65 | $defaults = array(); |
---|
| 66 | $defaults['type'] = isset($b['type']) ? $b['type'] : 'submit'; |
---|
| 67 | $b = array_merge($defaults, $b); |
---|
| 68 | if (isset($b['href'])) { |
---|
| 69 | echo '<li><a'; |
---|
| 70 | foreach (array_diff_key($b, array('value' => null)) as $key => $value) { |
---|
| 71 | printf(' %s="%s"', $key, oTxt($value)); |
---|
| 72 | } |
---|
| 73 | echo '>' . oTxt($b['value']) . '</a></li>'; |
---|
| 74 | } else if (isset($b['name'])) { |
---|
| 75 | $defaults['id'] = isset($b['id']) ? $b['id'] : sprintf('sc-%s-button', $b['name']); |
---|
| 76 | echo '<li><input'; |
---|
| 77 | foreach ($b as $key => $value) { |
---|
| 78 | printf(' %s="%s"', $key, oTxt($value)); |
---|
| 79 | } |
---|
| 80 | echo ' /></li>'; |
---|
| 81 | } else { |
---|
| 82 | $app->logMsg(sprintf('Button missing name or href: %s', getDump($b)), LOG_ERR, __FILE__, __LINE__); |
---|
| 83 | continue; |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | ?></ul><?php |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /* |
---|
| 90 | * Return an array of key-value pairs matching a database query for the given parameters. This is useful for |
---|
| 91 | * injecting into HTML::printSelectOptions(). |
---|
| 92 | * |
---|
| 93 | * @access public |
---|
| 94 | * @param string $db_table database table to lookup |
---|
| 95 | * @param string $key_column column containing the human-readable titles for the select menu |
---|
| 96 | * @param string $val_column column containing the computer values for the select menu |
---|
| 97 | * @param string $preselected the currently selected value of the menu. compared to the $val_column |
---|
| 98 | * @param bool $first Optional first item; set true for a blank item, array for item with name and value. |
---|
| 99 | * @param string $extra_clause SQL exclude clause. Something like "WHERE girls != 'buckteeth'" |
---|
| 100 | * @return array Array of options suitable to pass into HTML::printSelectOptions(). |
---|
| 101 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 102 | * @version 1.0 |
---|
| 103 | * @since 12 Sep 2014 11:43:23 |
---|
| 104 | */ |
---|
| 105 | static public function getSelectOptions($db_table, $key_column, $val_column, $preselected, $first=false, $extra_clause='', $sql_format='SELECT %1$s, %2$s FROM %3$s %4$s') |
---|
| 106 | { |
---|
| 107 | $db =& DB::getInstance(); |
---|
| 108 | |
---|
| 109 | // Sometimes preselected comes as a comma list. |
---|
| 110 | if (!is_array($preselected)) { |
---|
| 111 | $preselected = array($preselected); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | $options = array(); |
---|
| 115 | if (true === $first) { |
---|
| 116 | // Include a blank first option. |
---|
| 117 | $options[] = array( |
---|
| 118 | 'value' => '', |
---|
| 119 | 'selected' => in_array('', $preselected), |
---|
| 120 | 'text' => '', |
---|
| 121 | ); |
---|
| 122 | } else if (is_array($first)) { |
---|
| 123 | // When the 'blank' first option needs a specific key->val pair. |
---|
| 124 | foreach ($first as $key => $val) { |
---|
| 125 | $options[] = array( |
---|
| 126 | 'value' => $key, |
---|
| 127 | 'selected' => in_array($key, $preselected), |
---|
| 128 | 'text' => $val, |
---|
| 129 | ); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | $db =& DB::getInstance(); |
---|
| 134 | $qid = $db->query(sprintf($sql_format, $key_column, $val_column, $db_table, $extra_clause), false); |
---|
| 135 | while ($row = mysql_fetch_assoc($qid)) { |
---|
| 136 | $options[] = array( |
---|
| 137 | 'value' => $row[$val_column], |
---|
| 138 | 'selected' => in_array($row[$val_column], $preselected), |
---|
| 139 | 'text' => $row[$key_column], |
---|
| 140 | ); |
---|
| 141 | } |
---|
| 142 | return $options; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * Prints option fields for a select form. Works only with enum or set |
---|
| 147 | * data types in table columns. |
---|
| 148 | * |
---|
| 149 | * @param string $db_table Database table to lookup |
---|
| 150 | * @param string $db_col Database column to lookup |
---|
| 151 | * @param string $preselected The currently selected value of the menu. compared to the $val_column |
---|
| 152 | * @param bool $first Optional first item; set true for a blank item, array for item with name and value. |
---|
| 153 | * @param bool $sort Sort the output. |
---|
| 154 | */ |
---|
| 155 | static public function getSelectOptionsEnum($db_table, $db_col, $preselected, $first=false, $sort=false) |
---|
| 156 | { |
---|
| 157 | // Sometimes preselected comes as a comma list. |
---|
| 158 | if (!is_array($preselected)) { |
---|
| 159 | $preselected = array($preselected); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | $options = array(); |
---|
| 163 | if (true === $first) { |
---|
| 164 | // Include a blank first option. |
---|
| 165 | $options[] = array( |
---|
| 166 | 'value' => '', |
---|
| 167 | 'selected' => in_array('', $preselected), |
---|
| 168 | 'text' => '', |
---|
| 169 | ); |
---|
| 170 | } else if (is_array($first)) { |
---|
| 171 | // When the 'blank' first option needs a specific key->val pair. |
---|
| 172 | foreach ($first as $key => $val) { |
---|
| 173 | $options[] = array( |
---|
| 174 | 'value' => $key, |
---|
| 175 | 'selected' => in_array($key, $preselected), |
---|
| 176 | 'text' => $val, |
---|
| 177 | ); |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | $db =& DB::getInstance(); |
---|
| 182 | $values = $db->getEnumValues($db_table, $db_col, $sort); |
---|
| 183 | foreach ($values as $v) { |
---|
| 184 | $options[] = array( |
---|
| 185 | 'value' => $v, |
---|
| 186 | 'selected' => in_array($v, $preselected), |
---|
| 187 | 'text' => $v, |
---|
| 188 | ); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | return $options; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | * Prints a select menu containing the specified values and keys of a table. |
---|
| 196 | * |
---|
| 197 | */ |
---|
| 198 | static public function printSelectOptions($options) |
---|
| 199 | { |
---|
| 200 | if (!isset($options) || !is_array($options)) { |
---|
| 201 | $app =& App::getInstance(); |
---|
| 202 | $app->logMsg(sprintf('Incorrect parameters passed to HTML::printSelectOptions(): %s', getDump($options)), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 203 | return false; |
---|
| 204 | } |
---|
| 205 | if (empty($options)) { |
---|
| 206 | return ''; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | foreach ($options as $o) { |
---|
| 210 | printf('<option value="%s"%s>%s</option>', |
---|
| 211 | oTxt($o['value']), |
---|
| 212 | ($o['selected'] ? ' selected' : ''), |
---|
| 213 | oTxt($o['text']) |
---|
| 214 | ); |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | } |
---|