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. |
---|
19 | $qid = $db->query("SHOW TABLES"); |
---|
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)) { |
---|
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))); |
---|
27 | } |
---|
28 | |
---|
29 | // Get DB table column info. |
---|
30 | $qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl)); |
---|
31 | while ($row = mysql_fetch_row($qid)) { |
---|
32 | $cols[] = $row; |
---|
33 | } |
---|
34 | |
---|
35 | $exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id'); |
---|
36 | $primary_key = '__///__'; |
---|
37 | $output = array(); |
---|
38 | |
---|
39 | // Loop through columns |
---|
40 | if (is_array($cols) && !empty($cols)) { |
---|
41 | foreach ($cols as $col) { |
---|
42 | |
---|
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]) { |
---|
48 | $primary_key = $field; |
---|
49 | } |
---|
50 | |
---|
51 | // Column types like this are usually single toggle checkboxes. |
---|
52 | if (preg_match("/enum\('true'\)/", $col[1])) { |
---|
53 | $type = 'toggle'; |
---|
54 | } |
---|
55 | |
---|
56 | if (in_array($field, $exclude) || $primary_key == $field) { |
---|
57 | // Don't add a field for this column. |
---|
58 | continue; |
---|
59 | } |
---|
60 | |
---|
61 | // Select menu from the column of a related database table. |
---|
62 | if (preg_match('/.*_id$/i', $field)) { |
---|
63 | $output[$field] = <<<E_O_F |
---|
64 | |
---|
65 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
66 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
67 | <select name="$field" id="$field" class="sc-small"><\x3fphp printSelectForm('__///___tbl', "CONCAT(__///___id, '—', __///__)", '$field', \$frm['$field'], true, 'ORDER BY $field ASC'); \x3f></select> |
---|
68 | <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> |
---|
69 | <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> |
---|
70 | </div> |
---|
71 | E_O_F; |
---|
72 | continue; |
---|
73 | } |
---|
74 | |
---|
75 | // File upload. |
---|
76 | if (preg_match('/file|image/i', $field)) { |
---|
77 | $output[$field] = <<<E_O_F |
---|
78 | |
---|
79 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
80 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
81 | <input type="file" name="$field" id="$field" /> |
---|
82 | <\x3fphp if ('' != \$upload->getFilenameGlob(getFormData('$primary_key') . '_*') && getFormData('op') == 'edit' || getFormData('op') == 'update') { \x3f> |
---|
83 | <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> |
---|
84 | <\x3fphp } \x3f> |
---|
85 | <div class="sc-help"><\x3fphp printf(_("Allowed file types: %s."), join(', ', \$upload->getParam('valid_file_extensions'))) \x3f></div> |
---|
86 | </div> |
---|
87 | E_O_F; |
---|
88 | continue; |
---|
89 | } |
---|
90 | |
---|
91 | // Password field. |
---|
92 | if (preg_match('/pass/i', $field)) { |
---|
93 | $output[$field] = <<<E_O_F |
---|
94 | |
---|
95 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
96 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
97 | <input type="password" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
98 | </div> |
---|
99 | E_O_F; |
---|
100 | continue; |
---|
101 | } |
---|
102 | |
---|
103 | switch ($type) { |
---|
104 | |
---|
105 | // Select menu (or radio buttons) |
---|
106 | case 'enum' : |
---|
107 | $output[$field] = <<<E_O_F |
---|
108 | |
---|
109 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
110 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
111 | <select name="$field" id="$field"><\x3fphp printSetSelectForm('$db_tbl', '$field', \$frm['$field'], true); \x3f></select> |
---|
112 | </div> |
---|
113 | E_O_F; |
---|
114 | break; |
---|
115 | |
---|
116 | // Set checkboxes |
---|
117 | case 'set' : |
---|
118 | $output[$field] = <<<E_O_F |
---|
119 | |
---|
120 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
121 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
122 | <\x3fphp printSetCheckboxes('$db_tbl', '$field', \$frm['$field'], 1) \x3f> |
---|
123 | </div> |
---|
124 | E_O_F; |
---|
125 | break; |
---|
126 | |
---|
127 | // Single checkbox |
---|
128 | case 'toggle' : |
---|
129 | $output[$field] = <<<E_O_F |
---|
130 | |
---|
131 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
132 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
133 | <label><input type="checkbox" name="$field" id="$field" <\x3fphp frmChecked(!empty(\$frm['$field'])) \x3f> /><\x3fphp echo _("Check this box to $title"); \x3f></label> |
---|
134 | </div> |
---|
135 | E_O_F; |
---|
136 | break; |
---|
137 | |
---|
138 | // Textarea small |
---|
139 | case 'text' : |
---|
140 | case 'tinyblob' : |
---|
141 | case 'blob' : |
---|
142 | $output[$field] = <<<E_O_F |
---|
143 | |
---|
144 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
145 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
146 | <textarea name="$field" id="$field" rows="8" cols="40" class="sc-short sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
147 | </div> |
---|
148 | E_O_F; |
---|
149 | break; |
---|
150 | |
---|
151 | // Textarea big |
---|
152 | case 'mediumtext' : |
---|
153 | case 'longtext' : |
---|
154 | case 'mediumblob' : |
---|
155 | case 'longblob' : |
---|
156 | $output[$field] = <<<E_O_F |
---|
157 | |
---|
158 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
159 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
160 | <textarea name="$field" id="$field" rows="8" cols="40" class="sc-short sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
161 | </div> |
---|
162 | E_O_F; |
---|
163 | break; |
---|
164 | |
---|
165 | // Number |
---|
166 | case 'tinyint' : |
---|
167 | case 'bit' : |
---|
168 | case 'bool' : |
---|
169 | case 'smallint' : |
---|
170 | case 'mediumint' : |
---|
171 | case 'int' : |
---|
172 | case 'integer' : |
---|
173 | case 'bigint' : |
---|
174 | |
---|
175 | case 'float' : |
---|
176 | case 'float' : |
---|
177 | case 'double' : |
---|
178 | case 'double' : |
---|
179 | case 'real' : |
---|
180 | case 'decimal' : |
---|
181 | case 'dec' : |
---|
182 | case 'numeric' : |
---|
183 | default : |
---|
184 | $output[$field] = <<<E_O_F |
---|
185 | |
---|
186 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
187 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
188 | <input type="text" name="$field" id="$field" class="sc-tiny" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
189 | </div> |
---|
190 | E_O_F; |
---|
191 | break; |
---|
192 | |
---|
193 | // Date |
---|
194 | case 'date' : |
---|
195 | case 'datetime' : |
---|
196 | case 'timestamp' : |
---|
197 | case 'time' : |
---|
198 | case 'year' : |
---|
199 | default : |
---|
200 | $output[$field] = <<<E_O_F |
---|
201 | |
---|
202 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
203 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
204 | <input type="text" name="$field" id="$field" class="sc-small" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
205 | </div> |
---|
206 | E_O_F; |
---|
207 | break; |
---|
208 | |
---|
209 | // Text |
---|
210 | case 'char' : |
---|
211 | case 'varchar' : |
---|
212 | case 'tinytext' : |
---|
213 | case 'tinyblob' : |
---|
214 | default : |
---|
215 | $output[$field] = <<<E_O_F |
---|
216 | |
---|
217 | <div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>"> |
---|
218 | <label for="$field"><\x3fphp echo _("$title"); \x3f></label> |
---|
219 | <input type="text" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
220 | </div> |
---|
221 | E_O_F; |
---|
222 | break; |
---|
223 | } |
---|
224 | } |
---|
225 | } else { |
---|
226 | die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n"); |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | echo join("\n", $output); |
---|
231 | |
---|
232 | ?> |
---|