RT# 82092 - added field validation
[freeside.git] / httemplate / edit / process / part_virtual_field.html
1 <% include( 'elements/process.html',
2     'table'          => 'part_virtual_field',
3     'viewall_dir'    => 'browse',
4     'precheck_callback' => sub {
5         my ($cgi) = @_;
6         $act = 'edit' if $cgi->param('vfieldpart');
7         my @permitted_tables = qw( svc_broadband router cust_main );
8         return 'Table not in list of permitted tables' 
9             unless $cgi->param('dbtable') 
10                 && grep { $_ eq $cgi->param('dbtable') } @permitted_tables;
11         '';
12     },
13     'noerror_callback' => sub {
14         my ($cgi, $object) = @_;
15         if ( $act eq 'add' ) {
16             use FS::Schema qw( reload_dbdef );
17             warn "cf add started schema modification, time=".time;
18             my $dbh = dbh;
19             my $sql = "ALTER TABLE ".$object->dbtable." ADD COLUMN cf_".$object->name." varchar(".$object->length.")";
20             $dbh->do($sql) or die $dbh->errstr;
21             $sql = "ALTER TABLE h_".$object->dbtable." ADD COLUMN cf_".$object->name." varchar(".$object->length.")";
22             $dbh->do($sql) or die $dbh->errstr;
23
24             # apparently nothing happens w/o commit here - but is this OK?
25             $dbh->commit or die $dbh->errstr; 
26
27             # reload schema
28             my $dbdef_file = "/usr/local/etc/freeside/dbdef.".datasrc # XXX: fix this
29             my $dbdef = new_native DBIx::DBSchema $dbh;
30             $dbdef->save($dbdef_file);
31             delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
32             reload_dbdef($dbdef_file);
33             
34             warn "cf add done schema modification, time=".time;
35         }
36         # XXX: edit is going to be harder: possibly add editing length and renaming column
37         '';
38     },
39 )
40 %>
41 <%init>
42
43 my $act = 'add';
44
45 ## make cgi->param("name") lowercase
46 my $vcf_name = $cgi->param('name');
47 $vcf_name =~ s/\s/_/g; $vcf_name =~ s/[^A-Za-z0-9\-_]//g;
48 $cgi->param('name' => lc $vcf_name);
49
50 die "access denied"
51   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
52
53 </%init>