source: tags/2.1.5/bin/module_maker/form_template.cli.php @ 377

Last change on this file since 377 was 377, checked in by quinn, 14 years ago

Releasing trunk as stable version 2.1.5

File size: 8.6 KB
Line 
1#!/usr/local/bin/php -q
2<?php
3/**
4 * The Strangecode Codebase - a general application development framework for PHP
5 * For details visit the project site: <http://trac.strangecode.com/codebase/>
6 * Copyright 2001-2010 Strangecode, LLC
7 *
8 * This file is part of The Strangecode Codebase.
9 *
10 * The Strangecode Codebase is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published by the
12 * Free Software Foundation, either version 3 of the License, or (at your option)
13 * any later version.
14 *
15 * The Strangecode Codebase is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/**
25 * form_template.cli.php
26 */
27
28include_once dirname(__FILE__) . '/_config.inc.php';
29
30// Test arguments.
31if (isset($_SERVER['argv'][2])) {
32    // Second arg is db table.
33    $db_tbl = $_SERVER['argv'][2];
34} else {
35    die(sprintf("Usage: %s site_directory db_table\n", basename($_SERVER['argv'][0])));
36}
37
38// Get DB tables.
39$qid = $db->query("SHOW TABLES");
40while (list($row) = mysql_fetch_row($qid)) {
41    $tables[] = $row;
42}
43
44// Make sure requested table is in database.
45if (!in_array($db_tbl, $tables)) {
46    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)));
47}
48
49// Get DB table column info.
50$qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl));
51while ($row = mysql_fetch_row($qid)) {
52    $cols[] = $row;
53}
54
55$exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id');
56$primary_key = '__///__';
57$output = array();
58
59// Loop through columns
60if (is_array($cols) && !empty($cols)) {
61    foreach ($cols as $col) {
62
63        // Human readable.
64        $field = $col[0];
65        $title = ucfirst(str_replace('_', ' ', $field));
66        $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]);
67        if ('PRI' == $col[3]) {
68            $primary_key = $field;
69        }
70
71        // Column types like this are usually single toggle checkboxes.
72        if (preg_match("/enum\('true'\)/", $col[1])) {
73            $type = 'toggle';
74        }
75
76        if (in_array($field, $exclude) || $primary_key == $field) {
77            // Don't add a field for this column.
78            continue;
79        }
80
81        // Select menu from the column of a related database table.
82        if (preg_match('/.*_id$/i', $field)) {
83            $output[$field] = <<<E_O_F
84           
85<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
86    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
87    <select name="$field" id="$field" class="sc-small"><\x3fphp printSelectForm('__///___tbl', "CONCAT(__///___id, '&mdash;', __///__)", '$field', \$frm['$field'], true, 'ORDER BY $field ASC'); \x3f></select>
88    <a href="<\x3fphp echo \$app->oHREF('/admin/__///__.php?op=add&boomerang=true'); \x3f>" class="sc-warn sc-add-link"><img src="/admin/i/plus.gif" width="10" height="10" alt="[+]" /></a>
89    <a href="<\x3fphp echo \$app->oHREF('/admin/__///__.php?op=edit&boomerang=true&__///___id=' . \$frm['__///___id']); \x3f>" class="sc-warn sc-edit-link"><img src="/admin/i/pen.gif" width="12" height="12" alt="Edit" /></a>
90</div>
91E_O_F;
92            continue;
93        }
94
95        // File upload.
96        if (preg_match('/file|image/i', $field)) {
97            $output[$field] = <<<E_O_F
98
99<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
100    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
101    <input type="file" name="$field" id="$field" />
102    <\x3fphp if ('' != \$upload->getFilenameGlob(getFormData('$primary_key') . '_*') && getFormData('op') == 'edit' || getFormData('op') == 'update') { \x3f>
103        <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>
104    <\x3fphp } \x3f>
105    <div class="sc-help"><\x3fphp printf(_("Allowed file types: %s."), join(', ', \$upload->getParam('valid_file_extensions'))) \x3f></div>
106</div>
107E_O_F;
108            continue;
109        }
110
111        // Password field.
112        if (preg_match('/pass/i', $field)) {
113            $output[$field] = <<<E_O_F
114
115<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
116    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
117    <input type="password" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
118</div>
119E_O_F;
120            continue;
121        }
122
123        switch ($type) {
124
125        // Select menu (or radio buttons)
126        case 'enum' :
127            $output[$field] = <<<E_O_F
128
129<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
130    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
131    <select name="$field" id="$field"><\x3fphp printSetSelectForm('$db_tbl', '$field', \$frm['$field'], true); \x3f></select>
132</div>
133E_O_F;
134            break;
135
136        // Set checkboxes
137        case 'set' :
138            $output[$field] = <<<E_O_F
139
140<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
141    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
142    <\x3fphp printSetCheckboxes('$db_tbl', '$field', \$frm['$field'], 1) \x3f>
143</div>
144E_O_F;
145            break;
146
147        // Single checkbox
148        case 'toggle' :
149            $output[$field] = <<<E_O_F
150
151<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
152    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
153    <label><input type="checkbox" name="$field" id="$field" <\x3fphp frmChecked(!empty(\$frm['$field'])) \x3f> /><\x3fphp echo _("Check this box to $title"); \x3f></label>
154</div>
155E_O_F;
156            break;
157
158        // Textarea small
159        case 'text' :
160        case 'tinyblob' :
161        case 'blob' :
162            $output[$field] = <<<E_O_F
163
164<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
165    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
166    <textarea name="$field" id="$field" rows="8" cols="40" class="sc-short sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea>
167</div>
168E_O_F;
169            break;
170
171        // Textarea big
172        case 'mediumtext' :
173        case 'longtext' :
174        case 'mediumblob' :
175        case 'longblob' :
176            $output[$field] = <<<E_O_F
177
178<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
179    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
180    <textarea name="$field" id="$field" rows="8" cols="40" class="sc-short sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea>
181</div>
182E_O_F;
183            break;
184
185        // Number
186        case 'tinyint' :
187        case 'bit' :
188        case 'bool' :
189        case 'smallint' :
190        case 'mediumint' :
191        case 'int' :
192        case 'integer' :
193        case 'bigint' :
194
195        case 'float' :
196        case 'float' :
197        case 'double' :
198        case 'double' :
199        case 'real' :
200        case 'decimal' :
201        case 'dec' :
202        case 'numeric' :
203        default :
204            $output[$field] = <<<E_O_F
205
206<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
207    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
208    <input type="text" name="$field" id="$field" class="sc-tiny" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
209</div>
210E_O_F;
211            break;
212
213        // Date
214        case 'date' :
215        case 'datetime' :
216        case 'timestamp' :
217        case 'time' :
218        case 'year' :
219        default :
220            $output[$field] = <<<E_O_F
221
222<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
223    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
224    <input type="text" name="$field" id="$field" class="sc-small" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
225</div>
226E_O_F;
227            break;
228
229        // Text
230        case 'char' :
231        case 'varchar' :
232        case 'tinytext' :
233        case 'tinyblob' :
234        default :
235            $output[$field] = <<<E_O_F
236
237<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
238    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
239    <input type="text" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
240</div>
241E_O_F;
242            break;
243        }
244    }
245} else {
246    die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n");
247}
248
249
250echo join("\n", $output);
251
252?>
Note: See TracBrowser for help on using the repository browser.