<%doc> Delete one or more objects <%args> $table => undef $id => undef $user => $ui->get_current_user($r) $submit => "Delete" $sid => undef $selectall => undef $selectall_ids => undef $return_url => undef $warning => undef <%init> my $DEBUG = 0; my $session; my @objs; print '%ARGS is

    ', Dumper(%ARGS), '

' if $DEBUG; my $manager = $ui->get_permission_manager($r); my %cascade;
%if ( $submit eq "Delete" ){ Are you sure you want to delete the following objects?

<%perl> eval { $session = $ui->mk_session(); }; if ( my $e = $@ ){ $m->comp('error.mhtml', error=>$e); } if ( $selectall && $selectall_ids && $table ){ # This means that we were passed a list of ids for a given table foreach my $id ( split /,/, $selectall_ids ){ my $key = $table . "__" . $id; $ARGS{$key} = ""; } }elsif ( $table && $id ){ # We were passed a single id for a given table my $key = $table . "__" . $id; $ARGS{$key} = ""; } foreach my $arg ( sort keys %ARGS ){ next unless $arg =~ /__/o; my ($table, $id) = split /__/, $arg; my $obj; unless ( $obj = $table->retrieve($id) ){ $m->comp('error.mhtml', error => "Error retrieving $table id '$id'"); } my $lbl = $obj->get_label; print "
- $table $lbl
"; push @objs, [$table, $obj->id]; } $session->{objects} = \@objs;

<%perl> my $is_device = 0; if ( $table && $id ){ my $obj; unless ( $obj = $table->retrieve($id) ){ $m->comp('error.mhtml', error => "Error retrieving $table id '$id'"); } if ( $table eq 'Device' ){ $is_device = 1; } my %linksfrom = $table->meta_data->get_links_from(); foreach my $i ( sort keys %linksfrom ){ # Table that points to us my $j = (keys %{ $linksfrom{$i} })[0]; my $fmt = $j->meta_data; next if $fmt->is_history; # Don't warn about history tables. next if $fmt->is_join; # Don't warn about join tables. Confuses the user. # Column in the other table that points to this table my $ffield = $linksfrom{$i}{$j}; my $attr = $fmt->get_column($ffield)->links_to_attrs(); if ( $attr->{cascade} eq 'Delete' ){ foreach my $ro ( $obj->$i ){ push @{$cascade{$j}}, $ro->get_label; } } } } % if ( $warning ne "" ){

WARNINGS: <% $warning %>

% } % if ( $is_device ){

Device removal notice: This hardware will be moved to the available asset pool. If you inted to decommission the hardware remember to remove the asset as well.

% } % if ( %cascade ){ Notice that by deleting this object, you will also delete:

% foreach my $t ( keys %cascade ){
- <% $t %>
  % foreach my $lbl ( @{$cascade{$t}} ){
  - <% $lbl %> % }
  % }

% } <%perl> }elsif ( $submit eq "Confirm" ){ eval{ $session = $ui->get_session($sid); }; if ( my $e = $@ ){ $m->comp('error.mhtml', error=>$e); } my @records = @{$session->{objects}}; my @objs; foreach my $l ( @records ){ my ($table, $id) = @$l; push @objs, $table->retrieve($id); } my @lbls = map { "$table " . $_->get_label } @objs; eval { Netdot::Model->do_transaction(sub{ foreach my $obj ( @objs ){ unless ( $manager && $manager->can($user, "delete", $obj) ){ $ui->throw_user("You don't have permission to delete this object"); } $obj->delete(); } }); }; if ( my $e = $@ ){ $m->comp('error.mhtml', error=>$e); } print "

\n"; foreach my $lbl ( @lbls ){ print "
- $lbl has been deleted
"; } print "

\n"; if ( $return_url ){ print '

'; print " [return]"; } }elsif ( $submit eq "Cancel" ){ print "

"; print "Delete cancelled"; if ( $session ){ $ui->rm_session($session); } }