]> wagnertech.de Git - mfinanz.git/blob - SL/Controller/CustomVariableConfig.pm
Merge branch 'master' of http://wagnertech.de/git/mfinanz
[mfinanz.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::DB::PartsGroup;
12 use SL::Helper::Flash;
13 use SL::Locale::String;
14 use SL::Presenter::CustomVariableConfig;
15 use Data::Dumper;
16
17 use Rose::Object::MakeMethods::Generic (
18   scalar                  => [ qw(config module module_description flags) ],
19   'scalar --get_set_init' => [ qw(translated_types modules) ],
20 );
21
22 __PACKAGE__->run_before('check_auth');
23 __PACKAGE__->run_before('check_module');
24 __PACKAGE__->run_before('load_config', only => [ qw(edit update destroy) ]);
25
26 our %translations = (
27   text      => t8('Free-form text'),
28   textfield => t8('Text field'),
29   htmlfield => t8('HTML field'),
30   number    => t8('Number'),
31   date      => t8('Date'),
32   timestamp => t8('Timestamp'),
33   bool      => t8('Yes/No (Checkbox)'),
34   select    => t8('Selection'),
35   customer  => t8('Customer'),
36   vendor    => t8('Vendor'),
37   part      => t8('Part'),
38 );
39
40 our @types = qw(text textfield htmlfield number date bool select customer vendor part); # timestamp
41
42 our @modules = qw(CT Contacts IC Projects RequirementSpecs ShipTo);
43
44 #
45 # actions
46 #
47
48 sub action_list {
49   my ($self) = @_;
50
51   my $configs = SL::DB::Manager::CustomVariableConfig->get_all_sorted(where => [ module => $self->module ]);
52
53   $self->setup_list_action_bar;
54   $::form->{title} = t8('List of custom variables');
55   $::form->header;
56   $self->render('custom_variable_config/list',
57                 title   => t8('List of custom variables'),
58                 CONFIGS => $configs);
59 }
60
61 sub action_new {
62   my ($self) = @_;
63
64   $self->config(SL::DB::CustomVariableConfig->new(module => $self->module));
65   $self->show_form(title => t8('Add custom variable'));
66 }
67
68 sub show_form {
69   my ($self, %params) = @_;
70
71   $self->flags({
72     map { split m/=/, $_, 2 }
73     split m/:/, ($self->config->flags || '')
74   });
75
76   $params{all_partsgroups} = SL::DB::Manager::PartsGroup->get_all();
77
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);
81 }
82
83 sub action_edit {
84   my ($self) = @_;
85
86   $self->show_form(title => t8('Edit custom variable'));
87 }
88
89 sub action_create {
90   my ($self) = @_;
91
92   $self->config(SL::DB::CustomVariableConfig->new);
93   $self->create_or_update;
94 }
95
96 sub action_update {
97   my ($self) = @_;
98   $self->create_or_update;
99 }
100
101 sub action_destroy {
102   my ($self) = @_;
103
104   # delete relationship to partsgroups (for filter) before cvar can be deleted
105   $self->config->update_attributes(partsgroups => []);
106
107   if (eval { $self->config->delete; 1; }) {
108     flash_later('info',  t8('The custom variable has been deleted.'));
109   } else {
110     flash_later('error', t8('The custom variable is in use and cannot be deleted.'));
111   }
112
113   $self->redirect_to(action => 'list', module => $self->module);
114 }
115
116 sub action_reorder {
117   my ($self) = @_;
118
119   SL::DB::CustomVariableConfig->reorder_list(@{ $::form->{cvarcfg_id} || [] });
120
121   $self->render(\'', { type => 'json' }); # ' make emacs happy
122 }
123
124 #
125 # filters
126 #
127
128 sub check_auth {
129   $::auth->assert('config');
130 }
131
132 sub check_module {
133   my ($self)          = @_;
134
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;
138
139   $self->module($mod_desc->{module});
140   $self->module_description($mod_desc->{description});
141 }
142
143 sub load_config {
144   my ($self) = @_;
145
146   $self->config(SL::DB::CustomVariableConfig->new(id => $::form->{id})->load);
147 }
148
149 #
150 # helpers
151 #
152
153 sub get_translation {
154   my ($self, $type) = @_;
155
156   return $translations{$type};
157 }
158
159 sub init_translated_types {
160   my ($self) = @_;
161
162   return [ map { { type => $_, translation => $translations{$_} } } @types ];
163 }
164
165 sub init_modules {
166   my ($self, %params) = @_;
167
168   return [
169     sort { $a->{description}->translated cmp $b->{description}->translated } (
170     map +{ module => $_, description => $SL::Presenter::CustomVariableConfig::t8{$_} },
171     @modules
172   )];
173 }
174
175 sub create_or_update {
176   my ($self) = @_;
177   my $is_new = !$self->config->id;
178
179   my $params = delete($::form->{config}) || { };
180   delete $params->{id};
181
182   if ($self->module eq 'IC') {
183     $params->{partsgroups} = [] if !$params->{flag_partsgroup_filter};
184   } else {
185     delete $params->{flag_partsgroup_filter};
186     $params->{partsgroups} = [];
187   }
188
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 };
193
194   $self->config->assign_attributes(%{ $params }, module => $self->module);
195
196   my @errors = $self->config->validate;
197
198   if (@errors) {
199     flash('error', @errors);
200     $self->show_form(title => $is_new ? t8('Add new custom variable') : t8('Edit custom variable'));
201     return;
202   }
203
204   SL::DB->client->with_transaction(sub {
205     my $dbh = SL::DB->client->dbh;
206
207     $self->config->save;
208     $self->_set_cvar_validity() if $is_new;
209     1;
210   }) or do { die SL::DB->client->error };
211
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);
214 }
215
216 sub _set_cvar_validity {
217   my ($self) = @_;
218
219   my $flags = {
220     map { split m/=/, $_, 2 }
221     split m/:/, ($self->config->flags || '')
222   };
223
224   # nothing to do to set valid
225   return if !$flags->{defaults_to_invalid};
226
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;
230   }
231 }
232
233 sub setup_list_action_bar {
234   my ($self) = @_;
235
236   for my $bar ($::request->layout->get('actionbar')) {
237     $bar->add(
238       action => [
239         t8('Add'),
240         link => $self->url_for(action => 'new', module => $self->module),
241       ],
242     );
243   }
244 }
245
246 sub setup_form_action_bar {
247   my ($self) = @_;
248
249   my $is_new = !$self->config->id;
250
251   for my $bar ($::request->layout->get('actionbar')) {
252     $bar->add(
253       combobox => [
254         action => [
255           t8('Save'),
256           submit    => [ '#form', { action => 'CustomVariableConfig/' . ($is_new ? 'create' : 'update') } ],
257           checks    => [ 'check_prerequisites' ],
258           accesskey => 'enter',
259         ],
260
261         action => [
262           t8('Save as new'),
263           submit => [ '#form', { action => 'CustomVariableConfig/create'} ],
264           checks => [ 'check_prerequisites' ],
265           not_if => $is_new,
266         ],
267       ], # end of combobox "Save"
268
269       action => [
270         t8('Delete'),
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,
274       ],
275
276       'separator',
277
278       link => [
279         t8('Abort'),
280         link => $self->url_for(action => 'list', module => $self->module),
281       ],
282     );
283   }
284 }
285
286 1;