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 " . addslashes($db_tbl)); |
---|
31 | while ($row = mysql_fetch_row($qid)) { |
---|
32 | $cols[] = $row; |
---|
33 | } |
---|
34 | |
---|
35 | $exclude = array('added_by_admin_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_admin_id'); |
---|
36 | $primary_key_field = ''; |
---|
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 = $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 == $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 | <tr> |
---|
65 | <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td> |
---|
66 | <td valign="top"> |
---|
67 | <select name="$field" class="formselect"> |
---|
68 | <\x3fphp printSelectForm('<##>_tbl', "CONCAT(<##>_id, '—', <##>)", '$field', \$frm['$field'], true, 'ORDER BY $field DESC'); \x3f> |
---|
69 | </select> |
---|
70 | </td> |
---|
71 | </tr> |
---|
72 | E_O_F; |
---|
73 | continue; |
---|
74 | } |
---|
75 | |
---|
76 | // File upload. |
---|
77 | if (preg_match('/file/i', $field)) { |
---|
78 | $output[$field] = <<<E_O_F |
---|
79 | <tr> |
---|
80 | <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td> |
---|
81 | <td valign="top"> |
---|
82 | <input type="file" name="$field" value="<\x3fphp echo \$_FILES['$field']['name']; \x3f>" /> |
---|
83 | <\x3fphp if (getFormData('op') == 'edit' || getFormData('op') == 'update') { \x3f> |
---|
84 | <div class="help"><\x3fphp printf(_("The current file <strong>%s</strong> will be overwritten if a new file is selected for upload."), \$frm['$field']) \x3f></div> |
---|
85 | <\x3fphp } \x3f> |
---|
86 | <div class="help"><\x3fphp printf(_("File to upload must have one of the following file-name extensions: %s."), join(', ', \$upload->valid_file_extensions)) \x3f></div> |
---|
87 | </td> |
---|
88 | </tr> |
---|
89 | E_O_F; |
---|
90 | continue; |
---|
91 | } |
---|
92 | |
---|
93 | // Password field. |
---|
94 | if (preg_match('/pass/i', $field)) { |
---|
95 | $output[$field] = <<<E_O_F |
---|
96 | <tr> |
---|
97 | <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td> |
---|
98 | <td valign="top"> |
---|
99 | <input type="password" class="forminputtext" size="50" name="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>"> |
---|
100 | </td> |
---|
101 | </tr> |
---|
102 | E_O_F; |
---|
103 | continue; |
---|
104 | } |
---|
105 | |
---|
106 | switch ($type) { |
---|
107 | |
---|
108 | // Select menu (or radio buttons) |
---|
109 | case 'enum' : |
---|
110 | $output[$field] = <<<E_O_F |
---|
111 | <tr> |
---|
112 | <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td> |
---|
113 | <td valign="top"> |
---|
114 | <select name="$field"> |
---|
115 | <\x3fphp printSetSelectForm('$db_tbl', '$field', \$frm['$field'], true); \x3f> |
---|
116 | </select> |
---|
117 | </td> |
---|
118 | </tr> |
---|
119 | E_O_F; |
---|
120 | break; |
---|
121 | |
---|
122 | // Set checkboxes |
---|
123 | case 'set' : |
---|
124 | $output[$field] = <<<E_O_F |
---|
125 | <tr> |
---|
126 | <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td> |
---|
127 | <td valign="top"> |
---|
128 | <\x3fphp printSetCheckboxes('$db_tbl', '$field', \$frm['$field'], 1) \x3f> |
---|
129 | </td> |
---|
130 | </tr> |
---|
131 | E_O_F; |
---|
132 | break; |
---|
133 | |
---|
134 | // Single checkbox |
---|
135 | case 'toggle' : |
---|
136 | $output[$field] = <<<E_O_F |
---|
137 | <tr> |
---|
138 | <td> </td> |
---|
139 | <td> |
---|
140 | <input type="checkbox" name="$field" <\x3fphp frmChecked(!empty(\$frm['$field'])) \x3f>><label><\x3fphp echo _("$title"); \x3f></label> |
---|
141 | </td> |
---|
142 | </tr> |
---|
143 | E_O_F; |
---|
144 | break; |
---|
145 | |
---|
146 | // Textarea |
---|
147 | case 'tinytext' : |
---|
148 | case 'text' : |
---|
149 | case 'mediumtext' : |
---|
150 | case 'longtext' : |
---|
151 | case 'tinyblob' : |
---|
152 | case 'blob' : |
---|
153 | case 'mediumblob' : |
---|
154 | case 'longblob' : |
---|
155 | $output[$field] = <<<E_O_F |
---|
156 | <tr> |
---|
157 | <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td> |
---|
158 | <td valign="top"> |
---|
159 | <textarea class="text" cols="75" rows="4" name="$field"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
160 | </td> |
---|
161 | </tr> |
---|
162 | E_O_F; |
---|
163 | break; |
---|
164 | |
---|
165 | |
---|
166 | // Text |
---|
167 | case 'tinyint' : |
---|
168 | case 'bit' : |
---|
169 | case 'bool' : |
---|
170 | case 'smallint' : |
---|
171 | case 'mediumint' : |
---|
172 | case 'int' : |
---|
173 | case 'integer' : |
---|
174 | case 'bigint' : |
---|
175 | |
---|
176 | case 'float' : |
---|
177 | case 'float' : |
---|
178 | case 'double' : |
---|
179 | case 'double' : |
---|
180 | case 'real' : |
---|
181 | case 'decimal' : |
---|
182 | case 'dec' : |
---|
183 | case 'numeric' : |
---|
184 | |
---|
185 | case 'date' : |
---|
186 | case 'datetime' : |
---|
187 | case 'timestamp' : |
---|
188 | case 'time' : |
---|
189 | case 'year' : |
---|
190 | |
---|
191 | case 'char' : |
---|
192 | case 'varchar' : |
---|
193 | default : |
---|
194 | $output[$field] = <<<E_O_F |
---|
195 | <tr> |
---|
196 | <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td> |
---|
197 | <td valign="top"> |
---|
198 | <input type="text" class="text" size="50" name="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>"> |
---|
199 | </td> |
---|
200 | </tr> |
---|
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 | ?> |
---|