1 package SL::Controller::CustomVariableConfig;
 
   5 use parent qw(SL::Controller::Base);
 
   7 use List::Util qw(first);
 
   9 use SL::DB::CustomVariableConfig;
 
  10 use SL::Helper::Flash;
 
  11 use SL::Locale::String;
 
  13 use Rose::Object::MakeMethods::Generic (
 
  14   scalar                  => [ qw(config module module_description flags) ],
 
  15   'scalar --get_set_init' => [ qw(translated_types modules) ],
 
  18 __PACKAGE__->run_before('check_auth');
 
  19 __PACKAGE__->run_before('check_module');
 
  20 __PACKAGE__->run_before('load_config', only => [ qw(edit update destroy) ]);
 
  23   text      => t8('Free-form text'),
 
  24   textfield => t8('Text field'),
 
  25   number    => t8('Number'),
 
  27   timestamp => t8('Timestamp'),
 
  28   bool      => t8('Yes/No (Checkbox)'),
 
  29   select    => t8('Selection'),
 
  30   customer  => t8('Customer'),
 
  31   vendor    => t8('Vendor'),
 
  35 our @types = qw(text textfield number date bool select customer vendor part); # timestamp
 
  44   my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => $self->module ]);
 
  47   $self->render('custom_variable_config/list',
 
  48                 title   => t8('List of custom variables'),
 
  55   $self->config(SL::DB::CustomVariableConfig->new(module => $self->module));
 
  56   $self->show_form(title => t8('Add custom variable'));
 
  60   my ($self, %params) = @_;
 
  63     map { split m/=/, $_, 2 }
 
  64     split m/:/, ($self->config->flags || '')
 
  67   $self->render('custom_variable_config/form', %params);
 
  73   $self->show_form(title => t8('Edit custom variable'));
 
  79   $self->config(SL::DB::CustomVariableConfig->new);
 
  80   $self->create_or_update;
 
  85   $self->create_or_update;
 
  91   if (eval { $self->config->delete; 1; }) {
 
  92     flash_later('info',  t8('The custom variable has been deleted.'));
 
  94     flash_later('error', t8('The custom variable is in use and cannot be deleted.'));
 
  97   $self->redirect_to(action => 'list');
 
 103   SL::DB::CustomVariableConfig->reorder_list(@{ $::form->{cvarcfg_id} || [] });
 
 105   $self->render(\'', { type => 'json' });
 
 113   $::auth->assert('config');
 
 119   $::form->{module} ||= 'CT';
 
 120   my $mod_desc        = first { $_->{module} eq $::form->{module} } @{ $self->modules };
 
 121   die "Invalid 'module' parameter '" . $::form->{module} . "'" if !$mod_desc;
 
 123   $self->module($mod_desc->{module});
 
 124   $self->module_description($mod_desc->{description});
 
 130   $self->config(SL::DB::CustomVariableConfig->new(id => $::form->{id})->load);
 
 137 sub get_translation {
 
 138   my ($self, $type) = @_;
 
 140   return $translations{$type};
 
 143 sub init_translated_types {
 
 146   return [ map { { type => $_, translation => $translations{$_} } } @types ];
 
 150   my ($self, %params) = @_;
 
 153     { module => 'CT',       description => t8('Customers and vendors')          },
 
 154     { module => 'Contacts', description => t8('Contact persons')                },
 
 155     { module => 'IC',       description => t8('Parts, services and assemblies') },
 
 156     { module => 'Projects', description => t8('Projects')                       },
 
 160 sub create_or_update {
 
 162   my $is_new = !$self->config->id;
 
 164   my $params = delete($::form->{config}) || { };
 
 165   delete $params->{id};
 
 167   $params->{default_value}       = $::form->parse_amount(\%::myconfig, $params->{default_value}) if $params->{type} eq 'number';
 
 168   $params->{included_by_default} = 0                                                             if !$params->{includeable};
 
 169   $params->{flags}               = join ':', map { m/^flag_(.*)/; "${1}=" . delete($params->{$_}) } grep { m/^flag_/ } keys %{ $params };
 
 171   $self->config->assign_attributes(%{ $params }, module => $self->module);
 
 173   my @errors = $self->config->validate;
 
 176     flash('error', @errors);
 
 177     $self->show_form(title => $is_new ? t8('Add new custom variable') : t8('Edit custom variable'));
 
 183   flash_later('info', $is_new ? t8('The custom variable has been created.') : t8('The custom variable has been saved.'));
 
 184   $self->redirect_to(action => 'list', module => $self->module);