6 use List::Util qw(reduce);
 
  11   $main::lxdebug->enter_sub();
 
  16   if ($params{mode} && ($params{mode} eq 'ids')) {
 
  17     Common::check_params_x(\%params, [ qw(from_ids to_ids) ]);
 
  20     Common::check_params(\%params, qw(links));
 
  26   if ($params{mode} && ($params{mode} eq 'ids')) {
 
  27     my ($from_to, $to_from) = $params{from_ids} ? qw(from to) : qw(to from);
 
  30     if ('ARRAY' eq ref $params{"${from_to}_ids"}) {
 
  31       $ids{$from_to} = $params{"${from_to}_ids"};
 
  33       $ids{$from_to} = [ grep { $_ } map { $_ * 1 } split m/\s+/, $params{"${from_to}_ids"} ];
 
  36     if (my $num = scalar @{ $ids{$from_to} }) {
 
  37       $ids{$to_from} = [ ($params{"${to_from}_id"}) x $num ];
 
  38       @links         = map { { 'from_table' => $params{from_table},
 
  39                                'from_id'    => $ids{from}->[$_],
 
  40                                'to_table'   => $params{to_table},
 
  41                                'to_id'      => $ids{to}->[$_],      } } (0 .. $num - 1);
 
  45     @links = @{ $params{links} };
 
  49     $main::lxdebug->leave_sub();
 
  53   my $myconfig = \%main::myconfig;
 
  54   my $form     = $main::form;
 
  56   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
  58   my $query    = qq|INSERT INTO record_links (from_table, from_id, to_table, to_id) VALUES (?, ?, ?, ?)|;
 
  59   my $sth      = prepare_query($form, $dbh, $query);
 
  61   foreach my $link (@links) {
 
  62     next if ('HASH' ne ref $link);
 
  63     next if (!$link->{from_table} || !$link->{from_id} || !$link->{to_table} || !$link->{to_id});
 
  65     do_statement($form, $sth, $query, $link->{from_table}, conv_i($link->{from_id}), $link->{to_table}, conv_i($link->{to_id}));
 
  68   $dbh->commit() unless ($params{dbh});
 
  70   $main::lxdebug->leave_sub();
 
  74   $main::lxdebug->enter_sub();
 
  79   Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
 
  81   my $myconfig   = \%main::myconfig;
 
  82   my $form       = $main::form;
 
  84   my $dbh        = $params{dbh} || $form->get_standard_dbh($myconfig);
 
  89   foreach my $col (qw(from_table from_id to_table to_id)) {
 
  90     next unless ($params{$col});
 
  92     if ('ARRAY' eq ref $params{$col}) {
 
  93       push @conditions, "$col IN (" . join(', ', ('?') x scalar(@{ $params{$col} })) . ")";
 
  94       push @values,     $col =~ m/table/ ? @{ $params{$col} } : map { conv_i($_) } @{ $params{$col} };
 
  97       push @conditions, "$col = ?";
 
  98       push @values,     $col =~ m/table/ ? $params{$col} : conv_i($params{$col});
 
 102   my $query = qq|SELECT from_table, from_id, to_table, to_id
 
 105   if (scalar @conditions) {
 
 106     $query .= qq| WHERE | . join(' AND ', map { "($_)" } @conditions);
 
 109   my $links = selectall_hashref_query($form, $dbh, $query, @values);
 
 111   $main::lxdebug->leave_sub();
 
 113   return wantarray ? @{ $links } : $links;
 
 117   $main::lxdebug->enter_sub();
 
 125   Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
 
 126   Common::check_params(\%params, "via");
 
 128   my @hops = ref $params{via} eq 'ARRAY'
 
 131   unshift @hops, +{ table => $params{from_table}, id => $params{from_id} };
 
 132   push    @hops, +{ table => $params{to_table},   id => $params{to_id} };
 
 134   my $myconfig   = \%main::myconfig;
 
 135   my $form       = $main::form;
 
 137   my $last_hop   = shift @hops;
 
 139   for my $hop (@hops) {
 
 141     my @temp_links = $self->get_links(
 
 142       from_table => $last_hop->{table},
 
 143       from_id    => $last_hop->{id},
 
 144       to_table   => $hop->{table},
 
 148     # short circuit if any of these are empty
 
 149     return wantarray ? () : [] unless scalar @temp_links;
 
 151     push @links, \@temp_links;
 
 155   my $result = reduce {
 
 159         if (   $a->{to_table} eq $b->{from_table}
 
 160             && $a->{to_id}    eq $b->{from_id} ) {
 
 161           +{ from_table => $a->{from_table},
 
 162              from_id    => $a->{from_id},
 
 163              to_table   => $b->{to_table},
 
 164              to_id      => $b->{to_id} }
 
 170   $main::lxdebug->leave_sub();
 
 172   return wantarray ? @{ $result } : $result;
 
 176   $main::lxdebug->enter_sub();
 
 181   Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
 
 183   my $myconfig   = \%main::myconfig;
 
 184   my $form       = $main::form;
 
 186   my $dbh        = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 189   my (@where_tokens, @where_values);
 
 191   for my $col (qw(from_table from_id to_table to_id)) {
 
 192     add_token(\@where_tokens, \@where_values, col => $col, val => $params{$col}) if $params{$col};
 
 195   my $where = "WHERE ". join ' AND ', map { "($_)" } @where_tokens if scalar @where_tokens;
 
 196   my $query = "DELETE FROM record_links $where";
 
 198   do_query($form, $dbh, $query, @where_values);
 
 200   $dbh->commit() unless ($params{dbh});
 
 202   $main::lxdebug->leave_sub();
 
 211 SL::RecordLinks - Verlinkung von Lx-Office Objekten.
 
 217   my @links = RecordLinks->get_links(
 
 222   my @links = RecordLinks->get_links_via(
 
 231   RecordLinks->create_links(
 
 238   RecordLinks->create_links(@links);
 
 246 Transitive RecordLinks mit get_links_via.
 
 248 get_links_via erwartet den zusätzlichen parameter via. via ist ein
 
 249 hashref mit den jeweils optionalen Einträgen table und id, die sich
 
 250 genauso verhalten wie die from/to_table/id werte der get_links funktion.
 
 252 Alternativ kann via auch ein Array dieser Hashes sein:
 
 259       table      => 'delivery_orders'
 
 272 Die Einträge in einem via-Array werden exakt in dieser Reihenfolge
 
 273 benutzt und sind nicht optional. Da obige Beispiel würde also die
 
 276   oe:11 -> ar:12 -> is:13 -> do:14
 
 280   oe:11 -> ar:13 -> do:14