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

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

${1}

File size: 8.0 KB
RevLine 
[1]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
[19]35$exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id');
[44]36$primary_key = '<##>';
[1]37$output = array();
38
39// Loop through columns
40if (is_array($cols) && !empty($cols)) {
41    foreach ($cols as $col) {
[42]42
[1]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]) {
[44]48            $primary_key = $field;
[1]49        }
[42]50
[1]51        // Column types like this are usually single toggle checkboxes.
52        if (preg_match("/enum\('true'\)/", $col[1])) {
53            $type = 'toggle';
54        }
[42]55
[44]56        if (in_array($field, $exclude) || $primary_key == $field) {
[1]57            // Don't add a field for this column.
58            continue;
59        }
[42]60
[1]61        // Select menu from the column of a related database table.
62        if (preg_match('/.*_id$/i', $field)) {
63            $output[$field] = <<<E_O_F
[51]64
65<label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label>
66<select name="$field" id="$field" class="small"><\x3fphp printSelectForm('<##>_tbl', "CONCAT(<##>_id, '&mdash;', <##>)", '$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>
[1]71E_O_F;
72            continue;
73        }
[42]74
[1]75        // File upload.
[20]76        if (preg_match('/file|image/i', $field)) {
[1]77            $output[$field] = <<<E_O_F
[51]78
79<label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \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="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="help"><\x3fphp printf(_("File to upload must have one of the following file-name extensions: %s."), join(', ', \$upload->getParam('valid_file_extensions'))) \x3f></div>
[1]85E_O_F;
86            continue;
87        }
[42]88
[1]89        // Password field.
90        if (preg_match('/pass/i', $field)) {
91            $output[$field] = <<<E_O_F
[51]92
93<label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label>
94<input type="password" class="medium" size="50" name="$field" id="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
[1]95E_O_F;
96            continue;
97        }
[42]98
[1]99        switch ($type) {
[42]100
[1]101        // Select menu (or radio buttons)
102        case 'enum' :
103            $output[$field] = <<<E_O_F
[51]104
105<label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label>
106<select name="$field" id="$field"><\x3fphp printSetSelectForm('$db_tbl', '$field', \$frm['$field'], true); \x3f></select>
[1]107E_O_F;
108            break;
[42]109
[1]110        // Set checkboxes
111        case 'set' :
112            $output[$field] = <<<E_O_F
[51]113
114<label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label>
115<\x3fphp printSetCheckboxes('$db_tbl', '$field', \$frm['$field'], 1) \x3f>
[1]116E_O_F;
117            break;
[42]118
[1]119        // Single checkbox
120        case 'toggle' :
121            $output[$field] = <<<E_O_F
[51]122
123<label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \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>
[1]125E_O_F;
126            break;
[42]127
[51]128        // Textarea small
[1]129        case 'text' :
[51]130        case 'tinyblob' :
131        case 'blob' :
132            $output[$field] = <<<E_O_F
133
134<label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label>
135<textarea class="large short" cols="75" rows="4" name="$field" id="$field"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea>
136E_O_F;
137            break;
138
139        // Textarea big
[1]140        case 'mediumtext' :
141        case 'longtext' :
142        case 'mediumblob' :
143        case 'longblob' :
144            $output[$field] = <<<E_O_F
[51]145
146<label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label>
147<textarea class="large tall" cols="75" rows="4" name="$field" id="$field"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea>
[1]148E_O_F;
149            break;
[42]150
[51]151        // Number
[1]152        case 'tinyint' :
153        case 'bit' :
154        case 'bool' :
155        case 'smallint' :
156        case 'mediumint' :
157        case 'int' :
158        case 'integer' :
159        case 'bigint' :
[42]160
[1]161        case 'float' :
162        case 'float' :
163        case 'double' :
164        case 'double' :
165        case 'real' :
166        case 'decimal' :
167        case 'dec' :
168        case 'numeric' :
[51]169        default :
170            $output[$field] = <<<E_O_F
[42]171
[51]172<label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label>
173<input type="text" class="tiny" size="12" name="$field" id="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
174E_O_F;
175            break;
176
177        // Date
[1]178        case 'date' :
179        case 'datetime' :
180        case 'timestamp' :
181        case 'time' :
182        case 'year' :
[51]183        default :
184            $output[$field] = <<<E_O_F
[42]185
[51]186<label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label>
187<input type="text" class="medium" size="25" name="$field" id="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
188E_O_F;
189            break;
190
191        // Text
[1]192        case 'char' :
193        case 'varchar' :
[51]194        case 'tinytext' :
195        case 'tinyblob' :
[1]196        default :
197            $output[$field] = <<<E_O_F
[51]198
199<label for="$field"<\x3fphp \$fv->err('$field', ' class="error"') \x3f>><\x3fphp echo _("$title"); \x3f></label>
200<input type="text" class="medium" size="50" name="$field" id="$field" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
[1]201E_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
210echo join("\n", $output);
211
212?>
Note: See TracBrowser for help on using the repository browser.