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

Last change on this file since 321 was 319, checked in by quinn, 17 years ago
File size: 8.2 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.
[136]19$qid = $db->query("SHOW TABLES");
[1]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)) {
[136]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)));
[1]27}
28
29// Get DB table column info.
[136]30$qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl));
[1]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');
[154]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
[295]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>
[154]69    <\x3fphp if ('' != \$frm['__///___id']) { \x3f>
[295]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>
[51]71    <\x3fphp } \x3f>
[295]72</div>
[1]73E_O_F;
74            continue;
75        }
[42]76
[1]77        // File upload.
[20]78        if (preg_match('/file|image/i', $field)) {
[1]79            $output[$field] = <<<E_O_F
[51]80
[295]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" />
[51]84    <\x3fphp if ('' != \$upload->getFilenameGlob(getFormData('$primary_key') . '_*') && getFormData('op') == 'edit' || getFormData('op') == 'update') { \x3f>
[295]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>
[51]86    <\x3fphp } \x3f>
[319]87    <div class="sc-help"><\x3fphp printf(_("Allowed file types: %s."), join(', ', \$upload->getParam('valid_file_extensions'))) \x3f></div>
[295]88</div>
[1]89E_O_F;
90            continue;
91        }
[42]92
[1]93        // Password field.
94        if (preg_match('/pass/i', $field)) {
95            $output[$field] = <<<E_O_F
[51]96
[295]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>
[1]101E_O_F;
102            continue;
103        }
[42]104
[1]105        switch ($type) {
[42]106
[1]107        // Select menu (or radio buttons)
108        case 'enum' :
109            $output[$field] = <<<E_O_F
[51]110
[295]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>
[1]115E_O_F;
116            break;
[42]117
[1]118        // Set checkboxes
119        case 'set' :
120            $output[$field] = <<<E_O_F
[51]121
[295]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>
[1]126E_O_F;
127            break;
[42]128
[1]129        // Single checkbox
130        case 'toggle' :
131            $output[$field] = <<<E_O_F
[51]132
[295]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>
[1]137E_O_F;
138            break;
[42]139
[51]140        // Textarea small
[1]141        case 'text' :
[51]142        case 'tinyblob' :
143        case 'blob' :
144            $output[$field] = <<<E_O_F
145
[295]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>
[51]150E_O_F;
151            break;
152
153        // Textarea big
[1]154        case 'mediumtext' :
155        case 'longtext' :
156        case 'mediumblob' :
157        case 'longblob' :
158            $output[$field] = <<<E_O_F
[51]159
[295]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>
[1]164E_O_F;
165            break;
[42]166
[51]167        // Number
[1]168        case 'tinyint' :
169        case 'bit' :
170        case 'bool' :
171        case 'smallint' :
172        case 'mediumint' :
173        case 'int' :
174        case 'integer' :
175        case 'bigint' :
[42]176
[1]177        case 'float' :
178        case 'float' :
179        case 'double' :
180        case 'double' :
181        case 'real' :
182        case 'decimal' :
183        case 'dec' :
184        case 'numeric' :
[51]185        default :
186            $output[$field] = <<<E_O_F
[42]187
[295]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>
[51]192E_O_F;
193            break;
194
195        // Date
[1]196        case 'date' :
197        case 'datetime' :
198        case 'timestamp' :
199        case 'time' :
200        case 'year' :
[51]201        default :
202            $output[$field] = <<<E_O_F
[42]203
[295]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>
[51]208E_O_F;
209            break;
210
211        // Text
[1]212        case 'char' :
213        case 'varchar' :
[51]214        case 'tinytext' :
215        case 'tinyblob' :
[1]216        default :
217            $output[$field] = <<<E_O_F
[51]218
[295]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>
[1]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.