[1] | 1 | #!/usr/local/bin/php -q |
---|
| 2 | <?php |
---|
| 3 | /** |
---|
| 4 | * form_template.cli.php |
---|
| 5 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | include_once dirname(__FILE__) . '/_config.inc.php'; |
---|
| 9 | |
---|
| 10 | // Test arguments. |
---|
| 11 | if (isset($_SERVER['argv'][2])) { |
---|
| 12 | // Second arg is db table. |
---|
| 13 | $db_tbl = $_SERVER['argv'][2]; |
---|
| 14 | } else { |
---|
| 15 | die(sprintf("Usage: %s site_directory db_table\n", basename($_SERVER['argv'][0]))); |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | // Get DB tables. |
---|
[136] | 19 | $qid = $db->query("SHOW TABLES"); |
---|
[1] | 20 | while (list($row) = mysql_fetch_row($qid)) { |
---|
| 21 | $tables[] = $row; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | // Make sure requested table is in database. |
---|
| 25 | if (!in_array($db_tbl, $tables)) { |
---|
[136] | 26 | 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] | 27 | } |
---|
| 28 | |
---|
| 29 | // Get DB table column info. |
---|
[136] | 30 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
[1] | 31 | while ($row = mysql_fetch_row($qid)) { |
---|
| 32 | $cols[] = $row; |
---|
| 33 | } |
---|
| 34 | |
---|
[19] | 35 | $exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id'); |
---|
[154] | 36 | $primary_key = '__///__'; |
---|
[1] | 37 | $output = array(); |
---|
| 38 | |
---|
| 39 | // Loop through columns |
---|
| 40 | if (is_array($cols) && !empty($cols)) { |
---|
| 41 | foreach ($cols as $col) { |
---|
[42] | 42 | |
---|
[1] | 43 | // Human readable. |
---|
| 44 | $field = $col[0]; |
---|
| 45 | $title = ucfirst(str_replace('_', ' ', $field)); |
---|
| 46 | $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]); |
---|
| 47 | if ('PRI' == $col[3]) { |
---|
[44] | 48 | $primary_key = $field; |
---|
[1] | 49 | } |
---|
[42] | 50 | |
---|
[1] | 51 | // Column types like this are usually single toggle checkboxes. |
---|
| 52 | if (preg_match("/enum\('true'\)/", $col[1])) { |
---|
| 53 | $type = 'toggle'; |
---|
| 54 | } |
---|
[42] | 55 | |
---|
[44] | 56 | if (in_array($field, $exclude) || $primary_key == $field) { |
---|
[1] | 57 | // Don't add a field for this column. |
---|
| 58 | continue; |
---|
| 59 | } |
---|
[42] | 60 | |
---|
[1] | 61 | // Select menu from the column of a related database table. |
---|
| 62 | if (preg_match('/.*_id$/i', $field)) { |
---|
| 63 | $output[$field] = <<<E_O_F |
---|
[51] | 64 | |
---|
[106] | 65 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
[154] | 66 | <select name="$field" id="$field" class="sc-small"><\x3fphp printSelectForm('__///___tbl', "CONCAT(__///___id, '—', __///__)", '$field', \$frm['$field'], true, 'ORDER BY $field ASC'); \x3f></select> |
---|
[270] | 67 | <span class="commanditem commandtext"><a href="<\x3fphp echo \$app->oHREF('__///__.php?op=add&boomerang=true'); \x3f>" onclick"javascript:return confirm('<\x3fphp echo _("Notice: You are about to leave this form page and any changes you may have made without saving will be lost."); \x3f>')"><\x3fphp echo _("Add __///__") \x3f></a></span> |
---|
[154] | 68 | <\x3fphp if ('' != \$frm['__///___id']) { \x3f> |
---|
[270] | 69 | <span class="commanditem commandtext"><a href="<\x3fphp echo \$app->oHREF('__///__.php?op=edit&boomerang=true&__///___id=' . \$frm['__///___id']); \x3f>" onclick"javascript:return confirm('<\x3fphp echo _("Notice: You are about to leave this form page and any changes you may have made without saving will be lost."); \x3f>')"><\x3fphp echo sprintf(_("Edit __///__ <em>%s</em>"), \$frm['__///__']) \x3f></a></span> |
---|
[51] | 70 | <\x3fphp } \x3f> |
---|
[1] | 71 | E_O_F; |
---|
| 72 | continue; |
---|
| 73 | } |
---|
[42] | 74 | |
---|
[1] | 75 | // File upload. |
---|
[20] | 76 | if (preg_match('/file|image/i', $field)) { |
---|
[1] | 77 | $output[$field] = <<<E_O_F |
---|
[51] | 78 | |
---|
[106] | 79 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
[51] | 80 | <input type="file" name="$field" id="$field" /> |
---|
| 81 | <\x3fphp if ('' != \$upload->getFilenameGlob(getFormData('$primary_key') . '_*') && getFormData('op') == 'edit' || getFormData('op') == 'update') { \x3f> |
---|
[154] | 82 | <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] | 83 | <\x3fphp } \x3f> |
---|
[121] | 84 | <div class="sc-help"><\x3fphp printf(_("File to upload must have one of the following file-name extensions: %s."), join(', ', \$upload->getParam('valid_file_extensions'))) \x3f></div> |
---|
[1] | 85 | E_O_F; |
---|
| 86 | continue; |
---|
| 87 | } |
---|
[42] | 88 | |
---|
[1] | 89 | // Password field. |
---|
| 90 | if (preg_match('/pass/i', $field)) { |
---|
| 91 | $output[$field] = <<<E_O_F |
---|
[51] | 92 | |
---|
[106] | 93 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
[121] | 94 | <input type="password" class="sc-medium" size="50" name="$field" id="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
[1] | 95 | E_O_F; |
---|
| 96 | continue; |
---|
| 97 | } |
---|
[42] | 98 | |
---|
[1] | 99 | switch ($type) { |
---|
[42] | 100 | |
---|
[1] | 101 | // Select menu (or radio buttons) |
---|
| 102 | case 'enum' : |
---|
| 103 | $output[$field] = <<<E_O_F |
---|
[51] | 104 | |
---|
[106] | 105 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
[51] | 106 | <select name="$field" id="$field"><\x3fphp printSetSelectForm('$db_tbl', '$field', \$frm['$field'], true); \x3f></select> |
---|
[1] | 107 | E_O_F; |
---|
| 108 | break; |
---|
[42] | 109 | |
---|
[1] | 110 | // Set checkboxes |
---|
| 111 | case 'set' : |
---|
| 112 | $output[$field] = <<<E_O_F |
---|
[51] | 113 | |
---|
[106] | 114 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
[51] | 115 | <\x3fphp printSetCheckboxes('$db_tbl', '$field', \$frm['$field'], 1) \x3f> |
---|
[1] | 116 | E_O_F; |
---|
| 117 | break; |
---|
[42] | 118 | |
---|
[1] | 119 | // Single checkbox |
---|
| 120 | case 'toggle' : |
---|
| 121 | $output[$field] = <<<E_O_F |
---|
[51] | 122 | |
---|
[106] | 123 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
[51] | 124 | <input type="checkbox" name="$field" id="$field" <\x3fphp frmChecked(!empty(\$frm['$field'])) \x3f> /><label class="left"><\x3fphp echo _("Check this box to $title"); \x3f></label> |
---|
[1] | 125 | E_O_F; |
---|
| 126 | break; |
---|
[42] | 127 | |
---|
[51] | 128 | // Textarea small |
---|
[1] | 129 | case 'text' : |
---|
[51] | 130 | case 'tinyblob' : |
---|
| 131 | case 'blob' : |
---|
| 132 | $output[$field] = <<<E_O_F |
---|
| 133 | |
---|
[106] | 134 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
[121] | 135 | <textarea class="sc-large sc-short" cols="75" rows="4" name="$field" id="$field"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
[51] | 136 | E_O_F; |
---|
| 137 | break; |
---|
| 138 | |
---|
| 139 | // Textarea big |
---|
[1] | 140 | case 'mediumtext' : |
---|
| 141 | case 'longtext' : |
---|
| 142 | case 'mediumblob' : |
---|
| 143 | case 'longblob' : |
---|
| 144 | $output[$field] = <<<E_O_F |
---|
[51] | 145 | |
---|
[106] | 146 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
[121] | 147 | <textarea class="sc-large sc-tall" cols="75" rows="4" name="$field" id="$field"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
[1] | 148 | E_O_F; |
---|
| 149 | break; |
---|
[42] | 150 | |
---|
[51] | 151 | // Number |
---|
[1] | 152 | case 'tinyint' : |
---|
| 153 | case 'bit' : |
---|
| 154 | case 'bool' : |
---|
| 155 | case 'smallint' : |
---|
| 156 | case 'mediumint' : |
---|
| 157 | case 'int' : |
---|
| 158 | case 'integer' : |
---|
| 159 | case 'bigint' : |
---|
[42] | 160 | |
---|
[1] | 161 | case 'float' : |
---|
| 162 | case 'float' : |
---|
| 163 | case 'double' : |
---|
| 164 | case 'double' : |
---|
| 165 | case 'real' : |
---|
| 166 | case 'decimal' : |
---|
| 167 | case 'dec' : |
---|
| 168 | case 'numeric' : |
---|
[51] | 169 | default : |
---|
| 170 | $output[$field] = <<<E_O_F |
---|
[42] | 171 | |
---|
[106] | 172 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
[121] | 173 | <input type="text" class="sc-tiny" size="12" name="$field" id="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
[51] | 174 | E_O_F; |
---|
| 175 | break; |
---|
| 176 | |
---|
| 177 | // Date |
---|
[1] | 178 | case 'date' : |
---|
| 179 | case 'datetime' : |
---|
| 180 | case 'timestamp' : |
---|
| 181 | case 'time' : |
---|
| 182 | case 'year' : |
---|
[51] | 183 | default : |
---|
| 184 | $output[$field] = <<<E_O_F |
---|
[42] | 185 | |
---|
[106] | 186 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
[121] | 187 | <input type="text" class="sc-medium" size="25" name="$field" id="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
[51] | 188 | E_O_F; |
---|
| 189 | break; |
---|
| 190 | |
---|
| 191 | // Text |
---|
[1] | 192 | case 'char' : |
---|
| 193 | case 'varchar' : |
---|
[51] | 194 | case 'tinytext' : |
---|
| 195 | case 'tinyblob' : |
---|
[1] | 196 | default : |
---|
| 197 | $output[$field] = <<<E_O_F |
---|
[51] | 198 | |
---|
[106] | 199 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
[121] | 200 | <input type="text" class="sc-medium" size="50" name="$field" id="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
[1] | 201 | E_O_F; |
---|
| 202 | break; |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | } else { |
---|
| 206 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | echo join("\n", $output); |
---|
| 211 | |
---|
| 212 | ?> |
---|