Beim Umwandeln von Angeboten/Preisanfragen in Aufträge die IDs in record_links speich...
[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 'ids')) {
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 'ids')) {
23     my ($from_to, $to_from) = $params{from_ids} ? qw(from to) : qw(to from);
24     my %ids;
25
26     if ('ARRAY' eq ref $params{"${from_to}_ids"}) {
27       $ids{$from_to} = $params{"${from_to}_ids"};
28     } else {
29       $ids{$from_to} = [ grep { $_ } map { $_ * 1 } split m/\s+/, $params{"${from_to}_ids"} ];
30     }
31
32     if (my $num = scalar @{ $ids{$from_to} }) {
33       $main::lxdebug->message(0, "3");
34       $ids{$to_from} = [ ($params{"${to_from}_id"}) x $num ];
35       @links         = map { { 'from_table' => $params{from_table},
36                                'from_id'    => $ids{from}->[$_],
37                                'to_table'   => $params{to_table},
38                                'to_id'      => $ids{to}->[$_],      } } (0 .. $num - 1);
39     }
40
41   } else {
42     @links = @{ $params{links} };
43   }
44
45   if (!scalar @links) {
46     $main::lxdebug->leave_sub();
47     return;
48   }
49
50   my $myconfig = \%main::myconfig;
51   my $form     = $main::form;
52
53   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
54
55   my $query    = qq|INSERT INTO record_links (from_table, from_id, to_table, to_id) VALUES (?, ?, ?, ?)|;
56   my $sth      = prepare_query($form, $dbh, $query);
57
58   foreach my $link (@links) {
59     next if ('HASH' ne ref $link);
60     next if (!$link->{from_table} || !$link->{from_id} || !$link->{to_table} || !$link->{to_id});
61
62     do_statement($form, $sth, $query, $link->{from_table}, conv_i($link->{from_id}), $link->{to_table}, conv_i($link->{to_id}));
63   }
64
65   $dbh->commit() unless ($params{dbh});
66
67   $main::lxdebug->leave_sub();
68 }
69
70 sub get_links {
71   $main::lxdebug->enter_sub();
72
73   my $self     = shift;
74   my %params   = @_;
75
76   Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
77
78   my $myconfig   = \%main::myconfig;
79   my $form       = $main::form;
80
81   my $dbh        = $params{dbh} || $form->get_standard_dbh($myconfig);
82
83   my @conditions = ();
84   my @values     = ();
85
86   foreach my $col (qw(from_table from_id to_table to_id)) {
87     next unless ($params{$col});
88
89     if ('ARRAY' eq ref $params{$col}) {
90       push @conditions, "$col IN (" . join(', ', ('?') x scalar(@{ $params{$col} })) . ")";
91       push @values,     $col =~ m/table/ ? @{ $params{$col} } : map { conv_i($_) } @{ $params{$col} };
92
93     } else {
94       push @conditions, "$col = ?";
95       push @values,     $col =~ m/table/ ? $params{$col} : conv_i($params{$col});
96     }
97   }
98
99   my $query = qq|SELECT from_table, from_id, to_table, to_id
100                  FROM record_links|;
101
102   if (scalar @conditions) {
103     $query .= qq| WHERE | . join(' AND ', map { "($_)" } @conditions);
104   }
105
106   my $links = selectall_hashref_query($form, $dbh, $query, @values);
107
108   $main::lxdebug->leave_sub();
109
110   return wantarray ? @{ $links } : $links;
111 }
112
113 1;