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