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 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
66 | <select name="$field" id="$field" class="sc-small"><\x3fphp printSelectForm('__///___tbl', "CONCAT(__///___id, '—', __///__)", '$field', \$frm['$field'], true, 'ORDER BY $field ASC'); \x3f></select> |
---|
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> |
---|
68 | <\x3fphp if ('' != \$frm['__///___id']) { \x3f> |
---|
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> |
---|
70 | <\x3fphp } \x3f> |
---|
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 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
80 | <input type="file" name="$field" id="$field" /> |
---|
81 | <\x3fphp if ('' != \$upload->getFilenameGlob(getFormData('$primary_key') . '_*') && getFormData('op') == 'edit' || getFormData('op') == 'update') { \x3f> |
---|
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> |
---|
83 | <\x3fphp } \x3f> |
---|
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> |
---|
85 | E_O_F; |
---|
86 | continue; |
---|
87 | } |
---|
88 | |
---|
89 | // Password field. |
---|
90 | if (preg_match('/pass/i', $field)) { |
---|
91 | $output[$field] = <<<E_O_F |
---|
92 | |
---|
93 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
94 | <input type="password" class="sc-medium" size="50" name="$field" id="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
95 | E_O_F; |
---|
96 | continue; |
---|
97 | } |
---|
98 | |
---|
99 | switch ($type) { |
---|
100 | |
---|
101 | // Select menu (or radio buttons) |
---|
102 | case 'enum' : |
---|
103 | $output[$field] = <<<E_O_F |
---|
104 | |
---|
105 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
106 | <select name="$field" id="$field"><\x3fphp printSetSelectForm('$db_tbl', '$field', \$frm['$field'], true); \x3f></select> |
---|
107 | E_O_F; |
---|
108 | break; |
---|
109 | |
---|
110 | // Set checkboxes |
---|
111 | case 'set' : |
---|
112 | $output[$field] = <<<E_O_F |
---|
113 | |
---|
114 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
115 | <\x3fphp printSetCheckboxes('$db_tbl', '$field', \$frm['$field'], 1) \x3f> |
---|
116 | E_O_F; |
---|
117 | break; |
---|
118 | |
---|
119 | // Single checkbox |
---|
120 | case 'toggle' : |
---|
121 | $output[$field] = <<<E_O_F |
---|
122 | |
---|
123 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
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> |
---|
125 | E_O_F; |
---|
126 | break; |
---|
127 | |
---|
128 | // Textarea small |
---|
129 | case 'text' : |
---|
130 | case 'tinyblob' : |
---|
131 | case 'blob' : |
---|
132 | $output[$field] = <<<E_O_F |
---|
133 | |
---|
134 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
135 | <textarea class="sc-large sc-short" cols="75" rows="4" name="$field" id="$field"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
136 | E_O_F; |
---|
137 | break; |
---|
138 | |
---|
139 | // Textarea big |
---|
140 | case 'mediumtext' : |
---|
141 | case 'longtext' : |
---|
142 | case 'mediumblob' : |
---|
143 | case 'longblob' : |
---|
144 | $output[$field] = <<<E_O_F |
---|
145 | |
---|
146 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
147 | <textarea class="sc-large sc-tall" cols="75" rows="4" name="$field" id="$field"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea> |
---|
148 | E_O_F; |
---|
149 | break; |
---|
150 | |
---|
151 | // Number |
---|
152 | case 'tinyint' : |
---|
153 | case 'bit' : |
---|
154 | case 'bool' : |
---|
155 | case 'smallint' : |
---|
156 | case 'mediumint' : |
---|
157 | case 'int' : |
---|
158 | case 'integer' : |
---|
159 | case 'bigint' : |
---|
160 | |
---|
161 | case 'float' : |
---|
162 | case 'float' : |
---|
163 | case 'double' : |
---|
164 | case 'double' : |
---|
165 | case 'real' : |
---|
166 | case 'decimal' : |
---|
167 | case 'dec' : |
---|
168 | case 'numeric' : |
---|
169 | default : |
---|
170 | $output[$field] = <<<E_O_F |
---|
171 | |
---|
172 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
173 | <input type="text" class="sc-tiny" size="12" name="$field" id="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
174 | E_O_F; |
---|
175 | break; |
---|
176 | |
---|
177 | // Date |
---|
178 | case 'date' : |
---|
179 | case 'datetime' : |
---|
180 | case 'timestamp' : |
---|
181 | case 'time' : |
---|
182 | case 'year' : |
---|
183 | default : |
---|
184 | $output[$field] = <<<E_O_F |
---|
185 | |
---|
186 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
187 | <input type="text" class="sc-medium" size="25" name="$field" id="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
188 | E_O_F; |
---|
189 | break; |
---|
190 | |
---|
191 | // Text |
---|
192 | case 'char' : |
---|
193 | case 'varchar' : |
---|
194 | case 'tinytext' : |
---|
195 | case 'tinyblob' : |
---|
196 | default : |
---|
197 | $output[$field] = <<<E_O_F |
---|
198 | |
---|
199 | <label for="$field"<\x3fphp \$fv->err('$field') \x3f>><\x3fphp echo _("$title"); \x3f></label> |
---|
200 | <input type="text" class="sc-medium" size="50" name="$field" id="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" /> |
---|
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 | ?> |
---|