6c6865cc3da9e0053a4a66c42e6b210a0ba39a5a
[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 require "bin/mozilla/common.pl";
42
43 use strict;
44
45 1;
46
47 # end of main
48
49 my $locale   = $main::locale;
50 our %translations = ('text'      => $locale->text('Free-form text'),
51                      'textfield' => $locale->text('Text field'),
52                      'number'    => $locale->text('Number'),
53                      'date'      => $locale->text('Date'),
54                      'timestamp' => $locale->text('Timestamp'),
55                      'bool'      => $locale->text('Yes/No (Checkbox)'),
56                      'select'    => $locale->text('Selection'),
57                      );
58
59 our @types = qw(text textfield number date bool select); # timestamp
60
61 our @modules = ({ module => 'CT',       description => $locale->text('Customers and vendors')          },
62                 { module => 'IC',       description => $locale->text('Parts, services and assemblies') },
63                 { module => 'Projects', description => $locale->text('Projects')                       },
64                );
65
66 sub add {
67   add_cvar_config();
68 }
69
70 sub edit {
71   edit_cvar_config();
72 }
73
74 sub _is_valid_module {
75   my $module = shift;
76
77   return any { $_->{module} eq $module } @modules;
78 }
79
80 sub list_cvar_configs {
81   $main::lxdebug->enter_sub();
82
83   my $form     = $main::form;
84   my $locale   = $main::locale;
85
86   $main::auth->assert('config');
87
88   $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
89   $form->{module} = 'CT' unless _is_valid_module($form->{module});
90
91   my @configs = @{ CVar->get_configs(module => $form->{module}) };
92
93   my $previous_config;
94
95   foreach my $config (@configs) {
96     $config->{type_tr} = $translations{$config->{type}};
97
98     if ($previous_config) {
99       $previous_config->{next_id} = $config->{id};
100       $config->{previous_id}      = $previous_config->{id};
101     }
102
103     $previous_config = $config;
104   }
105
106   $form->{title} = $locale->text('List of custom variables');
107   $form->header();
108   print $form->parse_html_template('amcvar/list_cvar_configs', { CONFIGS => \@configs,
109                                                                  MODULES => \@modules });
110
111   $main::lxdebug->dump(0, "modules", \@modules);
112
113   $main::lxdebug->leave_sub();
114 }
115
116 sub add_cvar_config {
117   $main::lxdebug->enter_sub();
118
119   my $form     = $main::form;
120
121   $main::auth->assert('config');
122
123   $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
124
125   $form->{edit} = 0;
126   display_cvar_config_form();
127
128   $main::lxdebug->leave_sub();
129 }
130
131 sub edit_cvar_config {
132   $main::lxdebug->enter_sub();
133
134   my $form     = $main::form;
135
136   $main::auth->assert('config');
137
138   my $config = CVar->get_config('id' => $form->{id});
139
140   map { $form->{$_} = $config->{$_} } keys %{ $config };
141
142   $form->{edit} = 1;
143   display_cvar_config_form();
144
145   $main::lxdebug->leave_sub();
146 }
147
148 sub save {
149   $main::lxdebug->enter_sub();
150
151   my $form     = $main::form;
152   my %myconfig = %main::myconfig;
153   my $locale   = $main::locale;
154
155   $main::auth->assert('config');
156
157   $form->isblank('name',        $locale->text('The name is missing.'));
158   $form->isblank('description', $locale->text('The description is missing.'));
159   $form->isblank('options',     $locale->text('The option field is empty.')) if ($form->{type} eq 'select');
160
161   if ($form->{name} !~ /^[a-z][a-z0-9_]*$/i) {
162     $form->error($locale->text('The name must only consist of letters, numbers and underscores and start with a letter.'));
163   }
164
165   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
166     $form->{default_value} = $form->parse_amount(\%myconfig, $form->{default_value});
167   }
168
169   $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
170   $form->{includeable}         = $form->{inclusion} ne 'no';
171   $form->{flags}               = join ':', map { m/^flag_(.*)/; "${1}=" . $form->{$_} } grep { m/^flag_/ } keys %{ $form };
172
173   CVar->save_config('module' => $form->{module},
174                     'config' => $form);
175
176   $form->{MESSAGE} = $locale->text('The custom variable has been saved.');
177
178   list_cvar_configs();
179
180   $main::lxdebug->leave_sub();
181 }
182
183 sub delete {
184   $main::lxdebug->enter_sub();
185
186   my $form     = $main::form;
187   my $locale   = $main::locale;
188
189   CVar->delete_config('id' => $form->{id});
190
191   $form->{MESSAGE} = $locale->text('The custom variable has been deleted.');
192
193   list_cvar_configs();
194
195   $main::lxdebug->leave_sub();
196 }
197
198 sub display_cvar_config_form {
199   $main::lxdebug->enter_sub();
200
201   my $form     = $main::form;
202   my %myconfig = %main::myconfig;
203   my $locale   = $main::locale;
204
205   $main::auth->assert('config');
206
207   my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
208
209   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
210     $form->{default_value} = $form->format_amount(\%myconfig, $form->{default_value});
211   }
212
213   $form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
214
215   $form->header();
216   print $form->parse_html_template("amcvar/display_cvar_config_form", { TYPES   => \@types,
217                                                                         MODULES => \@modules });
218
219   $main::lxdebug->leave_sub();
220 }
221
222 sub swap_cvar_configs {
223   $main::lxdebug->enter_sub();
224
225   my $form     = $main::form;
226   my %myconfig = %main::myconfig;
227
228   AM->swap_sortkeys(\%myconfig, $form, 'custom_variable_configs');
229
230   list_cvar_configs();
231
232   $main::lxdebug->leave_sub();
233 }
234
235 sub dispatcher {
236   my $form     = $main::form;
237   my $locale   = $main::locale;
238
239   foreach my $action (qw(list_cvar_configs add_cvar_config)) {
240     if ($form->{"action_${action}"}) {
241       call_sub($action);
242       return;
243     }
244   }
245
246   $form->error($locale->text('No action defined.'));
247 }
248
249 1;