Mandantennamen in allen MenĂ¼-Headern anzeigen
[kivitendo-erp.git] / bin / mozilla / amcvar.pl
index 795dd44..343350a 100644 (file)
@@ -36,13 +36,17 @@ use SL::CVar;
 use SL::Form;
 
 use Data::Dumper;
-
-1;
+use List::MoreUtils qw(any);
 
 require "bin/mozilla/common.pl";
 
+use strict;
+
+1;
+
 # end of main
 
+my $locale   = $main::locale;
 our %translations = ('text'      => $locale->text('Free-form text'),
                      'textfield' => $locale->text('Text field'),
                      'number'    => $locale->text('Number'),
@@ -50,9 +54,18 @@ our %translations = ('text'      => $locale->text('Free-form text'),
                      'timestamp' => $locale->text('Timestamp'),
                      'bool'      => $locale->text('Yes/No (Checkbox)'),
                      'select'    => $locale->text('Selection'),
+                     'customer'  => $locale->text('Customer'),
+                     'vendor'    => $locale->text('Vendor'),
+                     'part'      => $locale->text('Part'),
                      );
 
-our @types = qw(text textfield number date bool select); # timestamp
+our @types = qw(text textfield number date bool select customer vendor part); # timestamp
+
+our @modules = ({ module => 'CT',       description => $locale->text('Customers and vendors')          },
+                { module => 'Contacts', description => $locale->text('Contact persons')                },
+                { module => 'IC',       description => $locale->text('Parts, services and assemblies') },
+                { module => 'Projects', description => $locale->text('Projects')                       },
+               );
 
 sub add {
   add_cvar_config();
@@ -62,52 +75,60 @@ sub edit {
   edit_cvar_config();
 }
 
+sub _is_valid_module {
+  my $module = shift;
+
+  return any { $_->{module} eq $module } @modules;
+}
+
 sub list_cvar_configs {
-  $lxdebug->enter_sub();
+  $main::lxdebug->enter_sub();
 
-  $auth->assert('config');
+  my $form     = $main::form;
+  my $locale   = $main::locale;
 
-  $form->{module} ||= $form->{cvar_module};
+  $main::auth->assert('config');
 
-  my @configs = grep { $_->{module} eq $form->{module} } @{ CVar->get_configs() };
+  $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
+  $form->{module} = 'CT' unless _is_valid_module($form->{module});
 
-  my $previous_config;
+  my @configs = @{ CVar->get_configs(module => $form->{module}) };
 
   foreach my $config (@configs) {
     $config->{type_tr} = $translations{$config->{type}};
-
-    if ($previous_config) {
-      $previous_config->{next_id} = $config->{id};
-      $config->{previous_id}      = $previous_config->{id};
-    }
-
-    $previous_config = $config;
   }
 
   $form->{title} = $locale->text('List of custom variables');
   $form->header();
-  print $form->parse_html_template('amcvar/list_cvar_configs', { 'CONFIGS' => \@configs });
+  print $form->parse_html_template('amcvar/list_cvar_configs', { CONFIGS => \@configs,
+                                                                 MODULES => \@modules });
 
-  $lxdebug->leave_sub();
+#  $main::lxdebug->dump(0, "modules", \@modules);
+
+  $main::lxdebug->leave_sub();
 }
 
 sub add_cvar_config {
-  $lxdebug->enter_sub();
+  $main::lxdebug->enter_sub();
+
+  my $form     = $main::form;
 
-  $auth->assert('config');
+  $main::auth->assert('config');
 
-  $form->{module} ||= $form->{cvar_module};
+  $form->{module} = $form->{module} || $form->{cvar_module} || 'CT';
 
   $form->{edit} = 0;
   display_cvar_config_form();
 
-  $lxdebug->leave_sub();
+  $main::lxdebug->leave_sub();
 }
 
 sub edit_cvar_config {
-  $lxdebug->enter_sub();
+  $main::lxdebug->enter_sub();
 
-  $auth->assert('config');
+  my $form     = $main::form;
+
+  $main::auth->assert('config');
 
   my $config = CVar->get_config('id' => $form->{id});
 
@@ -116,13 +137,17 @@ sub edit_cvar_config {
   $form->{edit} = 1;
   display_cvar_config_form();
 
-  $lxdebug->leave_sub();
+  $main::lxdebug->leave_sub();
 }
 
 sub save {
-  $lxdebug->enter_sub();
+  $main::lxdebug->enter_sub();
+
+  my $form     = $main::form;
+  my %myconfig = %main::myconfig;
+  my $locale   = $main::locale;
 
-  $auth->assert('config');
+  $main::auth->assert('config');
 
   $form->isblank('name',        $locale->text('The name is missing.'));
   $form->isblank('description', $locale->text('The description is missing.'));
@@ -147,11 +172,14 @@ sub save {
 
   list_cvar_configs();
 
-  $lxdebug->leave_sub();
+  $main::lxdebug->leave_sub();
 }
 
 sub delete {
-  $lxdebug->enter_sub();
+  $main::lxdebug->enter_sub();
+
+  my $form     = $main::form;
+  my $locale   = $main::locale;
 
   CVar->delete_config('id' => $form->{id});
 
@@ -159,13 +187,17 @@ sub delete {
 
   list_cvar_configs();
 
-  $lxdebug->leave_sub();
+  $main::lxdebug->leave_sub();
 }
 
 sub display_cvar_config_form {
-  $lxdebug->enter_sub();
+  $main::lxdebug->enter_sub();
+
+  my $form     = $main::form;
+  my %myconfig = %main::myconfig;
+  my $locale   = $main::locale;
 
-  $auth->assert('config');
+  $main::auth->assert('config');
 
   my @types = map { { 'type' => $_, 'type_tr' => $translations{$_} } } @types;
 
@@ -176,19 +208,41 @@ sub display_cvar_config_form {
   $form->{title} = $form->{edit} ? $locale->text("Edit custom variable") : $locale->text("Add custom variable");
 
   $form->header();
-  print $form->parse_html_template("amcvar/display_cvar_config_form", { 'TYPES' => \@types });
+  print $form->parse_html_template("amcvar/display_cvar_config_form", { TYPES   => \@types,
+                                                                        MODULES => \@modules });
 
-  $lxdebug->leave_sub();
+  $main::lxdebug->leave_sub();
 }
 
-sub swap_cvar_configs {
-  $lxdebug->enter_sub();
+sub update {
+  $main::lxdebug->enter_sub();
 
-  AM->swap_sortkeys(\%myconfig, $form, 'custom_variable_configs');
+  my $form     = $main::form;
 
-  list_cvar_configs();
+  $main::auth->assert('config');
+
+  $form->{included_by_default} = $form->{inclusion} eq 'yes_default_on';
+  $form->{includeable}         = $form->{inclusion} ne 'no';
+
+  display_cvar_config_form();
 
-  $lxdebug->leave_sub();
+  $main::lxdebug->leave_sub();
+}
+
+
+sub dispatcher {
+  my $form     = $main::form;
+  my $locale   = $main::locale;
+
+  foreach my $action (qw(list_cvar_configs add_cvar_config update)) {
+    if ($form->{"action_${action}"}) {
+      call_sub($action);
+      return;
+    }
+  }
+
+  $form->error($locale->text('No action defined.'));
 }
 
 1;
+