1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
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.
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 #======================================================================
32 #======================================================================
39 use List::MoreUtils qw(any);
41 require "bin/mozilla/common.pl";
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 'vendor' => $locale->text('Vendor'),
59 'part' => $locale->text('Part'),
62 our @types = qw(text textfield number date bool select customer vendor part); # timestamp
64 our @modules = ({ module => 'CT', description => $locale->text('Customers and vendors') },
65 { module => 'Contacts', description => $locale->text('Contact persons') },
66 { module => 'IC', description => $locale->text('Parts, services and assemblies') },
67 { module => 'Projects', description => $locale->text('Projects') },
78 sub _is_valid_module {
81 return any { $_->{module} eq $module } @modules;
84 sub list_cvar_configs {
85 $main::lxdebug->enter_sub();
87 my $form = $main::form;
88 my $locale = $main::locale;
90 $main::auth->assert('config');
92 $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
93 $form->{module} = 'CT' unless _is_valid_module($form->{module});
95 my @configs = @{ CVar->get_configs(module => $form->{module}) };
97 foreach my $config (@configs) {
98 $config->{type_tr} = $translations{$config->{type}};
101 $form->{title} = $locale->text('List of custom variables');
103 print $form->parse_html_template('amcvar/list_cvar_configs', { CONFIGS => \@configs,
104 MODULES => \@modules });
106 # $main::lxdebug->dump(0, "modules", \@modules);
108 $main::lxdebug->leave_sub();
111 sub add_cvar_config {
112 $main::lxdebug->enter_sub();
114 my $form = $main::form;
116 $main::auth->assert('config');
118 $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
121 display_cvar_config_form();
123 $main::lxdebug->leave_sub();
126 sub edit_cvar_config {
127 $main::lxdebug->enter_sub();
129 my $form = $main::form;
131 $main::auth->assert('config');
133 my $config = CVar->get_config('id' => $form->{id});
135 map { $form->{$_} = $config->{$_} } keys %{ $config };
138 display_cvar_config_form();
140 $main::lxdebug->leave_sub();
144 $main::lxdebug->enter_sub();
146 my $form = $main::form;
147 my %myconfig = %main::myconfig;
148 my $locale = $main::locale;
150 $main::auth->assert('config');
152 $form->isblank('name', $locale->text('The name is missing.'));
153 $form->isblank('description', $locale->text('The description is missing.'));
154 $form->isblank('options', $locale->text('The option field is empty.')) if ($form->{type} eq 'select');
156 if ($form->{name} !~ /^[a-z][a-z0-9_]*$/i) {
157 $form->error($locale->text('The name must only consist of letters, numbers and underscores and start with a letter.'));
160 if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
161 $form->{default_value} = $form->parse_amount(\%myconfig, $form->{default_value});
164 $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
165 $form->{includeable} = $form->{inclusion} ne 'no';
166 $form->{flags} = join ':', map { m/^flag_(.*)/; "${1}=" . $form->{$_} } grep { m/^flag_/ } keys %{ $form };
168 CVar->save_config('module' => $form->{module},
171 $form->{MESSAGE} = $locale->text('The custom variable has been saved.');
175 $main::lxdebug->leave_sub();
179 $main::lxdebug->enter_sub();
181 my $form = $main::form;
182 my $locale = $main::locale;
184 CVar->delete_config('id' => $form->{id});
186 $form->{MESSAGE} = $locale->text('The custom variable has been deleted.');
190 $main::lxdebug->leave_sub();
193 sub display_cvar_config_form {
194 $main::lxdebug->enter_sub();
196 my $form = $main::form;
197 my %myconfig = %main::myconfig;
198 my $locale = $main::locale;
200 $main::auth->assert('config');
202 my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
204 if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
205 $form->{default_value} = $form->format_amount(\%myconfig, $form->{default_value});
208 $form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
211 print $form->parse_html_template("amcvar/display_cvar_config_form", { TYPES => \@types,
212 MODULES => \@modules });
214 $main::lxdebug->leave_sub();
218 $main::lxdebug->enter_sub();
220 my $form = $main::form;
222 $main::auth->assert('config');
224 $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
225 $form->{includeable} = $form->{inclusion} ne 'no';
227 display_cvar_config_form();
229 $main::lxdebug->leave_sub();
234 my $form = $main::form;
235 my $locale = $main::locale;
237 foreach my $action (qw(list_cvar_configs add_cvar_config update)) {
238 if ($form->{"action_${action}"}) {
244 $form->error($locale->text('No action defined.'));