X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FRecordLinks.pm;h=da5d54abb74b828d4ddfb0ee510a481f64bc3b05;hb=991369b17d679e2855f4d5086d0b9769dc2ae1aa;hp=09fd642c1b55e82ca0a278ad51ceca1643dd8974;hpb=76c486e3bf157e844b0cf11828d55dae2cb7e439;p=kivitendo-erp.git diff --git a/SL/RecordLinks.pm b/SL/RecordLinks.pm index 09fd642c1..da5d54abb 100644 --- a/SL/RecordLinks.pm +++ b/SL/RecordLinks.pm @@ -1,11 +1,13 @@ package RecordLinks; +use utf8; +use strict; + use SL::Common; use SL::DBUtils; use Data::Dumper; use List::Util qw(reduce); - -use strict; +use SL::DB; sub create_links { $main::lxdebug->enter_sub(); @@ -53,19 +55,21 @@ sub create_links { my $myconfig = \%main::myconfig; my $form = $main::form; - my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); + SL::DB->client->with_transaction(sub { + my $dbh = $params{dbh} || SL::DB->client->dbh; - my $query = qq|INSERT INTO record_links (from_table, from_id, to_table, to_id) VALUES (?, ?, ?, ?)|; - my $sth = prepare_query($form, $dbh, $query); + my $query = qq|INSERT INTO record_links (from_table, from_id, to_table, to_id) VALUES (?, ?, ?, ?)|; + my $sth = prepare_query($form, $dbh, $query); - foreach my $link (@links) { - next if ('HASH' ne ref $link); - next if (!$link->{from_table} || !$link->{from_id} || !$link->{to_table} || !$link->{to_id}); + foreach my $link (@links) { + next if ('HASH' ne ref $link); + next if (!$link->{from_table} || !$link->{from_id} || !$link->{to_table} || !$link->{to_id}); - do_statement($form, $sth, $query, $link->{from_table}, conv_i($link->{from_id}), $link->{to_table}, conv_i($link->{to_id})); - } + do_statement($form, $sth, $query, $link->{from_table}, conv_i($link->{from_id}), $link->{to_table}, conv_i($link->{to_id})); + } - $dbh->commit() unless ($params{dbh}); + 1; + }) or do { die SL::DB->client->error }; $main::lxdebug->leave_sub(); } @@ -183,23 +187,104 @@ sub delete { my $myconfig = \%main::myconfig; my $form = $main::form; - my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); + SL::DB->client->with_transaction(sub { + my $dbh = $params{dbh} || SL::DB->client->dbh; - # content - my (@where_tokens, @where_values); + # content + my (@where_tokens, @where_values); - for my $col (qw(from_table from_id to_table to_id)) { - add_token(\@where_tokens, \@where_values, col => $col, val => $params{$col}) if $params{$col}; - } + for my $col (qw(from_table from_id to_table to_id)) { + add_token(\@where_tokens, \@where_values, col => $col, val => $params{$col}) if $params{$col}; + } - my $where = "WHERE ". join ' AND ', map { "($_)" } @where_tokens if scalar @where_tokens; - my $query = "DELETE FROM record_links $where"; + my $where = @where_tokens ? "WHERE ". join ' AND ', map { "($_)" } @where_tokens : ''; + my $query = "DELETE FROM record_links $where"; - do_query($form, $dbh, $query, @where_values); + do_query($form, $dbh, $query, @where_values); - $dbh->commit() unless ($params{dbh}); + 1; + }) or die { SL::DB->client->error }; $main::lxdebug->leave_sub(); } 1; + +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::RecordLinks - Verlinkung von kivitendo Objekten. + +=head1 SYNOPSIS + + use SL::RecordLinks; + + my @links = RecordLinks->get_links( + from_table => 'ar', + from_id => 2, + to_table => 'oe', + ); + my @links = RecordLinks->get_links_via( + from_table => 'oe', + to_id => '14', + via => [ + { id => 12 }, + { id => 13}, + ], + ); + + RecordLinks->create_links( + mode => 'ids', + from_table => 'ar', + from_id => 1, + to_table => 'oe', + to_ids => [4, 6, 9], + ) + RecordLinks->create_links(@links); + + delete + +=head1 DESCRIPTION + +Transitive RecordLinks mit get_links_via. + +get_links_via erwartet den zusätzlichen parameter via. via ist ein +hashref mit den jeweils optionalen Einträgen table und id, die sich +genauso verhalten wie die from/to_table/id werte der get_links funktion. + +Alternativ kann via auch ein Array dieser Hashes sein: + + get_links_via( + from_table => 'oe', + from_id => 1, + to_table => 'ar', + via => { + table => 'delivery_orders' + }, + ) + + get_links_via( + from_table => 'oe', + to_id => '14', + via => [ + { id => 12 }, + { id => 13}, + ], + ) + +Die Einträge in einem via-Array werden exakt in dieser Reihenfolge +benutzt und sind nicht optional. Da obige Beispiel würde also die +Verknüpfung: + + oe:11 -> ar:12 -> is:13 -> do:14 + +finden, nicht aber: + + oe:11 -> ar:13 -> do:14 + +=cut