1 | #!/usr/bin/php -q |
---|
2 | <?php |
---|
3 | /** |
---|
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-2012 Strangecode, LLC |
---|
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 | /** |
---|
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. |
---|
39 | $qid = $db->query("SHOW TABLES"); |
---|
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)) { |
---|
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))); |
---|
47 | } |
---|
48 | |
---|
49 | // Get DB table column info. |
---|
50 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
51 | while ($row = mysql_fetch_row($qid)) { |
---|
52 | $cols[] = $row; |
---|
53 | } |
---|
54 | |
---|
55 | $exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id'); |
---|
56 | $primary_key = '__///__'; |
---|
57 | $output = array(); |
---|
58 | |
---|
59 | // Loop through columns |
---|
60 | if (is_array($cols) && !empty($cols)) { |
---|
61 | foreach ($cols as $col) { |
---|
62 | |
---|
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]) { |
---|
68 | $primary_key = $field; |
---|
69 | } |
---|
70 | |
---|
71 | // Column types like this are usually single toggle checkboxes. |
---|
72 | if (preg_match("/enum\('true'\)/", $col[1])) { |
---|
73 | $type = 'toggle'; |
---|
74 | } |
---|
75 | |
---|
76 | if (in_array($field, $exclude) || $primary_key == $field) { |
---|
77 | // Don't add a field for this column. |
---|
78 | continue; |
---|
79 | } |
---|
80 | |
---|
81 | // Select menu from the column of a related database table. |
---|
82 | if (preg_match('/.*_id$/i', $field)) { |
---|
83 | $output[$field] = <<<E_O_F |
---|
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> |
---|
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> |
---|
90 | </div> |
---|
91 | E_O_F; |
---|
92 | continue; |
---|
93 | } |
---|
94 | |
---|
95 | // File upload. |
---|
96 | if (preg_match('/file|image/i', $field)) { |
---|
97 | $output[$field] = <<<E_O_F |
---|
98 | |
---|
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" /> |
---|
102 | <\x3fphp if ('' != \$upload->getFilenameGlob(getFormData('$primary_key') . '_*') && getFormData('op') == 'edit' || getFormData('op') == 'update') { \x3f> |
---|
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> |
---|
104 | <\x3fphp } \x3f> |
---|
105 | <div class="sc-help"><\x3fphp printf(_("Allowed file types: %s."), join(', ', \$upload->getParam('valid_file_extensions'))) \x3f></div> |
---|
106 | </div> |
---|
107 | E_O_F; |
---|
108 | continue; |
---|
109 | } |
---|
110 | |
---|
111 | // Password field. |
---|
112 | if (preg_match('/pass/i', $field)) { |
---|
113 | $output[$field] = <<<E_O_F |
---|
114 | |
---|
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> |
---|
119 | E_O_F; |
---|
120 | continue; |
---|
121 | } |
---|
122 | |
---|
123 | switch ($type) { |
---|
124 | |
---|
125 | // Select menu (or radio buttons) |
---|
126 | case 'enum' : |
---|
127 | $output[$field] = <<<E_O_F |
---|
128 | |
---|
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> |
---|
133 | E_O_F; |
---|
134 | break; |
---|
135 | |
---|
136 | // Set checkboxes |
---|
137 | case 'set' : |
---|
138 | $output[$field] = <<<E_O_F |
---|
139 | |
---|
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> |
---|
144 | E_O_F; |
---|
145 | break; |
---|
146 | |
---|
147 | // Single checkbox |
---|
148 | case 'toggle' : |
---|
149 | $output[$field] = <<<E_O_F |
---|
150 | |
---|
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> |
---|
155 | E_O_F; |
---|
156 | break; |
---|
157 | |
---|
158 | // Textarea small |
---|
159 | case 'tinytiny' : |
---|
160 | case 'text' : |
---|
161 | case 'tinyblob' : |
---|
162 | case 'blob' : |
---|
163 | $output[$field] = <<<E_O_F |
---|
164 | |
---|
165 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
166 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
167 | <textarea name="$field" id="$field" rows="8" cols="40" class="sc-tall sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
168 | </div> |
---|
169 | E_O_F; |
---|
170 | break; |
---|
171 | |
---|
172 | // Textarea big |
---|
173 | case 'mediumtext' : |
---|
174 | case 'longtext' : |
---|
175 | case 'mediumblob' : |
---|
176 | case 'longblob' : |
---|
177 | $output[$field] = <<<E_O_F |
---|
178 | |
---|
179 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
180 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
181 | <textarea name="$field" id="$field" rows="8" cols="40" class="sc-fullscreen sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
182 | </div> |
---|
183 | E_O_F; |
---|
184 | break; |
---|
185 | |
---|
186 | // Number |
---|
187 | case 'tinyint' : |
---|
188 | case 'bit' : |
---|
189 | case 'bool' : |
---|
190 | case 'smallint' : |
---|
191 | case 'mediumint' : |
---|
192 | case 'int' : |
---|
193 | case 'integer' : |
---|
194 | case 'bigint' : |
---|
195 | |
---|
196 | case 'float' : |
---|
197 | case 'float' : |
---|
198 | case 'double' : |
---|
199 | case 'double' : |
---|
200 | case 'real' : |
---|
201 | case 'decimal' : |
---|
202 | case 'dec' : |
---|
203 | case 'numeric' : |
---|
204 | default : |
---|
205 | $output[$field] = <<<E_O_F |
---|
206 | |
---|
207 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
208 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
209 | <input type="text" name="$field" id="$field" class="sc-tiny" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
210 | </div> |
---|
211 | E_O_F; |
---|
212 | break; |
---|
213 | |
---|
214 | // Date |
---|
215 | case 'date' : |
---|
216 | case 'datetime' : |
---|
217 | case 'timestamp' : |
---|
218 | case 'time' : |
---|
219 | case 'year' : |
---|
220 | default : |
---|
221 | $output[$field] = <<<E_O_F |
---|
222 | |
---|
223 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
224 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
225 | <input type="text" name="$field" id="$field" class="sc-small" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
226 | </div> |
---|
227 | E_O_F; |
---|
228 | break; |
---|
229 | |
---|
230 | // Text |
---|
231 | case 'char' : |
---|
232 | case 'varchar' : |
---|
233 | case 'tinytext' : |
---|
234 | case 'tinyblob' : |
---|
235 | default : |
---|
236 | $output[$field] = <<<E_O_F |
---|
237 | |
---|
238 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
239 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
240 | <input type="text" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
241 | </div> |
---|
242 | E_O_F; |
---|
243 | break; |
---|
244 | } |
---|
245 | } |
---|
246 | } else { |
---|
247 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | echo join("\n", $output); |
---|