X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;ds=sidebyside;f=SL%2FRecordLinks.pm;h=6229cfe43389dcb1bdfefa8f053973e780148b45;hb=90daea72226506cd9f797ba2bfd7d3c39e19fe3e;hp=fb451a801489bbc6774d52f07aefbc4436d54206;hpb=00738f6f4e43355ae9e70cd5350d7c75c1533f5f;p=kivitendo-erp.git diff --git a/SL/RecordLinks.pm b/SL/RecordLinks.pm index fb451a801..6229cfe43 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(); @@ -9,7 +11,7 @@ sub create_links { my $self = shift; my %params = @_; - if ($params{mode} && ($params{mode} eq 'string')) { + if ($params{mode} && ($params{mode} eq 'ids')) { Common::check_params_x(\%params, [ qw(from_ids to_ids) ]); } else { @@ -19,12 +21,17 @@ sub create_links { my @links; - if ($params{mode} && ($params{mode} eq 'string')) { + if ($params{mode} && ($params{mode} eq 'ids')) { my ($from_to, $to_from) = $params{from_ids} ? qw(from to) : qw(to from); - my %ids = ( $from_to => [ grep { $_ } map { $_ * 1 } split m/\s+/, $params{"${from_to}_ids"} ] ); + my %ids; + + if ('ARRAY' eq ref $params{"${from_to}_ids"}) { + $ids{$from_to} = $params{"${from_to}_ids"}; + } else { + $ids{$from_to} = [ grep { $_ } map { $_ * 1 } split m/\s+/, $params{"${from_to}_ids"} ]; + } if (my $num = scalar @{ $ids{$from_to} }) { - $main::lxdebug->message(0, "3"); $ids{$to_from} = [ ($params{"${to_from}_id"}) x $num ]; @links = map { { 'from_table' => $params{from_table}, 'from_id' => $ids{from}->[$_], @@ -104,4 +111,63 @@ sub get_links { return wantarray ? @{ $links } : $links; } +sub get_links_via { + $main::lxdebug->enter_sub(); + + use SL::MoreCommon; + use Data::Dumper; + + my $self = shift; + my %params = @_; + + Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]); + Common::check_params(\%params, "via"); + + my @hops = ref $params{via} eq 'ARRAY' + ? @{ $params{via} } + : $params{via}; + unshift @hops, +{ table => $params{from_table}, id => $params{from_id} }; + push @hops, +{ table => $params{to_table}, id => $params{to_id} }; + + my $myconfig = \%main::myconfig; + my $form = $main::form; + + my $last_hop = shift @hops; + my @links; + for my $hop (@hops) { + + my @temp_links = $self->get_links( + from_table => $last_hop->{table}, + from_id => $last_hop->{id}, + to_table => $hop->{table}, + to_id => $hop->{id}, + ); + + # short circuit if any of these are empty + return wantarray ? () : [] unless scalar @temp_links; + + 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 ? @{ $result } : $result; +} + 1;