<%doc> edit-multiple.html Arguments: table -- name of the table the records are in tablefields -- array of fields to edit for this table submit -- used to select which operation to perform selectall -- Flag that indicates all records are selected selectall_ids -- list of record ids to act on <tablename>__<id> Individual records passed to us on first stage <tablename>__<id>__<field> = <value> Record values passed on second stage Possible States: * show list of available fields to edit for the given records - $submit eq "Edit" (default) - <tablename>__<id> elements are passed OR $selectall AND $selectall_ids are set * show list of records with editable fields - $sid (session id) is set - $submit eq "Go" * actually save the changes to the database - @tablefields AND <tablename>__<id>__<field> = <value> are passed - $submit eq "Save Changes" <%args> $table => undef @tablefields => undef $submit => 'Edit' $selectall => undef $selectall_ids => undef $sid => undef <%init> if(! $table){ $m->comp('/generic/error.mhtml', error=>"No database table provided to edit!"); } my @select_all; if($selectall_ids){ @select_all = qw[ $selectall_ids ]; } my $DEBUG = 0; print "

    ", Dumper(%ARGS), "

" if $DEBUG; my $session; my @columns = $table->meta_data->get_columns; my %objs; <%perl> if ( $submit eq 'Edit' ) { if ( $selectall && $selectall_ids ){ @{$objs{$table}} = map { $table->retrieve($_) } split ',', $selectall_ids; }else{ foreach my $arg ( keys %ARGS ){ next unless $arg =~ /__/; my ($table, $id) = split /__/, $arg; push @{$objs{$table}}, $id; } unless ( defined $objs{$table} && scalar(@{$objs{$table}}) ){ $m->comp('/generic/error.mhtml', error=>"No records selected!"); } # Sort foreach my $table ( keys %objs ){ my $lblf = ($table->meta_data->get_labels)[0]; my @objs = map { $table->retrieve($_) } @{$objs{$table}}; my $alpha = 0; foreach my $obj ( @objs ){ if ( $obj->$lblf =~ /D+/ ){ $alpha = 1; } } if ( $alpha ){ @{$objs{$table}} = sort { $a->$lblf cmp $b->$lblf } @objs; }else{ @{$objs{$table}} = sort { $a->$lblf <=> $b->$lblf } @objs; } } } print "

    ", Dumper(%objs), "

" if $DEBUG; # Build a session object to store the list of # records passed to us eval { $session = $ui->mk_session(); }; if ( my $e = $@ ){ $m->comp('error.mhtml', error=>$e); } $session->{objs} = \%objs; my @cells; foreach my $c ( sort { $a->tag cmp $b->tag } @columns ) { next if ( $c->name eq 'id' ); my $html = ''.$c->tag; push(@cells, $html); } % ##################################################################### % # Show table with possible fields to edit % #####################################################################
Choose fields to edit
<& wrapping_table.mhtml, data => \@cells, cols => 5 &>
<%perl> }elsif ( $submit eq 'Go' && $sid ) { eval { $session = $ui->get_session($sid); }; if ( my $e = $@ ){ $m->comp('error.mhtml', error=>$e); } %objs = %{ $session->{objs} }; print "

    ", Dumper(%objs), "

" if $DEBUG; unless ( defined $objs{$table} && scalar(@{$objs{$table}}) ){ $m->comp('/generic/error.mhtml', error=>"Could not retrieve object list from session data"); } my $numobjs = scalar @{$objs{$table}}; my (@headers, @rows); my $showeditbuttons; if ( @tablefields ) { push(@headers, "Name"); foreach my $field ( @tablefields ) { my $mcol = $table->meta_data->get_column($field); push(@headers, $mcol->tag); } if ( $selectall ) { my $hid = ""; my @row1 = (); push( @row1, 'Apply to all '.$numobjs.' records'.$hid ); foreach my $field ( @tablefields ) { my %tmp = $ui->form_field(table=>$table, column=>$field, edit=>1); push( @row1, $tmp{'value'} ); } push( @rows, \@row1 ); my @row2 = (); push( @row2, '
Note: The values you set here will be
applied to all '.$numobjs.' records, even if
the value is blank. If you do not want to
modify a field, go back and uncheck it.' ); for( my $i=0; $i<scalar(@tablefields); $i++ ) { push( @row2, " " ); } push( @rows, \@row2 ); } else { foreach my $obj ( @{$objs{$table}} ) { my @row = (); push( @row, $obj->get_label ); foreach my $field ( @tablefields ) { my %tmp = $ui->form_field(object=>$obj, column=>$field, edit=>1); push( @row, $tmp{'value'} ); } push( @rows, \@row ); } my @emptyrow = (); for( my $i=0; $i<(@tablefields)+1; $i++ ) { push( @emptyrow, " " ); } push( @rows, \@emptyrow ); my @row2 = (); push( @row2, 'Apply to all above' ); foreach my $field ( @tablefields ) { push( @row2, '' ); } push( @row2, ' ' ); push( @rows, \@row2 ); my @row1 = (); push( @row1, ' ' ); foreach my $field ( @tablefields ) { my %tmp = $ui->form_field(table=>$table, column=>$field, edit=>1); push( @row1, $tmp{'value'} ); } push( @row1, ' ' ); push( @rows, \@row1 ); } $showeditbuttons = 1; } else { push( @headers, "Error" ); my @row; push( @row, 'Go back and choose at least 1 field to edit' ); push( @rows, \@row ); $showeditbuttons = 0; }
Editing Records from "<% $table %>"
<& data_table.mhtml, field_headers=>\@headers, data=>\@rows &>
% if( $showeditbuttons ) { % }
<%perl> }elsif ($submit eq "Save Changes") { my %data; if( $selectall_ids ) { foreach my $id ( split(/,/, $selectall_ids) ) { foreach my $j ( keys %ARGS ) { # data comes in as table__id__field = value next if ($j !~ /__/ ); my ($table, $new, $field) = split("__", $j); my $val = $ARGS{$j}; if (exists($data{$id})) { $data{$id}{$field} = $val; } else { my %values; $values{$field} = $val; $data{$id} = \%values; } } } } else { # get values from request and build a hash with the following structure: # $data{$id}{$field} = $val; foreach my $j ( keys %ARGS ) { # data comes in as table__id__field = value next if ($j !~ /__/ ); my ($table, $id, $field) = split("__", $j); next if ($id eq "NEW"); my $val; if ($ARGS{'applytoall_'.$field} eq "on") { $val = $ARGS{$table.'__NEW__'.$field}; } else { $val = $ARGS{$j}; } if (exists($data{$id})) { $data{$id}{$field} = $val; } else { my %values; $values{$field} = $val; $data{$id} = \%values; } } } # now loop thru all the $id s in $data and do the update for each row my @objs; foreach my $id ( keys %data ) { my $obj = $table->retrieve($id) || $m->comp('error.mhtml', error=> "Could not retrieve $table id $id"); $obj->update($data{$id}); push @objs, $obj; }
Changes Saved
Your changes to the following <% $table %> records have been saved:
<& sortresults.mhtml, object=>\@objs &>
<%perl> } else { $m->comp("error.mhtml", error=>"Error with arguments to edit-multiple"); }