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::DB::PartsGroup;
12 use SL::Helper::Flash;
13 use SL::Locale::String;
16 use Rose::Object::MakeMethods::Generic (
17 scalar => [ qw(config module module_description flags) ],
18 'scalar --get_set_init' => [ qw(translated_types modules) ],
21 __PACKAGE__->run_before('check_auth');
22 __PACKAGE__->run_before('check_module');
23 __PACKAGE__->run_before('load_config', only => [ qw(edit update destroy) ]);
26 text => t8('Free-form text'),
27 textfield => t8('Text field'),
28 number => t8('Number'),
30 timestamp => t8('Timestamp'),
31 bool => t8('Yes/No (Checkbox)'),
32 select => t8('Selection'),
33 customer => t8('Customer'),
34 vendor => t8('Vendor'),
38 our @types = qw(text textfield number date bool select customer vendor part); # timestamp
47 my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => $self->module ]);
50 $self->render('custom_variable_config/list',
51 title => t8('List of custom variables'),
58 $self->config(SL::DB::CustomVariableConfig->new(module => $self->module));
59 $self->show_form(title => t8('Add custom variable'));
63 my ($self, %params) = @_;
66 map { split m/=/, $_, 2 }
67 split m/:/, ($self->config->flags || '')
70 $params{all_partsgroups} = SL::DB::Manager::PartsGroup->get_all();
72 $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
73 $self->render('custom_variable_config/form', %params);
79 $self->show_form(title => t8('Edit custom variable'));
85 $self->config(SL::DB::CustomVariableConfig->new);
86 $self->create_or_update;
91 $self->create_or_update;
97 # delete relationship to partsgroups (for filter) before cvar can be deleted
98 $self->config->update_attributes(partsgroups => []);
100 if (eval { $self->config->delete; 1; }) {
101 flash_later('info', t8('The custom variable has been deleted.'));
103 flash_later('error', t8('The custom variable is in use and cannot be deleted.'));
106 $self->redirect_to(action => 'list', module => $self->module);
112 SL::DB::CustomVariableConfig->reorder_list(@{ $::form->{cvarcfg_id} || [] });
114 $self->render(\'', { type => 'json' }); # ' make emacs happy
122 $::auth->assert('config');
128 $::form->{module} ||= 'CT';
129 my $mod_desc = first { $_->{module} eq $::form->{module} } @{ $self->modules };
130 die "Invalid 'module' parameter '" . $::form->{module} . "'" if !$mod_desc;
132 $self->module($mod_desc->{module});
133 $self->module_description($mod_desc->{description});
139 $self->config(SL::DB::CustomVariableConfig->new(id => $::form->{id})->load);
146 sub get_translation {
147 my ($self, $type) = @_;
149 return $translations{$type};
152 sub init_translated_types {
155 return [ map { { type => $_, translation => $translations{$_} } } @types ];
159 my ($self, %params) = @_;
161 return [ sort { $a->{description}->translated cmp $b->{description}->translated } (
162 { module => 'CT', description => t8('Customers and vendors') },
163 { module => 'Contacts', description => t8('Contact persons') },
164 { module => 'IC', description => t8('Parts, services and assemblies') },
165 { module => 'Projects', description => t8('Projects') },
166 { module => 'RequirementSpecs', description => t8('Requirement Specs') },
167 { module => 'ShipTo', description => t8('Shipping Address') },
171 sub create_or_update {
173 my $is_new = !$self->config->id;
175 my $params = delete($::form->{config}) || { };
176 delete $params->{id};
178 if ($self->module eq 'IC') {
179 $params->{partsgroups} = [] if !$params->{flag_partsgroup_filter};
181 delete $params->{flag_partsgroup_filter};
182 $params->{partsgroups} = [];
185 $params->{partsgroups} ||= []; # The list is empty, if control is not send by the browser.
186 $params->{default_value} = $::form->parse_amount(\%::myconfig, $params->{default_value}) if $params->{type} eq 'number';
187 $params->{included_by_default} = 0 if !$params->{includeable};
188 $params->{flags} = join ':', map { m/^flag_(.*)/; "${1}=" . delete($params->{$_}) } grep { m/^flag_/ } keys %{ $params };
190 $self->config->assign_attributes(%{ $params }, module => $self->module);
192 my @errors = $self->config->validate;
195 flash('error', @errors);
196 $self->show_form(title => $is_new ? t8('Add new custom variable') : t8('Edit custom variable'));
200 SL::DB->client->with_transaction(sub {
201 my $dbh = SL::DB->client->dbh;
204 $self->_set_cvar_validity() if $is_new;
206 }) or do { die SL::DB->client->error };
208 flash_later('info', $is_new ? t8('The custom variable has been created.') : t8('The custom variable has been saved.'));
209 $self->redirect_to(action => 'list', module => $self->module);
212 sub _set_cvar_validity {
216 map { split m/=/, $_, 2 }
217 split m/:/, ($self->config->flags || '')
220 # nothing to do to set valid
221 return if !$flags->{defaults_to_invalid};
223 my $all_parts = SL::DB::Manager::Part->get_all(where => [ or => [ obsolete => 0, obsolete => undef ] ]);
224 foreach my $part (@{ $all_parts }) {
225 SL::DB::CustomVariableValidity->new(config_id => $self->config->id, trans_id => $part->id)->save;