Transitive RecordLinks mit get_links_via.
[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       $ids{$to_from} = [ ($params{"${to_from}_id"}) x $num ];
34       @links         = map { { 'from_table' => $params{from_table},
35                                'from_id'    => $ids{from}->[$_],
36                                'to_table'   => $params{to_table},
37                                'to_id'      => $ids{to}->[$_],      } } (0 .. $num - 1);
38     }
39
40   } else {
41     @links = @{ $params{links} };
42   }
43
44   if (!scalar @links) {
45     $main::lxdebug->leave_sub();
46     return;
47   }
48
49   my $myconfig = \%main::myconfig;
50   my $form     = $main::form;
51
52   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
53
54   my $query    = qq|INSERT INTO record_links (from_table, from_id, to_table, to_id) VALUES (?, ?, ?, ?)|;
55   my $sth      = prepare_query($form, $dbh, $query);
56
57   foreach my $link (@links) {
58     next if ('HASH' ne ref $link);
59     next if (!$link->{from_table} || !$link->{from_id} || !$link->{to_table} || !$link->{to_id});
60
61     do_statement($form, $sth, $query, $link->{from_table}, conv_i($link->{from_id}), $link->{to_table}, conv_i($link->{to_id}));
62   }
63
64   $dbh->commit() unless ($params{dbh});
65
66   $main::lxdebug->leave_sub();
67 }
68
69 sub get_links {
70   $main::lxdebug->enter_sub();
71
72   my $self     = shift;
73   my %params   = @_;
74
75   Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
76
77   my $myconfig   = \%main::myconfig;
78   my $form       = $main::form;
79
80   my $dbh        = $params{dbh} || $form->get_standard_dbh($myconfig);
81
82   my @conditions = ();
83   my @values     = ();
84
85   foreach my $col (qw(from_table from_id to_table to_id)) {
86     next unless ($params{$col});
87
88     if ('ARRAY' eq ref $params{$col}) {
89       push @conditions, "$col IN (" . join(', ', ('?') x scalar(@{ $params{$col} })) . ")";
90       push @values,     $col =~ m/table/ ? @{ $params{$col} } : map { conv_i($_) } @{ $params{$col} };
91
92     } else {
93       push @conditions, "$col = ?";
94       push @values,     $col =~ m/table/ ? $params{$col} : conv_i($params{$col});
95     }
96   }
97
98   my $query = qq|SELECT from_table, from_id, to_table, to_id
99                  FROM record_links|;
100
101   if (scalar @conditions) {
102     $query .= qq| WHERE | . join(' AND ', map { "($_)" } @conditions);
103   }
104
105   my $links = selectall_hashref_query($form, $dbh, $query, @values);
106
107   $main::lxdebug->leave_sub();
108
109   return wantarray ? @{ $links } : $links;
110 }
111
112 sub get_links_via {
113   $main::lxdebug->enter_sub();
114
115   use SL::MoreCommon;
116   use Data::Dumper;
117
118   my $self     = shift;
119   my %params   = @_;
120
121   Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]);
122   Common::check_params(\%params, "via");
123
124   my @hops = ref $params{via} eq 'ARRAY'
125            ? @{ $params{via} }
126            :    $params{via};
127   unshift @hops, +{ table => $params{from_table}, id => $params{from_id} };
128   push    @hops, +{ table => $params{to_table},   id => $params{to_id} };
129
130   my $myconfig   = \%main::myconfig;
131   my $form       = $main::form;
132
133   my $last_hop = shift @hops;
134   my @links    = undef;
135   for my $hop (@hops) {
136
137     my @temp_links = $self->get_links(
138       from_table => $last_hop->{table},
139       from_id    => $last_hop->{id},
140       to_table   => $hop->{table},
141       to_id      => $hop->{id},
142     );
143
144     if (@links) {
145       @links = grep { $_ }
146                cross {
147                  if (   $a->{to_table} eq $b->{from_table}
148                      && $a->{to_id}    eq $b->{from_id} ) {
149                    +{ $a->{from_table}, $a->{from_id},
150                       $b->{to_table},   $b->{to_table} }
151                  }
152               } @links, @temp_links;
153     } else {
154       @links = @temp_links;
155     }
156
157     $last_hop = $hop;
158   }
159
160   $main::lxdebug->leave_sub();
161
162   return wantarray ? @links : \@links;
163 }
164
165 1;