Debugcode
[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 1;