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

Last change on this file since 718 was 718, checked in by anonymous, 4 years ago

Minor fixes

File size: 9.0 KB
Line 
1#!/usr/bin/env php
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-2012 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
28if ($_SERVER['argc'] > 1 && isset($_SERVER['argv'][1]) && '' != $_SERVER['argv'][1] && is_dir($_SERVER['argv'][1])) {
29    // First arg is path to current site. Realpath removes trailing /s
30    define('COMMON_BASE', realpath($_SERVER['argv'][1]));
31} else {
32    die("Error: First argument must be the directory path to an existing site (ex: /home/sc/www.strangecode.com).\n");
33}
34
35include_once dirname(__FILE__) . '/../_config.inc.php';
36
37// Test arguments.
38if (isset($_SERVER['argv'][2])) {
39    // Second arg is db table.
40    $db_tbl = $_SERVER['argv'][2];
41} else {
42    die(sprintf("Usage: %s site_directory db_table\n", basename($_SERVER['argv'][0])));
43}
44
45// Get DB tables.
46$qid = $db->query("SHOW TABLES");
47while (list($row) = mysql_fetch_row($qid)) {
48    $tables[] = $row;
49}
50
51// Make sure requested table is in database.
52if (!in_array($db_tbl, $tables)) {
53    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)));
54}
55
56// Get DB table column info.
57$qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl));
58while ($row = mysql_fetch_row($qid)) {
59    $cols[] = $row;
60}
61
62$exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id');
63$primary_key = '__///__';
64$output = array();
65
66// Loop through columns
67if (is_array($cols) && !empty($cols)) {
68    foreach ($cols as $col) {
69
70        // Human readable.
71        $field = $col[0];
72        $title = ucfirst(str_replace('_', ' ', $field));
73        $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]);
74        if ('PRI' == $col[3]) {
75            $primary_key = $field;
76        }
77
78        // Column types like this are usually single toggle checkboxes.
79        if (preg_match("/enum\('true'\)/", $col[1])) {
80            $type = 'toggle';
81        }
82
83        if (in_array($field, $exclude) || $primary_key == $field) {
84            // Don't add a field for this column.
85            continue;
86        }
87
88        // Select menu from the column of a related database table.
89        if (preg_match('/.*_id$/i', $field)) {
90            $output[$field] = <<<E_O_F
91
92<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
93    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
94    <select name="$field" id="$field" class="sc-small"><\x3fphp printSelectForm('__///___tbl', "CONCAT(__///___id, '&mdash;', __///__)", '$field', \$frm['$field'], true, 'ORDER BY $field ASC'); \x3f></select>
95    <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>
96    <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>
97</div>
98E_O_F;
99            continue;
100        }
101
102        // File upload.
103        if (preg_match('/file|image/i', $field)) {
104            $output[$field] = <<<E_O_F
105
106<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
107    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
108    <input type="file" name="$field" id="$field" />
109    <\x3fphp if ('' != \$upload->getFilenameGlob(getFormData('$primary_key') . '_*') && (getFormData('op') == 'edit' || getFormData('op') == 'update')) { \x3f>
110        <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>
111    <\x3fphp } \x3f>
112    <div class="sc-help"><\x3fphp printf(_("Allowed file types: %s."), join(', ', \$upload->getParam('valid_file_extensions'))) \x3f></div>
113</div>
114E_O_F;
115            continue;
116        }
117
118        // Password field.
119        if (preg_match('/pass/i', $field)) {
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    <input type="password" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
125</div>
126E_O_F;
127            continue;
128        }
129
130        switch ($type) {
131
132        // Select menu (or radio buttons)
133        case 'enum' :
134            $output[$field] = <<<E_O_F
135
136<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
137    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
138    <select name="$field" id="$field"><\x3fphp printSetSelectForm('$db_tbl', '$field', \$frm['$field'], true); \x3f></select>
139</div>
140E_O_F;
141            break;
142
143        // Set checkboxes
144        case 'set' :
145            $output[$field] = <<<E_O_F
146
147<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
148    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
149    <\x3fphp printSetCheckboxes('$db_tbl', '$field', \$frm['$field'], 1) \x3f>
150</div>
151E_O_F;
152            break;
153
154        // Single checkbox
155        case 'toggle' :
156            $output[$field] = <<<E_O_F
157
158<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
159    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
160    <label><input type="checkbox" name="$field" id="$field" <\x3fphp frmChecked(!empty(\$frm['$field'])) \x3f> /><\x3fphp echo _("Check this box to $title"); \x3f></label>
161</div>
162E_O_F;
163            break;
164
165        // Textarea small
166        case 'tinytiny' :
167        case 'text' :
168        case 'tinyblob' :
169        case 'blob' :
170            $output[$field] = <<<E_O_F
171
172<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
173    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
174    <textarea name="$field" id="$field" rows="8" cols="40" class="sc-tall sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea>
175</div>
176E_O_F;
177            break;
178
179        // Textarea big
180        case 'mediumtext' :
181        case 'longtext' :
182        case 'mediumblob' :
183        case 'longblob' :
184            $output[$field] = <<<E_O_F
185
186<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
187    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
188    <textarea name="$field" id="$field" rows="8" cols="40" class="sc-fullscreen sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea>
189</div>
190E_O_F;
191            break;
192
193        // Number
194        case 'tinyint' :
195        case 'bit' :
196        case 'bool' :
197        case 'smallint' :
198        case 'mediumint' :
199        case 'int' :
200        case 'integer' :
201        case 'bigint' :
202
203        case 'float' :
204        case 'float' :
205        case 'double' :
206        case 'double' :
207        case 'real' :
208        case 'decimal' :
209        case 'dec' :
210        case 'numeric' :
211            $output[$field] = <<<E_O_F
212
213<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
214    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
215    <input type="text" name="$field" id="$field" class="sc-tiny" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
216</div>
217E_O_F;
218            break;
219
220        // Date
221        case 'date' :
222        case 'datetime' :
223        case 'timestamp' :
224        case 'time' :
225        case 'year' :
226            $output[$field] = <<<E_O_F
227
228<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
229    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
230    <input type="text" name="$field" id="$field" class="sc-small" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
231</div>
232E_O_F;
233            break;
234
235        // Text
236        case 'char' :
237        case 'varchar' :
238        case 'tinytext' :
239        case 'tinyblob' :
240        default :
241            $output[$field] = <<<E_O_F
242
243<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
244    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
245    <input type="text" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
246</div>
247E_O_F;
248            break;
249        }
250    }
251} else {
252    die(basename($_SERVER['argv'][0]) . " Warning: $db_tbl does not have any columns.\n");
253}
254
255
256echo join("\n", $output);
Note: See TracBrowser for help on using the repository browser.