Merge branch 'test' of ../kivitendo-erp_20220811
[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   $::request->{layout}->use_javascript(map { "${_}.js" } qw(ckeditor/ckeditor ckeditor/adapters/jquery));
182   $form->header();
183   print $form->parse_html_template('generictranslations/edit_email_strings',{ 'MAIL_STRINGS' => \%mail_strings });
184
185   $main::lxdebug->leave_sub();
186 }
187
188 sub save_email_strings {
189   $main::lxdebug->enter_sub();
190
191   $main::auth->assert('config');
192
193   my $form     = $main::form;
194   my $locale   = $main::locale;
195
196   $form->get_lists('languages' => 'LANGUAGES');
197
198   unshift @{ $form->{LANGUAGES} }, { };
199   foreach my $language (@{ $form->{LANGUAGES} }) {
200     foreach (keys %mail_strings)  {
201       GenericTranslations->save('translation_type' => $_,
202                                 'translation_id'   => undef,
203                                 'language_id'      => $language->{id},
204                                 'translation'      => $form->{"translation__" . ($language->{id} || 'default') . "__" . $_},
205                                );
206     }
207   }
208   $form->{message} = $locale->text('The Mail strings have been saved.');
209
210   edit_email_strings();
211
212   $main::lxdebug->leave_sub();
213 }
214
215 sub edit_zugferd_notes {
216   $::auth->assert('config');
217
218   $::form->get_lists('languages' => 'LANGUAGES');
219
220   my $translation_list = GenericTranslations->list(translation_type => 'ZUGFeRD/notes');
221   my %translations     = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
222
223   unshift @{ $::form->{LANGUAGES} }, { 'id' => 'default', };
224
225   foreach my $language (@{ $::form->{LANGUAGES} }) {
226     $language->{translation} = $translations{$language->{id}};
227   }
228
229   setup_generictranslations_edit_zugferd_notes_action_bar();
230
231   $::form->{title} = $::locale->text('Edit Factur-X/ZUGFeRD notes');
232   $::form->header;
233   print $::form->parse_html_template('generictranslations/edit_zugferd_notes');
234 }
235
236 sub save_zugferd_notes {
237   $::auth->assert('config');
238
239   $::form->get_lists('languages' => 'LANGUAGES');
240
241   unshift @{ $::form->{LANGUAGES} }, { };
242
243   foreach my $language (@{ $::form->{LANGUAGES} }) {
244     GenericTranslations->save(
245       translation_type => 'ZUGFeRD/notes',
246       translation_id   => undef,
247       language_id      => $language->{id},
248       translation      => $::form->{"translation__" . ($language->{id} || 'default')},
249     );
250   }
251
252   $::form->{message} = $::locale->text('The Factur-X/ZUGFeRD notes have been saved.');
253
254   edit_zugferd_notes();
255 }
256
257 sub setup_generictranslations_edit_greetings_action_bar {
258   my %params = @_;
259
260   for my $bar ($::request->layout->get('actionbar')) {
261     $bar->add(
262       action => [
263         t8('Save'),
264         submit    => [ '#form', { action => "save_greetings" } ],
265         accesskey => 'enter',
266       ],
267     );
268   }
269 }
270
271 sub setup_generictranslations_edit_sepa_strings_action_bar {
272   my %params = @_;
273
274   for my $bar ($::request->layout->get('actionbar')) {
275     $bar->add(
276       action => [
277         t8('Save'),
278         submit    => [ '#form', { action => "save_sepa_strings" } ],
279         accesskey => 'enter',
280       ],
281     );
282   }
283 }
284
285 sub setup_generictranslations_edit_email_strings_action_bar {
286   my %params = @_;
287
288   for my $bar ($::request->layout->get('actionbar')) {
289     $bar->add(
290       action => [
291         t8('Save'),
292         submit    => [ '#form', { action => "save_email_strings" } ],
293         accesskey => 'enter',
294       ],
295     );
296   }
297 }
298
299 sub setup_generictranslations_edit_zugferd_notes_action_bar {
300   my %params = @_;
301
302   for my $bar ($::request->layout->get('actionbar')) {
303     $bar->add(
304       action => [
305         t8('Save'),
306         submit    => [ '#form', { action => "save_zugferd_notes" } ],
307         accesskey => 'enter',
308       ],
309     );
310   }
311 }
312
313 1;