1. Variable umbenannt, in der die IDs aus OE zwischengespeichert werden, aus denen...
[kivitendo-erp.git] / SL / RecordLinks.pm
1 package RecordLinks;
2
3 use SL::Common;
4 use SL::DBUtils;
5
6 sub create_links {
7   $main::lxdebug->enter_sub();
8
9   my $self     = shift;
10   my %params   = @_;
11
12   if ($params{mode} && ($params{mode} eq 'string')) {
13     Common::check_params_x(\%params, [ qw(from_ids to_ids) ]);
14
15   } else {
16     Common::check_params(\%params, qw(links));
17
18   }
19
20   my @links;
21
22   if ($params{mode} && ($params{mode} eq 'string')) {
23     my ($from_to, $to_from) = $params{from_ids} ? qw(from to) : qw(to from);
24     my %ids                 = ( $from_to => [ grep { $_ } map { $_ * 1 } split m/\s+/, $params{"${from_to}_ids"} ] );
25
26     if (my $num = scalar @{ $ids{$from_to} }) {
27       $main::lxdebug->message(0, "3");
28       $ids{$to_from} = [ ($params{"${to_from}_id"}) x $num ];
29       @links         = map { { 'from_table' => $params{from_table},
30                                'from_id'    => $ids{from}->[$_],
31                                'to_table'   => $params{to_table},
32                                'to_id'      => $ids{to}->[$_],      } } (0 .. $num - 1);
33     }
34
35   } else {
36     @links = @{ $params{links} };
37   }
38
39   if (!scalar @links) {
40     $main::lxdebug->leave_sub();
41     return;
42   }
43
44   my $myconfig = \%main::myconfig;
45   my $form     = $main::form;
46
47   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
48
49   my $query    = qq|INSERT INTO record_links (from_table, from_id, to_table, to_id) VALUES (?, ?, ?, ?)|;
50   my $sth      = prepare_query($form, $dbh, $query);
51
52   foreach my $link (@links) {
53     next if ('HASH' ne ref $link);
54     next if (!$link->{from_table} || !$link->{from_id} || !$link->{to_table} || !$link->{to_id});
55
56     do_statement($form, $sth, $query, $link->{from_table}, conv_i($link->{from_id}), $link->{to_table}, conv_i($link->{to_id}));
57   }
58
59   $dbh->commit() unless ($params{dbh});
60
61   $main::lxdebug->leave_sub();
62 }
63
64 sub get_links {
65   $main::lxdebug->enter_sub();
66
67   my $self     = shift;
68   my %params   = @_;
69
70   Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
71
72   my $myconfig   = \%main::myconfig;
73   my $form       = $main::form;
74
75   my $dbh        = $params{dbh} || $form->get_standard_dbh($myconfig);
76
77   my @conditions = ();
78   my @values     = ();
79
80   foreach my $col (qw(from_table from_id to_table to_id)) {
81     next unless ($params{$col});
82
83     if ('ARRAY' eq ref $params{$col}) {
84       push @conditions, "$col IN (" . join(', ', ('?') x scalar(@{ $params{$col} })) . ")";
85       push @values,     $col =~ m/table/ ? @{ $params{$col} } : map { conv_i($_) } @{ $params{$col} };
86
87     } else {
88       push @conditions, "$col = ?";
89       push @values,     $col =~ m/table/ ? $params{$col} : conv_i($params{$col});
90     }
91   }
92
93   my $query = qq|SELECT from_table, from_id, to_table, to_id
94                  FROM record_links|;
95
96   if (scalar @conditions) {
97     $query .= qq| WHERE | . join(' AND ', map { "($_)" } @conditions);
98   }
99
100   my $links = selectall_hashref_query($form, $dbh, $query, @values);
101
102   $main::lxdebug->leave_sub();
103
104   return wantarray ? @{ $links } : $links;
105 }
106
107 1;