Revision 70fedc9f
Von Tamino Steinert vor mehr als 1 Jahr hinzugefügt
SL/DB/Helper/HardRecordLinks.pm | ||
---|---|---|
1 |
package SL::DB::Helper::HardRecordLinks; |
|
2 |
|
|
3 |
use strict; |
|
4 |
use warnings; |
|
5 |
|
|
6 |
use List::Util qw(first any); |
|
7 |
|
|
8 |
use SL::DB::HardRecordLinksEmailJournalAndAP; |
|
9 |
use SL::DB::HardRecordLinksEmailJournalAndOrder; |
|
10 |
|
|
11 |
my %RECORD_MODULE_TO_KEY = ( |
|
12 |
'SL::DB::Order' => 'order_id', |
|
13 |
'SL::DB::PurchaseInvoice' => 'ap_id', |
|
14 |
'SL::DB::EmailJournal' => 'email_journal_id', |
|
15 |
); |
|
16 |
my %RECORD_MODULE_TO_HARD_LINK_MODULE_NAMES = ( |
|
17 |
'SL::DB::Order' => [ |
|
18 |
'HardRecordLinksEmailJournalAndOrder', |
|
19 |
], |
|
20 |
'SL::DB::PurchaseInvoice' => [ |
|
21 |
'HardRecordLinksEmailJournalAndAP', |
|
22 |
], |
|
23 |
'SL::DB::EmailJournal' => [ |
|
24 |
'HardRecordLinksEmailJournalAndOrder', |
|
25 |
'HardRecordLinksEmailJournalAndAP', |
|
26 |
], |
|
27 |
); |
|
28 |
|
|
29 |
sub get_links { |
|
30 |
my ($self, $record) = @_; |
|
31 |
|
|
32 |
my @ret_hard_links = (); |
|
33 |
foreach my $hard_link_modul (@{ $RECORD_MODULE_TO_HARD_LINK_MODULE_NAMES{ref $record} }) { |
|
34 |
my $hard_links = "SL::DB::Manager::$hard_link_modul"->get_all( |
|
35 |
query => [ |
|
36 |
$RECORD_MODULE_TO_KEY{ref $record} => $record->id |
|
37 |
]); |
|
38 |
push @ret_hard_links, @$hard_links; |
|
39 |
} |
|
40 |
|
|
41 |
return @ret_hard_links; |
|
42 |
} |
|
43 |
|
|
44 |
sub get_count { |
|
45 |
my ($self, $record) = @_; |
|
46 |
my @hard_links = $self->get_links($record); |
|
47 |
return scalar @hard_links; |
|
48 |
} |
|
49 |
|
|
50 |
sub create_link { |
|
51 |
my ($self, $record_1, $record_2) = @_; |
|
52 |
my $module_1 = ref $record_1; |
|
53 |
my $module_2 = ref $record_2; |
|
54 |
my $hard_link_moduls_1 = $RECORD_MODULE_TO_HARD_LINK_MODULE_NAMES{ref $record_1}; |
|
55 |
my $hard_link_moduls_2 = $RECORD_MODULE_TO_HARD_LINK_MODULE_NAMES{ref $record_2}; |
|
56 |
|
|
57 |
my $hard_link_modul = first { |
|
58 |
my $a = $_; any {$a eq $_} @$hard_link_moduls_1 |
|
59 |
} @$hard_link_moduls_2; |
|
60 |
die "No hard link modul found for $module_1 and $module_2" unless $hard_link_modul; |
|
61 |
|
|
62 |
my $hard_link = "SL::DB::$hard_link_modul"->new( |
|
63 |
$RECORD_MODULE_TO_KEY{$module_1} => $record_1->id, |
|
64 |
$RECORD_MODULE_TO_KEY{$module_2} => $record_2->id, |
|
65 |
)->save; |
|
66 |
} |
|
67 |
|
|
68 |
1; |
Auch abrufbar als: Unified diff
HardRecordLinks: Helfer Modul für feste Verknüpfungen