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'),
60 our @types = qw(text textfield number date bool select customer); # timestamp
62 our @modules = ({ module => 'CT', description => $locale->text('Customers and vendors') },
63 { module => 'Contacts', description => $locale->text('Contact persons') },
64 { module => 'IC', description => $locale->text('Parts, services and assemblies') },
65 { module => 'Projects', description => $locale->text('Projects') },
76 sub _is_valid_module {
79 return any { $_->{module} eq $module } @modules;
82 sub list_cvar_configs {
83 $main::lxdebug->enter_sub();
85 my $form = $main::form;
86 my $locale = $main::locale;
88 $main::auth->assert('config');
90 $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
91 $form->{module} = 'CT' unless _is_valid_module($form->{module});
93 my @configs = @{ CVar->get_configs(module => $form->{module}) };
95 foreach my $config (@configs) {
96 $config->{type_tr} = $translations{$config->{type}};
99 $form->{title} = $locale->text('List of custom variables');
101 print $form->parse_html_template('amcvar/list_cvar_configs', { CONFIGS => \@configs,
102 MODULES => \@modules });
104 # $main::lxdebug->dump(0, "modules", \@modules);
106 $main::lxdebug->leave_sub();
109 sub add_cvar_config {
110 $main::lxdebug->enter_sub();
112 my $form = $main::form;
114 $main::auth->assert('config');
116 $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
119 display_cvar_config_form();
121 $main::lxdebug->leave_sub();
124 sub edit_cvar_config {
125 $main::lxdebug->enter_sub();
127 my $form = $main::form;
129 $main::auth->assert('config');
131 my $config = CVar->get_config('id' => $form->{id});
133 map { $form->{$_} = $config->{$_} } keys %{ $config };
136 display_cvar_config_form();
138 $main::lxdebug->leave_sub();
142 $main::lxdebug->enter_sub();
144 my $form = $main::form;
145 my %myconfig = %main::myconfig;
146 my $locale = $main::locale;
148 $main::auth->assert('config');
150 $form->isblank('name', $locale->text('The name is missing.'));
151 $form->isblank('description', $locale->text('The description is missing.'));
152 $form->isblank('options', $locale->text('The option field is empty.')) if ($form->{type} eq 'select');
154 if ($form->{name} !~ /^[a-z][a-z0-9_]*$/i) {
155 $form->error($locale->text('The name must only consist of letters, numbers and underscores and start with a letter.'));
158 if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
159 $form->{default_value} = $form->parse_amount(\%myconfig, $form->{default_value});
162 $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
163 $form->{includeable} = $form->{inclusion} ne 'no';
164 $form->{flags} = join ':', map { m/^flag_(.*)/; "${1}=" . $form->{$_} } grep { m/^flag_/ } keys %{ $form };
166 CVar->save_config('module' => $form->{module},
169 $form->{MESSAGE} = $locale->text('The custom variable has been saved.');
173 $main::lxdebug->leave_sub();
177 $main::lxdebug->enter_sub();
179 my $form = $main::form;
180 my $locale = $main::locale;
182 CVar->delete_config('id' => $form->{id});
184 $form->{MESSAGE} = $locale->text('The custom variable has been deleted.');
188 $main::lxdebug->leave_sub();
191 sub display_cvar_config_form {
192 $main::lxdebug->enter_sub();
194 my $form = $main::form;
195 my %myconfig = %main::myconfig;
196 my $locale = $main::locale;
198 $main::auth->assert('config');
200 my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
202 if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
203 $form->{default_value} = $form->format_amount(\%myconfig, $form->{default_value});
206 $form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
209 print $form->parse_html_template("amcvar/display_cvar_config_form", { TYPES => \@types,
210 MODULES => \@modules });
212 $main::lxdebug->leave_sub();
216 $main::lxdebug->enter_sub();
218 my $form = $main::form;
220 $main::auth->assert('config');
222 $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
223 $form->{includeable} = $form->{inclusion} ne 'no';
225 display_cvar_config_form();
227 $main::lxdebug->leave_sub();
232 my $form = $main::form;
233 my $locale = $main::locale;
235 foreach my $action (qw(list_cvar_configs add_cvar_config update)) {
236 if ($form->{"action_${action}"}) {
242 $form->error($locale->text('No action defined.'));