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::DB::CustomVariableValidity;
11 use SL::Helper::Flash;
12 use SL::Locale::String;
15 use Rose::Object::MakeMethods::Generic (
16 scalar => [ qw(config module module_description flags) ],
17 'scalar --get_set_init' => [ qw(translated_types modules) ],
20 __PACKAGE__->run_before('check_auth');
21 __PACKAGE__->run_before('check_module');
22 __PACKAGE__->run_before('load_config', only => [ qw(edit update destroy) ]);
25 text => t8('Free-form text'),
26 textfield => t8('Text field'),
27 number => t8('Number'),
29 timestamp => t8('Timestamp'),
30 bool => t8('Yes/No (Checkbox)'),
31 select => t8('Selection'),
32 customer => t8('Customer'),
33 vendor => t8('Vendor'),
37 our @types = qw(text textfield number date bool select customer vendor part); # timestamp
46 my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => $self->module ]);
49 $self->render('custom_variable_config/list',
50 title => t8('List of custom variables'),
57 $self->config(SL::DB::CustomVariableConfig->new(module => $self->module));
58 $self->show_form(title => t8('Add custom variable'));
62 my ($self, %params) = @_;
65 map { split m/=/, $_, 2 }
66 split m/:/, ($self->config->flags || '')
69 $self->render('custom_variable_config/form', %params);
75 $self->show_form(title => t8('Edit custom variable'));
81 $self->config(SL::DB::CustomVariableConfig->new);
82 $self->create_or_update;
87 $self->create_or_update;
93 if (eval { $self->config->delete; 1; }) {
94 flash_later('info', t8('The custom variable has been deleted.'));
96 flash_later('error', t8('The custom variable is in use and cannot be deleted.'));
99 $self->redirect_to(action => 'list', module => $self->module);
105 SL::DB::CustomVariableConfig->reorder_list(@{ $::form->{cvarcfg_id} || [] });
107 $self->render(\'', { type => 'json' }); # ' make emacs happy
115 $::auth->assert('config');
121 $::form->{module} ||= 'CT';
122 my $mod_desc = first { $_->{module} eq $::form->{module} } @{ $self->modules };
123 die "Invalid 'module' parameter '" . $::form->{module} . "'" if !$mod_desc;
125 $self->module($mod_desc->{module});
126 $self->module_description($mod_desc->{description});
132 $self->config(SL::DB::CustomVariableConfig->new(id => $::form->{id})->load);
139 sub get_translation {
140 my ($self, $type) = @_;
142 return $translations{$type};
145 sub init_translated_types {
148 return [ map { { type => $_, translation => $translations{$_} } } @types ];
152 my ($self, %params) = @_;
155 { module => 'CT', description => t8('Customers and vendors') },
156 { module => 'Contacts', description => t8('Contact persons') },
157 { module => 'IC', description => t8('Parts, services and assemblies') },
158 { module => 'Projects', description => t8('Projects') },
162 sub create_or_update {
164 my $is_new = !$self->config->id;
166 my $params = delete($::form->{config}) || { };
167 delete $params->{id};
169 $params->{default_value} = $::form->parse_amount(\%::myconfig, $params->{default_value}) if $params->{type} eq 'number';
170 $params->{included_by_default} = 0 if !$params->{includeable};
171 $params->{flags} = join ':', map { m/^flag_(.*)/; "${1}=" . delete($params->{$_}) } grep { m/^flag_/ } keys %{ $params };
173 $self->config->assign_attributes(%{ $params }, module => $self->module);
175 my @errors = $self->config->validate;
178 flash('error', @errors);
179 $self->show_form(title => $is_new ? t8('Add new custom variable') : t8('Edit custom variable'));
183 my $dbh = $self->config->db;
187 $self->_set_cvar_validity() if $is_new;
191 flash_later('info', $is_new ? t8('The custom variable has been created.') : t8('The custom variable has been saved.'));
192 $self->redirect_to(action => 'list', module => $self->module);
195 sub _set_cvar_validity {
199 map { split m/=/, $_, 2 }
200 split m/:/, ($self->config->flags || '')
203 # nothing to do to set valid
204 return if !$flags->{defaults_to_invalid};
206 my $all_parts = SL::DB::Manager::Part->get_all(where => [ or => [ obsolete => 0, obsolete => undef ] ]);
207 foreach my $part (@{ $all_parts }) {
208 SL::DB::CustomVariableValidity->new(config_id => $self->config->id, trans_id => $part->id)->save;