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 htmlfield => t8('HTML field'),
29 number => t8('Number'),
31 timestamp => t8('Timestamp'),
32 bool => t8('Yes/No (Checkbox)'),
33 select => t8('Selection'),
34 customer => t8('Customer'),
35 vendor => t8('Vendor'),
39 our @types = qw(text textfield htmlfield number date bool select customer vendor part); # timestamp
48 my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => $self->module ]);
50 $self->setup_list_action_bar;
52 $self->render('custom_variable_config/list',
53 title => t8('List of custom variables'),
60 $self->config(SL::DB::CustomVariableConfig->new(module => $self->module));
61 $self->show_form(title => t8('Add custom variable'));
65 my ($self, %params) = @_;
68 map { split m/=/, $_, 2 }
69 split m/:/, ($self->config->flags || '')
72 $params{all_partsgroups} = SL::DB::Manager::PartsGroup->get_all();
74 $::request->layout->use_javascript("${_}.js") for qw(jquery.selectboxes jquery.multiselect2side);
75 $self->setup_form_action_bar;
76 $self->render('custom_variable_config/form', %params);
82 $self->show_form(title => t8('Edit custom variable'));
88 $self->config(SL::DB::CustomVariableConfig->new);
89 $self->create_or_update;
94 $self->create_or_update;
100 # delete relationship to partsgroups (for filter) before cvar can be deleted
101 $self->config->update_attributes(partsgroups => []);
103 if (eval { $self->config->delete; 1; }) {
104 flash_later('info', t8('The custom variable has been deleted.'));
106 flash_later('error', t8('The custom variable is in use and cannot be deleted.'));
109 $self->redirect_to(action => 'list', module => $self->module);
115 SL::DB::CustomVariableConfig->reorder_list(@{ $::form->{cvarcfg_id} || [] });
117 $self->render(\'', { type => 'json' }); # ' make emacs happy
125 $::auth->assert('config');
131 $::form->{module} ||= 'CT';
132 my $mod_desc = first { $_->{module} eq $::form->{module} } @{ $self->modules };
133 die "Invalid 'module' parameter '" . $::form->{module} . "'" if !$mod_desc;
135 $self->module($mod_desc->{module});
136 $self->module_description($mod_desc->{description});
142 $self->config(SL::DB::CustomVariableConfig->new(id => $::form->{id})->load);
149 sub get_translation {
150 my ($self, $type) = @_;
152 return $translations{$type};
155 sub init_translated_types {
158 return [ map { { type => $_, translation => $translations{$_} } } @types ];
162 my ($self, %params) = @_;
164 return [ sort { $a->{description}->translated cmp $b->{description}->translated } (
165 { module => 'CT', description => t8('Customers and vendors') },
166 { module => 'Contacts', description => t8('Contact persons') },
167 { module => 'IC', description => t8('Parts, services and assemblies') },
168 { module => 'Projects', description => t8('Projects') },
169 { module => 'RequirementSpecs', description => t8('Requirement Specs') },
170 { module => 'ShipTo', description => t8('Shipping Address') },
174 sub create_or_update {
176 my $is_new = !$self->config->id;
178 my $params = delete($::form->{config}) || { };
179 delete $params->{id};
181 if ($self->module eq 'IC') {
182 $params->{partsgroups} = [] if !$params->{flag_partsgroup_filter};
184 delete $params->{flag_partsgroup_filter};
185 $params->{partsgroups} = [];
188 $params->{partsgroups} ||= []; # The list is empty, if control is not send by the browser.
189 $params->{default_value} = $::form->parse_amount(\%::myconfig, $params->{default_value}) if $params->{type} eq 'number';
190 $params->{included_by_default} = 0 if !$params->{includeable};
191 $params->{flags} = join ':', map { m/^flag_(.*)/; "${1}=" . delete($params->{$_}) } grep { m/^flag_/ } keys %{ $params };
193 $self->config->assign_attributes(%{ $params }, module => $self->module);
195 my @errors = $self->config->validate;
198 flash('error', @errors);
199 $self->show_form(title => $is_new ? t8('Add new custom variable') : t8('Edit custom variable'));
203 SL::DB->client->with_transaction(sub {
204 my $dbh = SL::DB->client->dbh;
207 $self->_set_cvar_validity() if $is_new;
209 }) or do { die SL::DB->client->error };
211 flash_later('info', $is_new ? t8('The custom variable has been created.') : t8('The custom variable has been saved.'));
212 $self->redirect_to(action => 'list', module => $self->module);
215 sub _set_cvar_validity {
219 map { split m/=/, $_, 2 }
220 split m/:/, ($self->config->flags || '')
223 # nothing to do to set valid
224 return if !$flags->{defaults_to_invalid};
226 my $all_parts = SL::DB::Manager::Part->get_all(where => [ or => [ obsolete => 0, obsolete => undef ] ]);
227 foreach my $part (@{ $all_parts }) {
228 SL::DB::CustomVariableValidity->new(config_id => $self->config->id, trans_id => $part->id)->save;
232 sub setup_list_action_bar {
235 for my $bar ($::request->layout->get('actionbar')) {
239 link => $self->url_for(action => 'new', module => $self->module),
245 sub setup_form_action_bar {
248 my $is_new = !$self->config->id;
250 for my $bar ($::request->layout->get('actionbar')) {
255 submit => [ '#form', { action => 'CustomVariableConfig/' . ($is_new ? 'create' : 'update') } ],
256 checks => [ 'check_prerequisites' ],
257 accesskey => 'enter',
262 submit => [ '#form', { action => 'CustomVariableConfig/create'} ],
263 checks => [ 'check_prerequisites' ],
266 ], # end of combobox "Save"
270 submit => [ '#form', { action => 'CustomVariableConfig/destroy' } ],
271 confirm => t8('Do you really want to delete this object?'),
272 disabled => $is_new ? t8('This object has not been saved yet.') : undef,
279 link => $self->url_for(action => 'list', module => $self->module),