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;
14 use SL::Presenter::CustomVariableConfig;
17 use Rose::Object::MakeMethods::Generic (
18 scalar => [ qw(config module module_description flags) ],
19 'scalar --get_set_init' => [ qw(translated_types modules) ],
22 __PACKAGE__->run_before('check_auth');
23 __PACKAGE__->run_before('check_module');
24 __PACKAGE__->run_before('load_config', only => [ qw(edit update destroy) ]);
27 text => t8('Free-form text'),
28 textfield => t8('Text field'),
29 htmlfield => t8('HTML field'),
30 number => t8('Number'),
32 timestamp => t8('Timestamp'),
33 bool => t8('Yes/No (Checkbox)'),
34 select => t8('Selection'),
35 customer => t8('Customer'),
36 vendor => t8('Vendor'),
40 our @types = qw(text textfield htmlfield number date bool select customer vendor part); # timestamp
42 our @modules = qw(CT Contacts IC Projects RequirementSpecs ShipTo);
51 my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => $self->module ]);
53 $self->setup_list_action_bar;
54 $::form->{title} = t8('List of custom variables');
56 $self->render('custom_variable_config/list',
57 title => t8('List of custom variables'),
64 $self->config(SL::DB::CustomVariableConfig->new(module => $self->module));
65 $self->show_form(title => t8('Add custom variable'));
69 my ($self, %params) = @_;
72 map { split m/=/, $_, 2 }
73 split m/:/, ($self->config->flags || '')
76 $params{all_partsgroups} = SL::DB::Manager::PartsGroup->get_all();
78 $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
79 $self->setup_form_action_bar;
80 $self->render('custom_variable_config/form', %params);
86 $self->show_form(title => t8('Edit custom variable'));
92 $self->config(SL::DB::CustomVariableConfig->new);
93 $self->create_or_update;
98 $self->create_or_update;
104 # delete relationship to partsgroups (for filter) before cvar can be deleted
105 $self->config->update_attributes(partsgroups => []);
107 if (eval { $self->config->delete; 1; }) {
108 flash_later('info', t8('The custom variable has been deleted.'));
110 flash_later('error', t8('The custom variable is in use and cannot be deleted.'));
113 $self->redirect_to(action => 'list', module => $self->module);
119 SL::DB::CustomVariableConfig->reorder_list(@{ $::form->{cvarcfg_id} || [] });
121 $self->render(\'', { type => 'json' }); # ' make emacs happy
129 $::auth->assert('config');
135 $::form->{module} ||= 'CT';
136 my $mod_desc = first { $_->{module} eq $::form->{module} } @{ $self->modules };
137 die "Invalid 'module' parameter '" . $::form->{module} . "'" if !$mod_desc;
139 $self->module($mod_desc->{module});
140 $self->module_description($mod_desc->{description});
146 $self->config(SL::DB::CustomVariableConfig->new(id => $::form->{id})->load);
153 sub get_translation {
154 my ($self, $type) = @_;
156 return $translations{$type};
159 sub init_translated_types {
162 return [ map { { type => $_, translation => $translations{$_} } } @types ];
166 my ($self, %params) = @_;
169 sort { $a->{description}->translated cmp $b->{description}->translated } (
170 map +{ module => $_, description => $SL::Presenter::CustomVariableConfig::t8{$_} },
175 sub create_or_update {
177 my $is_new = !$self->config->id;
179 my $params = delete($::form->{config}) || { };
180 delete $params->{id};
182 if ($self->module eq 'IC') {
183 $params->{partsgroups} = [] if !$params->{flag_partsgroup_filter};
185 delete $params->{flag_partsgroup_filter};
186 $params->{partsgroups} = [];
189 $params->{partsgroups} ||= []; # The list is empty, if control is not send by the browser.
190 $params->{default_value} = $::form->parse_amount(\%::myconfig, $params->{default_value}) if $params->{type} eq 'number';
191 $params->{included_by_default} = 0 if !$params->{includeable};
192 $params->{flags} = join ':', map { m/^flag_(.*)/; "${1}=" . delete($params->{$_}) } grep { m/^flag_/ } keys %{ $params };
194 $self->config->assign_attributes(%{ $params }, module => $self->module);
196 my @errors = $self->config->validate;
199 flash('error', @errors);
200 $self->show_form(title => $is_new ? t8('Add new custom variable') : t8('Edit custom variable'));
204 SL::DB->client->with_transaction(sub {
205 my $dbh = SL::DB->client->dbh;
208 $self->_set_cvar_validity() if $is_new;
210 }) or do { die SL::DB->client->error };
212 flash_later('info', $is_new ? t8('The custom variable has been created.') : t8('The custom variable has been saved.'));
213 $self->redirect_to(action => 'list', module => $self->module);
216 sub _set_cvar_validity {
220 map { split m/=/, $_, 2 }
221 split m/:/, ($self->config->flags || '')
224 # nothing to do to set valid
225 return if !$flags->{defaults_to_invalid};
227 my $all_parts = SL::DB::Manager::Part->get_all(where => [ or => [ obsolete => 0, obsolete => undef ] ]);
228 foreach my $part (@{ $all_parts }) {
229 SL::DB::CustomVariableValidity->new(config_id => $self->config->id, trans_id => $part->id)->save;
233 sub setup_list_action_bar {
236 for my $bar ($::request->layout->get('actionbar')) {
240 link => $self->url_for(action => 'new', module => $self->module),
246 sub setup_form_action_bar {
249 my $is_new = !$self->config->id;
251 for my $bar ($::request->layout->get('actionbar')) {
256 submit => [ '#form', { action => 'CustomVariableConfig/' . ($is_new ? 'create' : 'update') } ],
257 checks => [ 'check_prerequisites' ],
258 accesskey => 'enter',
263 submit => [ '#form', { action => 'CustomVariableConfig/create'} ],
264 checks => [ 'check_prerequisites' ],
267 ], # end of combobox "Save"
271 submit => [ '#form', { action => 'CustomVariableConfig/destroy' } ],
272 confirm => t8('Do you really want to delete this object?'),
273 disabled => $is_new ? t8('This object has not been saved yet.') : undef,
280 link => $self->url_for(action => 'list', module => $self->module),