Sprache "American English" nicht mehr zur Verfügung stellen.
[kivitendo-erp.git] / bin / mozilla / amcvar.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # administration
31 #
32 #======================================================================
33
34 use SL::AM;
35 use SL::CVar;
36 use SL::Form;
37
38 use Data::Dumper;
39 use List::MoreUtils qw(any);
40
41 1;
42
43 require "bin/mozilla/common.pl";
44
45 # end of main
46
47 our %translations = ('text'      => $locale->text('Free-form text'),
48                      'textfield' => $locale->text('Text field'),
49                      'number'    => $locale->text('Number'),
50                      'date'      => $locale->text('Date'),
51                      'timestamp' => $locale->text('Timestamp'),
52                      'bool'      => $locale->text('Yes/No (Checkbox)'),
53                      'select'    => $locale->text('Selection'),
54                      );
55
56 our @types = qw(text textfield number date bool select); # timestamp
57
58 our @modules = ({ module => 'CT',       description => $locale->text('Customers and vendors')          },
59                 { module => 'IC',       description => $locale->text('Parts, services and assemblies') },
60                 { module => 'Projects', description => $locale->text('Projects')                       },
61                );
62
63 sub add {
64   add_cvar_config();
65 }
66
67 sub edit {
68   edit_cvar_config();
69 }
70
71 sub _is_valid_module {
72   my $module = shift;
73
74   return any { $_->{module} eq $module } @modules;
75 }
76
77 sub list_cvar_configs {
78   $lxdebug->enter_sub();
79
80   $auth->assert('config');
81
82   $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
83   $form->{module} = 'CT' unless _is_valid_module($form->{module});
84
85   my @configs = @{ CVar->get_configs(module => $form->{module}) };
86
87   my $previous_config;
88
89   foreach my $config (@configs) {
90     $config->{type_tr} = $translations{$config->{type}};
91
92     if ($previous_config) {
93       $previous_config->{next_id} = $config->{id};
94       $config->{previous_id}      = $previous_config->{id};
95     }
96
97     $previous_config = $config;
98   }
99
100   $form->{title} = $locale->text('List of custom variables');
101   $form->header();
102   print $form->parse_html_template('amcvar/list_cvar_configs', { CONFIGS => \@configs,
103                                                                  MODULES => \@modules });
104
105   $main::lxdebug->dump(0, "modules", \@modules);
106
107   $lxdebug->leave_sub();
108 }
109
110 sub add_cvar_config {
111   $lxdebug->enter_sub();
112
113   $auth->assert('config');
114
115   $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
116
117   $form->{edit} = 0;
118   display_cvar_config_form();
119
120   $lxdebug->leave_sub();
121 }
122
123 sub edit_cvar_config {
124   $lxdebug->enter_sub();
125
126   $auth->assert('config');
127
128   my $config = CVar->get_config('id' => $form->{id});
129
130   map { $form->{$_} = $config->{$_} } keys %{ $config };
131
132   $form->{edit} = 1;
133   display_cvar_config_form();
134
135   $lxdebug->leave_sub();
136 }
137
138 sub save {
139   $lxdebug->enter_sub();
140
141   $auth->assert('config');
142
143   $form->isblank('name',        $locale->text('The name is missing.'));
144   $form->isblank('description', $locale->text('The description is missing.'));
145   $form->isblank('options',     $locale->text('The option field is empty.')) if ($form->{type} eq 'select');
146
147   if ($form->{name} !~ /^[a-z][a-z0-9_]*$/i) {
148     $form->error($locale->text('The name must only consist of letters, numbers and underscores and start with a letter.'));
149   }
150
151   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
152     $form->{default_value} = $form->parse_amount(\%myconfig, $form->{default_value});
153   }
154
155   $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
156   $form->{includeable}         = $form->{inclusion} ne 'no';
157   $form->{flags}               = join ':', map { m/^flag_(.*)/; "${1}=" . $form->{$_} } grep { m/^flag_/ } keys %{ $form };
158
159   CVar->save_config('module' => $form->{module},
160                     'config' => $form);
161
162   $form->{MESSAGE} = $locale->text('The custom variable has been saved.');
163
164   list_cvar_configs();
165
166   $lxdebug->leave_sub();
167 }
168
169 sub delete {
170   $lxdebug->enter_sub();
171
172   CVar->delete_config('id' => $form->{id});
173
174   $form->{MESSAGE} = $locale->text('The custom variable has been deleted.');
175
176   list_cvar_configs();
177
178   $lxdebug->leave_sub();
179 }
180
181 sub display_cvar_config_form {
182   $lxdebug->enter_sub();
183
184   $auth->assert('config');
185
186   my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
187
188   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
189     $form->{default_value} = $form->format_amount(\%myconfig, $form->{default_value});
190   }
191
192   $form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
193
194   $form->header();
195   print $form->parse_html_template("amcvar/display_cvar_config_form", { TYPES   => \@types,
196                                                                         MODULES => \@modules });
197
198   $lxdebug->leave_sub();
199 }
200
201 sub swap_cvar_configs {
202   $lxdebug->enter_sub();
203
204   AM->swap_sortkeys(\%myconfig, $form, 'custom_variable_configs');
205
206   list_cvar_configs();
207
208   $lxdebug->leave_sub();
209 }
210
211 sub dispatcher {
212   foreach my $action (qw(list_cvar_configs add_cvar_config)) {
213     if ($form->{"action_${action}"}) {
214       call_sub($action);
215       return;
216     }
217   }
218
219   $form->error($locale->text('No action defined.'));
220 }
221
222 1;