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

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

Minor fixes

File size: 9.0 KB
RevLine 
[612]1#!/usr/bin/env php
[1]2<?php
3/**
[362]4 * The Strangecode Codebase - a general application development framework for PHP
5 * For details visit the project site: <http://trac.strangecode.com/codebase/>
[396]6 * Copyright 2001-2012 Strangecode, LLC
[485]7 *
[362]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.
[485]14 *
[362]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.
[485]19 *
[362]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/**
[1]25 * form_template.cli.php
26 */
27
[533]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}
[1]34
[533]35include_once dirname(__FILE__) . '/../_config.inc.php';
36
[1]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.
[136]46$qid = $db->query("SHOW TABLES");
[1]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)) {
[136]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)));
[1]54}
55
56// Get DB table column info.
[136]57$qid = $db->query("DESCRIBE " . $db->escapeString($db_tbl));
[1]58while ($row = mysql_fetch_row($qid)) {
59    $cols[] = $row;
60}
61
[19]62$exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id');
[154]63$primary_key = '__///__';
[1]64$output = array();
65
66// Loop through columns
67if (is_array($cols) && !empty($cols)) {
68    foreach ($cols as $col) {
[42]69
[1]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]) {
[44]75            $primary_key = $field;
[1]76        }
[42]77
[1]78        // Column types like this are usually single toggle checkboxes.
79        if (preg_match("/enum\('true'\)/", $col[1])) {
80            $type = 'toggle';
81        }
[42]82
[44]83        if (in_array($field, $exclude) || $primary_key == $field) {
[1]84            // Don't add a field for this column.
85            continue;
86        }
[42]87
[1]88        // Select menu from the column of a related database table.
89        if (preg_match('/.*_id$/i', $field)) {
90            $output[$field] = <<<E_O_F
[485]91
[295]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>
[324]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>
[295]97</div>
[1]98E_O_F;
99            continue;
100        }
[42]101
[1]102        // File upload.
[20]103        if (preg_match('/file|image/i', $field)) {
[1]104            $output[$field] = <<<E_O_F
[51]105
[295]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" />
[523]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>
[51]111    <\x3fphp } \x3f>
[319]112    <div class="sc-help"><\x3fphp printf(_("Allowed file types: %s."), join(', ', \$upload->getParam('valid_file_extensions'))) \x3f></div>
[295]113</div>
[1]114E_O_F;
115            continue;
116        }
[42]117
[1]118        // Password field.
119        if (preg_match('/pass/i', $field)) {
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    <input type="password" name="$field" id="$field" class="sc-medium" value="<\x3fphp echo oTxt(\$frm['$field']); \x3f>" />
125</div>
[1]126E_O_F;
127            continue;
128        }
[42]129
[1]130        switch ($type) {
[42]131
[1]132        // Select menu (or radio buttons)
133        case 'enum' :
134            $output[$field] = <<<E_O_F
[51]135
[295]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>
[1]140E_O_F;
141            break;
[42]142
[1]143        // Set checkboxes
144        case 'set' :
145            $output[$field] = <<<E_O_F
[51]146
[295]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>
[1]151E_O_F;
152            break;
[42]153
[1]154        // Single checkbox
155        case 'toggle' :
156            $output[$field] = <<<E_O_F
[51]157
[295]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>
[1]162E_O_F;
163            break;
[42]164
[51]165        // Textarea small
[485]166        case 'tinytiny' :
[1]167        case 'text' :
[51]168        case 'tinyblob' :
169        case 'blob' :
170            $output[$field] = <<<E_O_F
171
[295]172<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
173    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
[485]174    <textarea name="$field" id="$field" rows="8" cols="40" class="sc-tall sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea>
[295]175</div>
[51]176E_O_F;
177            break;
178
179        // Textarea big
[1]180        case 'mediumtext' :
181        case 'longtext' :
182        case 'mediumblob' :
183        case 'longblob' :
184            $output[$field] = <<<E_O_F
[51]185
[295]186<div class="sc-form-row<\x3fphp \$fv->err('$field'); \x3f>">
187    <label for="$field"><\x3fphp echo _("$title"); \x3f></label>
[485]188    <textarea name="$field" id="$field" rows="8" cols="40" class="sc-fullscreen sc-medium"><\x3fphp echo oTxt(\$frm['$field']); \x3f></textarea>
[295]189</div>
[1]190E_O_F;
191            break;
[42]192
[51]193        // Number
[1]194        case 'tinyint' :
195        case 'bit' :
196        case 'bool' :
197        case 'smallint' :
198        case 'mediumint' :
199        case 'int' :
200        case 'integer' :
201        case 'bigint' :
[42]202
[1]203        case 'float' :
204        case 'float' :
205        case 'double' :
206        case 'double' :
207        case 'real' :
208        case 'decimal' :
209        case 'dec' :
210        case 'numeric' :
[51]211            $output[$field] = <<<E_O_F
[42]212
[295]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>
[51]217E_O_F;
218            break;
219
220        // Date
[1]221        case 'date' :
222        case 'datetime' :
223        case 'timestamp' :
224        case 'time' :
225        case 'year' :
[51]226            $output[$field] = <<<E_O_F
[42]227
[295]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>
[51]232E_O_F;
233            break;
234
235        // Text
[1]236        case 'char' :
237        case 'varchar' :
[51]238        case 'tinytext' :
239        case 'tinyblob' :
[1]240        default :
241            $output[$field] = <<<E_O_F
[51]242
[295]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>
[1]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.