Handle hostnames with upper-case letters
[webmin.git] / mysql / edit_field.cgi
1 #!/usr/local/bin/perl
2 # edit_field.cgi
3 # Display a form for editing an existing field or creating a new one
4
5 require './mysql-lib.pl';
6 &ReadParse();
7 &can_edit_db($in{'db'}) || &error($text{'dbase_ecannot'});
8 $access{'edonly'} && &error($text{'dbase_ecannot'});
9 $desc = &text('field_in', "<tt>$in{'table'}</tt>", "<tt>$in{'db'}</tt>");
10 if ($in{'type'}) {
11         # Creating a new field
12         &ui_print_header($desc, $text{'field_title1'}, "", "create_field");
13         $type = $in{'type'};
14         }
15 else {
16         # Editing an existing field
17         &ui_print_header($desc, $text{'field_title2'}, "", "edit_field");
18         @desc = &table_structure($in{'db'}, $in{'table'});
19         $f = $desc[$in{'idx'}];
20         $type = $f->{'type'};
21         }
22
23 # Start of form
24 print &ui_form_start("save_field.cgi");
25 print &ui_hidden("db", $in{'db'});
26 print &ui_hidden("table", $in{'table'});
27 print &ui_hidden("new", $in{'type'});
28 print &ui_table_start($text{'field_header'}, undef, 2);
29
30 # Field name
31 print &ui_table_row($text{'field_name'},
32         &ui_textbox("field", $f->{'field'}, 20));
33 print &ui_hidden("old", $f->{'field'}) if (!$in{'type'});
34
35 # Type
36 if ($type =~ /^(\S+)\((.*)\)(.*)/) {
37         $type = $1;
38         $size = $2;
39         $extra = $3;
40         }
41 print &ui_hidden("type", $type);
42 if ($in{'type'}) {
43         # New field .. just show chosen type
44         $tsel = $type;
45         }
46 else {
47         # Existing field .. allow type change
48         $tsel = &ui_select("newtype", $type, \@type_list)." ".
49                 $text{'field_typewarn'};
50         }
51 print &ui_table_row($text{'field_type'}, $tsel);
52
53 if ($type eq 'enum' || $type eq 'set') {
54         # List of values
55         local @ev = &split_enum($size);
56         print &ui_table_row($text{'field_enum'},
57                 &ui_textarea("size", join("\n", @ev), 4, 20));
58         }
59 elsif ($type eq 'float' || $type eq 'double' || $type eq 'decimal') {
60         # Two values, for total and decimal
61         print &ui_table_row($text{'field_dual'},
62                 &ui_textbox("size1", $size =~ /^(\d+)/ ? $1 : '', 5)."\n".
63                 &ui_textbox("size2", $size =~ /(\d+)$/ ? $1 : '', 5));
64         }
65 elsif ($type eq 'date' || $type eq 'datetime' || $type eq 'time' ||
66        $type eq 'timestamp' || $type =~ /(blob|text)$/) {
67         # No width!
68         }
69 elsif ($type ne 'varchar' && $type ne 'char' && $in{'type'}) {
70         # Size is optional for new fields of most types
71         print &ui_table_row($text{'field_size'},
72                 &ui_opt_textbox("size", undef, 10, $text{'default'}));
73         }
74 else {
75         # Size is one value
76         print &ui_table_row($text{'field_size'},
77                 &ui_textbox("size", $size, 10));
78         }
79
80 if ($type =~ /int$/) {
81         # Display unsigned/zerofill option
82         print &ui_table_row($text{'field_opts'},
83                 &ui_radio("opts", $extra =~ /unsigned/ ? 'unsigned' :
84                                   $extra =~ /zerofill/ ? 'zerofill' : '',
85                           [ [ '', $text{'field_none'} ],
86                             [ 'unsigned', $text{'field_unsigned'} ],
87                             [ 'zerofill', $text{'field_zerofill'} ] ]));
88
89         # Display auto-increment option
90         print &ui_table_row($text{'field_auto'},
91                 &ui_radio("ext", $f->{'extra'} =~ /auto_increment/ ?
92                                         'auto_increment' : '',
93                           [ [ 'auto_increment', $text{'yes'} ],
94                             [ '', $text{'no'} ] ]));
95         }
96 elsif ($type eq 'float' || $type eq 'double' || $type eq 'decimal') {
97         # Display zerofill option
98         print &ui_table_row($text{'field_opts'},
99                 &ui_radio("opts", $extra =~ /zerofill/ ? 'zerofill' : '',
100                           [ [ '', $text{'field_none'} ],
101                             [ 'zerofill', $text{'field_zerofill'} ] ]));
102         }
103 elsif ($type eq 'char' || $type eq 'varchar') {
104         # Display binary option
105         print &ui_table_row($text{'field_opts'},
106                 &ui_radio("opts", $extra =~ /binary/ ? 'binary' : '',
107                           [ [ '', $text{'field_ascii'} ],
108                             [ 'binary', $text{'field_binary'} ] ]));
109         }
110
111 # Allow nulls?
112 print &ui_table_row($text{'field_null'},
113         &ui_radio("null", $in{'type'} || $f->{'null'} eq 'YES' ? 1 : 0,
114                   [ [ 1, $text{'yes'} ], [ 0, $text{'no'} ] ]));
115
116 # Default value
117 $defmode = $f->{'default'} eq 'NULL' || !defined($f->{'default'}) ? 0 :
118            $f->{'default'} eq 'CURRENT_TIMESTAMP' ? 2 :
119            $f->{'default'} eq '' ? 3 : 1;
120 @defs = ( [ 0, 'NULL' ] );
121 if ($in{'type'}) {
122         # Let MySQL decide
123         push(@defs, [ 3, $text{'field_defdef'} ]);
124         }
125 elsif ($type eq 'char' || $type eq 'varchar') {
126         # Empty string
127         push(@defs, [ 3, $text{'field_defempty'} ]);
128         }
129 if ($type eq "timestamp") {
130         push(@defs, [ 2, $text{'field_current'} ]);
131         }
132 push(@defs, [ 1, $text{'field_defval'}." ".
133          &ui_textbox("default", $defmode == 1 ? $f->{'default'} : "", 40) ]);
134 print &ui_table_row($text{'field_default'},
135         &ui_radio("default_def", $defmode, \@defs));
136
137 # Part of primary key
138 print &ui_table_row($text{'field_key'},
139         &ui_yesno_radio("key", $f->{'key'} eq 'PRI' ? 1 : 0));
140 print &ui_hidden("oldkey", $f->{'key'} eq 'PRI' ? 1 : 0);
141
142 print &ui_table_end();
143 if ($in{'type'}) {
144         print &ui_form_end([ [ undef, $text{'create'} ] ]);
145         }
146 else {
147         print &ui_form_end([ [ undef, $text{'save'} ],
148                     @desc > 1 ? ( [ 'delete', $text{'delete'} ] ): ( ) ]);
149         }
150
151 &ui_print_footer("edit_table.cgi?db=$in{'db'}&table=".&urlize($in{'table'}),
152                  $text{'table_return'},
153                  "edit_dbase.cgi?db=$in{'db'}", $text{'dbase_return'},
154                  "", $text{'index_return'});
155