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