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