kivitendo/bin/mozilla/amcvar.pl @ a818dbfa
8688e71e | Moritz Bunkus | #=====================================================================
|
||
# LX-Office ERP
|
||||
# Copyright (C) 2004
|
||||
# Based on SQL-Ledger Version 2.1.9
|
||||
# Web http://www.lx-office.org
|
||||
#
|
||||
#=====================================================================
|
||||
# SQL-Ledger Accounting
|
||||
# Copyright (c) 1998-2002
|
||||
#
|
||||
# Author: Dieter Simader
|
||||
# Email: dsimader@sql-ledger.org
|
||||
# Web: http://www.sql-ledger.org
|
||||
#
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
#======================================================================
|
||||
#
|
||||
# administration
|
||||
#
|
||||
#======================================================================
|
||||
use SL::AM;
|
||||
use SL::CVar;
|
||||
use SL::Form;
|
||||
use Data::Dumper;
|
||||
5b64d6fc | Moritz Bunkus | use List::MoreUtils qw(any);
|
||
8688e71e | Moritz Bunkus | |||
require "bin/mozilla/common.pl";
|
||||
f4a98169 | Sven Schöling | use strict;
|
||
1;
|
||||
8688e71e | Moritz Bunkus | # end of main
|
||
f4a98169 | Sven Schöling | my $locale = $main::locale;
|
||
8688e71e | Moritz Bunkus | our %translations = ('text' => $locale->text('Free-form text'),
|
||
'textfield' => $locale->text('Text field'),
|
||||
'number' => $locale->text('Number'),
|
||||
'date' => $locale->text('Date'),
|
||||
'timestamp' => $locale->text('Timestamp'),
|
||||
'bool' => $locale->text('Yes/No (Checkbox)'),
|
||||
'select' => $locale->text('Selection'),
|
||||
);
|
||||
our @types = qw(text textfield number date bool select); # timestamp
|
||||
5b64d6fc | Moritz Bunkus | our @modules = ({ module => 'CT', description => $locale->text('Customers and vendors') },
|
||
{ module => 'IC', description => $locale->text('Parts, services and assemblies') },
|
||||
{ module => 'Projects', description => $locale->text('Projects') },
|
||||
);
|
||||
8688e71e | Moritz Bunkus | sub add {
|
||
add_cvar_config();
|
||||
}
|
||||
sub edit {
|
||||
edit_cvar_config();
|
||||
}
|
||||
5b64d6fc | Moritz Bunkus | sub _is_valid_module {
|
||
my $module = shift;
|
||||
return any { $_->{module} eq $module } @modules;
|
||||
}
|
||||
8688e71e | Moritz Bunkus | sub list_cvar_configs {
|
||
f4a98169 | Sven Schöling | $main::lxdebug->enter_sub();
|
||
my $form = $main::form;
|
||||
my $locale = $main::locale;
|
||||
8688e71e | Moritz Bunkus | |||
f4a98169 | Sven Schöling | $main::auth->assert('config');
|
||
8688e71e | Moritz Bunkus | |||
5b64d6fc | Moritz Bunkus | $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
|
||
$form->{module} = 'CT' unless _is_valid_module($form->{module});
|
||||
8688e71e | Moritz Bunkus | |||
5b64d6fc | Moritz Bunkus | my @configs = @{ CVar->get_configs(module => $form->{module}) };
|
||
8688e71e | Moritz Bunkus | |||
my $previous_config;
|
||||
b2f44e3d | Moritz Bunkus | foreach my $config (@configs) {
|
||
$config->{type_tr} = $translations{$config->{type}};
|
||||
8688e71e | Moritz Bunkus | if ($previous_config) {
|
||
b2f44e3d | Moritz Bunkus | $previous_config->{next_id} = $config->{id};
|
||
$config->{previous_id} = $previous_config->{id};
|
||||
8688e71e | Moritz Bunkus | }
|
||
b2f44e3d | Moritz Bunkus | $previous_config = $config;
|
||
8688e71e | Moritz Bunkus | }
|
||
$form->{title} = $locale->text('List of custom variables');
|
||||
$form->header();
|
||||
5b64d6fc | Moritz Bunkus | print $form->parse_html_template('amcvar/list_cvar_configs', { CONFIGS => \@configs,
|
||
MODULES => \@modules });
|
||||
$main::lxdebug->dump(0, "modules", \@modules);
|
||||
8688e71e | Moritz Bunkus | |||
f4a98169 | Sven Schöling | $main::lxdebug->leave_sub();
|
||
8688e71e | Moritz Bunkus | }
|
||
sub add_cvar_config {
|
||||
f4a98169 | Sven Schöling | $main::lxdebug->enter_sub();
|
||
my $form = $main::form;
|
||||
8688e71e | Moritz Bunkus | |||
f4a98169 | Sven Schöling | $main::auth->assert('config');
|
||
8688e71e | Moritz Bunkus | |||
5b64d6fc | Moritz Bunkus | $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
|
||
8688e71e | Moritz Bunkus | |||
$form->{edit} = 0;
|
||||
display_cvar_config_form();
|
||||
f4a98169 | Sven Schöling | $main::lxdebug->leave_sub();
|
||
8688e71e | Moritz Bunkus | }
|
||
sub edit_cvar_config {
|
||||
f4a98169 | Sven Schöling | $main::lxdebug->enter_sub();
|
||
8688e71e | Moritz Bunkus | |||
f4a98169 | Sven Schöling | my $form = $main::form;
|
||
$main::auth->assert('config');
|
||||
8688e71e | Moritz Bunkus | |||
my $config = CVar->get_config('id' => $form->{id});
|
||||
map { $form->{$_} = $config->{$_} } keys %{ $config };
|
||||
$form->{edit} = 1;
|
||||
display_cvar_config_form();
|
||||
f4a98169 | Sven Schöling | $main::lxdebug->leave_sub();
|
||
8688e71e | Moritz Bunkus | }
|
||
sub save {
|
||||
f4a98169 | Sven Schöling | $main::lxdebug->enter_sub();
|
||
my $form = $main::form;
|
||||
my %myconfig = %main::myconfig;
|
||||
my $locale = $main::locale;
|
||||
8688e71e | Moritz Bunkus | |||
f4a98169 | Sven Schöling | $main::auth->assert('config');
|
||
8688e71e | Moritz Bunkus | |||
$form->isblank('name', $locale->text('The name is missing.'));
|
||||
$form->isblank('description', $locale->text('The description is missing.'));
|
||||
$form->isblank('options', $locale->text('The option field is empty.')) if ($form->{type} eq 'select');
|
||||
if ($form->{name} !~ /^[a-z][a-z0-9_]*$/i) {
|
||||
$form->error($locale->text('The name must only consist of letters, numbers and underscores and start with a letter.'));
|
||||
}
|
||||
if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
|
||||
$form->{default_value} = $form->parse_amount(\%myconfig, $form->{default_value});
|
||||
}
|
||||
$form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
|
||||
$form->{includeable} = $form->{inclusion} ne 'no';
|
||||
b2f44e3d | Moritz Bunkus | $form->{flags} = join ':', map { m/^flag_(.*)/; "${1}=" . $form->{$_} } grep { m/^flag_/ } keys %{ $form };
|
||
8688e71e | Moritz Bunkus | |||
CVar->save_config('module' => $form->{module},
|
||||
'config' => $form);
|
||||
$form->{MESSAGE} = $locale->text('The custom variable has been saved.');
|
||||
list_cvar_configs();
|
||||
f4a98169 | Sven Schöling | $main::lxdebug->leave_sub();
|
||
8688e71e | Moritz Bunkus | }
|
||
sub delete {
|
||||
f4a98169 | Sven Schöling | $main::lxdebug->enter_sub();
|
||
my $form = $main::form;
|
||||
my $locale = $main::locale;
|
||||
8688e71e | Moritz Bunkus | |||
CVar->delete_config('id' => $form->{id});
|
||||
$form->{MESSAGE} = $locale->text('The custom variable has been deleted.');
|
||||
list_cvar_configs();
|
||||
f4a98169 | Sven Schöling | $main::lxdebug->leave_sub();
|
||
8688e71e | Moritz Bunkus | }
|
||
sub display_cvar_config_form {
|
||||
f4a98169 | Sven Schöling | $main::lxdebug->enter_sub();
|
||
8688e71e | Moritz Bunkus | |||
f4a98169 | Sven Schöling | my $form = $main::form;
|
||
my %myconfig = %main::myconfig;
|
||||
my $locale = $main::locale;
|
||||
$main::auth->assert('config');
|
||||
8688e71e | Moritz Bunkus | |||
my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
|
||||
if (($form->{type} eq 'number') && ($form->{default_value} ne '')) {
|
||||
$form->{default_value} = $form->format_amount(\%myconfig, $form->{default_value});
|
||||
}
|
||||
$form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
|
||||
$form->header();
|
||||
5b64d6fc | Moritz Bunkus | print $form->parse_html_template("amcvar/display_cvar_config_form", { TYPES => \@types,
|
||
MODULES => \@modules });
|
||||
8688e71e | Moritz Bunkus | |||
f4a98169 | Sven Schöling | $main::lxdebug->leave_sub();
|
||
8688e71e | Moritz Bunkus | }
|
||
sub swap_cvar_configs {
|
||||
f4a98169 | Sven Schöling | $main::lxdebug->enter_sub();
|
||
my $form = $main::form;
|
||||
my %myconfig = %main::myconfig;
|
||||
8688e71e | Moritz Bunkus | |||
AM->swap_sortkeys(\%myconfig, $form, 'custom_variable_configs');
|
||||
list_cvar_configs();
|
||||
f4a98169 | Sven Schöling | $main::lxdebug->leave_sub();
|
||
8688e71e | Moritz Bunkus | }
|
||
5b64d6fc | Moritz Bunkus | sub dispatcher {
|
||
f4a98169 | Sven Schöling | my $form = $main::form;
|
||
my $locale = $main::locale;
|
||||
5b64d6fc | Moritz Bunkus | foreach my $action (qw(list_cvar_configs add_cvar_config)) {
|
||
if ($form->{"action_${action}"}) {
|
||||
call_sub($action);
|
||||
return;
|
||||
}
|
||||
}
|
||||
$form->error($locale->text('No action defined.'));
|
||||
}
|
||||
8688e71e | Moritz Bunkus | 1;
|