4e6d263288d0a11f3b3e670926d0653ac23d1a50
[kivitendo-erp.git] / bin / mozilla / generictranslations.pl
1 use SL::Auth;
2 use SL::Form;
3 use SL::GenericTranslations;
4 use SL::Locale::String qw(t8);
5
6 use strict;
7
8 # convention:
9 # preset_text_$formname will generate a input textarea
10 # and will be preset in $form email dialog if the form name matches
11
12 my %mail_strings = (
13   salutation_male                             => t8('Salutation male'),
14   salutation_female                           => t8('Salutation female'),
15   salutation_general                          => t8('Salutation general'),
16   salutation_punctuation_mark                 => t8('Salutation punctuation mark'),
17   preset_text_sales_quotation                 => t8('Preset email text for sales quotations'),
18   preset_text_sales_order                     => t8('Preset email text for sales orders'),
19   preset_text_sales_delivery_order            => t8('Preset email text for sales delivery orders'),
20   preset_text_invoice                         => t8('Preset email text for sales invoices'),
21   preset_text_invoice_direct_debit            => t8('Preset email text for sales invoices with direct debit'),
22   preset_text_request_quotation               => t8('Preset email text for requests (rfq)'),
23   preset_text_purchase_order                  => t8('Preset email text for purchase orders'),
24   preset_text_periodic_invoices_email_body    => t8('Preset email body for periodic invoices'),
25   preset_text_periodic_invoices_email_subject => t8('Preset email subject for periodic invoices'),
26 );
27
28 sub edit_greetings {
29   $main::lxdebug->enter_sub();
30
31   $main::auth->assert('config');
32
33   my $form     = $main::form;
34   my $locale   = $main::locale;
35
36   $form->get_lists('languages' => 'LANGUAGES');
37
38   my $translation_list = GenericTranslations->list();
39   my %translations     = ();
40
41   my @types            = qw(male female);
42
43   foreach my $translation (@{ $translation_list }) {
44     $translation->{language_id}                                                          ||= 'default';
45     $translations{$translation->{language_id} . '::' . $translation->{translation_type}}   = $translation;
46   }
47
48   unshift @{ $form->{LANGUAGES} }, { 'id' => 'default', };
49
50   foreach my $language (@{ $form->{LANGUAGES} }) {
51     foreach my $type (@types) {
52       $language->{$type} = { };
53       my $translation    = $translations{"$language->{id}::greetings::${type}"} || { };
54       $language->{$type} = $translation->{translation};
55     }
56   }
57
58   setup_generictranslations_edit_greetings_action_bar();
59
60   $form->{title} = $locale->text('Edit greetings');
61   $form->header();
62   print $form->parse_html_template('generictranslations/edit_greetings');
63
64   $main::lxdebug->leave_sub();
65 }
66
67 sub save_greetings {
68   $main::lxdebug->enter_sub();
69
70   $main::auth->assert('config');
71
72   my $form     = $main::form;
73   my $locale   = $main::locale;
74
75   $form->get_lists('languages' => 'LANGUAGES');
76
77   unshift @{ $form->{LANGUAGES} }, { };
78
79   my @types  = qw(male female);
80
81   foreach my $language (@{ $form->{LANGUAGES} }) {
82     foreach my $type (@types) {
83       GenericTranslations->save('translation_type' => "greetings::${type}",
84                                 'translation_id'   => undef,
85                                 'language_id'      => $language->{id},
86                                 'translation'      => $form->{"translation__" . ($language->{id} || 'default') . "__${type}"},);
87     }
88   }
89
90   $form->{message} = $locale->text('The greetings have been saved.');
91
92   edit_greetings();
93
94   $main::lxdebug->leave_sub();
95 }
96
97 sub edit_sepa_strings {
98   $main::lxdebug->enter_sub();
99
100   $main::auth->assert('config');
101
102   my $form     = $main::form;
103   my $locale   = $main::locale;
104
105   $form->get_lists('languages' => 'LANGUAGES');
106
107   my $translation_list = GenericTranslations->list(translation_type => 'sepa_remittance_info_pfx');
108   my %translations     = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
109
110   my $translation_list_vc = GenericTranslations->list(translation_type => 'sepa_remittance_vc_no_pfx');
111   my %translations_vc     =  map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list_vc };
112
113   unshift @{ $form->{LANGUAGES} }, { 'id' => 'default', };
114
115   foreach my $language (@{ $form->{LANGUAGES} }) {
116     $language->{translation}    = $translations{$language->{id}};
117     $language->{translation_vc} = $translations_vc{$language->{id}};
118   }
119
120   setup_generictranslations_edit_sepa_strings_action_bar();
121
122   $form->{title} = $locale->text('Edit SEPA strings');
123   $form->header();
124   print $form->parse_html_template('generictranslations/edit_sepa_strings');
125
126   $main::lxdebug->leave_sub();
127 }
128
129 sub save_sepa_strings {
130   $main::lxdebug->enter_sub();
131
132   $main::auth->assert('config');
133
134   my $form     = $main::form;
135   my $locale   = $main::locale;
136
137   $form->get_lists('languages' => 'LANGUAGES');
138
139   unshift @{ $form->{LANGUAGES} }, { };
140
141   foreach my $language (@{ $form->{LANGUAGES} }) {
142     GenericTranslations->save('translation_type' => 'sepa_remittance_info_pfx',
143                               'translation_id'   => undef,
144                               'language_id'      => $language->{id},
145                               'translation'      => $form->{"translation__" . ($language->{id} || 'default')},);
146     GenericTranslations->save('translation_type' => 'sepa_remittance_vc_no_pfx',
147                               'translation_id'   => undef,
148                               'language_id'      => $language->{id},
149                               'translation'      => $form->{"translation__" . ($language->{id} || 'default') . "__vc" },);
150   }
151
152   $form->{message} = $locale->text('The SEPA strings have been saved.');
153
154   edit_sepa_strings();
155
156   $main::lxdebug->leave_sub();
157 }
158 sub edit_email_strings {
159   $main::lxdebug->enter_sub();
160
161   $main::auth->assert('config');
162
163   my $form     = $main::form;
164   my $locale   = $main::locale;
165
166   $form->get_lists('languages' => 'LANGUAGES');
167   unshift @{ $form->{LANGUAGES} }, { 'id' => 'default', };
168
169   my (%translations, $translation_list);
170   foreach (keys %mail_strings)  {
171     $translation_list = GenericTranslations->list(translation_type => $_);
172     %translations     = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
173
174     foreach my $language (@{ $form->{LANGUAGES} }) {
175       $language->{$_} = $translations{$language->{id}};
176     }
177   }
178   setup_generictranslations_edit_email_strings_action_bar();
179
180   $form->{title} = $locale->text('Edit preset email strings');
181   $form->header();
182   print $form->parse_html_template('generictranslations/edit_email_strings',{ 'MAIL_STRINGS' => \%mail_strings });
183
184   $main::lxdebug->leave_sub();
185 }
186
187 sub save_email_strings {
188   $main::lxdebug->enter_sub();
189
190   $main::auth->assert('config');
191
192   my $form     = $main::form;
193   my $locale   = $main::locale;
194
195   $form->get_lists('languages' => 'LANGUAGES');
196
197   unshift @{ $form->{LANGUAGES} }, { };
198   foreach my $language (@{ $form->{LANGUAGES} }) {
199     foreach (keys %mail_strings)  {
200       GenericTranslations->save('translation_type' => $_,
201                                 'translation_id'   => undef,
202                                 'language_id'      => $language->{id},
203                                 'translation'      => $form->{"translation__" . ($language->{id} || 'default') . "__" . $_},
204                                );
205     }
206   }
207   $form->{message} = $locale->text('The Mail strings have been saved.');
208
209   edit_email_strings();
210
211   $main::lxdebug->leave_sub();
212 }
213
214 sub edit_zugferd_notes {
215   $::auth->assert('config');
216
217   $::form->get_lists('languages' => 'LANGUAGES');
218
219   my $translation_list = GenericTranslations->list(translation_type => 'ZUGFeRD/notes');
220   my %translations     = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
221
222   unshift @{ $::form->{LANGUAGES} }, { 'id' => 'default', };
223
224   foreach my $language (@{ $::form->{LANGUAGES} }) {
225     $language->{translation} = $translations{$language->{id}};
226   }
227
228   setup_generictranslations_edit_zugferd_notes_action_bar();
229
230   $::form->{title} = $::locale->text('Edit Factur-X/ZUGFeRD notes');
231   $::form->header;
232   print $::form->parse_html_template('generictranslations/edit_zugferd_notes');
233 }
234
235 sub save_zugferd_notes {
236   $::auth->assert('config');
237
238   $::form->get_lists('languages' => 'LANGUAGES');
239
240   unshift @{ $::form->{LANGUAGES} }, { };
241
242   foreach my $language (@{ $::form->{LANGUAGES} }) {
243     GenericTranslations->save(
244       translation_type => 'ZUGFeRD/notes',
245       translation_id   => undef,
246       language_id      => $language->{id},
247       translation      => $::form->{"translation__" . ($language->{id} || 'default')},
248     );
249   }
250
251   $::form->{message} = $::locale->text('The Factur-X/ZUGFeRD notes have been saved.');
252
253   edit_zugferd_notes();
254 }
255
256 sub setup_generictranslations_edit_greetings_action_bar {
257   my %params = @_;
258
259   for my $bar ($::request->layout->get('actionbar')) {
260     $bar->add(
261       action => [
262         t8('Save'),
263         submit    => [ '#form', { action => "save_greetings" } ],
264         accesskey => 'enter',
265       ],
266     );
267   }
268 }
269
270 sub setup_generictranslations_edit_sepa_strings_action_bar {
271   my %params = @_;
272
273   for my $bar ($::request->layout->get('actionbar')) {
274     $bar->add(
275       action => [
276         t8('Save'),
277         submit    => [ '#form', { action => "save_sepa_strings" } ],
278         accesskey => 'enter',
279       ],
280     );
281   }
282 }
283
284 sub setup_generictranslations_edit_email_strings_action_bar {
285   my %params = @_;
286
287   for my $bar ($::request->layout->get('actionbar')) {
288     $bar->add(
289       action => [
290         t8('Save'),
291         submit    => [ '#form', { action => "save_email_strings" } ],
292         accesskey => 'enter',
293       ],
294     );
295   }
296 }
297
298 sub setup_generictranslations_edit_zugferd_notes_action_bar {
299   my %params = @_;
300
301   for my $bar ($::request->layout->get('actionbar')) {
302     $bar->add(
303       action => [
304         t8('Save'),
305         submit    => [ '#form', { action => "save_zugferd_notes" } ],
306         accesskey => 'enter',
307       ],
308     );
309   }
310 }
311
312 1;