source: trunk/bin/module_maker/validation.cli.php @ 533

Last change on this file since 533 was 533, checked in by anonymous, 9 years ago

Adapted module maker scripts to use the new cli config file. Updated config to load codebase classes from its own codebase dir.

File size: 9.3 KB
Line 
1#!/usr/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-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 * validation.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 these fields.
63$exclude = array('added_by_user_id', 'added_datetime', 'hit_count', 'modified_datetime', 'modified_by_user_id');
64
65// Loop through columns
66if (is_array($cols) && !empty($cols)) {
67
68    $o = array();
69
70    foreach ($cols as $col) {
71
72        // Human readable.
73        $field = $col[0];
74        $title = ucfirst(str_replace('_', ' ', $field));
75        $type = preg_replace('/^(\w+).*$/', '\\1', $col[1]);
76        $is_primary_key = ('PRI' == $col[3]);
77        $unsigned = preg_match('/\s+unsigned\s*$/i', $col[1]);
78
79        if (in_array($field, $exclude)) {
80            continue;
81        }
82
83        // ----------- isEmpty ------------
84        $o[] = "\$fv->notEmpty('$field', sprintf(_(\"%s cannot be blank.\"), _(\"$title\")));";
85
86        // ----------- stringLength ------------
87        $max_length = null;
88        $min = null;
89        $max = null;
90        $len_type = null;
91        switch ($type) {
92        case 'enum' :
93        case 'set' :
94            $max_length = 255;
95            $len_type = 'setenum';
96            break;
97
98        case 'date' :
99        case 'datetime' :
100        case 'timestamp' :
101        case 'time' :
102        case 'year' :
103            $len_type = 'string';
104            $max_length = 127;
105            break;
106
107        case 'tinytext' :
108        case 'tinyblob' :
109        case 'char' :
110        case 'varchar' :
111            $len_type = 'string';
112            $max_length = 255;
113            break;
114
115        case 'text' :
116        case 'blob' :
117            $len_type = 'string';
118            $max_length = 65535;
119            break;
120
121        case 'mediumtext' :
122        case 'mediumblob' :
123            $len_type = 'string';
124            $max_length = 16777215;
125            break;
126
127        case 'longtext' :
128        case 'longblob' :
129            $len_type = 'string';
130            $max_length = 4294967295;
131            break;
132
133        case 'tinyint' :
134        case 'bit' :
135        case 'bool' :
136            $len_type = 'num';
137            if ($unsigned) {
138                $min = 0;
139                $max = 255;
140            } else {
141                $min = -128;
142                $max = 127;
143            }
144            break;
145
146        case 'smallint' :
147            $len_type = 'num';
148            if ($unsigned) {
149                $min = 0;
150                $max = 65536;
151            } else {
152                $min = -32768;
153                $max = 32767;
154            }
155            break;
156
157        case 'mediumint' :
158            $len_type = 'num';
159            if ($unsigned) {
160                $min = 0;
161                $max = 16777215;
162            } else {
163                $min = -8388608;
164                $max = 8388607;
165            }
166            break;
167
168        case 'int' :
169        case 'integer' :
170            $len_type = 'num';
171            if ($unsigned) {
172                $min = 0;
173                $max = 4294967295;
174            } else {
175                $min = -2147483648;
176                $max = 2147483647;
177            }
178            break;
179
180        case 'bigint' :
181            $len_type = 'num';
182            if ($unsigned) {
183                $min = 0;
184                $max = 1.84467E+19;
185            } else {
186                $min = -9.22337E+18;
187                $max = 9.22337E+18;
188            }
189            break;
190
191        case 'float' :
192            $len_type = 'num';
193            $min = -3.40282E+38;
194            $max = 3.40282E+38;
195            break;
196
197        case 'double' :
198        case 'double precision' :
199        case 'real' :
200        case 'decimal' :
201        case 'dec' :
202        case 'numeric' :
203            $len_type = 'num';
204            $min = -1.79769E+308;
205            $max = 1.79769E+308;
206            break;
207
208        default :
209            $len_type = null;
210            break;
211        }
212        if ($max_length > 0 && $len_type == 'setenum') {
213            $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s has an invalid selection.\"), _(\"$title\")));";
214        }
215        if ($max_length > 0 && $len_type == 'string') {
216            $o[] = "\$fv->stringLength('$field', 0, $max_length, sprintf(_(\"%s must be %d-to-%d characters in length.\"), _(\"$title\"), 0, $max_length));";
217        }
218        if ($len_type == 'num') {
219            $o[] = "\$fv->numericRange('$field', $min, $max, sprintf(_(\"%s must be a number between %d and %d.\"), _(\"$title\"), $min, $max));";
220        }
221
222        // ----------- type check ------------
223        switch ($type) {
224
225        case 'enum' :
226
227        case 'set' :
228            break;
229
230        case 'char' :
231        case 'varchar' :
232
233        case 'tinytext' :
234        case 'text' :
235        case 'mediumtext' :
236        case 'longtext' :
237        case 'tinyblob' :
238        case 'blob' :
239        case 'mediumblob' :
240        case 'longblob' :
241//             $o[] = "\$fv->isString('$field', _(\"<strong>$title</strong> must be a string.\"));"; // Pretty useless
242            break;
243
244        case 'tinyint' :
245        case 'bit' :
246        case 'bool' :
247        case 'smallint' :
248        case 'mediumint' :
249        case 'int' :
250        case 'integer' :
251        case 'bigint' :
252            $negative_ok = $unsigned ? '' : ', true';
253            $o[] = "\$fv->isInteger('$field', sprintf(_(\"%s must be an integer.\"), _(\"$title\"))$negative_ok);";
254            break;
255
256        case 'float' :
257        case 'float' :
258        case 'double' :
259        case 'double' :
260        case 'real' :
261        case 'decimal' :
262        case 'dec' :
263        case 'numeric' :
264            $negative_ok = $unsigned ? '' : ', true';
265            $o[] = "\$fv->isFloat('$field', sprintf(_(\"%s must be a valid number.\"), _(\"$title\"))$negative_ok);";
266            break;
267
268        case 'date' :
269        case 'datetime' :
270            // \$fv->validateStrDate('$field', sprintf(_(\"%s must be a valid date in YYYY-MM-DD format.\"), _(\"$title\")));
271            $o[] = "\$fv->validateStrDate('$field', sprintf(_(\"%s must be a valid date in YYYY-MM-DD format.\"), _(\"$title\")));";
272            $o[] = "\$fv->checkRegex('$field', '/^\d{4}-\d{2}-\d{2}$/', true, sprintf(_(\"%s must be in YYYY-MM-DD format.\"), _(\"$title\")));";
273            break;
274
275        case 'timestamp' :
276            $o[] = "\$fv->checkRegex('$field', '/^\d{14}$/', true, sprintf(_(\"%s must be a valid mysql timestamp in YYYYMMDDhhmmss format.\"), _(\"$title\")));";
277            break;
278
279        case 'time' :
280            $o[] = "\$fv->checkRegex('$field', '/^\d{1,3}:\d{2}:\d{2}$/', true, sprintf(_(\"%s must be a valid time in hh:mm:ss format.\"), _(\"$title\")));";
281            break;
282
283        case 'year' :
284            $o[] = "\$fv->checkRegex('$field', '/^\d{4}$/', true, sprintf(_(\"%s must be a valid year in YYYY format.\"), _(\"$title\")));";
285            break;
286
287        default :
288            break;
289        }
290
291        if (preg_match('/email/', $field)) {
292            $o[] = "\$fv->validateEmail('$field');";
293        } else if (preg_match('/phone/', $field)) {
294            $o[] = "\$fv->validatePhone('$field');";
295        }
296
297        // Blank between cols?
298        $o[] = '';
299    }
300} else {
301    die(sprintf("%s Warning: %s does not have any columns.\n", basename($_SERVER['argv'][0]), $db_tbl));
302}
303
304// Sort?
305// natsort($o);
306
307// Remove last empty element.
308if ('' == end($o)) {
309    array_pop($o);
310}
311
312echo "function validateInput()
313{
314    global \$fv;
315
316    " . join("\n    ", $o) . "
317}";
Note: See TracBrowser for help on using the repository browser.