source: trunk/bin/module_maker/form_template.cli.php @ 295

Last change on this file since 295 was 295, checked in by quinn, 16 years ago

Updated example config file. Added admin2.inc.css and minor corrections into HTML. Module maker fixes.

File size: 8.3 KB
Line 
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
8include_once dirname(__FILE__) . '/_config.inc.php';
9
10// Test arguments.
11if (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");
20while (list($row) = mysql_fetch_row($qid)) {
21    $tables[] = $row;
22}
23
24// Make sure requested table is in database.
25if (!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));
31while ($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
40if (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, '&mdash;', __///__)", '$field', \$frm['$field'], true, 'ORDER BY $field ASC'); \x3f></select>
68    <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>
69    <\x3fphp if ('' != \$frm['__///___id']) { \x3f>
70        <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>
71    <\x3fphp } \x3f>
72</div>
73E_O_F;
74            continue;
75        }
76
77        // File upload.
78        if (preg_match('/file|image/i', $field)) {
79            $output[$field] = <<<E_O_F
80
81<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
82    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
83    <input type="file" name="$field" id="$field" />
84    <\x3fphp if ('' != \$upload->getFilenameGlob(getFormData('$primary_key') . '_*') && getFormData('op') == 'edit' || getFormData('op') == 'update') { \x3f>
85        <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>
86    <\x3fphp } \x3f>
87    <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>
88</div>
89E_O_F;
90            continue;
91        }
92
93        // Password field.
94        if (preg_match('/pass/i', $field)) {
95            $output[$field] = <<<E_O_F
96
97<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
98    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
99    <input type="password" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
100</div>
101E_O_F;
102            continue;
103        }
104
105        switch ($type) {
106
107        // Select menu (or radio buttons)
108        case 'enum' :
109            $output[$field] = <<<E_O_F
110
111<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
112    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
113    <select name="$field" id="$field"><\x3fphp printSetSelectForm('$db_tbl', '$field', \$frm['$field'], true); \x3f></select>
114</div>
115E_O_F;
116            break;
117
118        // Set checkboxes
119        case 'set' :
120            $output[$field] = <<<E_O_F
121
122<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
123    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
124    <\x3fphp printSetCheckboxes('$db_tbl', '$field', \$frm['$field'], 1) \x3f>
125</div>
126E_O_F;
127            break;
128
129        // Single checkbox
130        case 'toggle' :
131            $output[$field] = <<<E_O_F
132
133<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
134    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
135    <label><input type="checkbox" name="$field" id="$field" <\x3fphp frmChecked(!empty(\$frm['$field'])) \x3f> /><\x3fphp echo _("Check this box to $title"); \x3f></label>
136</div>
137E_O_F;
138            break;
139
140        // Textarea small
141        case 'text' :
142        case 'tinyblob' :
143        case 'blob' :
144            $output[$field] = <<<E_O_F
145
146<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
147    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
148    <textarea name="$field" id="$field" rows="8" cols="40" class="sc-short sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea>
149</div>
150E_O_F;
151            break;
152
153        // Textarea big
154        case 'mediumtext' :
155        case 'longtext' :
156        case 'mediumblob' :
157        case 'longblob' :
158            $output[$field] = <<<E_O_F
159
160<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
161    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
162    <textarea name="$field" id="$field" rows="8" cols="40" class="sc-short sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea>
163</div>
164E_O_F;
165            break;
166
167        // Number
168        case 'tinyint' :
169        case 'bit' :
170        case 'bool' :
171        case 'smallint' :
172        case 'mediumint' :
173        case 'int' :
174        case 'integer' :
175        case 'bigint' :
176
177        case 'float' :
178        case 'float' :
179        case 'double' :
180        case 'double' :
181        case 'real' :
182        case 'decimal' :
183        case 'dec' :
184        case 'numeric' :
185        default :
186            $output[$field] = <<<E_O_F
187
188<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
189    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
190    <input type="text" name="$field" id="$field" class="sc-tiny" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
191</div>
192E_O_F;
193            break;
194
195        // Date
196        case 'date' :
197        case 'datetime' :
198        case 'timestamp' :
199        case 'time' :
200        case 'year' :
201        default :
202            $output[$field] = <<<E_O_F
203
204<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
205    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
206    <input type="text" name="$field" id="$field" class="sc-small" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
207</div>
208E_O_F;
209            break;
210
211        // Text
212        case 'char' :
213        case 'varchar' :
214        case 'tinytext' :
215        case 'tinyblob' :
216        default :
217            $output[$field] = <<<E_O_F
218
219<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
220    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
221    <input type="text" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
222</div>
223E_O_F;
224            break;
225        }
226    }
227} else {
228    die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n");
229}
230
231
232echo join("\n", $output);
233
234?>
Note: See TracBrowser for help on using the repository browser.