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

Last change on this file since 151 was 151, checked in by scdev, 18 years ago

Q - Changed one more SessionCache? -> Cache, small bug fixt to Prefs, added operation help to modulemaker scripts.

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