X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FRecordLinks.pm;h=fa5de38f79c6eb7b328292e323a99c49514abdb0;hb=1043d7f814fccf5864e677b1e38577d0a150026c;hp=c5049987457b736f9a754c8b1af364d1510cb7df;hpb=3af71f2e27419024b28a2ff65e8b4d7cf499035d;p=kivitendo-erp.git diff --git a/SL/RecordLinks.pm b/SL/RecordLinks.pm index c50499874..fa5de38f7 100644 --- a/SL/RecordLinks.pm +++ b/SL/RecordLinks.pm @@ -2,6 +2,8 @@ package RecordLinks; use SL::Common; use SL::DBUtils; +use Data::Dumper; +use List::Util qw(reduce); sub create_links { $main::lxdebug->enter_sub(); @@ -130,8 +132,8 @@ sub get_links_via { my $myconfig = \%main::myconfig; my $form = $main::form; - my $last_hop = shift @hops; - my @links = undef; + my $last_hop = shift @hops; + my @links; for my $hop (@hops) { my @temp_links = $self->get_links( @@ -141,25 +143,61 @@ sub get_links_via { to_id => $hop->{id}, ); - if (@links) { - @links = grep { $_ } - cross { - if ( $a->{to_table} eq $b->{from_table} - && $a->{to_id} eq $b->{from_id} ) { - +{ $a->{from_table}, $a->{from_id}, - $b->{to_table}, $b->{to_table} } - } - } @links, @temp_links; - } else { - @links = @temp_links; - } + # short circuit if any of these are empty + return wantarray ? () : [] unless scalar @temp_links; - $last_hop = $hop; + push @links, \@temp_links; + $last_hop = $hop; } + my $result = reduce { + [ + grep { $_ } + cross { + if ( $a->{to_table} eq $b->{from_table} + && $a->{to_id} eq $b->{from_id} ) { + +{ from_table => $a->{from_table}, + from_id => $a->{from_id}, + to_table => $b->{to_table}, + to_id => $b->{to_id} } + } + } @{ $a }, @{ $b } + ] + } @links; + $main::lxdebug->leave_sub(); - return wantarray ? @links : \@links; + return wantarray ? @{ $result } : $result; +} + +sub delete { + $main::lxdebug->enter_sub(); + + my $self = shift; + my %params = @_; + + Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]); + + my $myconfig = \%main::myconfig; + my $form = $main::form; + + my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); + + # content + my (@where_tokens, @where_values); + + for my $col (qw(from_table from_id to_table to_id)) { + add_token(\@where_tokens, \@where_values, col => $col, val => $params{$col}) if $params{$col}; + } + + my $where = "WHERE ". join ' AND ', map { "($_)" } @where_tokens if scalar @where_tokens; + my $query = "DELETE FROM record_links $where"; + + do_query($form, $dbh, $query, @where_values); + + $dbh->commit() unless ($params{dbh}); + + $main::lxdebug->leave_sub(); } 1;