[1] | 1 | #!/usr/local/bin/php -q |
---|
| 2 | <?php |
---|
| 3 | /** |
---|
[362] | 4 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 5 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
| 6 | * Copyright 2001-2009 Strangecode Internet Consultancy |
---|
| 7 | * |
---|
| 8 | * This file is part of The Strangecode Codebase. |
---|
| 9 | * |
---|
| 10 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 11 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 12 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 13 | * any later version. |
---|
| 14 | * |
---|
| 15 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 18 | * details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License along with |
---|
| 21 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | /** |
---|
[1] | 25 | * form_template.cli.php |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | include_once dirname(__FILE__) . '/_config.inc.php'; |
---|
| 29 | |
---|
| 30 | // Test arguments. |
---|
| 31 | if (isset($_SERVER['argv'][2])) { |
---|
| 32 | // Second arg is db table. |
---|
| 33 | $db_tbl = $_SERVER['argv'][2]; |
---|
| 34 | } else { |
---|
| 35 | die(sprintf("Usage: %s site_directory db_table\n", basename($_SERVER['argv'][0]))); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | // Get DB tables. |
---|
[136] | 39 | $qid = $db->query("SHOW TABLES"); |
---|
[1] | 40 | while (list($row) = mysql_fetch_row($qid)) { |
---|
| 41 | $tables[] = $row; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | // Make sure requested table is in database. |
---|
| 45 | if (!in_array($db_tbl, $tables)) { |
---|
[136] | 46 | die(sprintf("%s Warning: %s does not exist in database %s. Please select one of: \n\n%s\n\n", basename($_SERVER['argv'][0]), $db_tbl, $app->getParam('db_name'), join("\n", $tables))); |
---|
[1] | 47 | } |
---|
| 48 | |
---|
| 49 | // Get DB table column info. |
---|
[136] | 50 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
[1] | 51 | while ($row = mysql_fetch_row($qid)) { |
---|
| 52 | $cols[] = $row; |
---|
| 53 | } |
---|
| 54 | |
---|
[19] | 55 | $exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id'); |
---|
[154] | 56 | $primary_key = '__///__'; |
---|
[1] | 57 | $output = array(); |
---|
| 58 | |
---|
| 59 | // Loop through columns |
---|
| 60 | if (is_array($cols) && !empty($cols)) { |
---|
| 61 | foreach ($cols as $col) { |
---|
[42] | 62 | |
---|
[1] | 63 | // Human readable. |
---|
| 64 | $field = $col[0]; |
---|
| 65 | $title = ucfirst(str_replace('_', ' ', $field)); |
---|
| 66 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
| 67 | if ('PRI' == $col[3]) { |
---|
[44] | 68 | $primary_key = $field; |
---|
[1] | 69 | } |
---|
[42] | 70 | |
---|
[1] | 71 | // Column types like this are usually single toggle checkboxes. |
---|
| 72 | if (preg_match("/enum\('true'\)/", $col[1])) { |
---|
| 73 | $type = 'toggle'; |
---|
| 74 | } |
---|
[42] | 75 | |
---|
[44] | 76 | if (in_array($field, $exclude) || $primary_key == $field) { |
---|
[1] | 77 | // Don't add a field for this column. |
---|
| 78 | continue; |
---|
| 79 | } |
---|
[42] | 80 | |
---|
[1] | 81 | // Select menu from the column of a related database table. |
---|
| 82 | if (preg_match('/.*_id$/i', $field)) { |
---|
| 83 | $output[$field] = <<<E_O_F |
---|
[295] | 84 | |
---|
| 85 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 86 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 87 | <select name="$field" id="$field" class="sc-small"><\x3fphp printSelectForm('__///___tbl', "CONCAT(__///___id, '—', __///__)", '$field', \$frm['$field'], true, 'ORDER BY $field ASC'); \x3f></select> |
---|
[324] | 88 | <a href="<\x3fphp echo \$app->oHREF('/admin/__///__.php?op=add&boomerang=true'); \x3f>" class="sc-warn sc-add-link"><img src="/admin/i/plus.gif" width="10" height="10" alt="[+]" /></a> |
---|
| 89 | <a href="<\x3fphp echo \$app->oHREF('/admin/__///__.php?op=edit&boomerang=true&__///___id=' . \$frm['__///___id']); \x3f>" class="sc-warn sc-edit-link"><img src="/admin/i/pen.gif" width="12" height="12" alt="Edit" /></a> |
---|
[295] | 90 | </div> |
---|
[1] | 91 | E_O_F; |
---|
| 92 | continue; |
---|
| 93 | } |
---|
[42] | 94 | |
---|
[1] | 95 | // File upload. |
---|
[20] | 96 | if (preg_match('/file|image/i', $field)) { |
---|
[1] | 97 | $output[$field] = <<<E_O_F |
---|
[51] | 98 | |
---|
[295] | 99 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 100 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 101 | <input type="file" name="$field" id="$field" /> |
---|
[51] | 102 | <\x3fphp if ('' != \$upload->getFilenameGlob(getFormData('$primary_key') . '_*') && getFormData('op') == 'edit' || getFormData('op') == 'update') { \x3f> |
---|
[295] | 103 | <div class="sc-help"><\x3fphp printf(_("The current file <a href=\"%s/%2\\\$s\"><strong>%2\\\$s</strong></a> will be deleted if a new file is selected for upload."), '/_db_files/__///__', \$upload->getFilenameGlob(getFormData('$primary_key') . '_*')) \x3f></div> |
---|
[51] | 104 | <\x3fphp } \x3f> |
---|
[319] | 105 | <div class="sc-help"><\x3fphp printf(_("Allowed file types: %s."), join(', ', \$upload->getParam('valid_file_extensions'))) \x3f></div> |
---|
[295] | 106 | </div> |
---|
[1] | 107 | E_O_F; |
---|
| 108 | continue; |
---|
| 109 | } |
---|
[42] | 110 | |
---|
[1] | 111 | // Password field. |
---|
| 112 | if (preg_match('/pass/i', $field)) { |
---|
| 113 | $output[$field] = <<<E_O_F |
---|
[51] | 114 | |
---|
[295] | 115 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 116 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 117 | <input type="password" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
| 118 | </div> |
---|
[1] | 119 | E_O_F; |
---|
| 120 | continue; |
---|
| 121 | } |
---|
[42] | 122 | |
---|
[1] | 123 | switch ($type) { |
---|
[42] | 124 | |
---|
[1] | 125 | // Select menu (or radio buttons) |
---|
| 126 | case 'enum' : |
---|
| 127 | $output[$field] = <<<E_O_F |
---|
[51] | 128 | |
---|
[295] | 129 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 130 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 131 | <select name="$field" id="$field"><\x3fphp printSetSelectForm('$db_tbl', '$field', \$frm['$field'], true); \x3f></select> |
---|
| 132 | </div> |
---|
[1] | 133 | E_O_F; |
---|
| 134 | break; |
---|
[42] | 135 | |
---|
[1] | 136 | // Set checkboxes |
---|
| 137 | case 'set' : |
---|
| 138 | $output[$field] = <<<E_O_F |
---|
[51] | 139 | |
---|
[295] | 140 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 141 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 142 | <\x3fphp printSetCheckboxes('$db_tbl', '$field', \$frm['$field'], 1) \x3f> |
---|
| 143 | </div> |
---|
[1] | 144 | E_O_F; |
---|
| 145 | break; |
---|
[42] | 146 | |
---|
[1] | 147 | // Single checkbox |
---|
| 148 | case 'toggle' : |
---|
| 149 | $output[$field] = <<<E_O_F |
---|
[51] | 150 | |
---|
[295] | 151 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 152 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 153 | <label><input type="checkbox" name="$field" id="$field" <\x3fphp frmChecked(!empty(\$frm['$field'])) \x3f> /><\x3fphp echo _("Check this box to $title"); \x3f></label> |
---|
| 154 | </div> |
---|
[1] | 155 | E_O_F; |
---|
| 156 | break; |
---|
[42] | 157 | |
---|
[51] | 158 | // Textarea small |
---|
[1] | 159 | case 'text' : |
---|
[51] | 160 | case 'tinyblob' : |
---|
| 161 | case 'blob' : |
---|
| 162 | $output[$field] = <<<E_O_F |
---|
| 163 | |
---|
[295] | 164 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 165 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 166 | <textarea name="$field" id="$field" rows="8" cols="40" class="sc-short sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
| 167 | </div> |
---|
[51] | 168 | E_O_F; |
---|
| 169 | break; |
---|
| 170 | |
---|
| 171 | // Textarea big |
---|
[1] | 172 | case 'mediumtext' : |
---|
| 173 | case 'longtext' : |
---|
| 174 | case 'mediumblob' : |
---|
| 175 | case 'longblob' : |
---|
| 176 | $output[$field] = <<<E_O_F |
---|
[51] | 177 | |
---|
[295] | 178 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 179 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 180 | <textarea name="$field" id="$field" rows="8" cols="40" class="sc-short sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
| 181 | </div> |
---|
[1] | 182 | E_O_F; |
---|
| 183 | break; |
---|
[42] | 184 | |
---|
[51] | 185 | // Number |
---|
[1] | 186 | case 'tinyint' : |
---|
| 187 | case 'bit' : |
---|
| 188 | case 'bool' : |
---|
| 189 | case 'smallint' : |
---|
| 190 | case 'mediumint' : |
---|
| 191 | case 'int' : |
---|
| 192 | case 'integer' : |
---|
| 193 | case 'bigint' : |
---|
[42] | 194 | |
---|
[1] | 195 | case 'float' : |
---|
| 196 | case 'float' : |
---|
| 197 | case 'double' : |
---|
| 198 | case 'double' : |
---|
| 199 | case 'real' : |
---|
| 200 | case 'decimal' : |
---|
| 201 | case 'dec' : |
---|
| 202 | case 'numeric' : |
---|
[51] | 203 | default : |
---|
| 204 | $output[$field] = <<<E_O_F |
---|
[42] | 205 | |
---|
[295] | 206 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 207 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 208 | <input type="text" name="$field" id="$field" class="sc-tiny" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
| 209 | </div> |
---|
[51] | 210 | E_O_F; |
---|
| 211 | break; |
---|
| 212 | |
---|
| 213 | // Date |
---|
[1] | 214 | case 'date' : |
---|
| 215 | case 'datetime' : |
---|
| 216 | case 'timestamp' : |
---|
| 217 | case 'time' : |
---|
| 218 | case 'year' : |
---|
[51] | 219 | default : |
---|
| 220 | $output[$field] = <<<E_O_F |
---|
[42] | 221 | |
---|
[295] | 222 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 223 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 224 | <input type="text" name="$field" id="$field" class="sc-small" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
| 225 | </div> |
---|
[51] | 226 | E_O_F; |
---|
| 227 | break; |
---|
| 228 | |
---|
| 229 | // Text |
---|
[1] | 230 | case 'char' : |
---|
| 231 | case 'varchar' : |
---|
[51] | 232 | case 'tinytext' : |
---|
| 233 | case 'tinyblob' : |
---|
[1] | 234 | default : |
---|
| 235 | $output[$field] = <<<E_O_F |
---|
[51] | 236 | |
---|
[295] | 237 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 238 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 239 | <input type="text" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
| 240 | </div> |
---|
[1] | 241 | E_O_F; |
---|
| 242 | break; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | } else { |
---|
| 246 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | echo join("\n", $output); |
---|
| 251 | |
---|
| 252 | ?> |
---|