9 use List::Util qw(reduce);
 
  12   $main::lxdebug->enter_sub();
 
  17   if ($params{mode} && ($params{mode} eq 'ids')) {
 
  18     Common::check_params_x(\%params, [ qw(from_ids to_ids) ]);
 
  21     Common::check_params(\%params, qw(links));
 
  27   if ($params{mode} && ($params{mode} eq 'ids')) {
 
  28     my ($from_to, $to_from) = $params{from_ids} ? qw(from to) : qw(to from);
 
  31     if ('ARRAY' eq ref $params{"${from_to}_ids"}) {
 
  32       $ids{$from_to} = $params{"${from_to}_ids"};
 
  34       $ids{$from_to} = [ grep { $_ } map { $_ * 1 } split m/\s+/, $params{"${from_to}_ids"} ];
 
  37     if (my $num = scalar @{ $ids{$from_to} }) {
 
  38       $ids{$to_from} = [ ($params{"${to_from}_id"}) x $num ];
 
  39       @links         = map { { 'from_table' => $params{from_table},
 
  40                                'from_id'    => $ids{from}->[$_],
 
  41                                'to_table'   => $params{to_table},
 
  42                                'to_id'      => $ids{to}->[$_],      } } (0 .. $num - 1);
 
  46     @links = @{ $params{links} };
 
  50     $main::lxdebug->leave_sub();
 
  54   my $myconfig = \%main::myconfig;
 
  55   my $form     = $main::form;
 
  57   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
  59   my $query    = qq|INSERT INTO record_links (from_table, from_id, to_table, to_id) VALUES (?, ?, ?, ?)|;
 
  60   my $sth      = prepare_query($form, $dbh, $query);
 
  62   foreach my $link (@links) {
 
  63     next if ('HASH' ne ref $link);
 
  64     next if (!$link->{from_table} || !$link->{from_id} || !$link->{to_table} || !$link->{to_id});
 
  66     do_statement($form, $sth, $query, $link->{from_table}, conv_i($link->{from_id}), $link->{to_table}, conv_i($link->{to_id}));
 
  69   $dbh->commit() unless ($params{dbh});
 
  71   $main::lxdebug->leave_sub();
 
  75   $main::lxdebug->enter_sub();
 
  80   Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
 
  82   my $myconfig   = \%main::myconfig;
 
  83   my $form       = $main::form;
 
  85   my $dbh        = $params{dbh} || $form->get_standard_dbh($myconfig);
 
  90   foreach my $col (qw(from_table from_id to_table to_id)) {
 
  91     next unless ($params{$col});
 
  93     if ('ARRAY' eq ref $params{$col}) {
 
  94       push @conditions, "$col IN (" . join(', ', ('?') x scalar(@{ $params{$col} })) . ")";
 
  95       push @values,     $col =~ m/table/ ? @{ $params{$col} } : map { conv_i($_) } @{ $params{$col} };
 
  98       push @conditions, "$col = ?";
 
  99       push @values,     $col =~ m/table/ ? $params{$col} : conv_i($params{$col});
 
 103   my $query = qq|SELECT from_table, from_id, to_table, to_id
 
 106   if (scalar @conditions) {
 
 107     $query .= qq| WHERE | . join(' AND ', map { "($_)" } @conditions);
 
 110   my $links = selectall_hashref_query($form, $dbh, $query, @values);
 
 112   $main::lxdebug->leave_sub();
 
 114   return wantarray ? @{ $links } : $links;
 
 118   $main::lxdebug->enter_sub();
 
 126   Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
 
 127   Common::check_params(\%params, "via");
 
 129   my @hops = ref $params{via} eq 'ARRAY'
 
 132   unshift @hops, +{ table => $params{from_table}, id => $params{from_id} };
 
 133   push    @hops, +{ table => $params{to_table},   id => $params{to_id} };
 
 135   my $myconfig   = \%main::myconfig;
 
 136   my $form       = $main::form;
 
 138   my $last_hop   = shift @hops;
 
 140   for my $hop (@hops) {
 
 142     my @temp_links = $self->get_links(
 
 143       from_table => $last_hop->{table},
 
 144       from_id    => $last_hop->{id},
 
 145       to_table   => $hop->{table},
 
 149     # short circuit if any of these are empty
 
 150     return wantarray ? () : [] unless scalar @temp_links;
 
 152     push @links, \@temp_links;
 
 156   my $result = reduce {
 
 160         if (   $a->{to_table} eq $b->{from_table}
 
 161             && $a->{to_id}    eq $b->{from_id} ) {
 
 162           +{ from_table => $a->{from_table},
 
 163              from_id    => $a->{from_id},
 
 164              to_table   => $b->{to_table},
 
 165              to_id      => $b->{to_id} }
 
 171   $main::lxdebug->leave_sub();
 
 173   return wantarray ? @{ $result } : $result;
 
 177   $main::lxdebug->enter_sub();
 
 182   Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
 
 184   my $myconfig   = \%main::myconfig;
 
 185   my $form       = $main::form;
 
 187   my $dbh        = $params{dbh} || $form->get_standard_dbh($myconfig);
 
 190   my (@where_tokens, @where_values);
 
 192   for my $col (qw(from_table from_id to_table to_id)) {
 
 193     add_token(\@where_tokens, \@where_values, col => $col, val => $params{$col}) if $params{$col};
 
 196   my $where = @where_tokens ? "WHERE ". join ' AND ', map { "($_)" } @where_tokens : '';
 
 197   my $query = "DELETE FROM record_links $where";
 
 199   do_query($form, $dbh, $query, @where_values);
 
 201   $dbh->commit() unless ($params{dbh});
 
 203   $main::lxdebug->leave_sub();
 
 212 SL::RecordLinks - Verlinkung von kivitendo Objekten.
 
 218   my @links = RecordLinks->get_links(
 
 223   my @links = RecordLinks->get_links_via(
 
 232   RecordLinks->create_links(
 
 239   RecordLinks->create_links(@links);
 
 245 Transitive RecordLinks mit get_links_via.
 
 247 get_links_via erwartet den zusätzlichen parameter via. via ist ein
 
 248 hashref mit den jeweils optionalen Einträgen table und id, die sich
 
 249 genauso verhalten wie die from/to_table/id werte der get_links funktion.
 
 251 Alternativ kann via auch ein Array dieser Hashes sein:
 
 258       table      => 'delivery_orders'
 
 271 Die Einträge in einem via-Array werden exakt in dieser Reihenfolge
 
 272 benutzt und sind nicht optional. Da obige Beispiel würde also die
 
 275   oe:11 -> ar:12 -> is:13 -> do:14
 
 279   oe:11 -> ar:13 -> do:14