795dd44b7f36ca65785d2f67a6aa5050fe7b9636
[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     if ($previous_config) {
80       $previous_config->{next_id} = $config->{id};
81       $config->{previous_id}      = $previous_config->{id};
82     }
83
84     $previous_config = $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   $form->{flags}               = join ':', map { m/^flag_(.*)/; "${1}=" . $form->{$_} } grep { m/^flag_/ } keys %{ $form };
142
143   CVar->save_config('module' => $form->{module},
144                     'config' => $form);
145
146   $form->{MESSAGE} = $locale->text('The custom variable has been saved.');
147
148   list_cvar_configs();
149
150   $lxdebug->leave_sub();
151 }
152
153 sub delete {
154   $lxdebug->enter_sub();
155
156   CVar->delete_config('id' => $form->{id});
157
158   $form->{MESSAGE} = $locale->text('The custom variable has been deleted.');
159
160   list_cvar_configs();
161
162   $lxdebug->leave_sub();
163 }
164
165 sub display_cvar_config_form {
166   $lxdebug->enter_sub();
167
168   $auth->assert('config');
169
170   my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
171
172   if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
173     $form->{default_value} = $form->format_amount(\%myconfig, $form->{default_value});
174   }
175
176   $form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
177
178   $form->header();
179   print $form->parse_html_template("amcvar/display_cvar_config_form", { 'TYPES' => \@types });
180
181   $lxdebug->leave_sub();
182 }
183
184 sub swap_cvar_configs {
185   $lxdebug->enter_sub();
186
187   AM->swap_sortkeys(\%myconfig, $form, 'custom_variable_configs');
188
189   list_cvar_configs();
190
191   $lxdebug->leave_sub();
192 }
193
194 1;