|
1 |
package GenericTranslations;
|
|
2 |
|
|
3 |
use SL::DBUtils;
|
|
4 |
|
|
5 |
sub get {
|
|
6 |
$main::lxdebug->enter_sub();
|
|
7 |
|
|
8 |
my $self = shift;
|
|
9 |
my %params = @_;
|
|
10 |
|
|
11 |
Common::check_params(\%params, qw(translation_type));
|
|
12 |
|
|
13 |
my $myconfig = \%main::myconfig;
|
|
14 |
my $form = $main::form;
|
|
15 |
|
|
16 |
my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
|
|
17 |
|
|
18 |
my $joins = '';
|
|
19 |
|
|
20 |
my @values = ($params{translation_type});
|
|
21 |
my @where = ('gt.translation_type = ?');
|
|
22 |
|
|
23 |
if ($params{translation_id}) {
|
|
24 |
push @values, conv_i($params{translation_id});
|
|
25 |
push @where, 'gt.translation_id = ?';
|
|
26 |
|
|
27 |
} else {
|
|
28 |
push @where, 'gt.translation_id IS NULL';
|
|
29 |
}
|
|
30 |
|
|
31 |
if ($params{language_id}) {
|
|
32 |
push @values, conv_i($params{language_id});
|
|
33 |
push @where, $params{allow_fallback} ? '(gt.language_id IS NULL) OR (gt.language_id = ?)' : 'gt.language_id = ?';
|
|
34 |
|
|
35 |
} else {
|
|
36 |
push @where, 'gt.language_id IS NULL';
|
|
37 |
}
|
|
38 |
|
|
39 |
my $query = qq|SELECT gt.translation
|
|
40 |
FROM generic_translations gt
|
|
41 |
$joins
|
|
42 |
WHERE | . join(' AND ', map { "($_)" } @where) . qq|
|
|
43 |
ORDER BY gt.language_id ASC|;
|
|
44 |
|
|
45 |
my ($translation) = selectfirst_array_query($form, $dbh, $query, @values);
|
|
46 |
$translation ||= $params{default};
|
|
47 |
|
|
48 |
$main::lxdebug->leave_sub();
|
|
49 |
|
|
50 |
return $translation;
|
|
51 |
}
|
|
52 |
|
|
53 |
sub list {
|
|
54 |
$main::lxdebug->enter_sub();
|
|
55 |
|
|
56 |
my $self = shift;
|
|
57 |
my %params = @_;
|
|
58 |
|
|
59 |
my $myconfig = \%main::myconfig;
|
|
60 |
my $form = $main::form;
|
|
61 |
|
|
62 |
my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
|
|
63 |
|
|
64 |
my @values = ();
|
|
65 |
my @where = ();
|
|
66 |
|
|
67 |
if ($params{translation_type}) {
|
|
68 |
push @values, $params{translation_type};
|
|
69 |
push @where, 'translation_type = ?';
|
|
70 |
}
|
|
71 |
|
|
72 |
if ($params{translation_id}) {
|
|
73 |
push @values, conv_i($params{translation_id});
|
|
74 |
push @where, 'translation_id = ?';
|
|
75 |
}
|
|
76 |
|
|
77 |
my $where_s = scalar(@where) ? 'WHERE ' . join(' AND ', map { "($_)" } @where) : '';
|
|
78 |
|
|
79 |
my $query = qq|SELECT id, language_id, translation_type, translation_id, translation
|
|
80 |
FROM generic_translations
|
|
81 |
$where_s|;
|
|
82 |
|
|
83 |
my $results = selectall_hashref_query($form, $dbh, $query, @values);
|
|
84 |
|
|
85 |
$main::lxdebug->leave_sub();
|
|
86 |
|
|
87 |
return $results;
|
|
88 |
}
|
|
89 |
|
|
90 |
sub save {
|
|
91 |
$main::lxdebug->enter_sub();
|
|
92 |
|
|
93 |
my $self = shift;
|
|
94 |
my %params = @_;
|
|
95 |
|
|
96 |
Common::check_params(\%params, qw(translation_type));
|
|
97 |
|
|
98 |
my $myconfig = \%main::myconfig;
|
|
99 |
my $form = $main::form;
|
|
100 |
|
|
101 |
my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
|
|
102 |
|
|
103 |
$params{translation} =~ s/^\s+//;
|
|
104 |
$params{translation} =~ s/\s+$//;
|
|
105 |
|
|
106 |
my @v_insert = (conv_i($params{language_id}), $params{translation_type}, conv_i($params{translation_id}), $params{translation});
|
|
107 |
my @v_seldel = ($params{translation_type});
|
|
108 |
my @w_seldel = ('translation_type = ?');
|
|
109 |
|
|
110 |
foreach (qw(language_id translation_id)) {
|
|
111 |
if ($params{$_}) {
|
|
112 |
push @v_seldel, conv_i($params{$_});
|
|
113 |
push @w_seldel, "$_ = ?";
|
|
114 |
} else {
|
|
115 |
push @w_seldel, "$_ IS NULL";
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
my $q_lookup = qq|SELECT id
|
|
120 |
FROM generic_translations
|
|
121 |
WHERE | . join(' AND ', map { "($_)" } @w_seldel);
|
|
122 |
my $q_delete = qq|DELETE FROM generic_translations
|
|
123 |
WHERE | . join(' AND ', map { "($_)" } @w_seldel);
|
|
124 |
my $q_update = qq|UPDATE generic_translations
|
|
125 |
SET translation = ?
|
|
126 |
WHERE id = ?|;
|
|
127 |
my $q_insert = qq|INSERT INTO generic_translations (language_id, translation_type, translation_id, translation)
|
|
128 |
VALUES (?, ?, ?, ?)|;
|
|
129 |
|
|
130 |
my ($id) = selectfirst_array_query($form, $dbh, $q_lookup, @v_seldel);
|
|
131 |
|
|
132 |
if ($id && !$params{translation}) {
|
|
133 |
do_query($form, $dbh, $q_delete, @v_seldel);
|
|
134 |
} elsif ($id) {
|
|
135 |
do_query($form, $dbh, $q_update, $params{translation}, $id);
|
|
136 |
} elsif ($params{translation}) {
|
|
137 |
do_query($form, $dbh, $q_insert, @v_insert);
|
|
138 |
}
|
|
139 |
|
|
140 |
$dbh->commit() unless ($params{dbh});
|
|
141 |
|
|
142 |
$main::lxdebug->leave_sub();
|
|
143 |
}
|
|
144 |
|
|
145 |
|
|
146 |
1;
|
GenericTranslations.pm und debug-code entfernt