0261ef1b1b7ef0c1c6f53962a989763ac0278571
[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_request_quotation               => t8('Preset email text for requests (rfq)'),
22   preset_text_purchase_order                  => t8('Preset email text for purchase orders'),
23   preset_text_periodic_invoices_email_body    => t8('Preset email body for periodic invoices'),
24   preset_text_periodic_invoices_email_subject => t8('Preset email subject for periodic invoices'),
25 );
26
27 sub edit_greetings {
28   $main::lxdebug->enter_sub();
29
30   $main::auth->assert('config');
31
32   my $form     = $main::form;
33   my $locale   = $main::locale;
34
35   $form->get_lists('languages' => 'LANGUAGES');
36
37   my $translation_list = GenericTranslations->list();
38   my %translations     = ();
39
40   my @types            = qw(male female);
41
42   foreach my $translation (@{ $translation_list }) {
43     $translation->{language_id}                                                          ||= 'default';
44     $translations{$translation->{language_id} . '::' . $translation->{translation_type}}   = $translation;
45   }
46
47   unshift @{ $form->{LANGUAGES} }, { 'id' => 'default', };
48
49   foreach my $language (@{ $form->{LANGUAGES} }) {
50     foreach my $type (@types) {
51       $language->{$type} = { };
52       my $translation    = $translations{"$language->{id}::greetings::${type}"} || { };
53       $language->{$type} = $translation->{translation};
54     }
55   }
56
57   setup_generictranslations_edit_greetings_action_bar();
58
59   $form->{title} = $locale->text('Edit greetings');
60   $form->header();
61   print $form->parse_html_template('generictranslations/edit_greetings');
62
63   $main::lxdebug->leave_sub();
64 }
65
66 sub save_greetings {
67   $main::lxdebug->enter_sub();
68
69   $main::auth->assert('config');
70
71   my $form     = $main::form;
72   my $locale   = $main::locale;
73
74   $form->get_lists('languages' => 'LANGUAGES');
75
76   unshift @{ $form->{LANGUAGES} }, { };
77
78   my @types  = qw(male female);
79
80   foreach my $language (@{ $form->{LANGUAGES} }) {
81     foreach my $type (@types) {
82       GenericTranslations->save('translation_type' => "greetings::${type}",
83                                 'translation_id'   => undef,
84                                 'language_id'      => $language->{id},
85                                 'translation'      => $form->{"translation__" . ($language->{id} || 'default') . "__${type}"},);
86     }
87   }
88
89   $form->{message} = $locale->text('The greetings have been saved.');
90
91   edit_greetings();
92
93   $main::lxdebug->leave_sub();
94 }
95
96 sub edit_sepa_strings {
97   $main::lxdebug->enter_sub();
98
99   $main::auth->assert('config');
100
101   my $form     = $main::form;
102   my $locale   = $main::locale;
103
104   $form->get_lists('languages' => 'LANGUAGES');
105
106   my $translation_list = GenericTranslations->list(translation_type => 'sepa_remittance_info_pfx');
107   my %translations     = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
108
109   my $translation_list_vc = GenericTranslations->list(translation_type => 'sepa_remittance_vc_no_pfx');
110   my %translations_vc     =  map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list_vc };
111
112   unshift @{ $form->{LANGUAGES} }, { 'id' => 'default', };
113
114   foreach my $language (@{ $form->{LANGUAGES} }) {
115     $language->{translation}    = $translations{$language->{id}};
116     $language->{translation_vc} = $translations_vc{$language->{id}};
117   }
118
119   setup_generictranslations_edit_sepa_strings_action_bar();
120
121   $form->{title} = $locale->text('Edit SEPA strings');
122   $form->header();
123   print $form->parse_html_template('generictranslations/edit_sepa_strings');
124
125   $main::lxdebug->leave_sub();
126 }
127
128 sub save_sepa_strings {
129   $main::lxdebug->enter_sub();
130
131   $main::auth->assert('config');
132
133   my $form     = $main::form;
134   my $locale   = $main::locale;
135
136   $form->get_lists('languages' => 'LANGUAGES');
137
138   unshift @{ $form->{LANGUAGES} }, { };
139
140   foreach my $language (@{ $form->{LANGUAGES} }) {
141     GenericTranslations->save('translation_type' => 'sepa_remittance_info_pfx',
142                               'translation_id'   => undef,
143                               'language_id'      => $language->{id},
144                               'translation'      => $form->{"translation__" . ($language->{id} || 'default')},);
145     GenericTranslations->save('translation_type' => 'sepa_remittance_vc_no_pfx',
146                               'translation_id'   => undef,
147                               'language_id'      => $language->{id},
148                               'translation'      => $form->{"translation__" . ($language->{id} || 'default') . "__vc" },);
149   }
150
151   $form->{message} = $locale->text('The SEPA strings have been saved.');
152
153   edit_sepa_strings();
154
155   $main::lxdebug->leave_sub();
156 }
157 sub edit_email_strings {
158   $main::lxdebug->enter_sub();
159
160   $main::auth->assert('config');
161
162   my $form     = $main::form;
163   my $locale   = $main::locale;
164
165   $form->get_lists('languages' => 'LANGUAGES');
166   unshift @{ $form->{LANGUAGES} }, { 'id' => 'default', };
167
168   my (%translations, $translation_list);
169   foreach (keys %mail_strings)  {
170     $translation_list = GenericTranslations->list(translation_type => $_);
171     %translations     = map { ( ($_->{language_id} || 'default') => $_->{translation} ) } @{ $translation_list };
172
173     foreach my $language (@{ $form->{LANGUAGES} }) {
174       $language->{$_} = $translations{$language->{id}};
175     }
176   }
177   setup_generictranslations_edit_email_strings_action_bar();
178
179   $form->{title} = $locale->text('Edit preset email strings');
180   $form->header();
181   print $form->parse_html_template('generictranslations/edit_email_strings',{ 'MAIL_STRINGS' => \%mail_strings });
182
183   $main::lxdebug->leave_sub();
184 }
185
186 sub save_email_strings {
187   $main::lxdebug->enter_sub();
188
189   $main::auth->assert('config');
190
191   my $form     = $main::form;
192   my $locale   = $main::locale;
193
194   $form->get_lists('languages' => 'LANGUAGES');
195
196   unshift @{ $form->{LANGUAGES} }, { };
197   foreach my $language (@{ $form->{LANGUAGES} }) {
198     foreach (keys %mail_strings)  {
199       GenericTranslations->save('translation_type' => $_,
200                                 'translation_id'   => undef,
201                                 'language_id'      => $language->{id},
202                                 'translation'      => $form->{"translation__" . ($language->{id} || 'default') . "__" . $_},
203                                );
204     }
205   }
206   $form->{message} = $locale->text('The Mail strings have been saved.');
207
208   edit_email_strings();
209
210   $main::lxdebug->leave_sub();
211 }
212
213 sub setup_generictranslations_edit_greetings_action_bar {
214   my %params = @_;
215
216   for my $bar ($::request->layout->get('actionbar')) {
217     $bar->add(
218       action => [
219         t8('Save'),
220         submit    => [ '#form', { action => "save_greetings" } ],
221         accesskey => 'enter',
222       ],
223     );
224   }
225 }
226
227 sub setup_generictranslations_edit_sepa_strings_action_bar {
228   my %params = @_;
229
230   for my $bar ($::request->layout->get('actionbar')) {
231     $bar->add(
232       action => [
233         t8('Save'),
234         submit    => [ '#form', { action => "save_sepa_strings" } ],
235         accesskey => 'enter',
236       ],
237     );
238   }
239 }
240 sub setup_generictranslations_edit_email_strings_action_bar {
241   my %params = @_;
242
243   for my $bar ($::request->layout->get('actionbar')) {
244     $bar->add(
245       action => [
246         t8('Save'),
247         submit    => [ '#form', { action => "save_email_strings" } ],
248         accesskey => 'enter',
249       ],
250     );
251   }
252 }
253
254 1;