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);
43 require "bin/mozilla/common.pl";
47 our %translations = ('text' => $locale->text('Free-form text'),
48 'textfield' => $locale->text('Text field'),
49 'number' => $locale->text('Number'),
50 'date' => $locale->text('Date'),
51 'timestamp' => $locale->text('Timestamp'),
52 'bool' => $locale->text('Yes/No (Checkbox)'),
53 'select' => $locale->text('Selection'),
56 our @types = qw(text textfield number date bool select); # timestamp
58 our @modules = ({ module => 'CT', description => $locale->text('Customers and vendors') },
59 { module => 'IC', description => $locale->text('Parts, services and assemblies') },
60 { module => 'Projects', description => $locale->text('Projects') },
71 sub _is_valid_module {
74 return any { $_->{module} eq $module } @modules;
77 sub list_cvar_configs {
78 $lxdebug->enter_sub();
80 $auth->assert('config');
82 $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
83 $form->{module} = 'CT' unless _is_valid_module($form->{module});
85 my @configs = @{ CVar->get_configs(module => $form->{module}) };
89 foreach my $config (@configs) {
90 $config->{type_tr} = $translations{$config->{type}};
92 if ($previous_config) {
93 $previous_config->{next_id} = $config->{id};
94 $config->{previous_id} = $previous_config->{id};
97 $previous_config = $config;
100 $form->{title} = $locale->text('List of custom variables');
102 print $form->parse_html_template('amcvar/list_cvar_configs', { CONFIGS => \@configs,
103 MODULES => \@modules });
105 $main::lxdebug->dump(0, "modules", \@modules);
107 $lxdebug->leave_sub();
110 sub add_cvar_config {
111 $lxdebug->enter_sub();
113 $auth->assert('config');
115 $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
118 display_cvar_config_form();
120 $lxdebug->leave_sub();
123 sub edit_cvar_config {
124 $lxdebug->enter_sub();
126 $auth->assert('config');
128 my $config = CVar->get_config('id' => $form->{id});
130 map { $form->{$_} = $config->{$_} } keys %{ $config };
133 display_cvar_config_form();
135 $lxdebug->leave_sub();
139 $lxdebug->enter_sub();
141 $auth->assert('config');
143 $form->isblank('name', $locale->text('The name is missing.'));
144 $form->isblank('description', $locale->text('The description is missing.'));
145 $form->isblank('options', $locale->text('The option field is empty.')) if ($form->{type} eq 'select');
147 if ($form->{name} !~ /^[a-z][a-z0-9_]*$/i) {
148 $form->error($locale->text('The name must only consist of letters, numbers and underscores and start with a letter.'));
151 if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
152 $form->{default_value} = $form->parse_amount(\%myconfig, $form->{default_value});
155 $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
156 $form->{includeable} = $form->{inclusion} ne 'no';
157 $form->{flags} = join ':', map { m/^flag_(.*)/; "${1}=" . $form->{$_} } grep { m/^flag_/ } keys %{ $form };
159 CVar->save_config('module' => $form->{module},
162 $form->{MESSAGE} = $locale->text('The custom variable has been saved.');
166 $lxdebug->leave_sub();
170 $lxdebug->enter_sub();
172 CVar->delete_config('id' => $form->{id});
174 $form->{MESSAGE} = $locale->text('The custom variable has been deleted.');
178 $lxdebug->leave_sub();
181 sub display_cvar_config_form {
182 $lxdebug->enter_sub();
184 $auth->assert('config');
186 my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
188 if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
189 $form->{default_value} = $form->format_amount(\%myconfig, $form->{default_value});
192 $form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
195 print $form->parse_html_template("amcvar/display_cvar_config_form", { TYPES => \@types,
196 MODULES => \@modules });
198 $lxdebug->leave_sub();
201 sub swap_cvar_configs {
202 $lxdebug->enter_sub();
204 AM->swap_sortkeys(\%myconfig, $form, 'custom_variable_configs');
208 $lxdebug->leave_sub();
212 foreach my $action (qw(list_cvar_configs add_cvar_config)) {
213 if ($form->{"action_${action}"}) {
219 $form->error($locale->text('No action defined.'));