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

Last change on this file since 22 was 22, checked in by scdev, 18 years ago

More bugs and shifting things about.

File size: 7.7 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 " . addslashes($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_field = '';
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 = $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>
67            <select name="$field" class="small">
68            <\x3fphp printSelectForm('<##>_tbl', "CONCAT(<##>_id, '&mdash;', <##>)", '$field', \$frm['$field'], true, 'ORDER BY $field ASC'); \x3f>
69            </select>
70            <span class="commandtext">
71                <span class="commanditem"><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>
72                <\x3fphp if ('' != \$frm['<##>_id']) { \x3f>
73                    <span class="commanditem"><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>
74                <\x3fphp } \x3f>
75            </span>
76        </td>
77    </tr>
78E_O_F;
79            continue;
80        }
81       
82        // File upload.
83        if (preg_match('/file|image/i', $field)) {
84            $output[$field] = <<<E_O_F
85    <tr>
86        <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td>
87        <td>
88            <input type="file" name="$field" />
89            <\x3fphp if ('' != \$upload->getFilenameGlob(getFormData('$primary_key_field') . '_*') && getFormData('op') == 'edit' || getFormData('op') == 'update') { \x3f>
90            <div class="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_field') . '_*')) \x3f></div>
91            <\x3fphp } \x3f>
92            <div class="help"><\x3fphp printf(_("File to upload must have one of the following file-name extensions: %s."), join(', ', \$upload->getParam('valid_file_extensions'))) \x3f></div>
93        </td>
94    </tr>
95E_O_F;
96            continue;
97        }
98       
99        // Password field.
100        if (preg_match('/pass/i', $field)) {
101            $output[$field] = <<<E_O_F
102    <tr>
103        <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td>
104        <td>
105            <input type="password" class="medium" size="50" name="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
106        </td>
107    </tr>
108E_O_F;
109            continue;
110        }
111       
112        switch ($type) {
113       
114        // Select menu (or radio buttons)
115        case 'enum' :
116            $output[$field] = <<<E_O_F
117    <tr>
118        <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td>
119        <td>
120            <select name="$field">
121            <\x3fphp printSetSelectForm('$db_tbl', '$field', \$frm['$field'], true); \x3f>
122            </select>
123        </td>
124    </tr>
125E_O_F;
126            break;
127       
128        // Set checkboxes
129        case 'set' :
130            $output[$field] = <<<E_O_F
131    <tr>
132        <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td>
133        <td>
134            <\x3fphp printSetCheckboxes('$db_tbl', '$field', \$frm['$field'], 1) \x3f>
135        </td>
136    </tr>
137E_O_F;
138            break;
139       
140        // Single checkbox
141        case 'toggle' :
142            $output[$field] = <<<E_O_F
143    <tr>
144        <td>&nbsp;</td>
145        <td>
146            <input type="checkbox" name="$field" <\x3fphp frmChecked(!empty(\$frm['$field'])) \x3f> /><label><\x3fphp echo _("$title"); \x3f></label>
147        </td>
148    </tr>
149E_O_F;
150            break;
151           
152        // Textarea
153        case 'tinytext' :
154        case 'text' :
155        case 'mediumtext' :
156        case 'longtext' :
157        case 'tinyblob' :
158        case 'blob' :
159        case 'mediumblob' :
160        case 'longblob' :
161            $output[$field] = <<<E_O_F
162    <tr>
163        <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td>
164        <td>
165            <textarea class="large" cols="75" rows="4" name="$field"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea>
166        </td>
167    </tr>
168E_O_F;
169            break;
170       
171           
172        // Text
173        case 'tinyint' :
174        case 'bit' :
175        case 'bool' :
176        case 'smallint' :
177        case 'mediumint' :
178        case 'int' :
179        case 'integer' :
180        case 'bigint' :
181       
182        case 'float' :
183        case 'float' :
184        case 'double' :
185        case 'double' :
186        case 'real' :
187        case 'decimal' :
188        case 'dec' :
189        case 'numeric' :
190       
191        case 'date' :
192        case 'datetime' :
193        case 'timestamp' :
194        case 'time' :
195        case 'year' :
196       
197        case 'char' :
198        case 'varchar' :
199        default :
200            $output[$field] = <<<E_O_F
201    <tr>
202        <td class="right"><label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label></td>
203        <td>
204            <input type="text" class="medium" size="50" name="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
205        </td>
206    </tr>
207E_O_F;
208            break;
209        }
210    }
211} else {
212    die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n");
213}
214
215
216echo join("\n", $output);
217
218?>
Note: See TracBrowser for help on using the repository browser.