Benutzerdef. Var. als HTML-Feld: allgemeine Infrastruktur
[kivitendo-erp.git] / SL / Controller / CustomVariableConfig.pm
index b366ffb..193523a 100644 (file)
@@ -7,8 +7,11 @@ use parent qw(SL::Controller::Base);
 use List::Util qw(first);
 
 use SL::DB::CustomVariableConfig;
+use SL::DB::CustomVariableValidity;
+use SL::DB::PartsGroup;
 use SL::Helper::Flash;
 use SL::Locale::String;
+use Data::Dumper;
 
 use Rose::Object::MakeMethods::Generic (
   scalar                  => [ qw(config module module_description flags) ],
@@ -22,6 +25,7 @@ __PACKAGE__->run_before('load_config', only => [ qw(edit update destroy) ]);
 our %translations = (
   text      => t8('Free-form text'),
   textfield => t8('Text field'),
+  htmlfield => t8('HTML field'),
   number    => t8('Number'),
   date      => t8('Date'),
   timestamp => t8('Timestamp'),
@@ -32,7 +36,7 @@ our %translations = (
   part      => t8('Part'),
 );
 
-our @types = qw(text textfield number date bool select customer vendor part); # timestamp
+our @types = qw(text textfield htmlfield number date bool select customer vendor part); # timestamp
 
 #
 # actions
@@ -43,6 +47,7 @@ sub action_list {
 
   my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => $self->module ]);
 
+  $self->setup_list_action_bar;
   $::form->header;
   $self->render('custom_variable_config/list',
                 title   => t8('List of custom variables'),
@@ -59,11 +64,15 @@ sub action_new {
 sub show_form {
   my ($self, %params) = @_;
 
-  $self->flags([
-    map { split m/=/, 2 }
-    split m/;/, ($self->config->flags || '')
-  ]);
+  $self->flags({
+    map { split m/=/, $_, 2 }
+    split m/:/, ($self->config->flags || '')
+  });
 
+  $params{all_partsgroups} = SL::DB::Manager::PartsGroup->get_all();
+
+  $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
+  $self->setup_form_action_bar;
   $self->render('custom_variable_config/form', %params);
 }
 
@@ -88,13 +97,16 @@ sub action_update {
 sub action_destroy {
   my ($self) = @_;
 
+  # delete relationship to partsgroups (for filter) before cvar can be deleted
+  $self->config->update_attributes(partsgroups => []);
+
   if (eval { $self->config->delete; 1; }) {
     flash_later('info',  t8('The custom variable has been deleted.'));
   } else {
     flash_later('error', t8('The custom variable is in use and cannot be deleted.'));
   }
 
-  $self->redirect_to(action => 'list');
+  $self->redirect_to(action => 'list', module => $self->module);
 }
 
 sub action_reorder {
@@ -102,7 +114,7 @@ sub action_reorder {
 
   SL::DB::CustomVariableConfig->reorder_list(@{ $::form->{cvarcfg_id} || [] });
 
-  $self->render(\'', { type => 'json' });
+  $self->render(\'', { type => 'json' }); # ' make emacs happy
 }
 
 #
@@ -149,12 +161,14 @@ sub init_translated_types {
 sub init_modules {
   my ($self, %params) = @_;
 
-  return [
-    { module => 'CT',       description => t8('Customers and vendors')          },
-    { module => 'Contacts', description => t8('Contact persons')                },
-    { module => 'IC',       description => t8('Parts, services and assemblies') },
-    { module => 'Projects', description => t8('Projects')                       },
-  ];
+  return [ sort { $a->{description}->translated cmp $b->{description}->translated } (
+    { module => 'CT',               description => t8('Customers and vendors')          },
+    { module => 'Contacts',         description => t8('Contact persons')                },
+    { module => 'IC',               description => t8('Parts, services and assemblies') },
+    { module => 'Projects',         description => t8('Projects')                       },
+    { module => 'RequirementSpecs', description => t8('Requirement Specs')              },
+    { module => 'ShipTo',           description => t8('Shipping Address')               },
+  )];
 }
 
 sub create_or_update {
@@ -164,6 +178,14 @@ sub create_or_update {
   my $params = delete($::form->{config}) || { };
   delete $params->{id};
 
+  if ($self->module eq 'IC') {
+    $params->{partsgroups} = [] if !$params->{flag_partsgroup_filter};
+  } else {
+    delete $params->{flag_partsgroup_filter};
+    $params->{partsgroups} = [];
+  }
+
+  $params->{partsgroups}       ||= []; # The list is empty, if control is not send by the browser.
   $params->{default_value}       = $::form->parse_amount(\%::myconfig, $params->{default_value}) if $params->{type} eq 'number';
   $params->{included_by_default} = 0                                                             if !$params->{includeable};
   $params->{flags}               = join ':', map { m/^flag_(.*)/; "${1}=" . delete($params->{$_}) } grep { m/^flag_/ } keys %{ $params };
@@ -178,10 +200,86 @@ sub create_or_update {
     return;
   }
 
-  $self->config->save;
+  SL::DB->client->with_transaction(sub {
+    my $dbh = SL::DB->client->dbh;
+
+    $self->config->save;
+    $self->_set_cvar_validity() if $is_new;
+    1;
+  }) or do { die SL::DB->client->error };
 
   flash_later('info', $is_new ? t8('The custom variable has been created.') : t8('The custom variable has been saved.'));
   $self->redirect_to(action => 'list', module => $self->module);
 }
 
+sub _set_cvar_validity {
+  my ($self) = @_;
+
+  my $flags = {
+    map { split m/=/, $_, 2 }
+    split m/:/, ($self->config->flags || '')
+  };
+
+  # nothing to do to set valid
+  return if !$flags->{defaults_to_invalid};
+
+  my $all_parts  = SL::DB::Manager::Part->get_all(where => [ or => [ obsolete => 0, obsolete => undef ] ]);
+  foreach my $part (@{ $all_parts }) {
+    SL::DB::CustomVariableValidity->new(config_id => $self->config->id, trans_id => $part->id)->save;
+  }
+}
+
+sub setup_list_action_bar {
+  my ($self) = @_;
+
+  for my $bar ($::request->layout->get('actionbar')) {
+    $bar->add(
+      action => [
+        t8('Add'),
+        link => $self->url_for(action => 'new', module => $self->module),
+      ],
+    );
+  }
+}
+
+sub setup_form_action_bar {
+  my ($self) = @_;
+
+  my $is_new = !$self->config->id;
+
+  for my $bar ($::request->layout->get('actionbar')) {
+    $bar->add(
+      combobox => [
+        action => [
+          t8('Save'),
+          submit    => [ '#form', { action => 'CustomVariableConfig/' . ($is_new ? 'create' : 'update') } ],
+          checks    => [ 'check_prerequisites' ],
+          accesskey => 'enter',
+        ],
+
+        action => [
+          t8('Save as new'),
+          submit => [ '#form', { action => 'CustomVariableConfig/create'} ],
+          checks => [ 'check_prerequisites' ],
+          not_if => $is_new,
+        ],
+      ], # end of combobox "Save"
+
+      action => [
+        t8('Delete'),
+        submit   => [ '#form', { action => 'CustomVariableConfig/destroy' } ],
+        confirm  => t8('Do you really want to delete this object?'),
+        disabled => $is_new ? t8('This object has not been saved yet.') : undef,
+      ],
+
+      'separator',
+
+      link => [
+        t8('Abort'),
+        link => $self->url_for(action => 'list', module => $self->module),
+      ],
+    );
+  }
+}
+
 1;