Das Definieren, Erstellen und Bearbeiten von benutzerdefinierten Variablen bei Waren...
[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
40 1;
41
42 require "bin/mozilla/common.pl";
43
44 # end of main
45
46 our %translations = ('text'      => $locale->text('Free-form text'),
47                      'textfield' => $locale->text('Text field'),
48                      'number'    => $locale->text('Number'),
49                      'date'      => $locale->text('Date'),
50                      'timestamp' => $locale->text('Timestamp'),
51                      'bool'      => $locale->text('Yes/No (Checkbox)'),
52                      'select'    => $locale->text('Selection'),
53                      );
54
55 our @types = qw(text textfield number date bool select); # timestamp
56
57 sub add {
58   add_cvar_config();
59 }
60
61 sub edit {
62   edit_cvar_config();
63 }
64
65 sub list_cvar_configs {
66   $lxdebug->enter_sub();
67
68   $auth->assert('config');
69
70   $form->{module} ||= $form->{cvar_module};
71
72   my @configs = grep { $_->{module} eq $form->{module} } @{ CVar->get_configs() };
73
74   my $previous_config;
75
76   foreach my $config (@configs) {
77     $config->{type_tr} = $translations{$config->{type}};
78
79     foreach my $flag (split m/:/, $config->{flags}) {
80       if ($flag =~ m/(.*?)=(.*)/) {
81         $config->{"flag_${1}"}    = $2;
82       } else {
83         $config->{"flag_${flag}"} = 1;
84       }
85     }
86
87     if ($previous_config) {
88       $previous_config->{next_id} = $config->{id};
89       $config->{previous_id}      = $previous_config->{id};
90     }
91
92     $previous_config = $config;
93   }
94
95   $form->{title} = $locale->text('List of custom variables');
96   $form->header();
97   print $form->parse_html_template('amcvar/list_cvar_configs', { 'CONFIGS' => \@configs });
98
99   $lxdebug->leave_sub();
100 }
101
102 sub add_cvar_config {
103   $lxdebug->enter_sub();
104
105   $auth->assert('config');
106
107   $form->{module} ||= $form->{cvar_module};
108
109   $form->{edit} = 0;
110   display_cvar_config_form();
111
112   $lxdebug->leave_sub();
113 }
114
115 sub edit_cvar_config {
116   $lxdebug->enter_sub();
117
118   $auth->assert('config');
119
120   my $config = CVar->get_config('id' => $form->{id});
121
122   map { $form->{$_} = $config->{$_} } keys %{ $config };
123
124   $form->{edit} = 1;
125   display_cvar_config_form();
126
127   $lxdebug->leave_sub();
128 }
129
130 sub save {
131   $lxdebug->enter_sub();
132
133   $auth->assert('config');
134
135   $form->isblank('name',        $locale->text('The name is missing.'));
136   $form->isblank('description', $locale->text('The description is missing.'));
137   $form->isblank('options',     $locale->text('The option field is empty.')) if ($form->{type} eq 'select');
138
139   if ($form->{name} !~ /^[a-z][a-z0-9_]*$/i) {
140     $form->error($locale->text('The name must only consist of letters, numbers and underscores and start with a letter.'));
141   }
142
143   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
144     $form->{default_value} = $form->parse_amount(\%myconfig, $form->{default_value});
145   }
146
147   $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
148   $form->{includeable}         = $form->{inclusion} ne 'no';
149   $form->{flags}               = join ':', map { m/^flag_(.*)/; "${1}=" . $form->{$_} } grep { m/^flag_/ } keys %{ $form };
150
151   CVar->save_config('module' => $form->{module},
152                     'config' => $form);
153
154   $form->{MESSAGE} = $locale->text('The custom variable has been saved.');
155
156   list_cvar_configs();
157
158   $lxdebug->leave_sub();
159 }
160
161 sub delete {
162   $lxdebug->enter_sub();
163
164   CVar->delete_config('id' => $form->{id});
165
166   $form->{MESSAGE} = $locale->text('The custom variable has been deleted.');
167
168   list_cvar_configs();
169
170   $lxdebug->leave_sub();
171 }
172
173 sub display_cvar_config_form {
174   $lxdebug->enter_sub();
175
176   $auth->assert('config');
177
178   my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
179
180   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
181     $form->{default_value} = $form->format_amount(\%myconfig, $form->{default_value});
182   }
183
184   $form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
185
186   $form->header();
187   print $form->parse_html_template("amcvar/display_cvar_config_form", { 'TYPES' => \@types });
188
189   $lxdebug->leave_sub();
190 }
191
192 sub swap_cvar_configs {
193   $lxdebug->enter_sub();
194
195   AM->swap_sortkeys(\%myconfig, $form, 'custom_variable_configs');
196
197   list_cvar_configs();
198
199   $lxdebug->leave_sub();
200 }
201
202 1;