Implementation des Features "Benutzerdefinierte Variablen für Kunden- und Lieferanten...
[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 (@configs) {
77     $_->{type_tr} = $translations{$_->{type}};
78
79     if ($previous_config) {
80       $previous_config->{next_id} = $_->{id};
81       $_->{previous_id}           = $previous_config->{id};
82     }
83
84     $previous_config = $_;
85   }
86
87   $form->{title} = $locale->text('List of custom variables');
88   $form->header();
89   print $form->parse_html_template('amcvar/list_cvar_configs', { 'CONFIGS' => \@configs });
90
91   $lxdebug->leave_sub();
92 }
93
94 sub add_cvar_config {
95   $lxdebug->enter_sub();
96
97   $auth->assert('config');
98
99   $form->{module} ||= $form->{cvar_module};
100
101   $form->{edit} = 0;
102   display_cvar_config_form();
103
104   $lxdebug->leave_sub();
105 }
106
107 sub edit_cvar_config {
108   $lxdebug->enter_sub();
109
110   $auth->assert('config');
111
112   my $config = CVar->get_config('id' => $form->{id});
113
114   map { $form->{$_} = $config->{$_} } keys %{ $config };
115
116   $form->{edit} = 1;
117   display_cvar_config_form();
118
119   $lxdebug->leave_sub();
120 }
121
122 sub save {
123   $lxdebug->enter_sub();
124
125   $auth->assert('config');
126
127   $form->isblank('name',        $locale->text('The name is missing.'));
128   $form->isblank('description', $locale->text('The description is missing.'));
129   $form->isblank('options',     $locale->text('The option field is empty.')) if ($form->{type} eq 'select');
130
131   if ($form->{name} !~ /^[a-z][a-z0-9_]*$/i) {
132     $form->error($locale->text('The name must only consist of letters, numbers and underscores and start with a letter.'));
133   }
134
135   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
136     $form->{default_value} = $form->parse_amount(\%myconfig, $form->{default_value});
137   }
138
139   $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
140   $form->{includeable}         = $form->{inclusion} ne 'no';
141
142   CVar->save_config('module' => $form->{module},
143                     'config' => $form);
144
145   $form->{MESSAGE} = $locale->text('The custom variable has been saved.');
146
147   list_cvar_configs();
148
149   $lxdebug->leave_sub();
150 }
151
152 sub delete {
153   $lxdebug->enter_sub();
154
155   CVar->delete_config('id' => $form->{id});
156
157   $form->{MESSAGE} = $locale->text('The custom variable has been deleted.');
158
159   list_cvar_configs();
160
161   $lxdebug->leave_sub();
162 }
163
164 sub display_cvar_config_form {
165   $lxdebug->enter_sub();
166
167   $auth->assert('config');
168
169   my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
170
171   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
172     $form->{default_value} = $form->format_amount(\%myconfig, $form->{default_value});
173   }
174
175   $form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
176
177   $form->header();
178   print $form->parse_html_template("amcvar/display_cvar_config_form", { 'TYPES' => \@types });
179
180   $lxdebug->leave_sub();
181 }
182
183 sub swap_cvar_configs {
184   $lxdebug->enter_sub();
185
186   AM->swap_sortkeys(\%myconfig, $form, 'custom_variable_configs');
187
188   list_cvar_configs();
189
190   $lxdebug->leave_sub();
191 }
192
193 1;