Nach dem Löschen von einer CVar wieder die Liste des entspr. Moduls anzeigen.
[kivitendo-erp.git] / SL / Controller / CustomVariableConfig.pm
1 package SL::Controller::CustomVariableConfig;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use List::Util qw(first);
8
9 use SL::DB::CustomVariableConfig;
10 use SL::DB::CustomVariableValidity;
11 use SL::Helper::Flash;
12 use SL::Locale::String;
13 use Data::Dumper;
14
15 use Rose::Object::MakeMethods::Generic (
16   scalar                  => [ qw(config module module_description flags) ],
17   'scalar --get_set_init' => [ qw(translated_types modules) ],
18 );
19
20 __PACKAGE__->run_before('check_auth');
21 __PACKAGE__->run_before('check_module');
22 __PACKAGE__->run_before('load_config', only => [ qw(edit update destroy) ]);
23
24 our %translations = (
25   text      => t8('Free-form text'),
26   textfield => t8('Text field'),
27   number    => t8('Number'),
28   date      => t8('Date'),
29   timestamp => t8('Timestamp'),
30   bool      => t8('Yes/No (Checkbox)'),
31   select    => t8('Selection'),
32   customer  => t8('Customer'),
33   vendor    => t8('Vendor'),
34   part      => t8('Part'),
35 );
36
37 our @types = qw(text textfield number date bool select customer vendor part); # timestamp
38
39 #
40 # actions
41 #
42
43 sub action_list {
44   my ($self) = @_;
45
46   my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => $self->module ]);
47
48   $::form->header;
49   $self->render('custom_variable_config/list',
50                 title   => t8('List of custom variables'),
51                 CONFIGS => $configs);
52 }
53
54 sub action_new {
55   my ($self) = @_;
56
57   $self->config(SL::DB::CustomVariableConfig->new(module => $self->module));
58   $self->show_form(title => t8('Add custom variable'));
59 }
60
61 sub show_form {
62   my ($self, %params) = @_;
63
64   $self->flags({
65     map { split m/=/, $_, 2 }
66     split m/:/, ($self->config->flags || '')
67   });
68
69   $self->render('custom_variable_config/form', %params);
70 }
71
72 sub action_edit {
73   my ($self) = @_;
74
75   $self->show_form(title => t8('Edit custom variable'));
76 }
77
78 sub action_create {
79   my ($self) = @_;
80
81   $self->config(SL::DB::CustomVariableConfig->new);
82   $self->create_or_update;
83 }
84
85 sub action_update {
86   my ($self) = @_;
87   $self->create_or_update;
88 }
89
90 sub action_destroy {
91   my ($self) = @_;
92
93   if (eval { $self->config->delete; 1; }) {
94     flash_later('info',  t8('The custom variable has been deleted.'));
95   } else {
96     flash_later('error', t8('The custom variable is in use and cannot be deleted.'));
97   }
98
99   $self->redirect_to(action => 'list', module => $self->module);
100 }
101
102 sub action_reorder {
103   my ($self) = @_;
104
105   SL::DB::CustomVariableConfig->reorder_list(@{ $::form->{cvarcfg_id} || [] });
106
107   $self->render(\'', { type => 'json' }); # ' make emacs happy
108 }
109
110 #
111 # filters
112 #
113
114 sub check_auth {
115   $::auth->assert('config');
116 }
117
118 sub check_module {
119   my ($self)          = @_;
120
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;
124
125   $self->module($mod_desc->{module});
126   $self->module_description($mod_desc->{description});
127 }
128
129 sub load_config {
130   my ($self) = @_;
131
132   $self->config(SL::DB::CustomVariableConfig->new(id => $::form->{id})->load);
133 }
134
135 #
136 # helpers
137 #
138
139 sub get_translation {
140   my ($self, $type) = @_;
141
142   return $translations{$type};
143 }
144
145 sub init_translated_types {
146   my ($self) = @_;
147
148   return [ map { { type => $_, translation => $translations{$_} } } @types ];
149 }
150
151 sub init_modules {
152   my ($self, %params) = @_;
153
154   return [
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')                       },
159   ];
160 }
161
162 sub create_or_update {
163   my ($self) = @_;
164   my $is_new = !$self->config->id;
165
166   my $params = delete($::form->{config}) || { };
167   delete $params->{id};
168
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 };
172
173   $self->config->assign_attributes(%{ $params }, module => $self->module);
174
175   my @errors = $self->config->validate;
176
177   if (@errors) {
178     flash('error', @errors);
179     $self->show_form(title => $is_new ? t8('Add new custom variable') : t8('Edit custom variable'));
180     return;
181   }
182
183   my $dbh = $self->config->db;
184   $dbh->begin_work;
185
186   $self->config->save;
187   $self->_set_cvar_validity() if $is_new;
188
189   $dbh->commit;
190
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);
193 }
194
195 sub _set_cvar_validity {
196   my ($self) = @_;
197
198   my $flags = {
199     map { split m/=/, $_, 2 }
200     split m/:/, ($self->config->flags || '')
201   };
202
203   # nothing to do to set valid
204   return if !$flags->{defaults_to_invalid};
205
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;
209   }
210 }
211
212 1;