[412] | 1 | #!/usr/bin/php -q |
---|
[1] | 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/> |
---|
[396] | 6 | * Copyright 2001-2012 Strangecode, LLC |
---|
[485] | 7 | * |
---|
[362] | 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. |
---|
[485] | 14 | * |
---|
[362] | 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. |
---|
[485] | 19 | * |
---|
[362] | 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 | |
---|
[533] | 28 | if ($_SERVER['argc'] > 1 && isset($_SERVER['argv'][1]) && '' != $_SERVER['argv'][1] && is_dir($_SERVER['argv'][1])) { |
---|
| 29 | // First arg is path to current site. Realpath removes trailing /s |
---|
| 30 | define('COMMON_BASE', realpath($_SERVER['argv'][1])); |
---|
| 31 | } else { |
---|
| 32 | die("Error: First argument must be the directory path to an existing site (ex: /home/sc/www.strangecode.com).\n"); |
---|
| 33 | } |
---|
[1] | 34 | |
---|
[533] | 35 | include_once dirname(__FILE__) . '/../_config.inc.php'; |
---|
| 36 | |
---|
[1] | 37 | // Test arguments. |
---|
| 38 | if (isset($_SERVER['argv'][2])) { |
---|
| 39 | // Second arg is db table. |
---|
| 40 | $db_tbl = $_SERVER['argv'][2]; |
---|
| 41 | } else { |
---|
| 42 | die(sprintf("Usage: %s site_directory db_table\n", basename($_SERVER['argv'][0]))); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | // Get DB tables. |
---|
[136] | 46 | $qid = $db->query("SHOW TABLES"); |
---|
[1] | 47 | while (list($row) = mysql_fetch_row($qid)) { |
---|
| 48 | $tables[] = $row; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | // Make sure requested table is in database. |
---|
| 52 | if (!in_array($db_tbl, $tables)) { |
---|
[136] | 53 | 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] | 54 | } |
---|
| 55 | |
---|
| 56 | // Get DB table column info. |
---|
[136] | 57 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
[1] | 58 | while ($row = mysql_fetch_row($qid)) { |
---|
| 59 | $cols[] = $row; |
---|
| 60 | } |
---|
| 61 | |
---|
[19] | 62 | $exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id'); |
---|
[154] | 63 | $primary_key = '__///__'; |
---|
[1] | 64 | $output = array(); |
---|
| 65 | |
---|
| 66 | // Loop through columns |
---|
| 67 | if (is_array($cols) && !empty($cols)) { |
---|
| 68 | foreach ($cols as $col) { |
---|
[42] | 69 | |
---|
[1] | 70 | // Human readable. |
---|
| 71 | $field = $col[0]; |
---|
| 72 | $title = ucfirst(str_replace('_', ' ', $field)); |
---|
| 73 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
| 74 | if ('PRI' == $col[3]) { |
---|
[44] | 75 | $primary_key = $field; |
---|
[1] | 76 | } |
---|
[42] | 77 | |
---|
[1] | 78 | // Column types like this are usually single toggle checkboxes. |
---|
| 79 | if (preg_match("/enum\('true'\)/", $col[1])) { |
---|
| 80 | $type = 'toggle'; |
---|
| 81 | } |
---|
[42] | 82 | |
---|
[44] | 83 | if (in_array($field, $exclude) || $primary_key == $field) { |
---|
[1] | 84 | // Don't add a field for this column. |
---|
| 85 | continue; |
---|
| 86 | } |
---|
[42] | 87 | |
---|
[1] | 88 | // Select menu from the column of a related database table. |
---|
| 89 | if (preg_match('/.*_id$/i', $field)) { |
---|
| 90 | $output[$field] = <<<E_O_F |
---|
[485] | 91 | |
---|
[295] | 92 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 93 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 94 | <select name="$field" id="$field" class="sc-small"><\x3fphp printSelectForm('__///___tbl', "CONCAT(__///___id, '—', __///__)", '$field', \$frm['$field'], true, 'ORDER BY $field ASC'); \x3f></select> |
---|
[324] | 95 | <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> |
---|
| 96 | <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] | 97 | </div> |
---|
[1] | 98 | E_O_F; |
---|
| 99 | continue; |
---|
| 100 | } |
---|
[42] | 101 | |
---|
[1] | 102 | // File upload. |
---|
[20] | 103 | if (preg_match('/file|image/i', $field)) { |
---|
[1] | 104 | $output[$field] = <<<E_O_F |
---|
[51] | 105 | |
---|
[295] | 106 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 107 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 108 | <input type="file" name="$field" id="$field" /> |
---|
[523] | 109 | <\x3fphp if ('' != \$upload->getFilenameGlob(getFormData('$primary_key') . '_*') && (getFormData('op') == 'edit' || getFormData('op') == 'update')) { \x3f> |
---|
| 110 | <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] | 111 | <\x3fphp } \x3f> |
---|
[319] | 112 | <div class="sc-help"><\x3fphp printf(_("Allowed file types: %s."), join(', ', \$upload->getParam('valid_file_extensions'))) \x3f></div> |
---|
[295] | 113 | </div> |
---|
[1] | 114 | E_O_F; |
---|
| 115 | continue; |
---|
| 116 | } |
---|
[42] | 117 | |
---|
[1] | 118 | // Password field. |
---|
| 119 | if (preg_match('/pass/i', $field)) { |
---|
| 120 | $output[$field] = <<<E_O_F |
---|
[51] | 121 | |
---|
[295] | 122 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 123 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 124 | <input type="password" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
| 125 | </div> |
---|
[1] | 126 | E_O_F; |
---|
| 127 | continue; |
---|
| 128 | } |
---|
[42] | 129 | |
---|
[1] | 130 | switch ($type) { |
---|
[42] | 131 | |
---|
[1] | 132 | // Select menu (or radio buttons) |
---|
| 133 | case 'enum' : |
---|
| 134 | $output[$field] = <<<E_O_F |
---|
[51] | 135 | |
---|
[295] | 136 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 137 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 138 | <select name="$field" id="$field"><\x3fphp printSetSelectForm('$db_tbl', '$field', \$frm['$field'], true); \x3f></select> |
---|
| 139 | </div> |
---|
[1] | 140 | E_O_F; |
---|
| 141 | break; |
---|
[42] | 142 | |
---|
[1] | 143 | // Set checkboxes |
---|
| 144 | case 'set' : |
---|
| 145 | $output[$field] = <<<E_O_F |
---|
[51] | 146 | |
---|
[295] | 147 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 148 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 149 | <\x3fphp printSetCheckboxes('$db_tbl', '$field', \$frm['$field'], 1) \x3f> |
---|
| 150 | </div> |
---|
[1] | 151 | E_O_F; |
---|
| 152 | break; |
---|
[42] | 153 | |
---|
[1] | 154 | // Single checkbox |
---|
| 155 | case 'toggle' : |
---|
| 156 | $output[$field] = <<<E_O_F |
---|
[51] | 157 | |
---|
[295] | 158 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 159 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 160 | <label><input type="checkbox" name="$field" id="$field" <\x3fphp frmChecked(!empty(\$frm['$field'])) \x3f> /><\x3fphp echo _("Check this box to $title"); \x3f></label> |
---|
| 161 | </div> |
---|
[1] | 162 | E_O_F; |
---|
| 163 | break; |
---|
[42] | 164 | |
---|
[51] | 165 | // Textarea small |
---|
[485] | 166 | case 'tinytiny' : |
---|
[1] | 167 | case 'text' : |
---|
[51] | 168 | case 'tinyblob' : |
---|
| 169 | case 'blob' : |
---|
| 170 | $output[$field] = <<<E_O_F |
---|
| 171 | |
---|
[295] | 172 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 173 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
[485] | 174 | <textarea name="$field" id="$field" rows="8" cols="40" class="sc-tall sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
[295] | 175 | </div> |
---|
[51] | 176 | E_O_F; |
---|
| 177 | break; |
---|
| 178 | |
---|
| 179 | // Textarea big |
---|
[1] | 180 | case 'mediumtext' : |
---|
| 181 | case 'longtext' : |
---|
| 182 | case 'mediumblob' : |
---|
| 183 | case 'longblob' : |
---|
| 184 | $output[$field] = <<<E_O_F |
---|
[51] | 185 | |
---|
[295] | 186 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 187 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
[485] | 188 | <textarea name="$field" id="$field" rows="8" cols="40" class="sc-fullscreen sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
[295] | 189 | </div> |
---|
[1] | 190 | E_O_F; |
---|
| 191 | break; |
---|
[42] | 192 | |
---|
[51] | 193 | // Number |
---|
[1] | 194 | case 'tinyint' : |
---|
| 195 | case 'bit' : |
---|
| 196 | case 'bool' : |
---|
| 197 | case 'smallint' : |
---|
| 198 | case 'mediumint' : |
---|
| 199 | case 'int' : |
---|
| 200 | case 'integer' : |
---|
| 201 | case 'bigint' : |
---|
[42] | 202 | |
---|
[1] | 203 | case 'float' : |
---|
| 204 | case 'float' : |
---|
| 205 | case 'double' : |
---|
| 206 | case 'double' : |
---|
| 207 | case 'real' : |
---|
| 208 | case 'decimal' : |
---|
| 209 | case 'dec' : |
---|
| 210 | case 'numeric' : |
---|
[51] | 211 | default : |
---|
| 212 | $output[$field] = <<<E_O_F |
---|
[42] | 213 | |
---|
[295] | 214 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 215 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 216 | <input type="text" name="$field" id="$field" class="sc-tiny" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
| 217 | </div> |
---|
[51] | 218 | E_O_F; |
---|
| 219 | break; |
---|
| 220 | |
---|
| 221 | // Date |
---|
[1] | 222 | case 'date' : |
---|
| 223 | case 'datetime' : |
---|
| 224 | case 'timestamp' : |
---|
| 225 | case 'time' : |
---|
| 226 | case 'year' : |
---|
[51] | 227 | default : |
---|
| 228 | $output[$field] = <<<E_O_F |
---|
[42] | 229 | |
---|
[295] | 230 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 231 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 232 | <input type="text" name="$field" id="$field" class="sc-small" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
| 233 | </div> |
---|
[51] | 234 | E_O_F; |
---|
| 235 | break; |
---|
| 236 | |
---|
| 237 | // Text |
---|
[1] | 238 | case 'char' : |
---|
| 239 | case 'varchar' : |
---|
[51] | 240 | case 'tinytext' : |
---|
| 241 | case 'tinyblob' : |
---|
[1] | 242 | default : |
---|
| 243 | $output[$field] = <<<E_O_F |
---|
[51] | 244 | |
---|
[295] | 245 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
| 246 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
| 247 | <input type="text" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
| 248 | </div> |
---|
[1] | 249 | E_O_F; |
---|
| 250 | break; |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | } else { |
---|
| 254 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | |
---|
| 258 | echo join("\n", $output); |
---|